Exemplo n.º 1
0
        public void Calculate()
        {
            double alpha = 0.4;
            double beta  = 0.1;
            double gamma = 0.3;

            double[] forecast = ArrayBased.Join(Winters.Calculate(input, alpha, beta, gamma, 4, 4));
            //double[] expectedValue = new double[] { 563.257, 328.859, 222.565,
            //  375.344, 367.063, 249.255, 195.221, 315.576, 300.945, 202.255, 121.863,
            //  201.294, 292.949, 263.306, 211.335, 423.599, 532.584, 351.428, 264.999,
            //  568.284, 650.706, 468.626, 360.255, 712.712, 778.179, 521.917, 393.430,
            //  716.726 };
            //
            // Assuming the following expected values are correct for the time being
            // Original values will never match since the initial values are calculated
            //  different.

            double[] expectedValue = new double[] { 500.000, 350.000, 250.000,
                                                    406.033, 403.272, 329.032, 284.676, 319.287, 330.535, 232.415, 196.029,
                                                    327.750, 357.644, 277.437, 318.510, 500.807, 479.548, 380.671, 428.440,
                                                    551.577, 594.451, 493.315, 504.022, 658.566, 712.861, 588.357, 569.817,
                                                    649.326, 657.755, 666.184, 674.614 };

            double[] simplifiedForecast = forecast.Select(f => Math.Round(f, 3)).ToArray();

            Assert.Equal(simplifiedForecast, expectedValue);
        }
Exemplo n.º 2
0
        public void Calculate_zeroPeriods()
        {
            double[] input    = new double[] { 200, 225, 245 };
            double[] forecast = ArrayBased.Join(SimpleAverage.Calculate(input, 0));

            Assert.Equal(input, forecast);
        }
Exemplo n.º 3
0
        public void Calculate_1Periods()
        {
            double[] input         = new double[] { 275, 291, 307, 281, 295 };
            double[] forecast      = ArrayBased.Join(MovingAverage.Calculate(input, 5));
            double   expectedValue = 289.8;

            Assert.Equal(expectedValue, forecast.Last(), 1);
        }
Exemplo n.º 4
0
        public void Calculate_1Period()
        {
            double[] input         = new double[] { 100, 150, 200 };
            double[] forecast      = ArrayBased.Join(SimpleAverage.Calculate(input, 1));
            double[] expectedValue = new double[] { 100, 150, 200, 150 };

            Assert.Equal(expectedValue, forecast);
        }
Exemplo n.º 5
0
        public void Calculate_0Periods()
        {
            double[] input              = new double[] { 654, 658, 665, 672, 673, 671, 693, 694, 701, 703, 702, 710, 712, 711, 728 };
            double[] forecast           = ArrayBased.Join(MovingAverage.Calculate(input, 3));
            double[] expectedValue      = new double[] { 659, 665, 670, 672, 679, 686, 696, 699, 702, 705, 708, 711, 717 };
            double[] simplifiedForecast = forecast.Select(d => Math.Round(d)).ToArray <double>();

            Assert.Equal(expectedValue, simplifiedForecast);
        }
Exemplo n.º 6
0
 public static (double[], double[]) Calculate(double[] inputValue, int amountOfPeriodsToCalculate)
 {
     Name = "Promedio simple";
     //Call function to calculate the 'n' next periods
     double[] full = SimpleAverageHelper.Calculate(
         inputValue.ToList(), inputValue.Sum(), amountOfPeriodsToCalculate).ToArray <double>();
     //Check if a value is less than 0
     if (full.Where(f => f < 0).Count() > 0)
     {
         Name = "!!!" + Name;
     }
     return(ArrayBased.Split(full, amountOfPeriodsToCalculate));
 }
Exemplo n.º 7
0
        public void Calculate()
        {
            double alpha = 0.3;
            double beta  = 0.1;

            double[] forecastValue = ArrayBased.Join(Holt.Calculate(input, 1, alpha, beta));
            double[] expectedValue = new double[] { 500.0, 500.7, 451.0, 380.1,
                                                    376.1, 390.6, 369.4, 304.6, 289.0, 295.0, 251.3, 202.7, 249.6,
                                                    336.4, 337.6, 305.8, 380.9, 438.5, 432.7, 411.1, 476.7, 575.8,
                                                    567.9, 527.3, 577.6, 681 };
            double[] simplifiedForecast = forecastValue.Select(f => Math.Round(f, 1)).ToArray <double>();

            Assert.Equal(expectedValue, simplifiedForecast);
        }
