Exemplo n.º 1
0
        /// <summary>
        /// Differentiate procedures for forecast and estimate ARIMA.
        /// </summary>
        /// <param name="params"> ARIMA parameters </param>
        /// <param name="dataForecastStationary"> stationary forecast data </param>
        /// <param name="hasSeasonalI"> has seasonal I or not based on the parameter </param>
        /// <param name="hasNonSeasonalI"> has NonseasonalI or not based on the parameter </param>
        /// <returns> merged forecast data </returns>
        private static double[] integrate(ArimaParams @params, double[] dataForecastStationary, bool hasSeasonalI, bool hasNonSeasonalI)
        {
            double[] forecast_merged;
            if (hasSeasonalI && hasNonSeasonalI)
            {
                @params.IntegrateSeasonal(dataForecastStationary);
                @params.IntegrateNonSeasonal(@params.LastIntegrateSeasonal);
                forecast_merged = @params.LastIntegrateNonSeasonal;
            }
            else if (hasSeasonalI)
            {
                @params.IntegrateSeasonal(dataForecastStationary);
                forecast_merged = @params.LastIntegrateSeasonal;
            }
            else if (hasNonSeasonalI)
            {
                @params.IntegrateNonSeasonal(dataForecastStationary);
                forecast_merged = @params.LastIntegrateNonSeasonal;
            }
            else
            {
                forecast_merged = new double[dataForecastStationary.Length];
                Array.Copy(dataForecastStationary, 0, forecast_merged, 0, dataForecastStationary.Length);
            }

            return(forecast_merged);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Differentiate procedures for forecast and estimate ARIMA.
        /// </summary>
        /// <param name="params"> ARIMA parameters </param>
        /// <param name="trainingData"> training data </param>
        /// <param name="hasSeasonalI"> has seasonal I or not based on the parameter </param>
        /// <param name="hasNonSeasonalI"> has NonseasonalI or not based on the parameter </param>
        /// <returns> stationary data </returns>
        private static double[] differentiate(ArimaParams @params, double[] trainingData, bool hasSeasonalI, bool hasNonSeasonalI)
        {
            double[] dataStationary; // currently un-centered
            if (hasSeasonalI && hasNonSeasonalI)
            {
                @params.differentiateSeasonal(trainingData);
                @params.DifferentiateNonSeasonal(@params.LastDifferenceSeasonal);
                dataStationary = @params.LastDifferenceNonSeasonal;
            }
            else if (hasSeasonalI)
            {
                @params.differentiateSeasonal(trainingData);
                dataStationary = @params.LastDifferenceSeasonal;
            }
            else if (hasNonSeasonalI)
            {
                @params.DifferentiateNonSeasonal(trainingData);
                dataStationary = @params.LastDifferenceNonSeasonal;
            }
            else
            {
                dataStationary = new double[trainingData.Length];
                Array.Copy(trainingData, 0, dataStationary, 0, trainingData.Length);
            }

            return(dataStationary);
        }
Exemplo n.º 3
0
 /// <summary>
 /// Set Sigma2(RMSE) and Predication Interval for forecast result.
 /// </summary>
 /// <param name="params"> ARIMA parameters </param>
 /// <param name="forecastResult"> MODIFIED. forecast result </param>
 /// <param name="forecastSize"> size of forecast </param>
 /// <returns> max normalized variance </returns>
 //JAVA TO C# CONVERTER WARNING: 'final' parameters are not available in .NET:
 //ORIGINAL LINE: public static double setSigma2AndPredicationInterval(final TimeSeries.Forecast.TimeSeries.Arima.struct.ArimaParams params, final TimeSeries.Forecast.TimeSeries.Arima.struct.ForecastResult forecastResult, final int forecastSize)
 public static double setSigma2AndPredicationInterval(ArimaParams @params, ForecastResult forecastResult, int forecastSize)
 {
     //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
     //ORIGINAL LINE: final double[] coeffs_AR = params.getCurrentARCoefficients();
     double[] coeffs_AR = @params.CurrentARCoefficients;
     //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
     //ORIGINAL LINE: final double[] coeffs_MA = params.getCurrentMACoefficients();
     double[] coeffs_MA = @params.CurrentMACoefficients;
     return(forecastResult.SetConfInterval(ForecastUtil.confidence_constant_95pct, ForecastUtil.getCumulativeSumOfCoeff(ForecastUtil.ARMAtoMA(coeffs_AR, coeffs_MA, forecastSize))));
 }
Exemplo n.º 4
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);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Input checker
        /// </summary>
        /// <param name="params"> ARIMA parameter </param>
        /// <param name="data"> original data </param>
        /// <param name="startIndex"> start index of ARIMA operation </param>
        /// <param name="endIndex"> end index of ARIMA operation </param>
        /// <returns> whether the inputs are valid </returns>
        private static bool checkARIMADataLength(ArimaParams @params, double[] data, int startIndex, int endIndex)
        {
            bool result = true;

            //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
            //ORIGINAL LINE: final int initialConditionSize = params.d + params.D * params.m;
            int initialConditionSize = @params.d + @params.D * @params.m;

            if (data.Length < initialConditionSize || startIndex < initialConditionSize || endIndex <= startIndex)
            {
                result = false;
            }

            return(result);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Creates the fitted ARIMA model based on the ARIMA parameters.
        /// </summary>
        /// <param name="params"> MODIFIED. ARIMA parameters </param>
        /// <param name="data"> UNMODIFIED. the original time series before differencing / centering </param>
        /// <param name="forecastStartIndex"> the index where the forecast starts. startIndex &le; data.length </param>
        /// <param name="forecastEndIndex"> the index where the forecast stops (exclusive). startIndex &lt; endIndex </param>
        /// <returns> fitted ARIMA model </returns>
        //JAVA TO C# CONVERTER WARNING: 'final' parameters are not available in .NET:
        //ORIGINAL LINE: public static TimeSeries.Forecast.TimeSeries.Arima.struct.ArimaModel estimateARIMA(final TimeSeries.Forecast.TimeSeries.Arima.struct.ArimaParams params, final double[] data, final int forecastStartIndex, final int forecastEndIndex)
        public static ArimaModel estimateARIMA(ArimaParams @params, double[] data, int forecastStartIndex, int forecastEndIndex)
        {
            if (!checkARIMADataLength(@params, data, forecastStartIndex, forecastEndIndex))
            {
                //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
                //ORIGINAL LINE: final int initialConditionSize = params.d + params.D * params.m;
                int initialConditionSize = @params.d + @params.D * @params.m;
                throw new Exception("not enough data for ARIMA. needed at least " + initialConditionSize + ", have " + data.Length + ", startIndex=" + forecastStartIndex + ", endIndex=" + forecastEndIndex);
            }

            //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
            //ORIGINAL LINE: final int forecast_length = forecastEndIndex - forecastStartIndex;
            int forecast_length = forecastEndIndex - forecastStartIndex;

            //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
            //ORIGINAL LINE: final double[] data_train = new double[forecastStartIndex];
            double[] data_train = new double[forecastStartIndex];
            Array.Copy(data, 0, data_train, 0, forecastStartIndex);

            //=======================================
            // DIFFERENTIATE
            //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
            //ORIGINAL LINE: final boolean hasSeasonalI = params.D > 0 && params.m > 0;
            bool hasSeasonalI = @params.D > 0 && @params.m > 0;
            //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
            //ORIGINAL LINE: final boolean hasNonSeasonalI = params.d > 0;
            bool hasNonSeasonalI = @params.d > 0;

            double[] data_stationary = differentiate(@params, data_train, hasSeasonalI, hasNonSeasonalI); // currently un-centered
                                                                                                          // END OF DIFFERENTIATE
                                                                                                          //==========================================

            //=========== CENTERING ====================
            //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
            //ORIGINAL LINE: final double mean_stationary = TimeSeries.Forecast.timeseries.timeseriesutil.Integrator.computeMean(data_stationary);
            double mean_stationary = Integrator.computeMean(data_stationary);

            Integrator.shift(data_stationary, (-1) * mean_stationary);
            //==========================================

            //==========================================
            // FORECAST
            HannanRissanen.estimateARMA(data_stationary, @params, forecast_length, maxIterationForHannanRissanen);

            return(new ArimaModel(@params, data, forecastStartIndex));
        }
Exemplo n.º 7
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);
            }
        }
