EzForecasting Class

New product

Try it for free view Licensing

Forecasting based on the analysis time series is used for many applications such as sales, supply chain, earthquakes, workload projections and others. The field of the time series analysis has been evolving at a fast pace in the past decades. A vast amount of univariate, multivariate, linear and nonlinear models and procedures have been developed. On the one hand, the complexity of the arising problems often requires the supervision of an expert, adept at the art and science of time series analysis. On the other hand, in many cases an automatic approach is unavoidable or desired. This could be the case when a large number of series have to be analyzed, or when for practical reasons the same method has to be applied to all series, or when the results must be reproducible. The automated predictions are often used as a reference or as a starting point for an analysis supervised by an expert. An automated procedure has to fulfill various requirements. It has to be sufficiently accurate, fast, robust, cheap and easy to use. Furthermore the results have to be easy to interpret and reproduce. As .NET developers for the industry, we quite often have to deal with customers asking about such automated time series analysis. As we do not feel that the available solutions fully meet the aforementioned requirements, we dared to take the big challange and develop our own class for time series prediction. So now the time has come and we can present the result of this project: the EzForecasting-class.

EzForecasting Class


June 28, 2015

Contents

1 General remarks
2 Constructor
2.1 Parameters
3 Members
4 Methods
4.1 Parameters
4.2 Return value
5 Example

1 General remarks

The EzForecasting class implements prediction of the future values of time series in terms of a finite number of past values. The forecasting algorithm does not depend on any particular process model (e.g. AR, MA, ARMA etc.) and is applicable to every time series, which is or can be reduced to stationary.
A detailed description of the prediction method can be found elsewhere [1]. In the following the most essential steps in the forecasting process implemented in EzForecasting will be discussed very briefly.

  1. Given a large sample of past observations X-m,X-m+1,....X0,X1,...XnEzForecasting computes the best linear predictor for the future value Xn+hin terms of X1,...Xn.

  2. Xn+his represented as a linear combination of X1,...Xn:

    ∑n Xn+h = a0 + aiXn+1 - i i=1

    (1)

  3. The coefficients aiare the solution of:

    ---→ Γ n-→an = γn(h )

    (2)


    with -→ an= (a1,a2,...,an)T, -- -→ γn (h )= [γ(h)(h+1),...,γ(n+h-1)]T,

    Γi,j= γ(i - j),i,j = 1...n and a0= μ∑n (1 - i=1 ai)

    Here γ() denotes the autocovariance function, which can be estimated from the sample and μ is the sample mean.

  4. The equations 1 and 2 can be used only in the case when {Xi} is stationary. Therefore the trend and the seasonal/cyclic fluctuations have to be eliminated from the raw data. EzForecasting implements the following procedure for trend elimination:

    1. Polynomial least squares fit. The determination of the polynomial order and the prevention of overfitting is based on criteria using the statistical significance of the fitted coefficients and the goodness of the fit.

    2. First order differencing

  5. Removal of seasonal/cyclic fluctuations:

    1. Using a simplex algorithm EzForecasting finds the amplitude, the phase and the period of the harmonic, which minimizes the sum of the residuals. The discrete Fourier transform of the raw data is used as input for the simplex algorithm. If required (i.e.NumHarms > 1, see below), this procedure can be repeated to identify further seasonal components.

2 Constructor

Reference: MathToolBox.dll
public EzForecasting(double[] X, int StepsFrwd, int NumHarms = 1,
int PolyFitOrd = 5, double CB = 0.95)

2.1 Parameters

  • X - the raw data

  • StepsFrwd - specifies the prediction range, how many steps into the future.

  • NumHarms - the maximum number of seasonal components, which should be eliminated. The actual number of eliminated seasonal components might be equal or less than NumHarms.

  • PolyFitOrd - the maximum order of the polynomial used for trend elimination. The actual order of the subtracted polynomial might be equal or less than PolyFitOrd. In the majority of the cases the actual value will not exceed 1. In general it is better to set PolyFitOrd to a large value (2) and let the fitting algorithm find the optimal value.

  • CB - the confidence bound used for the calculation of the upper- and the lower confidence bounds of the predicted future values.

