Time Series Analysis - Maple Programming Help

Online Help

All Products    Maple    MapleSim


Home : Support : Online Help : System : Information : Updates : Maple 18 : updates/Maple18/TimeSeriesAnalysis

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.

withTimeSeriesAnalysis

AIC,AICc,Apply,BIC,BoxCoxTransform,Decomposition,Difference,ExponentialSmoothingModel,Forecast,GetData,GetDateFormat,GetDates,GetHeaders,GetParameter,GetParameters,GetPeriod,Initialize,Join,LogLikelihood,LogTransform,LongestDefinedSubsequence,NumberOfParameters,OneStepForecasts,Optimize,SeasonalSubseriesPlot,SetParameter,Specialize,TimeSeries,TimeSeriesPlot,Unapply

(1)
  

 

Forecasting

Seasonal Analysis

 

 

Using the Time Series Analysis Package

Working with Time Series Data

More Details on Choosing Exponential Smoothing Models

References

Using the Time Series Analysis Package

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.

path:=FileTools:-JoinPathkernelopts'datadir',datasets,air_passengers.csv:

data:=ImportMatrixpath

data:= 145 x 2 MatrixData Type: anythingStorage: rectangularOrder: Fortran_order

(2)
• 

The data has one column of dates (year) and one column of data (monthly passengers). The first row is a header.

data1..5

DateMonthly Passengers1949-011121949-021181949-031321949-04129

(3)
• 

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:=TimeSeriesdata,'header'=1,'dates'=1,'period'=12

ts:=Time seriesMonthly Passengers144 rows of data:1949-01-01 - 1960-12-01

(4)
• 

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.

GetDatats

144 x 1 MatrixData Type: float8Storage: rectangularOrder: Fortran_order

(5)

GetDatests

1 .. 144 VectorcolumnData Type: anythingStorage: rectangularOrder: Fortran_order

(6)

GetHeadersts

Monthly Passengers

(7)
• 

Alternatively, you can plot the data using the TimeSeriesPlot command.

TimeSeriesPlotts

• 

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.

SeasonalSubseriesPlotts,seasonnames=January,February,March,April,May,June,July,August,September,October,November,December,size=800,300

• 

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.

tstraints..1958-12

tstrain:=Time seriesMonthly Passengers120 rows of data:1949-01-01 - 1958-12-01

(8)

tsverifyts1959 ..

tsverify:=Time seriesMonthly Passengers24 rows of data:1959-01-01 - 1960-12-01

(9)
• 

The family of models used is an exponential smoothing model.

model:=ExponentialSmoothingModeltstrain

model:=< an ETS(M,M,M) model >

(10)
• 

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.

GetParametersmodel

errors&equals;M&comma;trend&equals;M&comma;seasonal&equals;M&comma;&alpha;&equals;0.667352215015678&comma;&beta;&equals;0.00136509636089604&comma;&gamma;&equals;0.000347993880774949&comma;&phi;&equals;1.&comma;period&equals;12&comma;l0&equals;123.060900436068&comma;b0&equals;1.01002269643555&comma;s&equals; 1 .. 12 VectorcolumnData Type: float8Storage: rectangularOrder: Fortran_order&comma;&sigma;&equals;0.0346510099657471&comma;constraints&equals;both

(11)
• 

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:=Forecastmodel&comma;tstrain&comma;24

forecast:=Time seriesMonthly Passengers (forecast)24 rows of data:1958-12-31 - 1960-11-30

(12)
• 

The forecast is itself a time series object that can be inspected using GetData, GetHeaders, and other commands.

GetHeadersforecast

Monthly Passengers (forecast)

(13)

TimeSeriesPlottstrain&comma;tsverify&comma;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:=Forecastmodel&comma;tstrain&comma;24&comma;output&equals;confidenceintervals80&comma;95

confidence:=Time seriesMonthly Passengers (forecast - 2 percentile), ..., Monthly Passengers (forecast - 98 percentile)24 rows of data:1958-12-31 - 1960-11-30

(14)

TimeSeriesPlottstrain&comma;forecast&comma;confidence&comma;tsverify&comma;color&equals;Red&comma;thickness&equals;3

• 

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:=Decompositionmodel&comma;tstrain

decomposition:=Time seriesMonthly Passengers (residuals), ..., Monthly Passengers (seasonal)120 rows of data:1949-01-01 - 1958-12-01

(15)

TimeSeriesPlottstrain&comma;decomposition&comma;split&equals;pertimeseries

• 

Because the seasonal component is multiplicative, you can compensate for it by dividing the original data by it.

