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); }
public async Task LearnExtrapolator(double acceptableError = 0.000001, int maxIteration = 1000) { if (IfDataNotExist == IfDataNotExist.Aproximated) { var list = await Collector.List(); var input = new List <Vector>(); var ideal = new List <Vector>(); for (var i = 2; i < list.Count; i++) { input.Add(new double[] { Converter(list[i - 1], list[i - 2].Selector()) }); ideal.Add(new double[] { Converter(list[i], list[i - 1].Selector()) }); } Extrapolator.Learn(input.ToArray(), ideal.ToArray(), acceptableError, maxIteration); } }
/// <summary>Creates a new <see cref="IGridPointCurve<TLabel>"/> object with respect to specified interpolation and extrapolation approaches. /// </summary> /// <typeparam name="TLabel">The type of the label.</typeparam> /// <param name="curveInterpolator">The curve interpolator.</param> /// <param name="leftExtrapolator">The extrapolator on the left side, i.e. from the first grid point to -\infinity.</param> /// <param name="rightExtrapolator">The extrapolator on the right side, i.e. from the last grid point to \infinity.</param> /// <param name="capacity">The number of elements that the new grid point curve can initially store.</param> /// <returns>A <see cref="IGridPointCurve{TLabel}"/> object with respect to the desired interpolation and extrapolation approaches.</returns> public static IGridPointCurve <TLabel> Create <TLabel>(Interpolator curveInterpolator, Extrapolator leftExtrapolator, Extrapolator rightExtrapolator, int capacity = 20) where TLabel : IEquatable <TLabel> { if (curveInterpolator == null) { throw new ArgumentException(nameof(curveInterpolator)); } var interpolator = curveInterpolator.Create(); if (leftExtrapolator == null) { throw new ArgumentNullException(nameof(leftExtrapolator)); } if (leftExtrapolator.ExtrapolationBuildingDirection != Extrapolator.BuildingDirection.FromFirstGridPoint) { throw new ArgumentException("Wrong building direction of curve extrapolation", "leftExtrapolator"); } var left = leftExtrapolator.Create(interpolator); if (rightExtrapolator == null) { throw new ArgumentNullException(nameof(rightExtrapolator)); } var right = rightExtrapolator.Create(interpolator); if (rightExtrapolator.ExtrapolationBuildingDirection != Extrapolator.BuildingDirection.FromLastGridPoint) { throw new ArgumentException("Wrong building direction of curve extrapolation", "rightExtrapolation"); } if ((interpolator is IDifferentiableRealValuedCurve) && (left is IDifferentiableRealValuedCurve) && (right is IDifferentiableRealValuedCurve)) { return(new StandardGridPointCurve <TLabel> .Differentiable(curveInterpolator, interpolator, leftExtrapolator, left, rightExtrapolator, right, capacity)); } return(new StandardGridPointCurve <TLabel>(curveInterpolator, interpolator, leftExtrapolator, left, rightExtrapolator, right, capacity)); }
/// <summary>Creates a new read-only <see cref="IGridPointCurve<TLabel>"/> object with respect to specified interpolation and extrapolation approaches. /// </summary> /// <typeparam name="TLabel">The type of the label.</typeparam> /// <param name="curveInterpolator">The curve interpolator.</param> /// <param name="leftExtrapolator">The extrapolator on the left side, i.e. from the first grid point to -\infinity.</param> /// <param name="rightExtrapolator">The extrapolator on the right side, i.e. from the last grid point to \infinity.</param> /// <param name="gridPointCount">The number of grid points, i.e. the number of relevant elements of <paramref name="gridPointLabels"/>, <paramref name="gridPointArguments"/> and <paramref name="gridPointValues"/> to take into account.</param> /// <param name="gridPointLabels">The labels of the grid points (the reference will be stored only).</param> /// <param name="gridPointArguments">The arguments of the grid points, thus labels of the curve in its <see cref="System.Double"/> representation in ascending order.</param> /// <param name="gridPointValues">The values of the grid points corresponding to <paramref name="gridPointArguments"/>.</param> /// <param name="gridPointArgumentStartIndex">The null-based start index of <paramref name="gridPointArguments"/> and <paramref name="gridPointLabels"/> to take into account.</param> /// <param name="gridPointValueStartIndex">The null-based start index of <paramref name="gridPointValues"/> to take into account.</param> /// <param name="gridPointArgumentIncrement">The increment for <paramref name="gridPointArguments"/> and <paramref name="gridPointLabels"/>.</param> /// <param name="gridPointValueIncrement">The increment for <paramref name="gridPointValues"/>.</param> /// <returns>A read-only <see cref="IGridPointCurve<TLabel>"/> object with respect to the desired interpolation and extrapolation approaches.</returns> /// <remarks>This method is mainly used for two-dimensional surface interpolation. Assuming a large grid point matrix, for example 1.000 x 1.000 and one may apply for example a linear interpolation /// along horizontal direction and afterwards a linear interpolation along vertical direction. Then we create 1.000 curves in horizontal direction, where the (double) labels and values are constant. The /// standard implementation create a copy of 3 * 1.000 x 1.000 = 3.000.000 values and calles the update method of the underlying <see cref="ICurveDataFitting"/> object which again copy 2 * 1.000 * 1.000 = 2.000.000 values, i.e. in total 5 Mio values. /// This implementation takes into account references, the underlying <see cref="ICurveDataFitting"/> implementation creates deep copies of double values and grid point values only, i.e. 2 Mio double values.</remarks> public static IGridPointCurve <TLabel> Create <TLabel>(Interpolator curveInterpolator, Extrapolator leftExtrapolator, Extrapolator rightExtrapolator, int gridPointCount, IList <TLabel> gridPointLabels, IList <double> gridPointArguments, IList <double> gridPointValues, int gridPointArgumentStartIndex = 0, int gridPointValueStartIndex = 0, int gridPointArgumentIncrement = 1, int gridPointValueIncrement = 1) where TLabel : IEquatable <TLabel> { if (curveInterpolator == null) { throw new ArgumentException(nameof(curveInterpolator)); } var interpolator = curveInterpolator.Create(); if (leftExtrapolator == null) { throw new ArgumentNullException(nameof(leftExtrapolator)); } if (leftExtrapolator.ExtrapolationBuildingDirection != Extrapolator.BuildingDirection.FromFirstGridPoint) { throw new ArgumentException("Wrong building direction of curve extrapolation", "leftExtrapolator"); } var left = leftExtrapolator.Create(interpolator); if (rightExtrapolator == null) { throw new ArgumentNullException(nameof(rightExtrapolator)); } var right = rightExtrapolator.Create(interpolator); if (rightExtrapolator.ExtrapolationBuildingDirection != Extrapolator.BuildingDirection.FromLastGridPoint) { throw new ArgumentException("Wrong building direction of curve extrapolation", "rightExtrapolation"); } if ((interpolator is IDifferentiableRealValuedCurve) && (left is IDifferentiableRealValuedCurve) && (right is IDifferentiableRealValuedCurve)) { return(new SmartReadOnlyGridPointCurve <TLabel> .Differentiable(interpolator, left, right, gridPointCount, gridPointLabels, gridPointArguments, gridPointValues, gridPointArgumentStartIndex, gridPointValueStartIndex, gridPointArgumentIncrement, gridPointValueIncrement)); } return(new SmartReadOnlyGridPointCurve <TLabel>(interpolator, left, right, gridPointCount, gridPointLabels, gridPointArguments, gridPointValues, gridPointArgumentStartIndex, gridPointValueStartIndex, gridPointArgumentIncrement, gridPointValueIncrement)); }