Exemplo n.º 8
0
        /// <summary>
        /// Performs validation using Root Mean-Squared Error given a time series (with forecast) and
        /// true values
        /// </summary>
        /// <param name="data"> UNMODIFIED. time series data being evaluated </param>
        /// <param name="testDataPercentage"> percentage of data to be used to evaluate as test set </param>
        /// <param name="params"> MODIFIED. parameter of the ARIMA model </param>
        /// <returns> a Root Mean-Squared Error computed from the forecast and true data </returns>
        //JAVA TO C# CONVERTER WARNING: 'final' parameters are not available in .NET:
        //ORIGINAL LINE: public static double computeRMSEValidation(final double[] data, final double testDataPercentage, TimeSeries.Forecast.TimeSeries.Arima.struct.ArimaParams params)
        public static double computeRMSEValidation(double[] data, double testDataPercentage, ArimaParams @params)
        {
            int testDataLength       = (int)(data.Length * testDataPercentage);
            int trainingDataEndIndex = data.Length - testDataLength;

            //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
            //ORIGINAL LINE: final TimeSeries.Forecast.TimeSeries.Arima.struct.ArimaModel result = estimateARIMA(params, data, trainingDataEndIndex, data.length);
            ArimaModel result = estimateARIMA(@params, data, trainingDataEndIndex, data.Length);

            //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
            //ORIGINAL LINE: final double[] forecast = result.forecast(testDataLength).getForecast();
            double[] forecast = result.Forecast(testDataLength).Forecast;

            return(ComputeRMSE(data, forecast, trainingDataEndIndex, 0, forecast.Length));
        }
