Exemple #1
0
        } // pure static class

        /// <summary>
        /// Raw-level ARIMA forecasting function.
        /// </summary>
        /// <param name="data"> UNMODIFIED, list of double numbers representing time-series with constant time-gap </param>
        /// <param name="forecastSize"> integer representing how many data points AFTER the data series to be
        ///        forecasted </param>
        /// <param name="params"> ARIMA parameters </param>
        /// <returns> a ForecastResult object, which contains the forecasted values and/or error message(s) </returns>
        //JAVA TO C# CONVERTER WARNING: 'final' parameters are not available in .NET:
        //ORIGINAL LINE: public static TimeSeries.Forecast.TimeSeries.Arima.struct.ForecastResult forecast_arima(final double[] data, final int forecastSize, TimeSeries.Forecast.TimeSeries.Arima.struct.ArimaParams params)
        public static ForecastResult forecast_arima(double[] data, int forecastSize, ArimaParams @params)
        {
            try
            {
                int p = @params.p;

                int d = @params.d;

                int         q = @params.q;
                int         P = @params.P;
                int         D = @params.D;
                int         Q = @params.Q;
                int         m = @params.m;
                ArimaParams paramsForecast    = new ArimaParams(p, d, q, P, D, Q, m);
                ArimaParams paramsXValidation = new ArimaParams(p, d, q, P, D, Q, m);
                // estimate ARIMA model parameters for forecasting
                ArimaModel fittedModel = ArimaSolver.estimateARIMA(paramsForecast, data, data.Length, data.Length + 1);

                // compute RMSE to be used in confidence interval computation
                double rmseValidation = ArimaSolver.computeRMSEValidation(data, ForecastUtil.testSetPercentage, paramsXValidation);
                fittedModel.RMSE = rmseValidation;
                ForecastResult forecastResult = fittedModel.Forecast(forecastSize);

                // populate confidence interval
                forecastResult.Sigma2AndPredicationInterval = fittedModel.Params;

                // add logging messages
                forecastResult.Log("{" + "\"Best ModelInterface Param\" : \"" + fittedModel.Params.summary() + "\"," + "\"Forecast Size\" : \"" + forecastSize + "\"," + "\"Input Size\" : \"" + data.Length + "\"" + "}");

                // successfully built ARIMA model and its forecast
                return(forecastResult);
            }
            catch (Exception ex)
            {
                // failed to build ARIMA model
                throw new Exception("Failed to build ARIMA forecast: " + ex.Message);
            }
        }
Exemple #2
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;
        }