예제 #1
0
        /// <summary>
        /// </summary>
        /// <param name="discountCurve"></param>
        /// <param name="tenor"></param>
        /// <param name="baseDate"></param>
        /// <param name="interpolatedCurve"></param>
        /// <param name="paymentCalendar"></param>
        /// <param name="dayCounter"></param>
        /// <returns></returns>
        /// <exception cref="System.Exception"></exception>
        public static TermCurve ToForwardCurve(TermCurve discountCurve, Period tenor, DateTime baseDate,
                                               InterpolatedCurve interpolatedCurve, IBusinessCalendar paymentCalendar, IDayCounter dayCounter)
        {
            TermCurve result = TermCurve.Create(new List <TermPoint>());
            var       length = discountCurve.point.Length;
            var       offset = new Offset
            {
                dayType          = DayTypeEnum.Calendar,
                dayTypeSpecified = true,
                period           = tenor.period,
                periodMultiplier = tenor.periodMultiplier,
                periodSpecified  = true
            };

            if (paymentCalendar == null)
            {
                return(result);
            }
            for (int i = 0; i < length - 1; i++) //This will only go to the penultimate point. Extrapolation required for more.
            {
                var      pointStart        = discountCurve.point[i];
                DateTime startDate         = XsdClassesFieldResolver.TimeDimensionGetDate(pointStart.term);
                var      endDate           = paymentCalendar.Advance(startDate, offset, BusinessDayConventionEnum.FOLLOWING);
                var      endPoint          = new DateTimePoint1D(baseDate, endDate);
                var      endDF             = interpolatedCurve.Value(endPoint);
                double   time              = dayCounter.YearFraction(startDate, endDate);
                var      forwardRateDouble =
                    RateAnalytics.DiscountFactorsToForwardRate((double)pointStart.mid, endDF, time);
                TermPoint forwardPoint = TermPointFactory.Create(Convert.ToDecimal(forwardRateDouble), startDate);
                forwardPoint.id = tenor.id;
                result.Add(forwardPoint);
            }
            return(result);
        }
예제 #2
0
        ///<summary>
        ///</summary>
        ///<param name="discount"></param>
        ///<param name="shift"></param>
        public void Populate(QuarterlyDiscounts discount, QuarterlyShifts shift)
        {
            var forward                      = new double[_maxTenor + 1];
            var accumForward                 = new double[_maxTenor + 1];
            var accumForwardTimesRate        = new double[_maxTenor + 1];
            var accumForwardTimesShiftedRate = new double[_maxTenor + 1];

            accumForward[0]                 = 0;
            accumForwardTimesRate[0]        = 0;
            accumForwardTimesShiftedRate[0] = 0;
            var w = new double[_maxTenor];
            var h = new double[_maxTenor];

            for (int i = 0; i < _maxExpiry; i++)
            {
                for (int j = 0; j < _maxTenor - i; j++)
                {
                    //See "Engineering BGM" page 34 for explicit formula. Function definitions can be found in chapter 4.
                    forward[j + 1]      = RateAnalytics.ForwardContract(i + 1, i + j + 2, discount);
                    accumForward[j + 1] = accumForward[j] + forward[j + 1];
                    accumForwardTimesRate[j + 1]
                        = accumForwardTimesRate[j] + forward[j + 1] * RateAnalytics.CashForwardRate(i + j + 1, discount);
                    accumForwardTimesShiftedRate[j + 1]
                         = accumForwardTimesShiftedRate[j] + forward[j + 1] * RateAnalytics.ShiftedCashForwardRate(i + j + 1, discount, shift);
                    h[j] = 0.25 * RateAnalytics.ShiftedCashForwardRate(i + j + 1, discount, shift) / (1 + 0.25 * RateAnalytics.CashForwardRate(i + j + 1, discount));
                    for (int k = 0; k < j + 1; k++)
                    {
                        w[k] = forward[k + 1] * RateAnalytics.ShiftedCashForwardRate(i + k + 1, discount, shift) / accumForwardTimesShiftedRate[j + 1];
                        _weights[i][j][k] = w[k] - h[k] * ((accumForwardTimesRate[j + 1] - accumForwardTimesRate[k]) / accumForwardTimesShiftedRate[j + 1]
                                                           - (1 - accumForward[k] / accumForward[j + 1]) * accumForwardTimesRate[j + 1] / accumForwardTimesShiftedRate[j + 1]);
                    }
                }
            }
        }
예제 #3
0
        //  need a based date?
        //
        ///<summary>
        ///</summary>
        ///<param name="discountCurve"></param>
        ///<param name="baseDate"></param>
        ///<param name="frequency"></param>
        ///<param name="dayCounter"></param>
        ///<returns></returns>
        ///<exception cref="System.Exception"></exception>
        public static TermCurve ToZeroCurve(TermCurve discountCurve, DateTime baseDate,
                                            CompoundingFrequencyEnum frequency, IDayCounter dayCounter)
        {
            TermCurve result = TermCurve.Create(new List <TermPoint>());

            foreach (TermPoint point in discountCurve.point)
            {
                DateTime pointDate = XsdClassesFieldResolver.TimeDimensionGetDate(point.term);
                double   zeroRateDouble;
                if (baseDate != pointDate)
                {
                    double time = dayCounter.YearFraction(baseDate, pointDate);
                    zeroRateDouble = RateAnalytics.DiscountFactorToZeroRate((double)point.mid, time, frequency);
                }
                else
                {
                    // set after the loop
                    zeroRateDouble = 0;
                }
                TermPoint zeroPoint = TermPointFactory.Create(Convert.ToDecimal(zeroRateDouble), pointDate);
                zeroPoint.id = point.id;
                result.Add(zeroPoint);
            }
            if (result.point[0].mid == 0)
            {
                result.point[0].mid = result.point[1].mid;
            }
            return(result);
        }