Time Series Analysis (Moving Average Filters): Difference between revisions

From Met Dynamics
Jump to navigation Jump to search
md>Scott.Munro
imported>Scott.Munro
 
(2 intermediate revisions by 2 users not shown)
Line 8: Line 8:
== Model theory ==
== Model theory ==


{{Under construction|section}}
=== Simple moving average ===
 
The simple moving average (SMA) at element <math>t</math> of a data series is the unweighted mean of the previous <math>k</math> values, or the previous <math>t</math> values if <math>t<k</math>.<ref>Wikipedia Contributors (2024). ''Moving average''. [online] Wikipedia. Available at: https://en.wikipedia.org/wiki/Moving_average [Accessed 8 Jun. 2024].</ref> That is:
 
:<math>
\mathit{SMA}_{k,t} =
\begin{cases}
{\dfrac {1}{k}}\sum\limits_{i=t-k+1}^{t}p_{i} & t\geq k\\
{\dfrac {1}{t}}\sum\limits_{i=1}^{t}p_{i} & t<k\\
\end{cases}
</math>
 
where <math>p_1,p_2,\dots,p_t</math> are the data points to average.
 
=== Exponential moving average ===
 
The Exponential Moving Average (EMA), or exponential smoothing, of a data series at element <math>t</math> is:<ref>Wikipedia Contributors (2024). ''Exponential smoothing''. [online] Wikipedia. Available at: https://en.wikipedia.org/wiki/Exponential_smoothing [Accessed 8 Jun. 2024].</ref>
 
:<math>\mathit{EMA}_{\alpha,t} = \alpha \cdot p_t + (1 - \alpha) \cdot \mathit{EMA}_{\alpha,t-1}, \quad t>1</math>
 
where <math>\alpha</math> is the smoothing factor.
 
=== Linear regression moving average ===
 
A Linear Regression Moving Average (LRMA) fits a straight line to a moving window of data points with width <math>k</math>.
 
The LRMA at element <math>t</math> over the window of <math>k</math> elements is calculated from:
 
:<math>\mathit{LRMA}_{k,t} = a + b \cdot t</math>
 
where <math>a</math> (intercept) and <math>b</math> (slope) are determined by minimising the sum of squared residuals of the data points <math>(t-k+1, P_{t-k+1}),(t-k+2, P_{t-k+2}),\dots,(t,P_t)</math>.
 
The coefficients <math>a</math> and <math>b</math> can be directly calculated from:<ref>Wikipedia Contributors (2024). ''Simple linear regression''. [online] Wikipedia. Available at: https://en.wikipedia.org/wiki/Simple_linear_regression [Accessed 8 Jun. 2024].</ref>
 
:<math>
\begin{aligned}
a &= \dfrac{\sum\limits_{i=1}^{k} p_{t-k+i} - b \sum\limits_{i=1}^{k} i}{k}\\
b &= \dfrac{k \sum\limits_{i=1}^{k} i p_{t-k+i} - \sum\limits_{i=1}^{k} i \sum\limits_{i=1}^{k} p_{t-k+i}}{k \sum\limits_{i=1}^{k} i^2 - \left( \sum\limits_{i=1}^{k} i \right)^2}
\end{aligned}
</math>
 
for <math>t>1</math> and <math>k>2</math>.
 
The Linear Regression Moving Average method may alternatively be referred to as the ''Least Squares Moving Average''.


== Excel ==
== Excel ==
Line 52: Line 95:
   |-  style="vertical-align:top;"
   |-  style="vertical-align:top;"
   |colspan="2"|
   |colspan="2"|
where:
where <math>n</math> is the number of data points.
* <math>n</math> is the number of data points
* <math>p</math> are the data points to average
* <math>k</math> is the number of previous data points over which the moving average is taken
* <math>\mathit{SMA}_k</math> is the value of the simple moving average over the previous <math>k</math> data points
   |}  
   |}  
|}
|}
Line 90: Line 129:
   \mathit{mdTimeSeries\_EMA}  & =   
   \mathit{mdTimeSeries\_EMA}  & =   