Exemplo n.º 8
0
        public void Calculate_HighAlpha()
        {
            double alpha = 0.6;

            double[] forecast      = ArrayBased.Join(SimpleExponentialSmoothing.Calculate(input, alpha));
            double[] expectedValue = new double[] { 500.0, 500.0, 410.0, 314.0, 365.6,
                                                    416.2, 376.5, 270.6, 288.2, 325.3, 250.1, 190.0, 316.0, 456.4, 392.6,
                                                    307.0, 452.8, 511.1, 444.4, 387.8, 515.1, 656.0, 562.4, 465.0, 576.0,
                                                    740.4 };

            double[] simplifiedForecast = forecast.Select(f => Math.Round(f, 1)).ToArray <double>();

            Assert.Equal(simplifiedForecast, expectedValue);
        }
Exemplo n.º 9
0
        public void Calculate_LowAlpha()
        {
            double alpha = 0.1;

            double[] forecast      = ArrayBased.Join(SimpleExponentialSmoothing.Calculate(input, alpha));
            double[] expectedValue = new double[] { 500.0, 500.0, 485.0, 461.5, 455.4,
                                                    454.8, 444.3, 419.9, 407.9, 402.1, 381.9, 358.7, 362.8, 381.6, 378.4,
                                                    365.6, 384.0, 400.6, 400.5, 395.5, 415.9, 449.3, 454.4, 449.0, 469.1,
                                                    507.2 };

            double[] simplifiedForecast = forecast.Select(f => Math.Round(f, 1)).ToArray <double>();

            Assert.Equal(simplifiedForecast, expectedValue);
        }
Exemplo n.º 10
0
        public static double Calculation(double[] realValue, double[] forecastValue)
        {
            double[] newReal = ArrayBased.ShortenRealValueArray(realValue, forecastValue);
            int      n       = newReal.Length;
            int      m       = forecastValue.Length;

            if (0 == n || 0 == m)
            {
                return(-1);           // throw new EmptyParameterArray();
            }
            //Sum of square differences
            double sumOfDifferences =
                newReal.Zip(forecastValue, (r, f) => Math.Pow(r - f, 2)).Sum();

            return(sumOfDifferences / n);
        }
Exemplo n.º 11
0
 public static (double[], double[]) Calculate(double[] inputValue, double smoothingConstant)
 {
     if (0 == inputValue.Length)
     {
         throw new EmptyParameterArray();
     }
     Name = string.Format("Suavización exponencial simple | cte. de suavización: {0}", smoothingConstant);
     double[] full = SimpleExponentialSmoothingHelper.Calculate(
         inputValue.ToList(), inputValue.Take(1).ToList <double>(), smoothingConstant).ToArray();
     //Check if a value is less than 0
     if (full.Where(f => f < 0).Count() > 0)
     {
         Name = "!!!" + Name;
     }
     return(ArrayBased.Split(full, 1));
 }
Exemplo n.º 12
0
        public static (double[], double[]) Calculate(double[] inputValue, int amountOfPeriodsToCalculate)
        {
            Name = "Simple Linear Regression";
            int inputLength = inputValue.Length;

            double[] xData = Enumerable.Range(0, inputLength).Select(i => (double)i).ToArray();
            Tuple <double, double> parameters = Fit.Line(xData, inputValue);

            double[] full = Enumerable.Range(0, inputLength + amountOfPeriodsToCalculate)
                            .Select(i => parameters.Item1 + parameters.Item2 * i).ToArray();
            //Check if a value is less than 0
            if (full.Where(f => f < 0).Count() > 0)
            {
                Name = "!!!" + Name;
            }
            return(ArrayBased.Split(full, amountOfPeriodsToCalculate));
        }
Exemplo n.º 13
0
 public static (double[], double[]) CalculateBest(double[] inputValue, int amountOfPeriodsToCalculate)
 {
     for (decimal d = 0.1m; d < 1.0m; d += 0.1m)
     {
         for (decimal t = 0.1m; t < 1.0m; t += 0.1m)
         {
             Calculate(inputValue.Take(500).ToArray(), amountOfPeriodsToCalculate, d, t);
         }
     }
     Name = string.Format("Holt | cte. de nivel: {0}, cte. de tendencia: {1}", BestDataSmoothing, BestTrendSmoothing);
     //Check if a value is less than 0
     double[] full = ArrayBased.Join(BestResult);
     if (full.Where(f => f < 0).Count() > 0)
     {
         Name = "!!!" + Name;
     }
     return(BestResult);
 }
Exemplo n.º 14
0
        public static double Calculation(double[] realValue, double[] forecastValue)
        {
            double[] newReal = ArrayBased.ShortenRealValueArray(realValue, forecastValue);
            int      n       = newReal.Length;
            int      m       = forecastValue.Length;

            if (0 == n || 0 == m)
            {
                return(-1);           // throw new EmptyParameterArray();
            }
            //Check if there's any 0 on the realValue array
            if (newReal.Contains(0.0))
            {
                return(-1);                 // throw new ZeroInputArray();
            }
            //Sum of absolute differences
            double sumOfDifferences = newReal.Zip(forecastValue, (r, f) => Math.Abs(r - f) / Math.Abs(r)).Sum();

            return(sumOfDifferences / n);
        }
