Time Series Analysis
The TimeSeriesAnalysis package is new to Maple 18 and deals with any data that varies with time. In particular, any data where the time intervals between data points are regular, such as with macroeconomical data and in many other fields like statistics, signal processing, econometrics, and mathematical finance. The TimeSeriesAnalysis package has many tools working with data including tools for analyzing and modeling, finding patterns and forecasting, and visualizing time series data.
Using the Time Series Analysis Package
Working with Time Series Data
More Details on Choosing Exponential Smoothing Models
The following example uses a data set containing the number of monthly air passengers (in thousands of passengers) from 1949 until 1960. The data is from Box, Jenkins, and Reinsel, noted in the references below.
data:= 145 x 2 MatrixData Type: anythingStorage: rectangularOrder: Fortran_order
The data has one column of dates (year) and one column of data (monthly passengers). The first row is a header.
To work with this data, construct a TimeSeries object. Such an object can contain one or more data sets, measured at a common set of time points, as well as data headers and other metadata. You can construct this time series object as follows:
ts:=Time seriesMonthly Passengers144 rows of data:1949-01-01 - 1960-12-01
The options above indicate that the first row and column contain a header and the dates, respectively, and that you expect any seasonal characteristics to occur with period 12.
To inspect the data, you can use the GetData, GetDates, and GetHeaders commands.
144 x 1 MatrixData Type: float8Storage: rectangularOrder: Fortran_order
1 .. 144 VectorcolumnData Type: anythingStorage: rectangularOrder: Fortran_order
Alternatively, you can plot the data using the TimeSeriesPlot command.
We can also look for seasonal trends in our data using the SeasonalSubseriesPlot command. The following plot shows the number of passengers on a monthly basis.
You can now have Maple select a suitable model from a family of 30 related models, and adjust it to this time series. In fact, you will adjust it to the first 10 years of data; this can be done by specifying the time range as an index.
tstrain:=Time seriesMonthly Passengers120 rows of data:1949-01-01 - 1958-12-01
tsverify:=Time seriesMonthly Passengers24 rows of data:1959-01-01 - 1960-12-01
The family of models used is an exponential smoothing model.
model:=< an ETS(M,M,M) model >
You can observe that the best model in this case has multiplicative errors, a multiplicative trend, and multiplicative seasonal information. You can get the details on this exponential smoothing model using the GetParameters command.
errors=M,trend=M,seasonal=M,α=0.667352215015678,β=0.00136509636089604,γ=0.000347993880774949,φ=1.,period=12,l0=123.060900436068,b0=1.01002269643555,s= 1 .. 12 VectorcolumnData Type: float8Storage: rectangularOrder: Fortran_order,σ=0.0346510099657471,constraints=both
Use the model to predict two years of future data using the forecast command: the actual data for this time range is in tsverify.
forecast:=Time seriesMonthly Passengers (forecast)24 rows of data:1958-12-31 - 1960-11-30
The forecast is itself a time series object that can be inspected using GetData, GetHeaders, and other commands.
Monthly Passengers (forecast)
There is reasonable agreement between the forecast and the verification data. You get a better handle on this if you include confidence intervals for all data points. You can then see how often the true data falls within the boundaries of the confidence intervals.
confidence:=Time seriesMonthly Passengers (forecast - 2 percentile), ..., Monthly Passengers (forecast - 98 percentile)24 rows of data:1958-12-31 - 1960-11-30
Use the model to decompose the data set into several components; the number of components depends on the model. This is done using the Decomposition command. For an exponential smoothing model, there are always level and residual components. There may also be trend and seasonal components depending on whether or not the model has those properties. This can be used to, for example, correct for seasonal influences or smooth data.
decomposition:=Time seriesMonthly Passengers (residuals), ..., Monthly Passengers (seasonal)120 rows of data:1949-01-01 - 1958-12-01
Because the seasonal component is multiplicative, you can compensate for it by dividing the original data by it.
trainingdata:= 120 x 1 MatrixData Type: float8Storage: rectangularOrder: Fortran_order
seasonaldata:=GetData⁡decomposition.., Monthly Passengers (seasonal)
seasonaldata:= 120 x 1 MatrixData Type: float8Storage: rectangularOrder: Fortran_order
nonseasonaldata ≔ TimeSeriestrainingdata /~ seasonaldata, 'header' = Monthly Passengers (deseasonalized), 'dates' = GetDateststrain
nonseasonaldata:=Time seriesMonthly Passengers (deseasonalized)120 rows of data:1949-01-01 - 1958-12-01
There are several commands that can be used to modify existing time series objects. Using the data from above, you can use the Join command to merge the forecast with the training data set.
merged:=Time seriesMonthly Passengers, Monthly Passengers (forecast)144 rows of data:1949-01-01 - 1960-11-30
Another command, Difference, applies a differencing transformation in a flexible way. The LogTransform command applies a logarithm transformation, and BoxCoxTransform generalizes that to a general Box-Cox transformation.
differenced:=Time seriesMonthly Passengers (differenced)143 rows of data:1949-02-01 - 1960-12-01
logs:=Time seriesLogarithm of Monthly Passengers144 rows of data:1949-01-01 - 1960-12-01
boxcox:=Time seriesBox-Cox transform of Monthly Passengers144 rows of data:1949-01-01 - 1960-12-01
You can manually step through the process of finding a suitable model for the data set using the Specialize, Initialize, and Optimize commands. The ExponentialSmoothingModel command generates an exponential smoothing model object; it represents a wide range of models. In this case, you can see that the data has a strong seasonal component, so you might be able to guess that you can discard the models that do not take the seasonal component into account.
general_model:=< an ETS(*,*,*) model >
You can now specialize this to all individual model formulations represented by the general model. That is, where the seasonal component is additive or multiplicative. Some models are excluded by default because they are subject to numerical difficulties: the forecasts have infinite variance. (They can be included by overriding an option to Specialize.)
individual_models:=< an ETS(A,A,A) model >,< an ETS(A,Ad,A) model >,< an ETS(A,N,A) model >,< an ETS(M,A,A) model >,< an ETS(M,A,M) model >,< an ETS(M,Ad,A) model >,< an ETS(M,Ad,M) model >,< an ETS(M,M,M) model >,< an ETS(M,Md,M) model >,< an ETS(M,N,A) model >,< an ETS(M,N,M) model >
Each individual model now has a slightly different set of model equations. They all still have a number of parameters and initial values for the model's state variables and need to be optimized for the best fit. This optimization process consists of a number of iterations. In each iteration, Maple picks a new set of values for the parameters and initial state values, then runs the simulation using the model, and finally, computes the deviations from the actual observed data. The first set of values (which initializes the optimization process) is computed directly from the actual data, by the Initialize command.
Here is an example of these initialization values:
< an ETS(A,A,A) model >,tableb0=0.646614725853865,α=12,s= 1 .. 12 VectorcolumnData Type: float8Storage: rectangularOrder: Fortran_order,l0=122.838670948617,γ=1100,β=110
You can now perform the optimization.
The optimization process sets all parameters and initial state values to the optimal values found. The Optimize command returns a measure for how close the fit between the simulation run and the actual data is. However, these models all have different numbers of parameters. If a similar fit is achieved with a model that has fewer parameters, then the principle of parsimony says you should prefer the latter model. This is quantified in a so-called information criterion: a function that takes both the closeness of the fit and the number of parameters into account. The TimeSeriesAnalysis package has three information criteria built in: two versions of Akaike's Information Criterion (AICc, which includes a correction for small data sizes, and AIC, which does not), and the Bayesian Information Criterion (BIC). Let us compare the BIC for all these models.
forminindividual_modelsdo printm,BIC⁡m,tsend do:
< an ETS(A,A,A) model >,1412.79583526645
< an ETS(A,Ad,A) model >,1445.84094061076
< an ETS(A,N,A) model >,1422.76337107009
< an ETS(M,A,A) model >,1722.03500867297
< an ETS(M,A,M) model >,1133.89411113851
< an ETS(M,Ad,A) model >,1945.38209291183
< an ETS(M,Ad,M) model >,1141.84855312639
< an ETS(M,M,M) model >,1139.65948604423
< an ETS(M,Md,M) model >,1141.03566969852
< an ETS(M,N,A) model >,2087.66988840404
< an ETS(M,N,M) model >,1150.95261586989
From the results, the M,A,M, M,Ad,M, M,M,M, (M,N,M), and (M,Md,M) models give the best results in terms of the Bayesian information criterion.
Box, G.E.P., Jenkins, G.M., and Reinsel, G.C. (1976) Time Series Analysis, Forecasting and Control. Third Edition. Holden-Day. Series G.
Hyndman, R.J. and Athanasopoulos, G. (2013) Forecasting: principles and practice. http://otexts.org/fpp/. Accessed on 2013-10-09.
Hyndman, R.J., Koehler, A.B., Ord, J.K., and Snyder, R.D. (2008) Forecasting with Exponential Smoothing: The State Space Approach. Springer Series in Statistics. Springer-Verlag Berlin Heidelberg.
Overview of the TimeSeriesAnalysis Package
Download Help Document
What kind of issue would you like to report? (Optional)