\begin{bmatrix}
\begin{bmatrix}
\mathit{EMA}_{k,1}\\
\mathit{EMA}_{\alpha,1}\\
\vdots\\
\vdots\\
\mathit{EMA}_{\alpha,n}\\
\mathit{EMA}_{\alpha,n}\\
Line 98: Line 137:
   |-  style="vertical-align:top;"
   |-  style="vertical-align:top;"
   |colspan="2"|
   |colspan="2"|
where:
 
* <math>n</math> is the number of data points
* <math>p</math> are the data points to average
* <math>\alpha</math> is the smoothing factor
* <math>\mathit{EMA}_\alpha</math> is the value of the exponential moving average
   |}  
   |}  
|}
|}
Line 144: Line 179:
   |-  style="vertical-align:top;"
   |-  style="vertical-align:top;"
   |colspan="2"|
   |colspan="2"|
where:
* <math>n</math> is the number of data points
* <math>p</math> are the data points to average
* <math>k</math> is the number of previous data points over which the linear regression moving average is taken
* <math>\mathit{LRMA}_k</math> is the value of the linear regression moving average of the previous <math>k</math> data points
   |}  
   |}  
|}
|}

Latest revision as of 14:15, 8 June 2024

Description

This article describes several filters for the analysis of time series data, including:

  • Simple Moving Average (SMA)
  • Exponential Moving Average (EMA)
  • Linear Regression Moving Average (LRMA)

Model theory

Simple moving average

The simple moving average (SMA) at element of a data series is the unweighted mean of the previous values, or the previous values if .[1] That is:

where are the data points to average.

Exponential moving average

The Exponential Moving Average (EMA), or exponential smoothing, of a data series at element is:[2]

where is the smoothing factor.

Linear regression moving average

A Linear Regression Moving Average (LRMA) fits a straight line to a moving window of data points with width .

The LRMA at element over the window of elements is calculated from:

where (intercept) and (slope) are determined by minimising the sum of squared residuals of the data points .

The coefficients and can be directly calculated from:[3]

for and .

The Linear Regression Moving Average method may alternatively be referred to as the Least Squares Moving Average.

Excel

Figure 1. Example showing the selection of the TimeSeriesData (blue frame), k (red frame) and Results (light blue frame) arrays in Excel, for the Simple Moving Average (SMA) case.

The Simple Moving Average (SMA) filter may be invoked from the Excel formula bar with the following function calls:

=mdTimeSeries_SMA(TimeSeriesData as Range, k as Integer)

Invoking the function with no arguments will print Help text associated with the model, including a link to this page.

The input parameters and calculation results are defined below in matrix notation, along with an example image showing the selection of the same cells and arrays in the Excel interface:

where is the number of data points.

The Exponential Moving Average (EMA) filter may be invoked from the Excel formula bar with the following function calls:

=mdTimeSeries_EMA(TimeSeriesData as Range, alpha as Double)

Invoking the function with no arguments will print Help text associated with the model, including a link to this page.

The input parameters and calculation results are defined below in matrix notation, along with an example image showing the selection of the same cells and arrays in the Excel interface:

The Linear Regression Moving Average (LRMA) filter may be invoked from the Excel formula bar with the following function calls:

=mdTimeSeries_LRMA(TimeSeriesData as Range, k as Integer)

Invoking the function with no arguments will print Help text associated with the model, including a link to this page.

The input parameters and calculation results are defined below in matrix notation, along with an example image showing the selection of the same cells and arrays in the Excel interface:

References

  1. Wikipedia Contributors (2024). Moving average. [online] Wikipedia. Available at: https://en.wikipedia.org/wiki/Moving_average [Accessed 8 Jun. 2024].
  2. Wikipedia Contributors (2024). Exponential smoothing. [online] Wikipedia. Available at: https://en.wikipedia.org/wiki/Exponential_smoothing [Accessed 8 Jun. 2024].
  3. Wikipedia Contributors (2024). Simple linear regression. [online] Wikipedia. Available at: https://en.wikipedia.org/wiki/Simple_linear_regression [Accessed 8 Jun. 2024].