Exemplo n.º 15
0
 public static (double[], double[]) Calculate(double[] inputValue, int movingAverageTerms)
 {
     if (0 > movingAverageTerms)
     {
         throw new NegativeMovingAverageTerms();
     }
     if (inputValue.Length < movingAverageTerms)
     {
         return(new double[0], new double[0]);                                   // throw new MovingAverageTermsBiggerThanInputSize();
     }
     Name = string.Format("Promedio móvil ({0})", movingAverageTerms);
     //Call function to calculate the 'n' next periods
     double[] full = MovingAverageHelper.Calculate(
         inputValue.ToList(), movingAverageTerms, new List <double>()).ToArray <double>();
     //Check if a value is less than 0
     if (full.Where(f => f < 0).Count() > 0)
     {
         Name = "!!!" + Name;
     }
     return(ArrayBased.Split(full, 1));
 }
Exemplo n.º 16
0
        //116
        public static (double[], double[]) Calculate(double[] inputValue, int amountOfPeriodsToCalculate, int movingAverageTerms)
        {
            if (0 > movingAverageTerms)
            {
                throw new NegativeMovingAverageTerms();
            }
            if (inputValue.Length < movingAverageTerms)
            {
                return(new List <double>()
                {
                    -1
                }.ToArray(), new List <double>()
                {
                    -1
                }.ToArray());                                                                                                  // throw new MovingAverageTermsBiggerThanInputSize();
            }
            if (0 > amountOfPeriodsToCalculate)
            {
                throw new NegativePeriodsToCalculate();
            }
            Name = string.Format("Promedio móvil doble ({0})", movingAverageTerms);
            //Calculate first moving average
            double[] firstAverage = ArrayBased.Join(MovingAverage.Calculate(inputValue, movingAverageTerms));
            //Calculate second moving average
            double[] secondAverage = ArrayBased.Join(MovingAverage.Calculate(firstAverage, movingAverageTerms));
            //Remove first movingAverageTerms - 1 values since they are not use
            List <double> fixFirstAverage = firstAverage.ToList().Skip(movingAverageTerms - 1).ToList();

            //Call function to calculate the 'n' next periods
            double[] full = DoubleMovingAverageHelper.Calculate(
                fixFirstAverage, secondAverage.ToList(), new List <double>(),
                movingAverageTerms, 1, amountOfPeriodsToCalculate).ToArray <double>();
            //Check if a value is less than 0
            if (full.Where(f => f < 0).Count() > 0)
            {
                Name = "!!!" + Name;
            }
            return(ArrayBased.Split(full, amountOfPeriodsToCalculate));
        }
Exemplo n.º 17
0
 public static (double[], double[]) CalculateBest(double[] inputValue, int amountOfPeriodsToCalculate, int s)
 {
     SmallestError = double.MaxValue;
     for (decimal a = 0.1m; a < 1.0m; a += 0.1m)
     {
         for (decimal b = 0.1m; b < 1.0m; b += 0.1m)
         {
             for (decimal g = 0.1m; g < 1.0m; g += 0.1m)
             {
                 Calculate(inputValue.Take(500).ToArray(), a, b, g, amountOfPeriodsToCalculate, s);
             }
         }
     }
     Name = string.Format("Winters | cte. de nivel: {0}, cte. de tendencia: {1}, cte. de estacionalidad: {2}", BestAlpha, BestBeta, BestGamma);
     //Check if a value is less than 0
     double[] full = ArrayBased.Join(BestResult);
     if (full.Where(f => f < 0).Count() > 0)
     {
         Name = "!!!" + Name;
     }
     return(BestResult);
 }
Exemplo n.º 18
0
        public static (double[], double[]) Calculate(
            double[] inputValue, int amountOfPeriodsToCalculate, decimal dataSmoothingFactor, decimal trendSmoothingFactor)
        {
            SmallestError = double.MaxValue;
            double initalSmoothedValue = inputValue[0];

            (List <double>, List <double>)auxValues = HoltHelper.CalculteSmoothedAndTrendValues(
                inputValue.Select(d => (decimal)d).ToList(), dataSmoothingFactor, trendSmoothingFactor, new List <decimal>(), new List <decimal>());
            //Calculate forecast for periods which already have real values
            double[] full =
                HoltHelper.Calculate(auxValues.Item1, auxValues.Item2, inputValue.Take(1).ToList()).ToArray <double>();
            (double[], double[])calculated = ArrayBased.Split(full, amountOfPeriodsToCalculate);
            double mad = MeanAbsoluteDeviation.Calculation(inputValue, calculated.Item1);

            if (!double.IsNaN(mad) && mad < SmallestError)
            {
                SmallestError      = mad;
                BestDataSmoothing  = (double)dataSmoothingFactor;
                BestTrendSmoothing = (double)trendSmoothingFactor;
                BestResult         = calculated;
            }
            return(calculated);
        }