public void interpolate(string interpolate_method) { List <double> rates = jumps_; List <DateTime> jumpDates = jumpDates_; IInterpolation target = new KrugerCubicSpline(); if (interpolate_method == "Linear") { target = new Linear(); } rates.Insert(0, 0); double[] rate_old = rates.ToArray(); OMLib.Conventions.DayCount.ActualActual dc = new OMLib.Conventions.DayCount.ActualActual(OMLib.Conventions.DayCount.ActualActual.Convention.ISDA); double[] x = new double[jumpDates.Count + 1]; x[0] = 0; if (jumpDates != null) { for (int i = 0; i < jumpDates.Count; i++) { x[i + 1] = dc.DayCount(latestReference_, jumpDates[i]); } } int n = dc.DayCount(latestReference_, jumpDates.Last()); double[] x_New = new double[n + 1]; for (int i = 0; i < n; i++) { x_New[i] = (double)i; } double[] actual = target.Int(x, rate_old, x_New); List <DateTime> jumpdate = new List <DateTime>(); for (int i = 0; i < x_New.Count(); i++) { jumpdate.Add(latestReference_.AddDays(i)); } List <double> rate = actual.ToList(); rate[n] = rate_old.Last(); jumpDates_ = jumpdate; jumps_ = rate; }
/// <summary> /// This mimics JpmcdsDateListMakeRegular. Produces a set of ascending dates by following the rules:<para> /// If the stub is at the front end, we role backwards from the endDate at an integer multiple of the specified step size (e.g. 3M), /// adding these date until we pass the startDate(this date is not added). If the stub type is short, the startDate is added (as the first date), hence the first period /// will be less than (or equal to) the remaining periods. If the stub type is long, the startDate is also added, but the date immediately /// </para> /// after that is removed, so the first period is longer than the remaining.<para> /// If the stub is at the back end, we role forward from the startDate at an integer multiple of the specified step size (e.g. 3M), /// adding these date until we pass the endDate(this date is not added). If the stub type is short, the endDate is added (as the last date), hence the last period /// will be less than (or equal to) the other periods. If the stub type is long, the endDate is also added, but the date immediately /// before that is removed, so the last period is longer than the others. /// /// </para> /// </summary> /// <param name="startDate"> The start date - this will be the first entry in the list </param> /// <param name="endDate"> The end date - this will be the last entry in the list </param> /// <param name="step"> the step period (e.g. 3M - will produce dates every 3 months, with adjustments at the beginning or end based on stub type) </param> /// <param name="stubType"> the stub convention </param> /// <returns> an array of DateTime </returns> public static DateTime[] getUnadjustedDates(DateTime startDate, DateTime endDate, int step, StubConvention stubType) { if (DateTime.Compare(startDate, endDate) == 0) { // this can only happen if protectionStart == true DateTime[] tempDates = new DateTime[2]; tempDates[0] = startDate; tempDates[1] = endDate; return(tempDates); } OMLib.Conventions.DayCount.ActualActual dc = new OMLib.Conventions.DayCount.ActualActual(OMLib.Conventions.DayCount.ActualActual.Convention.ISDA); double days = (double)365.0 * (step / 12.0); int nApprox = 3 + (int)(dc.DayCount(startDate, endDate) / days); IList <DateTime> dates = new List <DateTime>(nApprox); // stub at front end, so start at endDate and work backwards if (stubType == StubConvention.SHORT_FINAL || stubType == StubConvention.LONG_FINAL || stubType == StubConvention.NONE) { int intervals = 0; DateTime tDate = endDate; while (DateTime.Compare(tDate, startDate) > 0) { dates.Add(tDate); int tStep = step * (++intervals); // this mimics ISDA c code, rather than true market convention tDate = endDate.AddMonths(tStep); } int n = dates.Count; if (tDate.Equals(startDate) || n == 1 || stubType == StubConvention.SHORT_INITIAL) { dates.Add(startDate); } else { // long front stub - remove the last date entry in the list and replace it with startDate dates.RemoveAt(n - 1); dates.Add(startDate); } int m = dates.Count; DateTime[] res = new DateTime[m]; // want to output in ascending chronological order, so need to reverse the list int j = m - 1; for (int i = 0; i < m; i++, j--) { res[j] = dates[i]; } return(res); // stub at back end, so start at startDate and work forward } else { int intervals = 0; DateTime tDate = startDate; while (DateTime.Compare(tDate, endDate) < 0) { dates.Add(tDate); int tStep = step * (++intervals); // this mimics ISDA c code, rather than true market convention tDate = startDate.AddMonths(tStep); } int n = dates.Count; if (tDate.Equals(endDate) || n == 1 || stubType == StubConvention.SHORT_FINAL) { dates.Add(endDate); } else { // long back stub - remove the last date entry in the list and replace it with endDate dates.RemoveAt(n - 1); dates.Add(endDate); } DateTime[] res = new DateTime[dates.Count]; res = dates.ToArray(); return(res); } }