Esempio n. 1
0
        public void Test_LinearRegression()
        {
            //----- slope
            {
                double[,] testVectors =
                {
                    { 100.00000, 0.00000, 0.00000, 0.00000, 0.00000, },
                    { 105.00000, 1.50000, 0.00000, 0.00000, 0.00000, },
                    { 102.50000, 1.25000, 1.50000, 0.00000, 0.00000, },
                    { 107.50000, 2.00000, 1.25000, 1.50000, 0.00000, },
                    { 105.00000, 0.50000, 2.00000, 1.25000, 1.50000, },
                    { 110.00000, 2.00000, 0.50000, 2.00000, 1.25000, },
                    { 107.50000, 0.50000, 2.00000, 0.50000, 2.00000, },
                    { 112.50000, 2.00000, 0.50000, 2.00000, 0.50000, },
                    { 110.00000, 0.50000, 2.00000, 0.50000, 2.00000, },
                    { 115.00000, 2.00000, 0.50000, 2.00000, 0.50000, },
                };

                TimeSeries <double> stimulus = new TimeSeries <double>();

                string msg = "";
                for (int row = 0; row <= testVectors.GetUpperBound(0); row++)
                {
                    stimulus.Value = testVectors[row, 0];
                    ITimeSeries <double> response = stimulus.LinRegression(4).Slope;
                    msg += string.Format("{{{0:F5}, ", testVectors[row, 0]);

                    for (int col = 1; col <= testVectors.GetUpperBound(1); col++)
                    {
                        int    t             = col - 1;
                        double responseValue = response[t];
                        double expectedValue = testVectors[row, col];
                        msg += string.Format("{0:F5}, ", responseValue);

                        Assert.IsTrue(Math.Abs(responseValue - expectedValue) < 1e-5);
                    }
                    msg += "},\n";
                }
                Output.WriteLine(msg);
            }

            //----- intercept
            {
                double[,] testVectors =
                {
                    { 100.00000, 100.00000, 100.00000, 100.00000, 100.00000, },
                    { 105.00000, 103.50000, 100.00000, 100.00000, 100.00000, },
                    { 102.50000, 103.75000, 103.50000, 100.00000, 100.00000, },
                    { 107.50000, 106.75000, 103.75000, 103.50000, 100.00000, },
                    { 105.00000, 105.75000, 106.75000, 103.75000, 103.50000, },
                    { 110.00000, 109.25000, 105.75000, 106.75000, 103.75000, },
                    { 107.50000, 108.25000, 109.25000, 105.75000, 106.75000, },
                    { 112.50000, 111.75000, 108.25000, 109.25000, 105.75000, },
                    { 110.00000, 110.75000, 111.75000, 108.25000, 109.25000, },
                    { 115.00000, 114.25000, 110.75000, 111.75000, 108.25000, },
                };

                TimeSeries <double> stimulus = new TimeSeries <double>();

                string msg = "";
                for (int row = 0; row <= testVectors.GetUpperBound(0); row++)
                {
                    stimulus.Value = testVectors[row, 0];
                    ITimeSeries <double> response = stimulus.LinRegression(4).Intercept;
                    msg += string.Format("{{{0:F5}, ", testVectors[row, 0]);

                    for (int col = 1; col <= testVectors.GetUpperBound(1); col++)
                    {
                        int    t             = col - 1;
                        double responseValue = response[t];
                        double expectedValue = testVectors[row, col];
                        msg += string.Format("{0:F5}, ", responseValue);

                        Assert.IsTrue(Math.Abs(responseValue - expectedValue) < 1e-5);
                    }
                    msg += "},\n";
                }
                Output.WriteLine(msg);
            }

            //----- R2
            {
                double[,] testVectors =
                {
                    { 100.00000, 0.00000, 0.00000, 0.00000, 0.00000, },
                    { 105.00000, 0.60000, 0.00000, 0.00000, 0.00000, },
                    { 102.50000, 0.45455, 0.60000, 0.00000, 0.00000, },
                    { 107.50000, 0.64000, 0.45455, 0.60000, 0.00000, },
                    { 105.00000, 0.10000, 0.64000, 0.45455, 0.60000, },
                    { 110.00000, 0.64000, 0.10000, 0.64000, 0.45455, },
                    { 107.50000, 0.10000, 0.64000, 0.10000, 0.64000, },
                    { 112.50000, 0.64000, 0.10000, 0.64000, 0.10000, },
                    { 110.00000, 0.10000, 0.64000, 0.10000, 0.64000, },
                    { 115.00000, 0.64000, 0.10000, 0.64000, 0.10000, },
                };

                TimeSeries <double> stimulus = new TimeSeries <double>();

                string msg = "";
                for (int row = 0; row <= testVectors.GetUpperBound(0); row++)
                {
                    stimulus.Value = testVectors[row, 0];
                    ITimeSeries <double> response = stimulus.LinRegression(4).R2;
                    msg += string.Format("{{{0:F5}, ", testVectors[row, 0]);

                    for (int col = 1; col <= testVectors.GetUpperBound(1); col++)
                    {
                        int    t             = col - 1;
                        double responseValue = response[t];
                        double expectedValue = testVectors[row, col];
                        msg += string.Format("{0:F5}, ", responseValue);

                        Assert.IsTrue(Math.Abs(responseValue - expectedValue) < 1e-5);
                    }
                    msg += "},\n";
                }
                //Output.WriteLine(msg);
            }
        }
        public void Test_LinearRegression()
        {
            //----- slope
            {
                double[,] testVectors =
                {
                    { 100.00000, 0.00000, 0.00000, 0.00000, 0.00000, },
                    { 102.50000, 0.75000, 0.00000, 0.00000, 0.00000, },
                    { 105.00000, 1.75000, 0.75000, 0.00000, 0.00000, },
                    { 107.50000, 2.50000, 1.75000, 0.75000, 0.00000, },
                    { 110.00000, 2.50000, 2.50000, 1.75000, 0.75000, },
                    { 112.50000, 2.50000, 2.50000, 2.50000, 1.75000, },
                    { 115.00000, 2.50000, 2.50000, 2.50000, 2.50000, },
                    { 117.50000, 2.50000, 2.50000, 2.50000, 2.50000, },
                    { 120.00000, 2.50000, 2.50000, 2.50000, 2.50000, },
                    { 122.50000, 2.50000, 2.50000, 2.50000, 2.50000, },
                };

                TimeSeries <double> stimulus = new TimeSeries <double>();

                for (int row = 0; row <= testVectors.GetUpperBound(0); row++)
                {
                    stimulus.Value = testVectors[row, 0];
                    ITimeSeries <double> response = stimulus.LinRegression(4).Slope;
                    //Output.Write("{{{0:F5}, ", testVectors[row, 0]);

                    for (int col = 1; col <= testVectors.GetUpperBound(1); col++)
                    {
                        int    t             = col - 1;
                        double responseValue = response[t];
                        double expectedValue = testVectors[row, col];
                        //Output.Write("{0:F5}, ", responseValue);

                        Assert.IsTrue(Math.Abs(responseValue - expectedValue) < 1e-5);
                    }
                    //Output.WriteLine("},");
                }
            }

            //----- intercept
            {
                double[,] testVectors =
                {
                    { 100.00000, 100.00000, 100.00000, 100.00000, 100.00000, },
                    { 102.50000, 101.75000, 100.00000, 100.00000, 100.00000, },
                    { 105.00000, 104.50000, 101.75000, 100.00000, 100.00000, },
                    { 107.50000, 107.50000, 104.50000, 101.75000, 100.00000, },
                    { 110.00000, 110.00000, 107.50000, 104.50000, 101.75000, },
                    { 112.50000, 112.50000, 110.00000, 107.50000, 104.50000, },
                    { 115.00000, 115.00000, 112.50000, 110.00000, 107.50000, },
                    { 117.50000, 117.50000, 115.00000, 112.50000, 110.00000, },
                    { 120.00000, 120.00000, 117.50000, 115.00000, 112.50000, },
                    { 122.50000, 122.50000, 120.00000, 117.50000, 115.00000, },
                };

                TimeSeries <double> stimulus = new TimeSeries <double>();

                for (int row = 0; row <= testVectors.GetUpperBound(0); row++)
                {
                    stimulus.Value = testVectors[row, 0];
                    ITimeSeries <double> response = stimulus.LinRegression(4).Intercept;
                    //Output.Write("{{{0:F5}, ", testVectors[row, 0]);

                    for (int col = 1; col <= testVectors.GetUpperBound(1); col++)
                    {
                        int    t             = col - 1;
                        double responseValue = response[t];
                        double expectedValue = testVectors[row, col];
                        //Output.Write("{0:F5}, ", responseValue);

                        Assert.IsTrue(Math.Abs(responseValue - expectedValue) < 1e-5);
                    }
                    //Output.WriteLine("},");
                }
            }

            //----- R2
            {
                double[,] testVectors =
                {
                    { 100.00000, 0.00000, 0.00000, 0.00000, 0.00000, },
                    { 102.50000, 0.33333, 0.00000, 0.00000, 0.00000, },
                    { 105.00000, 0.87755, 0.33333, 0.00000, 0.00000, },
                    { 107.50000, 1.00000, 0.87755, 0.33333, 0.00000, },
                    { 110.00000, 1.00000, 1.00000, 0.87755, 0.33333, },
                    { 112.50000, 1.00000, 1.00000, 1.00000, 0.87755, },
                    { 115.00000, 1.00000, 1.00000, 1.00000, 1.00000, },
                    { 117.50000, 1.00000, 1.00000, 1.00000, 1.00000, },
                    { 120.00000, 1.00000, 1.00000, 1.00000, 1.00000, },
                    { 122.50000, 1.00000, 1.00000, 1.00000, 1.00000, },
                };

                TimeSeries <double> stimulus = new TimeSeries <double>();

                for (int row = 0; row <= testVectors.GetUpperBound(0); row++)
                {
                    stimulus.Value = testVectors[row, 0];
                    ITimeSeries <double> response = stimulus.LinRegression(4).R2;
                    //Output.Write("{{{0:F5}, ", testVectors[row, 0]);

                    for (int col = 1; col <= testVectors.GetUpperBound(1); col++)
                    {
                        int    t             = col - 1;
                        double responseValue = response[t];
                        double expectedValue = testVectors[row, col];
                        //Output.Write("{0:F5}, ", responseValue);

                        Assert.IsTrue(Math.Abs(responseValue - expectedValue) < 1e-5);
                    }
                    //Output.WriteLine("},");
                }
            }
        }