Exemplo n.º 9
0
        } // pure static helper class

        /// <summary>
        /// Forecast ARMA
        /// </summary>
        /// <param name="params"> MODIFIED. ARIMA parameters </param>
        /// <param name="dataStationary"> UNMODIFIED. the time series AFTER differencing / centering </param>
        /// <param name="startIndex"> the index where the forecast starts. startIndex &le; data.length </param>
        /// <param name="endIndex"> the index where the forecast stops (exclusive). startIndex  endIndex </param>
        /// <returns> forecast ARMA data point </returns>
        //JAVA TO C# CONVERTER WARNING: 'final' parameters are not available in .NET:
        //ORIGINAL LINE: public static double[] forecastARMA(final TimeSeries.Forecast.TimeSeries.Arima.struct.ArimaParams params, final double[] dataStationary, final int startIndex, final int endIndex)
        public static double[] forecastARMA(ArimaParams @params, double[] dataStationary, int startIndex, int endIndex)
        {
            //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
            //ORIGINAL LINE: final int train_len = startIndex;
            int train_len = startIndex;
            //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
            //ORIGINAL LINE: final int total_len = endIndex;
            int total_len = endIndex;

            //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
            //ORIGINAL LINE: final double[] errors = new double[total_len];
            double[] errors = new double[total_len];
            //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
            //ORIGINAL LINE: final double[] data = new double[total_len];
            double[] data = new double[total_len];
            Array.Copy(dataStationary, 0, data, 0, train_len);
            //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
            //ORIGINAL LINE: final int forecast_len = endIndex - startIndex;
            int forecast_len = endIndex - startIndex;

            //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
            //ORIGINAL LINE: final double[] forecasts = new double[forecast_len];
            double[] forecasts = new double[forecast_len];
            //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
            //ORIGINAL LINE: final int _dp = params.getDegreeP();
            int _dp = @params.DegreeP;
            //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
            //ORIGINAL LINE: final int _dq = params.getDegreeQ();
            int _dq = @params.DegreeQ;
            //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
            //ORIGINAL LINE: final int start_idx = (_dp > _dq) ? _dp : _dq;
            int start_idx = (_dp > _dq) ? _dp : _dq;

            for (int j = 0; j < start_idx; ++j)
            {
                errors[j] = 0;
            }
            // populate errors and forecasts
            for (int j = start_idx; j < train_len; ++j)
            {
                //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
                //ORIGINAL LINE: final double forecast = params.forecastOnePointARMA(data, errors, j);
                double forecast = @params.forecastOnePointARMA(data, errors, j);
                //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
                //ORIGINAL LINE: final double error = data[j] - forecast;
                double error = data[j] - forecast;
                errors[j] = error;
            }
            // now we can forecast
            for (int j = train_len; j < total_len; ++j)
            {
                //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
                //ORIGINAL LINE: final double forecast = params.forecastOnePointARMA(data, errors, j);
                double forecast = @params.forecastOnePointARMA(data, errors, j);
                data[j]   = forecast;
                errors[j] = 0;
                forecasts[j - train_len] = forecast;
            }
            // return forecasted values
            return(forecasts);
        }
