Time Series Analysis - Maple Help

Home : Support : Online Help : System : Information : Updates : Maple 18 : Time Series Analysis

 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.

 > $\mathrm{with}\left(\mathrm{TimeSeriesAnalysis}\right)$
 $\left[{\mathrm{AIC}}{,}{\mathrm{AICc}}{,}{\mathrm{Apply}}{,}{\mathrm{BIC}}{,}{\mathrm{BoxCoxTransform}}{,}{\mathrm{Decomposition}}{,}{\mathrm{Difference}}{,}{\mathrm{ExponentialSmoothingModel}}{,}{\mathrm{Forecast}}{,}{\mathrm{GetData}}{,}{\mathrm{GetDateFormat}}{,}{\mathrm{GetDates}}{,}{\mathrm{GetHeaders}}{,}{\mathrm{GetParameter}}{,}{\mathrm{GetParameters}}{,}{\mathrm{GetPeriod}}{,}{\mathrm{Initialize}}{,}{\mathrm{Join}}{,}{\mathrm{LogLikelihood}}{,}{\mathrm{LogTransform}}{,}{\mathrm{LongestDefinedSubsequence}}{,}{\mathrm{NumberOfParameters}}{,}{\mathrm{OneStepForecasts}}{,}{\mathrm{Optimize}}{,}{\mathrm{SeasonalSubseriesPlot}}{,}{\mathrm{SetParameter}}{,}{\mathrm{Specialize}}{,}{\mathrm{TimeSeries}}{,}{\mathrm{TimeSeriesPlot}}{,}{\mathrm{Unapply}}\right]$ (1)

 Forecasting Seasonal Analysis

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.

 > $\mathrm{path}:=\mathrm{FileTools}:-\mathrm{JoinPath}\left(\left[\mathrm{kernelopts}\left('\mathrm{datadir}'\right),"datasets","air_passengers.csv"\right]\right):$
 > $\mathrm{data}:=\mathrm{ImportMatrix}\left(\mathrm{path}\right)$
 ${\mathrm{data}}{:=}\left[\begin{array}{c}{\mathrm{145 x 2}}{\mathrm{Matrix}}\\ {\mathrm{Data Type:}}{\mathrm{anything}}\\ {\mathrm{Storage:}}{\mathrm{rectangular}}\\ {\mathrm{Order:}}{\mathrm{Fortran_order}}\end{array}\right]$ (2)
 • The data has one column of dates (year) and one column of data (monthly passengers). The first row is a header.
 > $\mathrm{data}\left[1..5\right]$
 $\left[\begin{array}{cc}{"Date"}& {"Monthly Passengers"}\\ {"1949-01"}& {112}\\ {"1949-02"}& {118}\\ {"1949-03"}& {132}\\ {"1949-04"}& {129}\end{array}\right]$ (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:
 > $\mathrm{ts}:=\mathrm{TimeSeries}\left(\mathrm{data},'\mathrm{header}'=1,'\mathrm{dates}'=1,'\mathrm{period}'=12\right)$
 ${\mathrm{ts}}{:=}\left[\begin{array}{c}{\mathrm{Time series}}\\ {\mathrm{Monthly Passengers}}\\ {\mathrm{144 rows of data:}}\\ {\mathrm{1949-01-01 - 1960-12-01}}\end{array}\right]$ (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.
 > $\mathrm{GetData}\left(\mathrm{ts}\right)$
 $\left[\begin{array}{c}{\mathrm{144 x 1}}{\mathrm{Matrix}}\\ {\mathrm{Data Type:}}{{\mathrm{float}}}_{{8}}\\ {\mathrm{Storage:}}{\mathrm{rectangular}}\\ {\mathrm{Order:}}{\mathrm{Fortran_order}}\end{array}\right]$ (5)
 > $\mathrm{GetDates}\left(\mathrm{ts}\right)$
 $\left[\begin{array}{c}{\mathrm{1 .. 144}}{{\mathrm{Vector}}}_{{\mathrm{column}}}\\ {\mathrm{Data Type:}}{\mathrm{anything}}\\ {\mathrm{Storage:}}{\mathrm{rectangular}}\\ {\mathrm{Order:}}{\mathrm{Fortran_order}}\end{array}\right]$ (6)
 > $\mathrm{GetHeaders}\left(\mathrm{ts}\right)$
 $\left[{"Monthly Passengers"}\right]$ (7)
 • Alternatively, you can plot the data using the TimeSeriesPlot command.
 > $\mathrm{TimeSeriesPlot}\left(\mathrm{ts}\right)$

 • 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.
 > $\mathrm{SeasonalSubseriesPlot}\left(\mathrm{ts},\mathbit{seasonnames}=\left["January","February","March","April","May","June","July","August","September","October","November","December"\right],\mathrm{size}=\left[800,300\right]\right)$
 • 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.
 > $\mathrm{tstrain}≔\mathrm{ts}\left[.."1958-12"\right]$
 ${\mathrm{tstrain}}{:=}\left[\begin{array}{c}{\mathrm{Time series}}\\ {\mathrm{Monthly Passengers}}\\ {\mathrm{120 rows of data:}}\\ {\mathrm{1949-01-01 - 1958-12-01}}\end{array}\right]$ (8)
 >
 ${\mathrm{tsverify}}{:=}\left[\begin{array}{c}{\mathrm{Time series}}\\ {\mathrm{Monthly Passengers}}\\ {\mathrm{24 rows of data:}}\\ {\mathrm{1959-01-01 - 1960-12-01}}\end{array}\right]$ (9)
 • The family of models used is an exponential smoothing model.
 > $\mathrm{model}:=\mathrm{ExponentialSmoothingModel}\left(\mathrm{tstrain}\right)$
 ${\mathrm{model}}{:=}{\mathrm{< an ETS\left(M,M,M\right) 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.
 > $\mathrm{GetParameters}\left(\mathrm{model}\right)$
 $\left[{\mathrm{errors}}{=}\left\{{"M"}\right\}{,}{\mathrm{trend}}{=}\left\{{"M"}\right\}{,}{\mathrm{seasonal}}{=}\left\{{"M"}\right\}{,}{\mathrm{α}}{=}{0.667352215015678}{,}{\mathrm{β}}{=}{0.00136509636089604}{,}{\mathrm{γ}}{=}{0.000347993880774949}{,}{\mathrm{φ}}{=}{1.}{,}{\mathrm{period}}{=}{12}{,}{\mathrm{l0}}{=}{123.060900436068}{,}{\mathrm{b0}}{=}{1.01002269643555}{,}{s}{=}\left[\begin{array}{c}{\mathrm{1 .. 12}}{{\mathrm{Vector}}}_{{\mathrm{column}}}\\ {\mathrm{Data Type:}}{{\mathrm{float}}}_{{8}}\\ {\mathrm{Storage:}}{\mathrm{rectangular}}\\ {\mathrm{Order:}}{\mathrm{Fortran_order}}\end{array}\right]{,}{\mathrm{σ}}{=}{0.0346510099657471}{,}{\mathrm{constraints}}{=}{"both"}\right]$ (11)
 • Use the model to predict two years of future data using the forecast command: the actual data for this time range is in $\mathrm{tsverify}$.
 > $\mathrm{forecast}:=\mathrm{Forecast}\left(\mathrm{model},\mathrm{tstrain},24\right)$
 ${\mathrm{forecast}}{:=}\left[\begin{array}{c}{\mathrm{Time series}}\\ {\mathrm{Monthly Passengers \left(forecast\right)}}\\ {\mathrm{24 rows of data:}}\\ {\mathrm{1958-12-31 - 1960-11-30}}\end{array}\right]$ (12)
 • The forecast is itself a time series object that can be inspected using GetData, GetHeaders, and other commands.
 > $\mathrm{GetHeaders}\left(\mathrm{forecast}\right)$
 $\left[{"Monthly Passengers \left(forecast\right)"}\right]$ (13)
 > $\mathrm{TimeSeriesPlot}\left(\mathrm{tstrain},\mathrm{tsverify},\mathrm{forecast}\right)$

 • 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.
 > $\mathrm{confidence}:=\mathrm{Forecast}\left(\mathrm{model},\mathrm{tstrain},24,\mathrm{output}=\mathrm{confidenceintervals}\left(80,95\right)\right)$
 ${\mathrm{confidence}}{:=}\left[\begin{array}{c}{\mathrm{Time series}}\\ {\mathrm{Monthly Passengers \left(forecast - 2 percentile\right), ..., Monthly Passengers \left(forecast - 98 percentile\right)}}\\ {\mathrm{24 rows of data:}}\\ {\mathrm{1958-12-31 - 1960-11-30}}\end{array}\right]$ (14)
 > $\mathrm{TimeSeriesPlot}\left(\mathrm{tstrain},\mathrm{forecast},\mathrm{confidence},\left[\mathrm{tsverify},\mathrm{color}="Red",\mathrm{thickness}=3\right]\right)$

 • 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.
 > $\mathrm{decomposition}:=\mathrm{Decomposition}\left(\mathrm{model},\mathrm{tstrain}\right)$
 ${\mathrm{decomposition}}{:=}\left[\begin{array}{c}{\mathrm{Time series}}\\ {\mathrm{Monthly Passengers \left(residuals\right), ..., Monthly Passengers \left(seasonal\right)}}\\ {\mathrm{120 rows of data:}}\\ {\mathrm{1949-01-01 - 1958-12-01}}\end{array}\right]$ (15)
 > $\mathrm{TimeSeriesPlot}\left(\mathrm{tstrain},\mathrm{decomposition},\mathrm{split}=\mathrm{pertimeseries}\right)$

 • Because the seasonal component is multiplicative, you can compensate for it by dividing the original data by it.
 > $\mathrm{trainingdata}:=\mathrm{GetData}\left(\mathrm{tstrain}\right)$
 ${\mathrm{trainingdata}}{:=}\left[\begin{array}{c}{\mathrm{120 x 1}}{\mathrm{Matrix}}\\ {\mathrm{Data Type:}}{{\mathrm{float}}}_{{8}}\\ {\mathrm{Storage:}}{\mathrm{rectangular}}\\ {\mathrm{Order:}}{\mathrm{Fortran_order}}\end{array}\right]$ (16)
 >
 ${\mathrm{seasonaldata}}{:=}\left[\begin{array}{c}{\mathrm{120 x 1}}{\mathrm{Matrix}}\\ {\mathrm{Data Type:}}{{\mathrm{float}}}_{{8}}\\ {\mathrm{Storage:}}{\mathrm{rectangular}}\\ {\mathrm{Order:}}{\mathrm{Fortran_order}}\end{array}\right]$ (17)
 >
 ${\mathrm{nonseasonaldata}}{:=}\left[\begin{array}{c}{\mathrm{Time series}}\\ {\mathrm{Monthly Passengers \left(deseasonalized\right)}}\\ {\mathrm{120 rows of data:}}\\ {\mathrm{1949-01-01 - 1958-12-01}}\end{array}\right]$ (18)
 > $\mathrm{TimeSeriesPlot}\left(\mathrm{nonseasonaldata}\right)$

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.

 > $\mathrm{merged}:=\mathrm{Join}\left(\mathrm{tstrain},\mathrm{forecast}\right)$
 ${\mathrm{merged}}{:=}\left[\begin{array}{c}{\mathrm{Time series}}\\ {\mathrm{Monthly Passengers, Monthly Passengers \left(forecast\right)}}\\ {\mathrm{144 rows of data:}}\\ {\mathrm{1949-01-01 - 1960-11-30}}\end{array}\right]$ (19)
 > $\mathrm{TimeSeriesPlot}\left(\mathrm{merged}\right)$

 • 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.
 > $\mathrm{differenced}:=\mathrm{Apply}\left(\mathrm{Difference},\mathrm{ts}\right)$
 ${\mathrm{differenced}}{:=}\left[\begin{array}{c}{\mathrm{Time series}}\\ {\mathrm{Monthly Passengers \left(differenced\right)}}\\ {\mathrm{143 rows of data:}}\\ {\mathrm{1949-02-01 - 1960-12-01}}\end{array}\right]$ (20)
 > $\mathrm{logs}:=\mathrm{Apply}\left(\mathrm{LogTransform},\mathrm{ts}\right)$
 ${\mathrm{logs}}{:=}\left[\begin{array}{c}{\mathrm{Time series}}\\ {\mathrm{Logarithm of Monthly Passengers}}\\ {\mathrm{144 rows of data:}}\\ {\mathrm{1949-01-01 - 1960-12-01}}\end{array}\right]$ (21)
 > $\mathrm{boxcox}:=\mathrm{Apply}\left(\mathrm{BoxCoxTransform}\left(\mathrm{λ}=\frac{1}{3}\right),\mathrm{ts}\right)$
 ${\mathrm{boxcox}}{:=}\left[\begin{array}{c}{\mathrm{Time series}}\\ {\mathrm{Box-Cox transform of Monthly Passengers}}\\ {\mathrm{144 rows of data:}}\\ {\mathrm{1949-01-01 - 1960-12-01}}\end{array}\right]$ (22)
 > $\mathrm{TimeSeriesPlot}\left(\mathrm{ts},\mathrm{differenced},\mathrm{logs},\mathrm{boxcox},\mathrm{split}=\mathrm{pertimeseries}\right)$

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.

 > $\mathrm{general_model}:=\mathrm{ExponentialSmoothingModel}\left('\mathrm{seasonal}=\left\{A,M\right\}'\right)$
 ${\mathrm{general_model}}{:=}{\mathrm{< an ETS\left(*,*,*\right) 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.)
 > $\mathrm{individual_models}:=\mathrm{Specialize}\left(\mathrm{general_model},\mathrm{ts}\right)$
 ${\mathrm{individual_models}}{:=}\left[{\mathrm{< an ETS\left(A,A,A\right) model >}}{,}{\mathrm{< an ETS\left(A,Ad,A\right) model >}}{,}{\mathrm{< an ETS\left(A,N,A\right) model >}}{,}{\mathrm{< an ETS\left(M,A,A\right) model >}}{,}{\mathrm{< an ETS\left(M,A,M\right) model >}}{,}{\mathrm{< an ETS\left(M,Ad,A\right) model >}}{,}{\mathrm{< an ETS\left(M,Ad,M\right) model >}}{,}{\mathrm{< an ETS\left(M,M,M\right) model >}}{,}{\mathrm{< an ETS\left(M,Md,M\right) model >}}{,}{\mathrm{< an ETS\left(M,N,A\right) model >}}{,}{\mathrm{< an ETS\left(M,N,M\right) model >}}\right]$ (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.
 > $\mathrm{initialization_tables}:=\mathrm{map}\left(\mathrm{Initialize},\mathrm{individual_models},\mathrm{ts}\right):$
 • Here is an example of these initialization values:
 > ${\mathrm{individual_models}}_{1},{\mathrm{initialization_tables}}_{1}$
 ${\mathrm{< an ETS\left(A,A,A\right) model >}}{,}{\mathrm{table}}\left(\left[{\mathrm{b0}}{=}{0.646614725853865}{,}{\mathrm{α}}{=}\frac{{1}}{{2}}{,}{s}{=}\left[\begin{array}{c}{\mathrm{1 .. 12}}{{\mathrm{Vector}}}_{{\mathrm{column}}}\\ {\mathrm{Data Type:}}{{\mathrm{float}}}_{{8}}\\ {\mathrm{Storage:}}{\mathrm{rectangular}}\\ {\mathrm{Order:}}{\mathrm{Fortran_order}}\end{array}\right]{,}{\mathrm{l0}}{=}{122.838670948617}{,}{\mathrm{γ}}{=}\frac{{1}}{{100}}{,}{\mathrm{β}}{=}\frac{{1}}{{10}}\right]\right)$ (25)
 • You can now perform the optimization.
 > $\mathrm{map}\left(\mathrm{Optimize},\mathrm{individual_models},\mathrm{ts}\right)$
 $\left[{-}{579.1189017}{,}{-}{587.8357482}{,}{-}{587.5947092}{,}{-}{577.5718572}{,}{-}{527.4987042}{,}{-}{559.4653237}{,}{-}{528.6819747}{,}{-}{527.8440927}{,}{-}{527.3996887}{,}{-}{565.0019522}{,}{-}{535.5928547}\right]$ (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.
 >
 ${\mathrm{< an ETS\left(A,A,A\right) model >}}{,}{1412.79583526645}$
 ${\mathrm{< an ETS\left(A,Ad,A\right) model >}}{,}{1445.84094061076}$
 ${\mathrm{< an ETS\left(A,N,A\right) model >}}{,}{1422.76337107009}$
 ${\mathrm{< an ETS\left(M,A,A\right) model >}}{,}{1722.03500867297}$
 ${\mathrm{< an ETS\left(M,A,M\right) model >}}{,}{1133.89411113851}$
 ${\mathrm{< an ETS\left(M,Ad,A\right) model >}}{,}{1945.38209291183}$
 ${\mathrm{< an ETS\left(M,Ad,M\right) model >}}{,}{1141.84855312639}$
 ${\mathrm{< an ETS\left(M,M,M\right) model >}}{,}{1139.65948604423}$
 ${\mathrm{< an ETS\left(M,Md,M\right) model >}}{,}{1141.03566969852}$
 ${\mathrm{< an ETS\left(M,N,A\right) model >}}{,}{2087.66988840404}$
 ${\mathrm{< an ETS\left(M,N,M\right) model >}}{,}{1150.95261586989}$ (27)
 • From the results, the $\left(M,A,M\right)$, $\left(M,\mathrm{Ad},M\right)$,, $(M,N,M$), and (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.