Example #1
0
        //-------------------------------------------------------------------------

        public bool Equals(object obj)
        {
            if (this == obj)
            {
                return(true);
            }
            if (!base.Equals(obj))
            {
                return(false);
            }
            if (this.GetType() != obj.GetType())
            {
                return(false);
            }
            PremiumLegElement other = (PremiumLegElement)obj;

            if (_coupon == null)
            {
                if (other._coupon != null)
                {
                    return(false);
                }
            }
            else if (!_coupon.Equals(other._coupon))
            {
                return(false);
            }
            if (_creditCurveKnot != other._creditCurveKnot)
            {
                return(false);
            }
            if (_formula != other._formula)
            {
                return(false);
            }
            if (!Array.Equals(_knots, other._knots))
            {
                return(false);
            }
            if (_n != other._n)
            {
                return(false);
            }
            if ((long)_omega != (long)other._omega)
            {
                return(false);
            }
            if (!Array.Equals(_p, other._p))
            {
                return(false);
            }
            if (!Array.Equals(_rt, other._rt))
            {
                return(false);
            }
            return(true);
        }
        public CreditCurveCalibrator(MultiCdsAnalytic multiCDS, YieldTermStructure yieldCurve, AccrualOnDefaultFormulae formula)
        {
            _nCDS        = multiCDS.getNumMaturities();
            _t           = new double[_nCDS];
            _lgd         = new double[_nCDS];
            _unitAccured = new double[_nCDS];
            for (int i = 0; i < _nCDS; i++)
            {
                _t[i]           = multiCDS.getProtectionEnd(i);
                _lgd[i]         = multiCDS.getLGD();
                _unitAccured[i] = multiCDS.getAccruedPremiumPerUnitSpread(i);
            }
            _valuationDF = Math.Exp(-yieldCurve.getRT_(multiCDS.getCashSettleTime()));

            //This is the global set of knots - it will be truncated down for the various leg elements
            //TODO this will not match ISDA C for forward starting (i.e. accStart > tradeDate) CDS, and will give different answers
            //if the Markit 'fix' is used in that case
            double[] knots = DoublesScheduleGenerator.getIntegrationsPoints(
                multiCDS.getEffectiveProtectionStart(), _t[_nCDS - 1], yieldCurve.t.ToArray(), _t.ToArray());

            //The protection leg
            _protElems = new ProtectionLegElement[_nCDS];
            for (int i = 0; i < _nCDS; i++)
            {
                _protElems[i] = new ProtectionLegElement(
                    i == 0 ? multiCDS.getEffectiveProtectionStart() : _t[i - 1], _t[i], yieldCurve, i, knots);
            }

            _cds2CouponsMap      = new int[_nCDS][];
            _cdsCouponsUpdateMap = new int[_nCDS][];
            _knot2CouponsMap     = new int[_nCDS][];

            List <CdsCoupon> allCoupons = new List <CdsCoupon>(_nCDS + multiCDS.getTotalPayments() - 1);

            allCoupons.AddRange(multiCDS.getStandardCoupons().ToList());
            allCoupons.Add(multiCDS.getTerminalCoupon(_nCDS - 1));
            int[] temp = new int[multiCDS.getTotalPayments()];
            for (int i = 0; i < multiCDS.getTotalPayments(); i++)
            {
                temp[i] = i;
            }
            _cds2CouponsMap[_nCDS - 1] = temp;

            //complete the list of unique coupons and fill out the cds2CouponsMap
            for (int i = 0; i < _nCDS - 1; i++)
            {
                CdsCoupon c         = multiCDS.getTerminalCoupon(i);
                int       nPayments = Math.Max(0, multiCDS.getPaymentIndexForMaturity(i)) + 1;
                _cds2CouponsMap[i] = new int[nPayments];
                for (int jj = 0; jj < nPayments - 1; jj++)
                {
                    _cds2CouponsMap[i][jj] = jj;
                }
                //because of business-day adjustment, a terminal coupon can be identical to a standard coupon,
                //in which case it is not added again
                int index = allCoupons.IndexOf(c);
                if (index == -1)
                {
                    index = allCoupons.Count;
                    allCoupons.Add(c);
                }
                _cds2CouponsMap[i][nPayments - 1] = index;
            }

            //loop over the coupons to populate the couponUpdateMap
            _nCoupons = allCoupons.Count;
            int[] sizes = new int[_nCDS];
            int[] map   = new int[_nCoupons];
            for (int i = 0; i < _nCoupons; i++)
            {
                CdsCoupon c     = allCoupons[i];
                int       index = Array.BinarySearch(_t, c.getEffEnd());
                if (index < 0)
                {
                    index = -(index + 1);
                }
                sizes[index]++;
                map[i] = index;
            }

            //make the protection leg elements

            if (multiCDS.isPayAccOnDefault())
            {
                _premElems = new PremiumLegElement[_nCoupons];
                for (int i = 0; i < _nCoupons; i++)
                {
                    _premElems[i] = new PremiumLegElement(multiCDS.getEffectiveProtectionStart(), allCoupons[i], yieldCurve, map[i],
                                                          knots, formula);
                }
            }
            else
            {
                _premElems = new CouponOnlyElement[_nCoupons];
                for (int i = 0; i < _nCoupons; i++)
                {
                    _premElems[i] = new CouponOnlyElement(allCoupons[i], yieldCurve, map[i]);
                }
            }

            //sort a map from coupon to curve node, to a map from curve node to coupons
            for (int i = 0; i < _nCDS; i++)
            {
                _knot2CouponsMap[i] = new int[sizes[i]];
            }
            int[] indexes = new int[_nCDS];
            for (int i = 0; i < _nCoupons; i++)
            {
                int index = map[i];
                _knot2CouponsMap[index][indexes[index]++] = i;
            }

            //the cdsCouponsUpdateMap is the intersection of the cds2CouponsMap and knot2CouponsMap
            for (int i = 0; i < _nCDS; i++)
            {
                _cdsCouponsUpdateMap[i] = intersection(_knot2CouponsMap[i], _cds2CouponsMap[i]);
            }
        }
        public CreditCurveCalibrator(CDS[] cds, YieldTermStructure yieldCurve, AccrualOnDefaultFormulae formula)
        {
            _nCDS = cds.Length;
            Boolean payAccOnDefault = cds[0].isPayAccOnDefault();
            double  accStart        = cds[0].getAccStart();
            double  effectProtStart = cds[0].getEffectiveProtectionStart();
            double  cashSettleTime  = cds[0].getCashSettleTime();

            _t    = new double[_nCDS];
            _t[0] = cds[0].getProtectionEnd();
            //Check all the CDSs match
            for (int i = 1; i < _nCDS; i++)
            {
                _t[i] = cds[i].getProtectionEnd();
            }

            _valuationDF = Math.Exp(-yieldCurve.getRT_(cashSettleTime));
            _lgd         = new double[_nCDS];
            _unitAccured = new double[_nCDS];
            for (int i = 0; i < _nCDS; i++)
            {
                _lgd[i]         = cds[i].getLGD();
                _unitAccured[i] = cds[i].getAccruedYearFraction();
            }

            //This is the global set of knots - it will be truncated down for the various leg elements
            //TODO this will not match ISDA C for forward starting (i.e. accStart > tradeDate) CDS, and will give different answers
            //if the Markit 'fix' is used in that case

            double[] knots = DoublesScheduleGenerator.
                             getIntegrationsPoints(effectProtStart, _t[_nCDS - 1], yieldCurve.t.ToArray(), _t);

            //The protection leg
            _protElems = new ProtectionLegElement[_nCDS];
            for (int i = 0; i < _nCDS; i++)
            {
                _protElems[i] = new ProtectionLegElement(i == 0 ? effectProtStart : _t[i - 1], _t[i], yieldCurve, i, knots);
            }

            _cds2CouponsMap      = new int[_nCDS][];
            _cdsCouponsUpdateMap = new int[_nCDS][];
            _knot2CouponsMap     = new int[_nCDS][];

            int nPaymentsFinalCDS       = cds[_nCDS - 1].getNumPayments();
            List <CdsCoupon> allCoupons = new List <CdsCoupon>(_nCDS + nPaymentsFinalCDS - 1);

            allCoupons.AddRange(cds[_nCDS - 1].getCoupons());
            int[] temp = new int[nPaymentsFinalCDS];
            for (int i = 0; i < nPaymentsFinalCDS; i++)
            {
                temp[i] = i;
            }
            _cds2CouponsMap[_nCDS - 1] = temp;

            //complete the list of unique coupons and fill out the cds2CouponsMap
            for (int i = 0; i < _nCDS - 1; i++)
            {
                CdsCoupon[] c         = cds[i].getCoupons();
                int         nPayments = c.Length;
                _cds2CouponsMap[i] = new int[nPayments];
                for (int k = 0; k < nPayments; k++)
                {
                    int index = -1;
                    for (int j = 0; j < allCoupons.Count; j++)
                    {
                        if (allCoupons[j].Equals(c[k]))
                        {
                            index = j;
                            break;
                        }
                    }
                    if (index == -1)
                    {
                        index = allCoupons.Count;
                        allCoupons.Add(c[k]);
                    }
                    _cds2CouponsMap[i][k] = index;
                }
            }

            //loop over the coupons to populate the couponUpdateMap
            _nCoupons = allCoupons.Count;
            int[] sizes = new int[_nCDS];
            int[] map   = new int[_nCoupons];
            for (int i = 0; i < _nCoupons; i++)
            {
                CdsCoupon c     = allCoupons[i];
                int       index = Array.BinarySearch(_t, c.getEffEnd());
                if (index < 0)
                {
                    index = -(index + 1);
                }
                sizes[index]++;
                map[i] = index;
            }

            //make the protection leg elements

            if (payAccOnDefault)
            {
                _premElems = new PremiumLegElement[_nCoupons];
                for (int i = 0; i < _nCoupons; i++)
                {
                    _premElems[i] = new PremiumLegElement(effectProtStart, allCoupons[i], yieldCurve, map[i], knots, formula);
                }
            }
            else
            {
                _premElems = new CouponOnlyElement[_nCoupons];
                for (int i = 0; i < _nCoupons; i++)
                {
                    _premElems[i] = new CouponOnlyElement(allCoupons[i], yieldCurve, map[i]);
                }
            }

            //sort a map from coupon to curve node, to a map from curve node to coupons
            for (int i = 0; i < _nCDS; i++)
            {
                _knot2CouponsMap[i] = new int[sizes[i]];
            }
            int[] indexes = new int[_nCDS];
            for (int i = 0; i < _nCoupons; i++)
            {
                int index = map[i];
                _knot2CouponsMap[index][indexes[index]++] = i;
            }

            //the cdsCouponsUpdateMap is the intersection of the cds2CouponsMap and knot2CouponsMap
            for (int i = 0; i < _nCDS; i++)
            {
                _cdsCouponsUpdateMap[i] = intersection(_knot2CouponsMap[i], _cds2CouponsMap[i]);
            }
        }