protected async Task <double?> Approximate(DateTime date, TimeSpan step) { switch (IfDataNotExist) { case IfDataNotExist.TakeNearLeft: var last = (await Collector.List(date - TimeSpan.FromDays(365) * 3, date, step)).LastOrDefault(); if (last is null) { return(null); } return(Converter(last, GetNear(last, step))); case IfDataNotExist.Aproximated: var leftValues = await Collector.List(date - TimeSpan.FromDays(365) * 3, date, step); if (leftValues.Count > 0) { var rightValues = await Collector.List(date + step, date + TimeSpan.FromDays(365) * 3, step); if (rightValues.Count > 0) { var values = new List <Entity>() { leftValues.Last(), rightValues.First() }; // leftValues.TakeLast(1).Union(rightValues.Take(1)).ToList(); var interpolator = new LinearInterpolation( new KeyValuePair <double, double>(values[0].Date.Ticks, Converter(values[0], GetNear(values[0], step))), new KeyValuePair <double, double>(values[1].Date.Ticks, Converter(values[1], GetNear(values[1], step))) ); var interpolated = interpolator.GetValue(date.Ticks); return(interpolated); } if (!(Extrapolator is null)) { var previous = leftValues.LastOrDefault(); var extrapolatedNeural = Extrapolator.GetValue(Converter(previous, GetNear(previous, step)), previous.Date, date); return(extrapolatedNeural); } var extrapolator = new LagrangeApproximation(leftValues .TakeLast(2) .Select(x => new KeyValuePair <double, double>(x.Date.Ticks, Converter(x, GetNear(x, step)))) .ToList() ); var extrapolated = extrapolator.GetValue(date.Ticks); return(extrapolated); } return(null); case IfDataNotExist.Crush: return(null); } return(null); }
protected double GetNear(Entity current, TimeSpan step) { if (Collector.TryGet(current.Date - step, step, out var result)) { return(result.Selector()); } var previous = Collector.GetNearLeft(current.Date); var interpolation = new LinearInterpolation( new KeyValuePair <double, double>(previous.Date.Ticks, previous.Selector()), new KeyValuePair <double, double>(current.Date.Ticks, current.Selector()) ); var interpolatedResult = interpolation.GetValue((current.Date - step).Ticks); return(interpolatedResult); }