module MovingAverage(T)
Overview
The MovingAverage mixin provides methods for calculating different moving averages of a collection.
Including types must respond to basic arithmetic operators: +, -, *, and /.
MovingAverage uses the following methods which must be defined:
newfor creating a new instance ofself.sizefor determining the size of the collection.sumfor determining the sum of the collection.lastfor retrieving the last element of the collection.<<(value : T)for appending an element to the end of the collection.[] (range : Range(Int, Int))for retrieving a section of the collection.
Direct including types
Defined in:
moving_average/errors.crmoving_average/exponential_moving_average.cr
moving_average/simple_moving_average.cr
moving_average/smoothed_moving_average.cr
moving_average/version.cr
moving_average.cr
Constant Summary
-
VERSION =
"0.1.0"
Instance Method Summary
-
#ema(period : Int)
Alias for
#exponential_moving_average. -
#ema!(period : Int, value : T, previous_value = nil)
Alias for
#exponential_moving_average!. -
#exponential_moving_average(period : Int)
Creates a new
selfcontaining the exponential moving average (EMA) of each element. -
#exponential_moving_average!(period : Int, value : T, previous_ema = nil)
Evaluates the exponential moving average of value and appends it to
self. -
#simple_moving_average(period : Int)
Creates a new
selfcontaining the simple moving average (SMA) of each element. -
#sma(period : Int)
Alias for
#simple_moving_average. -
#smma(period : Int)
Alias for
#smoothed_moving_average. -
#smma!(period : Int, value : T, previous_smma = nil)
Alias for
#smoothed_moving_average!. -
#smoothed_moving_average(period : Int)
Creates a new
selfcontaining the smoothed moving average (SMMA) of each element. -
#smoothed_moving_average!(period : Int, value : T, previous_smma = nil)
Evaluates the smoothed moving average of value and appends it to
self.
Instance Method Detail
Creates a new self containing the exponential moving average (EMA) of each element.
The EMA can be defined as the series of weighted values in which the weighting decreases exponentially. The first element of the EMA is the simple moving average, all subsequent elements can be calculated as follows:
EMAi = weighting_factor * (Pi - EMAi-1) + EMAi-1
Where
- Pi is the value of element i.
- weighting_factor is a constant between 0 and 1, determined by the period.
[1.0, 2.0, 3.0, 4.0, 5.0].exponential_moving_average(4) # => [2.5, 3.5]
Evaluates the exponential moving average of value and appends it to self.
Useful for updating an existing EMA series quickly. If previous_ema is nil,
the last element in the collection will be used.
ema = [1, 2, 3, 4, 5].exponential_moving_average(4) # => [2, 3]
ema.exponential_moving_average!(4, 8) # => [2, 3, 5]Creates a new self containing the simple moving average (SMA) of each element.
The SMA of a single element can be defined as the mean of previous n data:
SMAi = (Pi + Pi-1 + ... + Pi-(n-1)) / n
Where
- Pi is the value of element i.
- n corresponds to the period and is the number of datum to consider.
[1, 2, 3, 4, 5].simple_moving_average(4) # => [2, 3]
Creates a new self containing the smoothed moving average (SMMA) of each element.
The SMMA is an exponential weighted average without a fixed period. Each previous datum has some affect to the current SMMA. The first element is the simple moving average, all subsequent elements can be calculated as follows:
SMMAi = (Pi - SMMAi-1) / n + SMMAi-1
Where
- Pi is the value of element i.
- n is the smoothing period.
[1, 2, 3, 4, 5].smoothed_moving_average(4) # => [2, 2]
Evaluates the smoothed moving average of value and appends it to self.
Useful when updating an existing SMMA series quickly. If previous_smma is nil,
the last element will be used.
smma = [1, 2, 3, 4, 5].smoothed_moving_average(4) # => [2, 2]
smma.smoothed_moving_average!(4, 8) # => [2, 2, 3]