Ejemplo n.º 1
0
        public void SSAForecast_Test()
        {
            var _ts = getAirPassengersData(); // Monthly time-series data
            //DataFrame.ToCsv("usa_deaths.csv",df);
            //var ts = df.ToSeries().Select(x => Convert.ToDouble(x));
            //embed
            var ssa = new SSA(_ts);
            //embed
            var x = ssa.Embedding(12);

            //decompose
            ssa.Decompose();

            //
            var charts = ssa.PlotEigenPairs();

            XPlot.Plotly.Chart.ShowAll(charts);


            var ff        = ssa.Forecast(new int[] { 1, 2, 3, 5, 7 }, 6, method: Forecasting.Rforecasing);
            var scatters1 = new Scatter()
            {
                name = "Predicted", x = Enumerable.Range(1, ff.Length), y = ff, mode = "line",
            };
            var scatters2 = new Scatter()
            {
                name = "Actual", x = Enumerable.Range(1, _ts.Count()), y = _ts, mode = "line", fillcolor = "Blue"
            };

            var chart = XPlot.Plotly.Chart.Plot <Trace>(new Trace[] { scatters1, scatters2 });

            chart.Show();
        }
Ejemplo n.º 2
0
        public void SSA_TestVForecast()
        {
            int stepsAhead = 11;

            var ssa = new SSA(_ts);

            ssa.Embedding(10);
            ssa.Decompose();
            //r forecasting
            var ts1 = ssa.Forecast(new int[2] {
                1, 2
            }, stepsAhead, method: Forecasting.Vforecasting);

            Assert.Equal(1, ts1[0], 5);
            Assert.Equal(2, ts1[1], 5);
            Assert.Equal(3, ts1[2], 5);
            Assert.Equal(4, ts1[3], 5);
            Assert.Equal(5, ts1[4], 5);
            Assert.Equal(6, ts1[5], 5);
            Assert.Equal(7, ts1[6], 5);
            Assert.Equal(8, ts1[7], 5);
            Assert.Equal(9, ts1[8], 5);
            Assert.Equal(10, ts1[9], 5);
            Assert.Equal(11, ts1[10], 5);
            Assert.Equal(12, ts1[11], 5);
            Assert.Equal(13, ts1[12], 5);
            Assert.Equal(14, ts1[13], 5);
            Assert.Equal(15, ts1[14], 5);
            Assert.Equal(16, ts1[15], 5);
            Assert.Equal(17, ts1[16], 5);
            Assert.Equal(18, ts1[17], 5);
            Assert.Equal(19, ts1[18], 5);
            Assert.Equal(20, ts1[19], 5);

            //forecast
            Assert.Equal(21, ts1[20], 5);
            Assert.Equal(22, ts1[21], 5);
            Assert.Equal(23, ts1[22], 5);
            Assert.Equal(24, ts1[23], 5);
            Assert.Equal(25, ts1[24], 5);
            Assert.Equal(26, ts1[25], 5);
            Assert.Equal(27, ts1[26], 5);
            Assert.Equal(28, ts1[27], 5);
            Assert.Equal(29, ts1[28], 5);
            Assert.Equal(30, ts1[29], 5);
            Assert.Equal(31, ts1[30], 5);
        }