Upon creation of the EzForecasting object the raw data is automatically transformed according to steps 4a-b and 5a (see above). The result is stored in the member variable StationarySeries.
Since the prediction method relies heavily on statistical values computed from the sample, there is a minimal required sample size of 125 + 1.25StepsFrwd.

3 Members

  • double[] StationarySeries - the original time series after elimination of the trend and the seasonal components (see 4a-b and 5a above).

  • double[] TrendSeasonal - the estimated seasonal trend component. If NumHarms > 1 it will be the sum of several seasonal components.

  • double[] TrendPoly - the estimated polynomial trend. The order of the polynomial is less or equal to PolyFitOrd.

  • double[] FutureValues - the predicted future values. The length of the array is equal to StepsFrwd.

  • double[] UpperPredBound - the upper bounds of the predicted values. This is a multiple of the OneSigmaErrors with proportionality factor depending on the specified value of CB. For example CB = 0.68 1, CB = 0.95 2, CB = 0.997 3.

  • double[] LowerPredBound - the lower bounds of the predicted values.

  • double[] OneSigmaErrors - the standard errors of the predicted values, derived from the sample autocovariance function and the coefficients {ai} , see 1. and 2. and 3. The standard errors are computed on the same scale as the original raw data and take into account the differencing transformations.

    Obviously OneSigmaErrors is redundant, since there is a well defined relation between the standard errors and the confidence bounds. OneSigmaErrors is included only for convenience.

  • double[] FutureSteps - the elements of this array a filled with the numbers 1... StepsFrwd. This is done for convenience while plotting the predicted values.

4 Methods

There is only one method exposed:

int Forecst(int NumberPastVals2Consider, bool UsePEM = false)

4.1 Parameters

  • NumberPastVals2Consider - this is the number of the historical data {X1,...Xn} used for the prediction (see Eq. 1).

  • UsePEM - if set to TRUE, the actual number of the historical data used for the prediction will be estimated using prediction error minimization (PEM). In this case NumberPastVals2Consider specifies the upper limit.

4.2 Return value

  • NumberPastVals2Consider - when UsePEM is FALSE

  • the optimal number of the historical data used for the prediction in the case when UsePEM is TRUE.

Forecst() automatically fills the output arrays StationarySeries, StationarySeries, TrendSeasonal, TrendPoly, FutureValues, UpperPredBound, LowerPredBound.

5 Example

In this example we are going to demonstrate the usage of EzForecasting to predict the electricity integrated hourly loads in the PJM Mid-Atlantic Region. We are going to use the history data for the entire 2014 and make a prediction for the first hours of 2015. In this special case we have the possibility to compare the prediction with the true data. Both the history data and the data for 2015 can be freely downloaded at the PJM web-page:
http://www.pjm.com/markets-and-operations/energy/real-time/loadhryr.aspx
The history data for 2014 are plotted in Fig.1


PIC

Figure 1: Integrated electricity hourly loads of PJM Mid-Atlantic Region for 2014.


The following code snippet illustrates how to use EzForecasting for to make the prediction. It is assumed that the raw data are stored in the array double[] PJM2014.

using MathToolBox;
...
EzForecasting ezf = new EzForecasting( PJM2014, 50);
ezf.Forecast(200, true);

In this case we will make a 50 steps prediction using the PEM-option and setting the maximal number of past values to 200. That’s all. Now we just have to plot the results stored in ezf.FutureValues and the upper- and the lower bounds ezf.UpperPredBound, ezf.LowerPredBound. The result is visualized in Fig. 2


PIC

Figure 2: Predicted and true values for the electricity integrated hourly loads in the early 2015.


Along with this manual, the class is distributed with the source of the demo project EzForecastingDemo, which can be freely downloaded at
http://bntech.info and can be used as a quick guide to the EzForecasting class. The demo project includes extended examples how to download and predict all of the about 20 000 stocks listed in Yahoo-finance.

References

[1]P.J. Brockwell and R.A. Davis. Introduction to Time Series and Forecasting, (2002), Springer-Verlag, New York.

€82.50 tax excl.

PayPal

SEO by smuggler@abv.bg . . . . . . . . . . . . . . . . . .news sports rent a car . . . . . . .

...................................