BoxCoxTransform - Maple Help

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

 > $\mathrm{with}\left(\mathrm{TimeSeriesAnalysis}\right):$
 > $\mathrm{sales}≔\mathrm{TimeSeries}\left(\left[150,147,114,113,91,164,56,39,32,86\right],\mathrm{startdate}="2010-01-01",\mathrm{frequency}="weekly",\mathrm{header}="Weekly Sales"\right)$
 ${\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)
 > $\mathrm{GetData}\left(\mathrm{sales}\right)\left[..4\right]$
 $\left[\begin{array}{c}{150.}\\ {147.}\\ {114.}\\ {113.}\end{array}\right]$ (2)

Here are the log-transformed data.

 > $\mathrm{log_sales}≔\mathrm{Apply}\left(\mathrm{LogTransform},\mathrm{sales}\right)$
 ${\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)
 > $\mathrm{GetData}\left(\mathrm{log_sales}\right)\left[..4\right]$
 $\left[\begin{array}{c}{5.01063529409626}\\ {4.99043258677874}\\ {4.73619844839450}\\ {4.72738781871234}\end{array}\right]$ (4)

Reconstructing the original data:

 > $\mathrm{original}≔\mathrm{Unapply}\left(\mathrm{LogTransform},\mathrm{log_sales}\right)$
 ${\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)
 > $\mathrm{GetData}\left(\mathrm{original}\right)\left[..4\right]$
 $\left[\begin{array}{c}{150.000000000000}\\ {147.000000000000}\\ {114.000000000000}\\ {113.000000000000}\end{array}\right]$ (6)

Apply the logarithm with base 10 instead.

 > $\mathrm{common_log_sales}≔\mathrm{Apply}\left(\mathrm{LogTransform}\left(\mathrm{base}=10\right),\mathrm{sales}\right)$
 ${\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)
 > $\mathrm{GetData}\left(\mathrm{common_log_sales}\right)\left[..4\right]$
 $\left[\begin{array}{c}{2.17609125905568}\\ {2.16731733474818}\\ {2.05690485133647}\\ {2.05307844348342}\end{array}\right]$ (8)
 > $\mathrm{original_2}≔\mathrm{Unapply}\left(\mathrm{LogTransform}\left(\mathrm{base}=10\right),\mathrm{common_log_sales}\right)$
 ${\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)
 > $\mathrm{GetData}\left(\mathrm{original_2}\right)\left[..4\right]$
 $\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.

 > $\mathrm{boxcox}≔\mathrm{Apply}\left(\mathrm{BoxCoxTransform}\left(\mathrm{\lambda }=\frac{2}{3},\mathrm{geometricmean}=\mathrm{true}\right),\mathrm{sales}\right)$
 ${\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)
 > $\mathrm{GetData}\left(\mathrm{boxcox}\right)\left[..4\right]$
 $\left[\begin{array}{c}{181.145998663384}\\ {178.633601318965}\\ {149.746747925208}\\ {148.830791928663}\end{array}\right]$ (12)
 > $\mathrm{original_3}≔\mathrm{Unapply}\left(\mathrm{BoxCoxTransform}\left(\mathrm{\lambda }=\frac{2}{3},\mathrm{geometricmean}=\mathrm{true}\right),\mathrm{boxcox}\right)$
 ${\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)
 > $\mathrm{GetData}\left(\mathrm{original_3}\right)\left[..4\right]$
 $\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.

 > $\mathrm{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⟩⟩$
 ${\mathrm{average_temperature_matrix}}{≔}\begin{array}{c}\left[\begin{array}{cc}{-12}& {-5}\\ {-11}& {-2}\\ {-6}& {2}\\ {1}& {11}\\ {9}& {18}\\ {12}& {23}\\ {16}& {29}\\ {14}& {26}\\ {10}& {21}\\ {4}& {14}\\ {⋮}& {⋮}\end{array}\right]\\ \hfill {\text{24 × 2 Matrix}}\end{array}$ (15)
 > $\mathrm{average_temperatures}≔\mathrm{TimeSeries}\left(\mathrm{average_temperature_matrix},\mathrm{startdate}="2011-01-01",\mathrm{frequency}="monthly",\mathrm{headers}=\left["average minimum temperature","average maximum temperature"\right]\right)$
 ${\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)
 > $\mathrm{GetData}\left(\mathrm{average_temperatures}\right)\left[..4\right]$
 $\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$.

 > $\mathrm{transformation}≔\mathrm{BoxCoxTransform}\left(\mathrm{shift}=20,\mathrm{\lambda }=\frac{1}{2},'\mathrm{geometricmean}'\right)$
 ${\mathrm{transformation}}{≔}{\mathrm{<< Box-Cox transformation with parameter 1/2 >>}}$ (18)
 > $\mathrm{transformed_temperatures}≔\mathrm{Apply}\left(\mathrm{transformation},\mathrm{average_temperatures}\right)$
 ${\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)
 > $\mathrm{GetData}\left(\mathrm{transformed_temperatures}\right)\left[..4\right]$
 $\left[\begin{array}{cc}{17.0113783673777}& {32.3535475864755}\\ {18.6076635345684}& {36.5163724025921}\\ {25.5079190900768}& {41.5589049817995}\\ {33.3316815594305}& {51.4389969826016}\end{array}\right]$ (20)
 > $\mathrm{original_temperatures}≔\mathrm{Unapply}\left(\mathrm{transformation},\mathrm{transformed_temperatures}\right)$
 ${\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)
 > $\mathrm{GetData}\left(\mathrm{original_temperatures}\right)\left[..4\right]$
 $\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.