Class: Array
- Inherits:
-
Object
- Object
- Array
- Defined in:
- lib/moving_average/moving_average.rb
Instance Method Summary collapse
-
#exponential_moving_average(idx = nil, tail = nil) ⇒ Object
(also: #ema)
Compute the exponential moving average (EMA) of the values of an Array.
-
#simple_moving_average(idx = nil, tail = nil) ⇒ Object
(also: #sma)
Compute the simple moving average (SMA) of the values of an Array.
-
#smoothed_moving_average(idx = nil, tail = nil) ⇒ Object
(also: #smma)
Compute the smoothed moving average of the values of an Array.
-
#sum ⇒ Object
Compute the sum of the values of an Array.
-
#weighted_moving_average(idx = nil, tail = nil) ⇒ Object
(also: #wma)
Compute the weighted moving average of the values of an Array.
Instance Method Details
#exponential_moving_average(idx = nil, tail = nil) ⇒ Object Also known as: ema
Compute the exponential moving average (EMA) of the values of an Array.
Formally, the EMA can be computed as n / d, where
n = p_1 + (1 - alpha)p_2 + (1 - alpha)^2p_3 + ... + (1 - alpha)^zp_(z + 1)
and
d = 1 + (1 - alpha) + (1 - alpha)^2 + (1 - alpha)^3 + ... + (1 - alpha)^z
where
alpha = 2 / (z + 1)
Formula taken from en.wikipedia.org/wiki/Moving_average#Exponential_moving_average
Parameters
-
idx
- Optional, the index of the last datum to consider. -
tail
- Optional, the number of data to consider.
47 48 49 50 51 52 53 54 |
# File 'lib/moving_average/moving_average.rb', line 47 def exponential_moving_average(idx=nil, tail=nil) idx, tail = idx_and_tail_or_defaults(idx, tail) valid_for_ma(idx, tail) alpha = 2.0 / (tail + 1) n = (1..tail).to_a.map{|tidx| (1 - alpha) ** (tidx - 1) * self[idx - tidx + 1]}.sum d = (1..tail).to_a.map{|tidx| (1 - alpha) ** (tidx - 1)}.sum n / d end |
#simple_moving_average(idx = nil, tail = nil) ⇒ Object Also known as: sma
Compute the simple moving average (SMA) of the values of an Array.
Parameters
-
idx
- Optional, the index of the last datum to consider. -
tail
- Optional, the number of data to consider.
63 64 65 66 67 |
# File 'lib/moving_average/moving_average.rb', line 63 def simple_moving_average(idx=nil, tail=nil) idx, tail = idx_and_tail_or_defaults(idx, tail) valid_for_ma(idx, tail) self[idx-tail+1..idx].sum.to_f / tail end |
#smoothed_moving_average(idx = nil, tail = nil) ⇒ Object Also known as: smma
Compute the smoothed moving average of the values of an Array.
Formally, given that the first value for the SMMA is the SMA, subsequent values can be computed as
(sma – smma_i-1 + a[i])
-----------------------
n
where
smma_i-1
is the smoothed moving average of the previous index.
Formula taken from mahifx.com/indicators/smoothed-moving-average-smma
Parameters
-
idx
- Optional, the index of the last datum to consider. -
tail
- Optional, the number of data to consider.
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/moving_average/moving_average.rb', line 92 def smoothed_moving_average(idx=nil, tail=nil) # Set these manually here since we need the leading SMA. if tail.nil? idx = self.size - 1 if idx.nil? tail = idx / 2 tail += 1 if idx.odd? end idx, tail = idx_and_tail_or_defaults(idx, tail) valid_for_ma(idx, tail) valid_for_ma(idx - tail, tail) smma1 = self[idx - 2 * tail + 1..idx - tail].sma (idx - tail + 1..idx).to_a.each do |tidx| prevsum = self[tidx - tail + 1..tidx].sum smma1 = (prevsum - smma1 + self[idx - (idx - tidx)]) / tail end smma1 end |
#sum ⇒ Object
Compute the sum of the values of an Array.
143 144 145 |
# File 'lib/moving_average/moving_average.rb', line 143 def sum inject(0){|s, n| s += n} end |
#weighted_moving_average(idx = nil, tail = nil) ⇒ Object Also known as: wma
Compute the weighted moving average of the values of an Array.
Formally, the WMA can be computed as n / d, where
n = zp_M + (z - 1)p_(M - 1) + (z - 2)p_(M - 3) + ...
and
d = z + (z - 1) + (z - 2) + ...
The denominator is a triangle number of the form
z(z + 1) / 2
Formula taken from en.wikipedia.org/wiki/Moving_average#Weighted_moving_average
Parameters
-
idx
- Optional, the index of the last datum to consider. -
tail
- Optional, the number of data to consider.
132 133 134 135 136 137 138 |
# File 'lib/moving_average/moving_average.rb', line 132 def weighted_moving_average(idx=nil, tail=nil) idx, tail = idx_and_tail_or_defaults(idx, tail) valid_for_ma(idx, tail) n = (0..tail-1).to_a.map{|tidx| (tail - tidx) * self[idx - tidx]}.sum d = (tail * (tail + 1)) / 2.0 n / d end |