/// <summary> /// Gets the rate lin365. /// </summary> /// <param name="yearFraction1">The year fraction1.</param> /// <param name="yearFraction2">The year fraction2.</param> /// <param name="ratedays">The ratedays.</param> /// <param name="rateamts">The rateamts.</param> /// <param name="daybasis">The daybasis.</param> /// <returns></returns> public static double GetRateCCLin365(double yearFraction1, double yearFraction2, int[] ratedays, double[] rateamts) { DiscreteCurve rc = CreateCurve(ratedays, rateamts, 365); var rateCurve = new InterpolatedCurve(rc, InterpolationFactory.Create("LinearInterpolation"), false); IPoint pt1 = new Point1D(yearFraction1); IPoint pt2 = new Point1D(yearFraction2); double rt1 = rateCurve.Value(pt1); double rt2 = rateCurve.Value(pt2); if (yearFraction1 != yearFraction2) { return((rt2 * yearFraction2 - rt1 * yearFraction1) / (yearFraction2 - yearFraction1)); } return(0); }
/// <summary> /// Gets the value. /// </summary> /// <param name="pt">The pt.</param> /// <param name="interpolationType">The interpolation.</param> /// <param name="extrapolation">if set to <c>true</c> [extrapolation].</param> /// <param name="times">A vertical array of times.</param> /// <param name="values">A vertical array of values.</param> /// <returns>The value at that point.</returns> public static Double GetValue(Double pt, string interpolationType, bool extrapolation, Double[] times, Double[] values) { var curve = new InterpolatedCurve(new DiscreteCurve(times, values), InterpolationFactory.Create(interpolationType), extrapolation); IPoint point = new Point1D(pt); return(curve.Value(point)); }
/// <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); }
/// <summary> /// Gets the df. /// </summary> /// <param name="days">The days.</param> /// <returns></returns> public decimal GetDf(int days) { var rateCurve = new InterpolatedCurve(new DiscreteCurve(GetYearsArray(), RateArray), InterpolationFactory.Create(InterpolationType), false); double maturityYrs = days / 365.0; IPoint point = new Point1D(maturityYrs); double rate = rateCurve.Value(point); double df = RateAnalytics.ZeroRateToDiscountFactor(rate, maturityYrs, RateType); decimal df0 = Convert.ToDecimal(df); return(df0); }
public void TestInterpolatedCubicCurve() { IInterpolation interp = new CubicSplineInterpolation(); DiscreteCurve curve = new DiscreteCurve(_pointCoords, _pointValues); IInterpolatedSpace interpCurve = new InterpolatedCurve(curve, interp, true); foreach (double point in _testPointArray) { IPoint p = new Point1D(point); Console.WriteLine(interpCurve.Value(p)); } }
public void TestPiecewiseRateInterpolatedCurve() { var interp = new PiecewiseConstantZeroRateInterpolation();//PWL requires the first point to be after the first 2 in the curve.. DiscreteCurve curve = new DiscreteCurve(_pointCoords, _pointValues); IInterpolatedSpace interpCurve = new InterpolatedCurve(curve, interp, true); foreach (double point in _testPointArray) { IPoint p = new Point1D(point); double val = interpCurve.Value(p); double rate = val; Console.WriteLine(val); Console.WriteLine(rate); } }
public void TestAllInterpolatedCurves() { foreach (string interp in _linearInterpolations) { TearDown(); IInterpolation interpolation = InterpolationFactory.Create(interp); DiscreteCurve curve = new DiscreteCurve(_times, _rates); IInterpolatedSpace interpCurve = new InterpolatedCurve(curve, interpolation, true); Debug.WriteLine($"interpolationType : {interp}"); foreach (double point in _testPointArray) { IPoint p = new Point1D(point); Console.WriteLine(interpCurve.Value(p)); } } foreach (string interp in _specialInterpolations) { TearDown(); var interpolation = (WingModelInterpolation)InterpolationFactory.Create(interp); interpolation.Forward = 1.0; interpolation.Spot = 1.0; var curve = new DiscreteCurve(Strikes, Vols); IInterpolatedSpace interpCurve = new InterpolatedCurve(curve, interpolation, true); Debug.WriteLine($"interpolationType : {interp}"); foreach (double point in _testPointArray) { IPoint p = new Point1D(point); Console.WriteLine(interpCurve.Value(p)); } } foreach (string interp in _logLinearInterpolations) { TearDown(); double[] exp = SetUp(); IInterpolation interpolation = InterpolationFactory.Create(interp); DiscreteCurve curve = new DiscreteCurve(_times, exp); IInterpolatedSpace interpCurve = new InterpolatedCurve(curve, interpolation, true); Debug.WriteLine($"interpolationType : {interp}"); foreach (double point in _testPointArray) { IPoint p = new Point1D(point); Console.WriteLine(interpCurve.Value(p)); } } }
/// <summary> /// Anns the yield. /// </summary> /// <param name="baseDate">The base date.</param> /// <param name="targetDate">The target date.</param> /// <param name="divCurve">The div curve.</param> /// <param name="ratetimes">The rate times.</param> /// <param name="rateamts">The rate amounts.</param> /// <param name="im">the im?</param> /// <param name="cf">The cf?</param> /// <returns></returns> public static double GetPVDivs(double yearFraction1, double yearFraction2, int[] divdays, double[] divamts, int[] ratedays, double[] rateamts, string im, CompoundingFrequencyEnum cf, int daybasis) { DiscreteCurve divCurve = CreateCurve(divdays, divamts, daybasis); DiscreteCurve rc = CreateCurve(ratedays, rateamts, daybasis); List <IPoint> points = divCurve.GetPointList(); double sum = 0; var rateCurve = new InterpolatedCurve(rc, InterpolationFactory.Create(im), false); foreach (IPoint pt in points) { decimal t = Convert.ToDecimal(pt.GetX()); decimal rate = Convert.ToDecimal(rateCurve.Value(pt)); double df = Convert.ToDouble(GetDiscountFactor(t, rate, cf)); if ((pt.GetX() <= yearFraction2) & (pt.GetX() > yearFraction1)) { sum += pt.FunctionValue * df; } } return(sum); }
/// <summary> /// Anns the yield. /// </summary> /// <param name="baseDate">The base date.</param> /// <param name="targetDate">The target date.</param> /// <param name="divCurve">The div curve.</param> /// <param name="ratetimes">The rate times.</param> /// <param name="rateamts">The rate amounts.</param> /// <param name="im">the im?</param> /// <param name="cf">The cf?</param> /// <returns></returns> public static double GetPVDivs(DateTime baseDate, DateTime targetDate, DiscreteCurve divCurve, double[] ratetimes, double[] rateamts, string im, CompoundingFrequencyEnum cf) { List <IPoint> points = divCurve.GetPointList(); double sum = 0; var rateCurve = new InterpolatedCurve(new DiscreteCurve(ratetimes, rateamts), InterpolationFactory.Create(im), false); int t0 = (targetDate - baseDate).Days; double maturity = t0 / 365.0; foreach (IPoint pt in points) { decimal t = Convert.ToDecimal(pt.GetX()); decimal rate = Convert.ToDecimal(rateCurve.Value(pt)); double df = Convert.ToDouble(GetDiscountFactor(t, rate, cf)); if ((pt.GetX() <= maturity) & (pt.GetX() > 0)) { sum += pt.FunctionValue * df; } } return(sum); }