trainingdata:=GetDatatstrain

trainingdata:= 120 x 1 MatrixData Type: float8Storage: rectangularOrder: Fortran_order

(16)

seasonaldata:=GetDatadecomposition..&comma; Monthly Passengers (seasonal)

seasonaldata:= 120 x 1 MatrixData Type: float8Storage: rectangularOrder: Fortran_order

(17)

nonseasonaldata  TimeSeriestrainingdata /~ seasonaldata&comma; &apos;header&apos; &equals; Monthly Passengers (deseasonalized)&comma; &apos;dates&apos; &equals; GetDateststrain

nonseasonaldata:=Time seriesMonthly Passengers (deseasonalized)120 rows of data:1949-01-01 - 1958-12-01

(18)

TimeSeriesPlotnonseasonaldata

Working with Time Series Data

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:=Jointstrain&comma;forecast

merged:=Time seriesMonthly Passengers, Monthly Passengers (forecast)144 rows of data:1949-01-01 - 1960-11-30

(19)

TimeSeriesPlotmerged

 

• 

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:=ApplyDifference&comma;ts

differenced:=Time seriesMonthly Passengers (differenced)143 rows of data:1949-02-01 - 1960-12-01

(20)

logs:=ApplyLogTransform&comma;ts

logs:=Time seriesLogarithm of Monthly Passengers144 rows of data:1949-01-01 - 1960-12-01

(21)

boxcox:=ApplyBoxCoxTransform&lambda;&equals;13&comma;ts

boxcox:=Time seriesBox-Cox transform of Monthly Passengers144 rows of data:1949-01-01 - 1960-12-01

(22)

TimeSeriesPlotts&comma;differenced&comma;logs&comma;boxcox&comma;split&equals;pertimeseries

More Details on Choosing Exponential Smoothing Models

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:=ExponentialSmoothingModel&apos;seasonal&equals;A&comma;M&apos;

general_model:=< an ETS(*,*,*) model >

(23)
• 

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:=Specializegeneral_model&comma;ts

individual_models:=< an ETS(A,A,A) model >&comma;< an ETS(A,Ad,A) model >&comma;< an ETS(A,N,A) model >&comma;< an ETS(M,A,A) model >&comma;< an ETS(M,A,M) model >&comma;< an ETS(M,Ad,A) model >&comma;< an ETS(M,Ad,M) model >&comma;< an ETS(M,M,M) model >&comma;< an ETS(M,Md,M) model >&comma;< an ETS(M,N,A) model >&comma;< an ETS(M,N,M) model >

(24)
• 

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.

initialization_tables:=mapInitialize&comma;individual_models&comma;ts&colon;

• 

Here is an example of these initialization values:

individual_models1&comma;initialization_tables1

< an ETS(A,A,A) model >&comma;tableb0&equals;0.646614725853865&comma;&alpha;&equals;12&comma;s&equals; 1 .. 12 VectorcolumnData Type: float8Storage: rectangularOrder: Fortran_order&comma;l0&equals;122.838670948617&comma;&gamma;&equals;1100&comma;&beta;&equals;110

(25)
• 

You can now perform the optimization.

mapOptimize&comma;individual_models&comma;ts

579.1189017&comma;587.8357482&comma;587.5947092&comma;577.5718572&comma;527.4987042&comma;559.4653237&comma;528.6819747&comma;527.8440927&comma;527.3996887&comma;565.0019522&comma;535.5928547

(26)
• 

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&comma;BICm&comma;tsend do&colon;

< an ETS(A,A,A) model >&comma;1412.79583526645

< an ETS(A,Ad,A) model >&comma;1445.84094061076

< an ETS(A,N,A) model >&comma;1422.76337107009

< an ETS(M,A,A) model >&comma;1722.03500867297

< an ETS(M,A,M) model >&comma;1133.89411113851

< an ETS(M,Ad,A) model >&comma;1945.38209291183

< an ETS(M,Ad,M) model >&comma;1141.84855312639

< an ETS(M,M,M) model >&comma;1139.65948604423

< an ETS(M,Md,M) model >&comma;1141.03566969852

< an ETS(M,N,A) model >&comma;2087.66988840404

< an ETS(M,N,M) model >&comma;1150.95261586989

(27)
• 

From the results, the M&comma;A&comma;M, M&comma;Ad&comma;M, M&comma;M&comma;M, &lpar;M&comma;N&comma;M), and (M&comma;Md&comma;M&rpar; models give the best results in terms of the Bayesian information criterion.

References

  

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.

See Also

Overview of the TimeSeriesAnalysis Package