Ejemplo n.º 3
0
        public static void Forecasting()
        {
            //
            var strPath = $"AirPassengers.csv";
            var mlDF    = DataFrame.FromCsv(strPath, sep: ',');
            var ts      = mlDF["#Passengers"].Select(f => Convert.ToDouble(f));//create time series

            //create Singular Spectrum Analysis object
            var ssa = new SSA(ts);

            //perform analysis
            ssa.Fit(36);

            //helper vars to hold data
            int n        = 4;
            var setIndex = Enumerable.Range(0, n);

            double[][] ts_components = new double[setIndex.Count()][];

            //cumulative components array
            double[] cumTS = new double[ts.Count()];

            //take the first n components and plot it
            var models = new List <PlotModel>();

            foreach (var i in setIndex)
            {
                ts_components[i] = ssa.Reconstruct(ssa.Xs[i]);
                cumTS            = cumTS.Add(ts_components[i]);
                var m11 = Daany.Plot.ChartComponent.LinePlot("", $"TS Components {i+1}",
                                                             Enumerable.Range(1, ts_components[i].Length).Select(t => (double)t).ToArray(), ts_components[i], System.Drawing.Color.Blue, OxyPlot.MarkerType.Plus);
                //
                models.Add(m11);
            }
            //plot component together
            ChartComponent.ShowPlots("Passenger TS Components", models.ToArray()).Wait();

            //plot sum of the components
            var x  = Enumerable.Range(1, cumTS.Length).Select(t => (double)t).ToArray();
            var m1 = Daany.Plot.ChartComponent.LineSeries("Predicted SSA",
                                                          x, cumTS, System.Drawing.Color.Blue, OxyPlot.MarkerType.Plus);
            var m2 = Daany.Plot.ChartComponent.LineSeries("Original Time Series",
                                                          x, ts.ToArray(), System.Drawing.Color.Red, OxyPlot.MarkerType.Plus);

            var model = new PlotModel();

            model.Series.Add(m1);
            model.Series.Add(m2);
            ChartComponent.ShowPlots("Passenger TS vs Predicted", model).Wait();

            //Forecasting of time series
            var stepAHead   = 12;
            var predictedTS = ssa.Reconstruct(15);
            var predicted   = ssa.Forecast(stepAHead);

            //create three plotmodel: actual TS, predicted TS, Forcast ts ahead
            var mm3 = Daany.Plot.ChartComponent.LineSeries("Predicted Time Series",
                                                           x, predictedTS, System.Drawing.Color.Green, OxyPlot.MarkerType.Plus);
            var mm2 = Daany.Plot.ChartComponent.LineSeries("Original Time Series",
                                                           x, ts.ToArray(), System.Drawing.Color.Red, OxyPlot.MarkerType.Plus);

            //forecast steps ahead
            var x1 = Enumerable.Range(cumTS.Length, stepAHead + 1).Select(t => (double)t).ToArray();
            //start with the last point form the times series
            var y   = predicted.Skip(ts.Count() - 1).Take(stepAHead + 1).ToArray();
            var mm1 = Daany.Plot.ChartComponent.LineSeries($"Forecast of {stepAHead}",
                                                           x1, y, System.Drawing.Color.Blue, OxyPlot.MarkerType.Plus);
            //add series to model
            var model1 = new PlotModel();

            model1.Series.Add(mm1);
            model1.Series.Add(mm2);
            model1.Series.Add(mm3);

            //plot forecasting
            ChartComponent.ShowPlots("Passenger TS vs Forecasting", model1).Wait();
        }
Ejemplo n.º 4
0
        public void SSA_Correlation_Test02()
        {
            //
            var df = getMonthlyDeatsData(); // Monthly time-series data
            //DataFrame.ToCsv("usa_deaths.csv",df);
            var ts = df.ToSeries().Select(x => Convert.ToDouble(x));
            //embed
            var ssa = new SSA(ts);
            //embed
            var x = ssa.Embedding(24);

            PlotMatrix(x).Show();

            //decompose
            ssa.Decompose();
            var plt = new List <XPlot.Plotly.PlotlyChart> ();

            foreach (var tm in ssa.EM)
            {
                plt.Add(PlotMatrix(tm.Value));
            }
            XPlot.Plotly.Chart.ShowAll(plt);

            var f1   = ssa.Reconstruct(1);
            var sct1 = new Scatter()
            {
                name = "Actual", x = Enumerable.Range(1, f1.Length), y = f1, mode = "line",
            };
            var chart1 = XPlot.Plotly.Chart.Plot <Trace>(new Trace[] { sct1 });

            chart1.Show();


            var ff        = ssa.Forecast(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 }, 6, method: Forecasting.Rforecasing);
            var scatters1 = new Scatter()
            {
                name = "Predicted", x = Enumerable.Range(1, ff.Length), y = ff, mode = "line",
            };
            var scatters2 = new Scatter()
            {
                name = "Actual", x = Enumerable.Range(1, ts.Count()), y = ts, mode = "line", fillcolor = "Blue"
            };

            var chart = XPlot.Plotly.Chart.Plot <Trace>(new Trace[] { scatters1, scatters2 });

            // chart.Show();
            // int i= 0;
            // ssa.PlotComponents(12);

            //
            ssa.PlotSingularValues().Show();

            //
            var charts = ssa.PlotEigenPairs();

            XPlot.Plotly.Chart.ShowAll(charts);

            var w = ssa.WCorrelation(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24 });

            ssa.PlotwCorrelation(w).Show();
        }