public virtual void ConstructorTests()
        {
            double[][] data = new double[][]
            {
                new double[] { 3.0, 3.0, 3.0 },
                new double[] { 3.0, 3.0, 3.0 },
                new double[] { 3.0, 3.0, 3.0 }
            };

            InsightsMatrix im1 = new InsightsMatrix(data, false);

            Assert.True(im1.NumberOfColumns == 3);
            Assert.True(im1.NumberOfRows == 3);
            for (int i = 0; i < im1.NumberOfColumns; i++)
            {
                for (int j = 0; j < im1.NumberOfColumns; j++)
                {
                    Assert.True(im1.get(i, j) == 3.0);
                }
            }
            im1.set(0, 0, 0.0);
            Assert.True(im1.get(0, 0) == 0.0);
            im1.set(0, 0, 3.0);

            InsightsVector iv = new InsightsVector(3, 3.0);

            for (int i = 0; i < im1.NumberOfColumns; i++)
            {
                Assert.True(im1.timesVector(iv).get(i) == 27.0);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Perform Yule-Walker algorithm to the given timeseries data
        /// </summary>
        /// <param name="data"> input data </param>
        /// <param name="p"> YuleWalker Parameter </param>
        /// <returns> array of Auto-Regressive parameter estimates. Index 0 contains coefficient of lag 1 </returns>
        //JAVA TO C# CONVERTER WARNING: 'final' parameters are not available in .NET:
        //ORIGINAL LINE: public static double[] fit(final double[] data, final int p)
        public static double[] fit(double[] data, int p)
        {
            int length = data.Length;

            if (length == 0 || p < 1)
            {
                throw new Exception("fitYuleWalker - Invalid Parameters" + "length=" + length + ", p = " + p);
            }

            double[] r = new double[p + 1];
            foreach (double aData in data)
            {
                r[0] += Math.Pow(aData, 2);
            }
            r[0] /= length;

            for (int j = 1; j < p + 1; j++)
            {
                for (int i = 0; i < length - j; i++)
                {
                    r[j] += data[i] * data[i + j];
                }
                r[j] /= (length);
            }

            //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
            //ORIGINAL LINE: final TimeSeries.Forecast.matrix.InsightsMatrix toeplitz = TimeSeries.Forecast.timeseries.timeseriesutil.ForecastUtil.initToeplitz(java.util.Arrays.copyOfRange(r, 0, p));
            InsightsMatrix toeplitz = ForecastUtil.initToeplitz(Utilities.CopyOfRange(r, 0, p));
            //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
            //ORIGINAL LINE: final TimeSeries.Forecast.matrix.InsightsVector rVector = new TimeSeries.Forecast.matrix.InsightsVector(java.util.Arrays.copyOfRange(r, 1, p + 1), false);
            InsightsVector rVector = new InsightsVector(Utilities.CopyOfRange(r, 1, p + 1), false);

            return(toeplitz.solveSPDIntoVector(rVector, ForecastUtil.maxConditionNumber).deepCopy());
        }
        public virtual void DotOperationExceptionTest()
        {
            InsightsVector rowVec = new InsightsVector(4, 1);
            InsightsVector colVec = new InsightsVector(3, 2);
            Exception      ex     = Assert.Throws <Exception>(() => rowVec.dot(colVec));

            Assert.Equal("[InsightsVector][dot] invalid vector size.", ex.Message);
        }
        public virtual void DotOperationTest()
        {
            InsightsVector rowVec = new InsightsVector(3, 1.1);
            InsightsVector colVec = new InsightsVector(3, 2.2);

            double expect = 1.1 * 2.2 * 3;
            double actual = rowVec.dot(colVec);

            Assert.Equal(expect, actual, 0);
        }
Exemplo n.º 5
0
        //JAVA TO C# CONVERTER WARNING: 'final' parameters are not available in .NET:
        //ORIGINAL LINE: private static TimeSeries.Forecast.matrix.InsightsVector iterationStep(final TimeSeries.Forecast.TimeSeries.Arima.struct.ArimaParams params, final double[] data, final double[] errors, final double[][] matrix, final int r, final int length, final int size)
        private static InsightsVector iterationStep(ArimaParams @params, double[] data, double[] errors, double[][] matrix, int r, int length, int size)
        {
            int rowIdx = 0;

            // copy over shifted timeseries data into matrix
            //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
            //ORIGINAL LINE: final int[] offsetsAR = params.getOffsetsAR();
            int[] offsetsAR = @params.OffsetsAR;
            foreach (int pIdx in offsetsAR)
            {
                Array.Copy(data, r - pIdx, matrix[rowIdx], 0, size);
                ++rowIdx;
            }
            // copy over shifted errors into matrix
            //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
            //ORIGINAL LINE: final int[] offsetsMA = params.getOffsetsMA();
            int[] offsetsMA = @params.OffsetsMA;
            foreach (int qIdx in offsetsMA)
            {
                Array.Copy(errors, r - qIdx, matrix[rowIdx], 0, size);
                ++rowIdx;
            }

            // instantiate matrix to perform least squares algorithm
            //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
            //ORIGINAL LINE: final TimeSeries.Forecast.matrix.InsightsMatrix zt = new TimeSeries.Forecast.matrix.InsightsMatrix(matrix, false);
            InsightsMatrix zt = new InsightsMatrix(matrix, false);

            // instantiate target vector
            //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
            //ORIGINAL LINE: final double[] vector = new double[size];
            double[] vector = new double[size];
            Array.Copy(data, r, vector, 0, size);
            //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
            //ORIGINAL LINE: final TimeSeries.Forecast.matrix.InsightsVector x = new TimeSeries.Forecast.matrix.InsightsVector(vector, false);
            InsightsVector x = new InsightsVector(vector, false);

            // obtain least squares solution
            //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
            //ORIGINAL LINE: final TimeSeries.Forecast.matrix.InsightsVector ztx = zt.timesVector(x);
            InsightsVector ztx = zt.timesVector(x);
            //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
            //ORIGINAL LINE: final TimeSeries.Forecast.matrix.InsightsMatrix ztz = zt.computeAAT();
            InsightsMatrix ztz = zt.computeAAT();
            //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
            //ORIGINAL LINE: final TimeSeries.Forecast.matrix.InsightsVector estimatedVector = ztz.solveSPDIntoVector(ztx, TimeSeries.Forecast.timeseries.timeseriesutil.ForecastUtil.maxConditionNumber);
            InsightsVector estimatedVector = ztz.solveSPDIntoVector(ztx, ForecastUtil.maxConditionNumber);

            return(estimatedVector);
        }
        public virtual void ConstructorTests()
        {
            InsightsVector iv = new InsightsVector(10, 3.0);

            Assert.True(iv.size() == 10);
            for (int i = 0; i < iv.size(); i++)
            {
                Assert.True(iv.get(i) == 3.0);
            }

            double[]       data = new double[] { 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0 };
            InsightsVector iv3  = new InsightsVector(data, false);

            for (int i = 0; i < iv3.size(); i++)
            {
                Assert.True(iv3.get(i) == 3.0);
            }
        }
        public virtual void SolverTestSimple()
        {
            double[][] A = new double[][]
            {
                new double[] { 2.0 }
            };
            double[] B        = new double[] { 4.0 };
            double[] solution = new double[] { 2.0 };

            InsightsMatrix im = new InsightsMatrix(A, true);
            InsightsVector iv = new InsightsVector(B, true);

            InsightsVector solved = im.solveSPDIntoVector(iv, -1);

            for (int i = 0; i < solved.size(); i++)
            {
                Assert.True(solved.get(i) == solution[i]);
            }
        }
        public virtual void TimesVectorTestIncorrectDimension()
        {
            double[][] A = new double[][]
            {
                new double[] { 1.0, 1.0, 1.0 },
                new double[] { 2.0, 2.0, 2.0 },
                new double[] { 3.0, 3.0, 3.0 }
            };

            double[] x = new double[] { 4.0, 4.0, 4.0, 4.0 };

            InsightsMatrix im = new InsightsMatrix(A, true);
            InsightsVector iv = new InsightsVector(x, true);

            Exception ex = Assert.Throws <Exception>(() => im.timesVector(iv));

            //InsightsVector solved =

            Assert.Equal("[InsightsMatrix][timesVector] size mismatch", ex.Message);
        }
        public virtual void TimesVectorTestSimple()
        {
            double[][] A = new double[][]
            {
                new double[] { 1.0, 1.0 },
                new double[] { 2.0, 2.0 }
            };

            double[] x        = new double[] { 3.0, 4.0 };
            double[] solution = new double[] { 7.0, 14.0 };

            InsightsMatrix im = new InsightsMatrix(A, true);
            InsightsVector iv = new InsightsVector(x, true);

            InsightsVector solved = im.timesVector(iv);

            for (int i = 0; i < solved.size(); i++)
            {
                Assert.True(solved.get(i) == solution[i]);
            }
        }
Exemplo n.º 10
0
        /// <summary>
        /// Estimate ARMA(p,q) parameters, i.e. AR-parameters: \phi_1, ... , \phi_p
        ///                                     MA-parameters: \theta_1, ... , \theta_q
        /// Input data is assumed to be stationary, has zero-mean, aligned, and imputed
        /// </summary>
        /// <param name="data_orig"> original data </param>
        /// <param name="params"> ARIMA parameters </param>
        /// <param name="forecast_length"> forecast length </param>
        /// <param name="maxIteration"> maximum number of iteration </param>
        //JAVA TO C# CONVERTER WARNING: 'final' parameters are not available in .NET:
        //ORIGINAL LINE: public static void estimateARMA(final double[] data_orig, final TimeSeries.Forecast.TimeSeries.Arima.struct.ArimaParams params, final int forecast_length, final int maxIteration)
        public static void estimateARMA(double[] data_orig, ArimaParams @params, int forecast_length, int maxIteration)
        {
            //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
            //ORIGINAL LINE: final double[] data = new double[data_orig.length];
            double[] data = new double[data_orig.Length];
            //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
            //ORIGINAL LINE: final int total_length = data.length;
            int total_length = data.Length;

            Array.Copy(data_orig, 0, data, 0, total_length);
            //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
            //ORIGINAL LINE: final int r = (params.getDegreeP() > params.getDegreeQ()) ? 1 + params.getDegreeP() : 1 + params.getDegreeQ();
            int r = (@params.DegreeP > @params.DegreeQ) ? 1 + @params.DegreeP : 1 + @params.DegreeQ;
            //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
            //ORIGINAL LINE: final int length = total_length - forecast_length;
            int length = total_length - forecast_length;
            //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
            //ORIGINAL LINE: final int size = length - r;
            int size = length - r;

            if (length < 2 * r)
            {
                throw new Exception("not enough data points: length=" + length + ", r=" + r);
            }

            // step 1: apply Yule-Walker method and estimate AR(r) model on input data
            //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
            //ORIGINAL LINE: final double[] errors = new double[length];
            double[] errors = new double[length];
            //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
            //ORIGINAL LINE: final double[] yuleWalkerParams = applyYuleWalkerAndGetInitialErrors(data, r, length, errors);
            double[] yuleWalkerParams = applyYuleWalkerAndGetInitialErrors(data, r, length, errors);
            for (int j = 0; j < r; ++j)
            {
                errors[j] = 0;
            }

            // step 2: iterate Least-Square fitting until the parameters converge
            // instantiate Z-matrix
            //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
            //ORIGINAL LINE: final double[][] matrix = new double[params.getNumParamsP() + params.getNumParamsQ()][size];
            //JAVA TO C# CONVERTER NOTE: The following call to the 'RectangularArrays' helper class reproduces the rectangular array initialization that is automatic in Java:
            //ORIGINAL LINE: double[][] matrix = new double[params.NumParamsP + params.NumParamsQ][size];
            double[][] matrix = RectangularArrays.ReturnRectangularDoubleArray(@params.NumParamsP + @params.NumParamsQ, size);

            double         bestRMSE        = -1; // initial value
            int            remainIteration = maxIteration;
            InsightsVector bestParams      = null;

            while (--remainIteration >= 0)
            {
                //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
                //ORIGINAL LINE: final TimeSeries.Forecast.matrix.InsightsVector estimatedParams = iterationStep(params, data, errors, matrix, r, length, size);
                InsightsVector estimatedParams = iterationStep(@params, data, errors, matrix, r, length, size);
                //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
                //ORIGINAL LINE: final TimeSeries.Forecast.matrix.InsightsVector originalParams = params.getParamsIntoVector();
                InsightsVector originalParams = @params.ParamsIntoVector;
                @params.ParamsFromVector = estimatedParams;

                // forecast for validation data and compute RMSE
                //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
                //ORIGINAL LINE: final double[] forecasts = ArimaSolver.forecastARMA(params, data, length, data.length);
                double[] forecasts = ArimaSolver.forecastARMA(@params, data, length, data.Length);
                //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
                //ORIGINAL LINE: final double anotherRMSE = ArimaSolver.computeRMSE(data, forecasts, length, 0, forecast_length);
                double anotherRMSE = ArimaSolver.ComputeRMSE(data, forecasts, length, 0, forecast_length);
                // update errors
                //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
                //ORIGINAL LINE: final double[] train_forecasts = ArimaSolver.forecastARMA(params, data, r, data.length);
                double[] train_forecasts = ArimaSolver.forecastARMA(@params, data, r, data.Length);
                for (int j = 0; j < size; ++j)
                {
                    errors[j + r] = data[j + r] - train_forecasts[j];
                }
                if (bestRMSE < 0 || anotherRMSE < bestRMSE)
                {
                    bestParams = estimatedParams;
                    bestRMSE   = anotherRMSE;
                }
            }
            @params.ParamsFromVector = bestParams;
        }