TimeSeriesAnalysis - Maple Programming Help

Home : Support : Online Help : Statistics and Data Analysis : Time Series Analysis Package : TimeSeriesAnalysis/BoxCoxTransform

TimeSeriesAnalysis

 BoxCoxTransform
 Box-Cox transformation
 LogTransform
 Log transformation

 Calling Sequence Apply(BoxCoxTransform, timeseries) Apply(BoxCoxTransform(lambda = lm, base = b, geometricmean = g, shift = s), timeseries) Apply(LogTransform, timeseries) Apply(LogTransform(base = b, geometricmean = g, shift = s), timeseries) Unapply(BoxCoxTransform, forecast) Unapply(BoxCoxTransform(lambda = lm, base = b, geometricmean = g, shift = s), forecast) Unapply(LogTransform, forecast) Unapply(LogTransform(base = b, geometricmean = g, shift = s), forecast)

Parameters

 timeseries - forecast - TimeSeries data set, typically obtained from a forecasting method lm - (optional) parameter of the Box-Cox transform, typically between $-2$ and $2$ b - (optional) if lm is $0$, the base of the logarithm g - (optional) true or false, indicating whether or not to correct the scale of the results using the geometric mean s - (optional) constant shift applied before the Box-Cox transform is applied

Description

 • The Box-Cox transformation takes a time series and replaces each value ${T}_{i}$ with the value $\frac{{T}_{i}^{\mathrm{\lambda }}-1}{\mathrm{\lambda }}$. If the parameter $\mathrm{\lambda }$ is set to $0$, the resulting value is $\underset{\mathrm{\lambda }\to 0}{{lim}}\frac{{T}_{i}^{\mathrm{\lambda }}-1}{\mathrm{\lambda }}=\mathrm{ln}\left({T}_{i}\right)$.
 • Apply this transformation to a time series using the Apply command. Translate transformed information, such as a forecast from transformed data, back to the original domain by using the Unapply command.
 • By default, the $\mathrm{\lambda }$ parameter has value $0$, and the transformation is the log transformation (the natural logarithm, with base $ⅇ$). By supplying the $\mathrm{\lambda }$ option, you can set this parameter to a different value. The command LogTransform is the same as BoxCoxTransform, except you cannot supply the lambda parameter: it is fixed to always be $0$.
 • If $\mathrm{\lambda }=0$, then you can choose to use a different base for the logarithm by supplying the $\mathrm{base}$ option. For example, to obtain the common logarithm, supply the option $\mathrm{base}=10$. It is an error to supply the $\mathrm{base}$ option if $\mathrm{\lambda }$ is set to a value different from $0$. The default base is $ⅇ$.
 • Negative data points result in undefined values. To prevent this from happening, you can supply the $\mathrm{shift}=s$ option. The value $s$ is added to each data point before the transformation is applied. By default, $s$ is $0$.
 • Applying a Box-Cox transformation may have the transformed time series being of a different order of magnitude than the original time series, and Box-Cox transformations with different values for the parameter $\mathrm{\lambda }$ may also have different orders of magnitude. If this is undesirable, for example when comparing an absolute measure of error involving the results of different Box-Cox transformations, you can supply the geometricmean = true option. This computes the geometric mean of each data set after the shift is applied but before the transformation is applied; then after applying the transformation, each data value is multiplied by this geometric mean, raised to the power $1-\mathrm{\lambda }$. This can be viewed as preserving the units of measurement that the time series data is expressed in.

Examples

 > with(TimeSeriesAnalysis):
 > sales := TimeSeries([150, 147, 114, 113, 91, 164, 56, 39, 32, 86], startdate="2010-01-01", frequency="weekly", header = "Weekly Sales");
 ${\mathrm{sales}}{≔}\left[\begin{array}{c}{\mathrm{Time series}}\\ {\mathrm{Weekly Sales}}\\ {\mathrm{10 rows of data:}}\\ {\mathrm{2010-01-01 - 2010-03-05}}\end{array}\right]$ (1)
 > GetData(sales)[..4];
 $\left[\begin{array}{c}{150.}\\ {147.}\\ {114.}\\ {113.}\end{array}\right]$ (2)

Here are the log-transformed data.

 > log_sales := Apply(LogTransform, sales);
 ${\mathrm{log_sales}}{≔}\left[\begin{array}{c}{\mathrm{Time series}}\\ {\mathrm{Logarithm of Weekly Sales}}\\ {\mathrm{10 rows of data:}}\\ {\mathrm{2010-01-01 - 2010-03-05}}\end{array}\right]$ (3)
 > GetData(log_sales)[..4];
 $\left[\begin{array}{c}{5.01063529409626}\\ {4.99043258677874}\\ {4.73619844839450}\\ {4.72738781871234}\end{array}\right]$ (4)

Reconstructing the original data:

 > original := Unapply(LogTransform, log_sales);
 ${\mathrm{original}}{≔}\left[\begin{array}{c}{\mathrm{Time series}}\\ {\mathrm{Weekly Sales}}\\ {\mathrm{10 rows of data:}}\\ {\mathrm{2010-01-01 - 2010-03-05}}\end{array}\right]$ (5)
 > GetData(original)[..4];
 $\left[\begin{array}{c}{150.000000000000}\\ {147.000000000000}\\ {114.000000000000}\\ {113.000000000000}\end{array}\right]$ (6)

