Moving Average
In statistics, a moving average (rolling average or running average) is a calculation to analyze data points by creating series of averages of different subsets of the full data set. It is also called a moving mean (MM)^{[1]} or rolling mean and is a type of finite impulse response filter. Variations include: simple, and cumulative, or weighted forms (described below).
Two parts of notion "Moving" and "Average" had to be defined mathematically:
- Moving as additive operation in vector space (continuous) or additive group (discrete). It incorporates to have reference position in space that moves.
- Average by creating a mean for a subset of the collected data according to the reference position in space (generalized expected value for the reference position)
Given a series of numbers and a fixed subset size, the first element of the moving average is obtained by taking the average of the initial fixed subset of the number series. Then the subset is modified by "shifting forward"; that is, excluding the first number of the series and including the next value in the subset.
A moving average is commonly used with time series data to smooth out short-term fluctuations and highlight longer-term trends or cycles. The threshold between short-term and long-term depends on the application, and the parameters of the moving average will be set accordingly. For example, it is often used in technical analysis of financial data, like stock prices, returns or trading volumes. It is also used in economics to examine gross domestic product, employment or other macroeconomic time series. Mathematically, a moving average is a type of convolution and so it can be viewed as an example of a low-pass filter used in signal processing. When used with non-time series data, a moving average filters higher frequency components without any specific connection to time, although typically some kind of ordering is implied. Viewed simplistically it can be regarded as smoothing the data.
Contents
- 1 Generic Approach to Moving Average
- 2 Simple moving average - discrete
- 3 Simple moving average - continuous
- 4 Cumulative moving average
- 5 Moving average applied on images
- 6 Weighted moving average
- 7 Exponential moving average
- 8 Probability Distribution as Distribution of Importance
- 9 Other weightings
- 10 Moving median
- 11 Moving average regression model
- 12 See also
- 13 Notes and references
- 14 External links
Generic Approach to Moving Average[edit]
An element moves in a additive Group (mathematics) or Vector Space V. In a generic approach, we have a moving probability distribution that defines how the values in the environment of have an impact on the moving average.
Discrete/continuous Moving Average[edit]
According to probability distributions we have to distinguish between a
- discrete (probability mass function ) and
- continuous (probability density function )
moving average. The terminology refers to probability distributions and the semantics of probability mass/density function describes the distrubtion of weights around the value . In the discrete setting the means that has a 20% impact on the moving average for .
Moving/Shift Distributions[edit]
If the probility distribution are shifted by in . This means that the probability mass functions resp. probability density functions are generated by a probability distribution at the zero element of the additive group resp. zero vector of the vector space. Due to nature of the collected data f(x) exists for a subset . In many cases T are the points in time for which data is collected. The and the shift of a distribution is defined by the following property:
- discrete: For all the probability mass function fulfills for
- continuous: For all probability density function fulfills
The moving average is defined by:
- discrete: (probability mass function )
Remark: for a countable subset of
- continuous probability density function
It is important for the definition of probability mass functions resp. probability density functions that the support (measure theory) of is a subset of T. This assures that 100% of the probability mass is assigned to collected data. The support is defined as:
Simple moving average - discrete[edit]
In financial applications a simple moving average (SMA) is the unweighted mean of the previous n data. However, in science and engineering the mean is normally taken from an equal number of data on either side of a central value. This ensures that variations in the mean are aligned with the variations in the data rather than being shifted in time. An example of a simple equally weighted running mean for a n-day sample of closing price is the mean of the previous n days' closing prices.
and for with as additive group.
Let be the cost/price of product at time . If those prices are and we want to create a simple moving average at day and looking back for time span of days then the formula is
When calculating successive values for other days/time , a new value comes into the sum and an old value drops out, meaning a full summation each time is unnecessary for this simple case,
The period selected depends on the type of movement of interest, such as short, intermediate, or long-term. In financial terms moving-average levels can be interpreted as support in a falling market, or resistance in a rising market. If you draw a graph for and cost function , you will identify, that the graph of runs smoother in the time
If the data used are not centered around the mean, a simple moving average lags behind the latest datum point by half the sample width. An SMA can also be disproportionately influenced by old datum points dropping out or new data coming in. One characteristic of the SMA is that if the data have a periodic fluctuation, then applying an SMA of that period will eliminate that variation (the average always containing one complete cycle). But a perfectly regular cycle is rarely encountered.^{[2]}
For a number of applications, it is advantageous to avoid the shifting induced by using only 'past' data. Hence a central moving average can be computed, using data equally spaced on either side of the point in the series where the mean is calculated.^{[3]} This requires using an odd number of datum points in the sample window.
and for with as additive group.
A major drawback of the SMA is that it lets through a significant amount of the signal shorter than the window length. Worse, it actually inverts it. This can lead to unexpected artifacts, such as peaks in the smoothed result appearing where there were troughs in the data. It also leads to the result being less smooth than expected since some of the higher frequencies are not properly removed.
Simple moving average - continuous[edit]
If we consider a continous measurement of value e.g. a force at time . The objective is to smooth the values with a continous simple moving average. We look a time span in the past. As probability distribution we use a uniform distribution (mathematics) for the intervall . The density function is:
- and
Application on the moving average definition for continuous probability distriubtions we get:
Cumulative moving average[edit]
Cumulative moving average - discrete[edit]
In a cumulative moving average, the data arrive in an ordered datum stream with , and the user would like to get the average of all of the data up until the current datum point . For example, an investor may want the average price of all of the stock transactions for a particular stock up until the current time . The starting point of data collection is . As each new transaction occurs, the average price at the time of the transaction can be calculated for all of the transactions up to that point using the cumulative average, typically an equally weighted average of the sequence of t+1 values up to the current time :
The brute-force method to calculate this would be to store all of the data and calculate the sum and divide by the number of datum points every time a new datum point arrived. However, it is possible to simply update cumulative average as a new value, becomes available, using the formula:
Thus the current cumulative average for a new datum point is equal to the previous cumulative average at time t-1, times t, plus the latest datum point, all divided by the number of points received so far, n+1. When all of the datum points arrive (n = N), then the cumulative average will equal the final average. It is also possible to store a running total of the datum point as well as the number of points and dividing the total by the number of datum points to get the CMA each time a new datum point arrives.
The derivation of the cumulative average formula is straightforward. Using
and similarly for t + 1, it is seen that
Solving this equation for results in:
Cumulative moving average - continuous[edit]
If we consider a continuous measurement of values e.g. a force at time . The objective is to smooth the values with a continous aggregated moving average. We look a time span in the past. As probability distribution we use a uniform distribution (mathematics) for the intervall . The density function is:
- .
Application on the cumulative moving average definition for continuous probability distriubtions we get:
Moving average applied on images[edit]
A weighted average is an average that has multiplying factors to give different weights to data at different positions in the sample window. Mathematically, the moving average is the convolution of the datum points with a fixed weighting function. One application is creating a pixelisation from a digital graphical image. For all the image on the right pixelisation is applied for several squares. All pixels in the square are replaced by the color average of all pixels in the square. Because colors are defined by three integer numbers so that color average must be rounded for that application. In order to understand color encoding with integer numbers see HTML Color Picker with the RGB color encoding. Three value between 0 and 255 (e.g. rgb(255, 153, 102) for light orange) encode a color. Due to the fact that the HTML colors for Red, Grenn, Blue (RGB) are integer numbers, the real values of the moving average are rounded as a technical constraint.
The image I with m pixels height and n pixels width is s matrix where all components of the matrix are RGB triples of integer values between 0 and 255, i.e. .
A single pixel at row r and column c is denoted as . If we define then the
- intensity of red is ,
- intensity of green is ,
- intensity of red is
If we calculate and average of colors, we calculate the average of red, green and blue separately. As an example we calculate an average of sub matrix of the image I for the four pixels:
The calculated moving average for this square is:
- Red:
- Green:
- Blue:
The calculate moving average for the sub matrix of the image I will replace all original colors of the square. Let the image with the moving average applied for all sub matrices, then the selected sub matrix above in IMA will look like this:
The last step assigns the calculated average color rgb(222, 84, 39) to all pixels of the 2x2-square submatrix.
Looking at the example image on right, the application of the moving average are visible, because they are applied on a large submatrix of the image.
For the image processing with the neutral element as a the additive group with addition:
- and
T is the set of all row and column indices of the pixels. The images is decomposed the squares or even rectangles . The moving average is calculated for all pixels in the rectangle similar to mentioned above. The calculated moving average from the original image I is assigned to all pixels of the square/rectangle in IMA. If the width and height of the rectangles have in general a default size. Close the borders of the images, the sizes of these rectangles have to be adapted to the remaining pixels at the right and bottom border of the image I.
Weighted moving average[edit]
In technical analysis of financial data, a weighted moving average (WMA) has the specific meaning of weights that decrease in arithmetical progression.^{[4]} In an n-day WMA the latest day has weight n, the second latest n − 1, etc., down to one. These weights create a discrete probability distribution with:
- and
The weighted moving average can be calculated for with the discrete probability mass function at time , where is the initial day, when data collection of the financial data begins and the price/cost of a product at day . the price/cost of a product at day for an arbitrary day x.
The denominator is a triangle number equal to which creates a discrete probability distribution by:
The graph at the right shows how the weights decrease, from highest weight at day t for the most recent datum points, down to zero at day t-n.
In the more general case with weights the denominator will always be the sum of the individual weights, i.e.:
- and as weight for for the most recent datum points at day t and as weight for the day , which is n-th day before the most recent day .
The discrete probability distribution is defined by:
The weighted moving average with arbitrary weights is calculated by:
This general approach can be compared to the weights in the exponential moving average in the following section.
Exponential moving average[edit]
An exponential moving average (EMA), also known as an exponentially weighted moving average (EWMA),^{[5]} is a type of infinite impulse response filter that applies weighting factors which decrease exponentially. The weighting for each older datum decreases exponentially, never reaching zero. The graph at right shows an example of the weight decrease.
The EMA for a series of collected data with a set of dates , where is the collected data at time index .
- First of all we have to defined a value with , that represents the degree of weighting decrease as a constant smoothing factor between 0 and 1. A lower α discounts older observations faster.
- The weights are defined by
- for all with (geometric series).
- The sum of weights from 0 to the time index is defined by:
- The discrete probability mass function is defined by:
The definition above creates the exponential moving average EMA with discrete probability mass function by
The at time index may be calculated recursively:
- and
- for all
Where:
- The coefficient represents the degree of weighting decrease from to . This implements the aging of data from to time index .
- the fraction adjusts the denominator for .
- the coefficient in the EMA at time index t+1.
Initialization of EMA and Elimination of MA Impact form old data[edit]
may be initialized in a number of different ways, most commonly by setting to the first collected data at time index 0 as shown above, though other techniques exist, such as starting the calculation of the moving average after the first 4 or 5 observations. Furthermore only a most recent subset of collected data before the time index from the total history of collected date might be used for the . The discrete probability mass function puts weights on the most recent values of the collected data by:
The limitations to the most recent values of the collected data eliminates the impact of very old data on the resultant moving average completely. By choosing a small old data is less important than recent data and discounts older observations faster, but even the oldest data has a impact on the calculation of at time index .
Tne initialiation of could incorporate something about values prior to the available data, i.e. history before . Tne initialiation could introduce an error in the . In view of this the early results should be regarded as unreliable until the iterations have had time to converge. This is sometimes called a 'spin-up' interval.
This formulation of EMA is designed as an application of an expected value, which is a standard definition in probability theory.
According to Hunter (1986).^{[6]} this can be written as an repeated application of the recursive formula for different times without standardisation, i.e.
- .
An alternate approach defined by Roberts (1959)^{[7]} is missing the standardisation of the probability distribution too, while the basic principle of exponential moving average remains the same.
Application to measuring computer performance[edit]
Some computer performance metrics, e.g. the average process queue length, or the average CPU utilization, use a form of exponential moving average with the recursive definition.
Here α is defined as a function of time between two readings. An example of a coefficient giving bigger weight to the current reading, and smaller weight to the older readings is
where exp() is the exponential function, time for readings t_{n} is expressed in seconds, and W is the period of time in minutes over which the reading is said to be averaged (the mean lifetime of each reading in the average). Given the above definition of α, the moving average can be expressed as
For example, a 15-minute average L of a process queue length Q, measured every 5 seconds (time difference is 5 seconds), is computed as
Probability Distribution as Distribution of Importance[edit]
The definition of expected value provides the mathematical foundation for moving averages in the discrete and continuous setting and the mathematical theory is just an application of basic principles of probability theory. Nevertheless the notion of probability is bit misleading because the semantic of moving average does not refer to probability of events. The probability must be regarded as distribution of importance. In time series e.g. less importance is assigned to older data and that does not mean that older data is less likely than recent data. The events that create the collected data are not considered from probability perspective in general.
Importance can be defined by moving averages by;
- proximity in time (old and recent data)
- proximity in space (see application of the moving average on images above)
To quantify this proximity a Metric or Norm on the underlying vector space can be assigned. Greater distance to reference point in lead to less importance, e.g. by
- .
The weight for the importance is 1 for . For increasing distance measure by the norm decreases the weight towards 0. Standardization with as sum of all weights for discrete moving averages (as mentioned for EMA) lead to the property of probability distributions:
- .
Furthermore there are other moving averages that incorporate negative weights. This leads to the fact that
- . This could happen when the positive/negative impact of collected data is assigned to the weight and the probability mass function. The assignment of impact factors of collected data to the probability/importance values mixes two different properities. This should be avoided and the impact on should be kept separately for a transparent definition of the moving average, i.e.
- with .
Other weightings[edit]
Other weighting systems are used occasionally – for example, in share trading a volume weighting will weight each time period in proportion to its trading volume.
A further weighting, used by actuaries, is Spencer's 15-Point Moving Average^{[8]} (a central moving average). The symmetric weight coefficients are −3, −6, −5, 3, 21, 46, 67, 74, 67, 46, 21, 3, −5, −6, −3.
Outside the world of finance, weighted running means have many forms and applications. Each weighting function or "kernel" has its own characteristics. In engineering and science the frequency and phase response of the filter is often of primary importance in understanding the desired and undesired distortions that a particular filter will apply to the data.
A mean does not just "smooth" the data. A mean is a form of low-pass filter. The effects of the particular filter used should be understood in order to make an appropriate choice. On this point, the French version of this article discusses the spectral effects of 3 kinds of means (cumulative, exponential, Gaussian).
Moving median[edit]
From a statistical point of view, the moving average, when used to estimate the underlying trend in a time series, is susceptible to rare events such as rapid shocks or other anomalies. A more robust estimate of the trend is the simple moving median over n time points:
where the median is found by, for example, sorting the values inside the brackets and finding the value in the middle. For larger values of n, the median can be efficiently computed by updating an indexable skiplist.^{[9]}
Statistically, the moving average is optimal for recovering the underlying trend of the time series when the fluctuations about the trend are normally distributed. However, the normal distribution does not place high probability on very large deviations from the trend which explains why such deviations will have a disproportionately large effect on the trend estimate. It can be shown that if the fluctuations are instead assumed to be Laplace distributed, then the moving median is statistically optimal.^{[10]} For a given variance, the Laplace distribution places higher probability on rare events than does the normal, which explains why the moving median tolerates shocks better than the moving mean.
When the simple moving median above is central, the smoothing is identical to the median filter which has applications in, for example, image signal processing.
Moving average regression model[edit]
- moving average regression model, a variable of interest is assumed to be a weighted moving average of unobserved independent error terms; the weights in the moving average are parameters to be estimated. In a
Those two concepts are often confused due to their name, but while they share many similarities, they represent distinct methods and are used in very different contexts.
See also[edit]
- Exponential smoothing
- Moving average convergence/divergence indicator
- Window function
- Moving average crossover
- Rising moving average
- Running total
- Local regression
- Kernel smoothing
- Moving least squares
Notes and references[edit]
- ↑ Hydrologic Variability of the Cosumnes River Floodplain (Booth et al., San Francisco Estuary and Watershed Science, Volume 4, Issue 2, 2006)
- ↑ Statistical Analysis, Ya-lun Chou, Holt International, 1975, ISBN 0-03-089422-0, section 17.9.
- ↑ The derivation and properties of the simple central moving average are given in full at Savitzky–Golay filter
- ↑ "Weighted Moving Averages: The Basics". Investopedia.
- ↑ http://lorien.ncl.ac.uk/ming/filter/filewma.htm
- ↑ NIST/SEMATECH e-Handbook of Statistical Methods: Single Exponential Smoothing at the National Institute of Standards and Technology
- ↑ NIST/SEMATECH e-Handbook of Statistical Methods: EWMA Control Charts at the National Institute of Standards and Technology
- ↑ Spencer's 15-Point Moving Average — from Wolfram MathWorld
- ↑ http://code.activestate.com/recipes/576930/
- ↑ G.R. Arce, "Nonlinear Signal Processing: A Statistical Approach", Wiley:New Jersey, USA, 2005.
External links[edit]
Download Excel file to calculate SMA & EMA
Subject classification: this is a statistics resource. |
Template:Technical analysis Template:Quantitative forecasting methods