public double GetRate(DateTime targetDate) { RateAtDate before = new RateAtDate(); RateAtDate after = new RateAtDate(); foreach (var item in sortedSource) { if (item.Date == targetDate) { return(item.Rate); } if (item.Date < targetDate) { before = item; } else { after = item; break; } } return(InterpolatedRate(before, after, targetDate)); }
protected override double InterpolatedRate(RateAtDate before, RateAtDate after, DateTime targetDate) { double mu = Utils.GetMu(before, after, targetDate); double mu2 = (1 - Math.Cos(mu * Math.PI)) / 2; return(before.Rate * (1 - mu2) + after.Rate * mu2); }
internal static double GetMu(RateAtDate before, RateAtDate after, DateTime targetDate) { Contract.Requires(after.Date != before.Date); return((targetDate - before.Date).TotalMilliseconds / (after.Date - before.Date).TotalMilliseconds); }
public double GetRate(DateTime targetDate) { Contract.Requires(SortedSource != null); Contract.Requires(targetDate >= SortedSource[0].Date); Contract.Requires(targetDate <= SortedSource[SortedSource.Count - 1].Date); RateAtDate before = new RateAtDate(); RateAtDate after = new RateAtDate(); foreach (var item in sortedSource) { if (item.Date == targetDate) { return(item.Rate); } if (item.Date < targetDate) { before = item; } else { after = item; break; } } return(InterpolatedRate(before, after, targetDate)); }
static void Main(string[] args) { RateAtDate[] rates = new RateAtDate[] { new RateAtDate("10/10/2009", 4.01), new RateAtDate("11/10/2009", 4.01), new RateAtDate("12/10/2009", 4.02), new RateAtDate("17/10/2009", 4.14), new RateAtDate("31/10/2009", 4.25), new RateAtDate("30/11/2009", 4.35), new RateAtDate("28/02/2010", 4.5), new RateAtDate("31/05/2010", 4.75), new RateAtDate("10/10/2010", 5.13), new RateAtDate("10/10/2015", 5.5), }; string rootPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); Visualizer visualizer = new Visualizer(rootPath + @"\testdata"); visualizer.Visualize(rates, 3000); }
protected abstract double InterpolatedRate(RateAtDate before, RateAtDate after, DateTime targetDate);
protected override double InterpolatedRate(RateAtDate before, RateAtDate after, DateTime targetDate) { double mu = Utils.GetMu(before, after, targetDate); return(before.Rate * (1 - mu) + after.Rate * mu); }