Exemplo n.º 10
0
        /// <summary>
        /// Produce forecast result based on input ARIMA parameters and forecast length.
        /// </summary>
        /// <param name="params"> UNMODIFIED. ARIMA parameters </param>
        /// <param name="data"> UNMODIFIED. the original time series before differencing / centering </param>
        /// <param name="forecastStartIndex"> the index where the forecast starts. startIndex &le; data.length </param>
        /// <param name="forecastEndIndex"> the index where the forecast stops (exclusive). startIndex &lt; endIndex </param>
        /// <returns> forecast result </returns>
        //JAVA TO C# CONVERTER WARNING: 'final' parameters are not available in .NET:
        //ORIGINAL LINE: public static TimeSeries.Forecast.TimeSeries.Arima.struct.ForecastResult forecastARIMA(final TimeSeries.Forecast.TimeSeries.Arima.struct.ArimaParams params, final double[] data, final int forecastStartIndex, final int forecastEndIndex)
        public static ForecastResult forecastARIMA(ArimaParams @params, double[] data, int forecastStartIndex, int forecastEndIndex)
        {
            if (!checkARIMADataLength(@params, data, forecastStartIndex, forecastEndIndex))
            {
                //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
                //ORIGINAL LINE: final int initialConditionSize = params.d + params.D * params.m;
                int initialConditionSize = @params.d + @params.D * @params.m;
                throw new Exception("not enough data for ARIMA. needed at least " + initialConditionSize + ", have " + data.Length + ", startIndex=" + forecastStartIndex + ", endIndex=" + forecastEndIndex);
            }

            //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
            //ORIGINAL LINE: final int forecast_length = forecastEndIndex - forecastStartIndex;
            int forecast_length = forecastEndIndex - forecastStartIndex;

            //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
            //ORIGINAL LINE: final double[] forecast = new double[forecast_length];
            double[] forecast = new double[forecast_length];
            //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
            //ORIGINAL LINE: final double[] data_train = new double[forecastStartIndex];
            double[] data_train = new double[forecastStartIndex];
            Array.Copy(data, 0, data_train, 0, forecastStartIndex);

            //=======================================
            // DIFFERENTIATE
            //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
            //ORIGINAL LINE: final boolean hasSeasonalI = params.D > 0 && params.m > 0;
            bool hasSeasonalI = @params.D > 0 && @params.m > 0;
            //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
            //ORIGINAL LINE: final boolean hasNonSeasonalI = params.d > 0;
            bool hasNonSeasonalI = @params.d > 0;

            double[] data_stationary = differentiate(@params, data_train, hasSeasonalI, hasNonSeasonalI); // currently un-centered
                                                                                                          // END OF DIFFERENTIATE
                                                                                                          //==========================================

            //=========== CENTERING ====================
            //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
            //ORIGINAL LINE: final double mean_stationary = TimeSeries.Forecast.timeseries.timeseriesutil.Integrator.computeMean(data_stationary);
            double mean_stationary = Integrator.computeMean(data_stationary);

            Integrator.shift(data_stationary, (-1) * mean_stationary);
            //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
            //ORIGINAL LINE: final double dataVariance = TimeSeries.Forecast.timeseries.timeseriesutil.Integrator.computeVariance(data_stationary);
            double dataVariance = Integrator.computeVariance(data_stationary);

            //==========================================

            //==========================================
            // FORECAST
            //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
            //ORIGINAL LINE: final double[] forecast_stationary = forecastARMA(params, data_stationary, data_stationary.length, data_stationary.length + forecast_length);
            double[] forecast_stationary = forecastARMA(@params, data_stationary, data_stationary.Length, data_stationary.Length + forecast_length);

            //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
            //ORIGINAL LINE: final double[] data_forecast_stationary = new double[data_stationary.length + forecast_length];
            double[] data_forecast_stationary = new double[data_stationary.Length + forecast_length];

            Array.Copy(data_stationary, 0, data_forecast_stationary, 0, data_stationary.Length);
            Array.Copy(forecast_stationary, 0, data_forecast_stationary, data_stationary.Length, forecast_stationary.Length);
            // END OF FORECAST
            //==========================================

            //=========== UN-CENTERING =================
            Integrator.shift(data_forecast_stationary, mean_stationary);
            //==========================================

            //===========================================
            // INTEGRATE
            double[] forecast_merged = integrate(@params, data_forecast_stationary, hasSeasonalI, hasNonSeasonalI);
            // END OF INTEGRATE
            //===========================================
            Array.Copy(forecast_merged, forecastStartIndex, forecast, 0, forecast_length);

            return(new ForecastResult(forecast, dataVariance));
        }
Exemplo n.º 11
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;
        }