Apply the logarithm with base 10 instead.

 > common_log_sales := Apply(LogTransform(base = 10), sales);
 ${\mathrm{common_log_sales}}{≔}\left[\begin{array}{c}{\mathrm{Time series}}\\ {\mathrm{Logarithm of Weekly Sales}}\\ {\mathrm{10 rows of data:}}\\ {\mathrm{2010-01-01 - 2010-03-05}}\end{array}\right]$ (7)
 > GetData(common_log_sales)[..4];
 $\left[\begin{array}{c}{2.17609125905568}\\ {2.16731733474818}\\ {2.05690485133647}\\ {2.05307844348342}\end{array}\right]$ (8)
 > original_2 := Unapply(LogTransform(base = 10), common_log_sales);
 ${\mathrm{original_2}}{≔}\left[\begin{array}{c}{\mathrm{Time series}}\\ {\mathrm{Weekly Sales}}\\ {\mathrm{10 rows of data:}}\\ {\mathrm{2010-01-01 - 2010-03-05}}\end{array}\right]$ (9)
 > GetData(original_2)[..4];
 $\left[\begin{array}{c}{150.000000000000}\\ {147.000000000000}\\ {114.000000000000}\\ {113.000000000000}\end{array}\right]$ (10)

We now apply the Box-Cox transformation with parameter $\mathrm{\lambda }=\frac{2}{3}$ to the sales data. We also apply the geometric mean correction.

 > boxcox := Apply(BoxCoxTransform(lambda=2/3, geometricmean=true), sales);
 ${\mathrm{boxcox}}{≔}\left[\begin{array}{c}{\mathrm{Time series}}\\ {\mathrm{Box-Cox transform of Weekly Sales}}\\ {\mathrm{10 rows of data:}}\\ {\mathrm{2010-01-01 - 2010-03-05}}\end{array}\right]$ (11)
 > GetData(boxcox)[..4];
 $\left[\begin{array}{c}{181.145998663384}\\ {178.633601318965}\\ {149.746747925208}\\ {148.830791928663}\end{array}\right]$ (12)
 > original_3 := Unapply(BoxCoxTransform(lambda=2/3, geometricmean=true), boxcox);
 ${\mathrm{original_3}}{≔}\left[\begin{array}{c}{\mathrm{Time series}}\\ {\mathrm{Weekly Sales}}\\ {\mathrm{10 rows of data:}}\\ {\mathrm{2010-01-01 - 2010-03-05}}\end{array}\right]$ (13)
 > GetData(original_3)[..4];
 $\left[\begin{array}{c}{150.000000000000}\\ {147.000000000000}\\ {114.000000000000}\\ {113.000000000000}\end{array}\right]$ (14)

Temperature data in Celsius is sometimes negative. These were the daily minimum and maximum temperatures in Waterloo, Ontario, Canada, averaged over a month.

 > average_temperature_matrix := <-12, -5; -11, -2; -6, 2; 1, 11; 9, 18; 12, 23; 16, 29; 14, 26; 10, 21; 4, 14; 1, 10; -4, 3; -7, 1; -5, 2; 0, 12; 0, 12; 9, 22; 13, 25; 15, 29; 13, 26; 9, 21; 5, 13; -2, 6; -3, 3>;
  (15)
 > average_temperatures := TimeSeries(average_temperature_matrix, startdate = "2011-01-01", frequency = "monthly", headers = ["average minimum temperature", "average maximum temperature"]);
 ${\mathrm{average_temperatures}}{≔}\left[\begin{array}{c}{\mathrm{Time series}}\\ {\mathrm{average minimum temperature, average maximum temperature}}\\ {\mathrm{24 rows of data:}}\\ {\mathrm{2011-01-01 - 2012-12-01}}\end{array}\right]$ (16)
 > GetData(average_temperatures)[..4];
 $\left[\begin{array}{cc}{-12.}& {-5.}\\ {-11.}& {-2.}\\ {-6.}& {2.}\\ {1.}& {11.}\end{array}\right]$ (17)

In order to apply the Box-Cox transform, we need to shift the data by a number greater than minus the minimum value, $12$.

 > transformation := BoxCoxTransform(shift = 20, lambda = 1/2, 'geometricmean');
 ${\mathrm{transformation}}{≔}{\mathrm{<< Box-Cox transformation with parameter 1/2 >>}}$ (18)
 > transformed_temperatures := Apply(transformation, average_temperatures);
 ${\mathrm{transformed_temperatures}}{≔}\left[\begin{array}{c}{\mathrm{Time series}}\\ {\mathrm{Box-Cox transform of average minimum temperature, Box-Cox transform of average maximum temperature}}\\ {\mathrm{24 rows of data:}}\\ {\mathrm{2011-01-01 - 2012-12-01}}\end{array}\right]$ (19)
 > GetData(transformed_temperatures)[..4];
 $\left[\begin{array}{cc}{17.0113783673777}& {32.3535475864755}\\ {18.6076635345684}& {36.5163724025921}\\ {25.5079190900768}& {41.5589049817995}\\ {33.3316815594305}& {51.4389969826016}\end{array}\right]$ (20)
 > original_temperatures := Unapply(transformation, transformed_temperatures);
 ${\mathrm{original_temperatures}}{≔}\left[\begin{array}{c}{\mathrm{Time series}}\\ {\mathrm{average minimum temperature, average maximum temperature}}\\ {\mathrm{24 rows of data:}}\\ {\mathrm{2011-01-01 - 2012-12-01}}\end{array}\right]$ (21)
 > GetData(original_temperatures)[..4];
 $\left[\begin{array}{cc}{-12.0000000000000}& {-5.00000000000000}\\ {-11.}& {-2.00000000000001}\\ {-6.}& {1.99999999999999}\\ {0.999999999999989}& {11.0000000000000}\end{array}\right]$ (22)

Compatibility

 • The TimeSeriesAnalysis[BoxCoxTransform] and TimeSeriesAnalysis[LogTransform] commands were introduced in Maple 18.