Class: ExoBasic::ShortTermAvg
- Inherits:
-
Object
- Object
- ExoBasic::ShortTermAvg
- Includes:
- AvgTraits
- Defined in:
- lib/exobasic/stats/short_term_avg.rb
Constant Summary collapse
- DEFAULT_D =
12
Instance Attribute Summary collapse
-
#d ⇒ Object
Returns the value of attribute d.
-
#history ⇒ Object
readonly
Returns the value of attribute history.
-
#meta ⇒ Object
readonly
Returns the value of attribute meta.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #avg ⇒ Object
-
#initialize(d = DEFAULT_D) ⇒ ShortTermAvg
constructor
A new instance of ShortTermAvg.
- #offer(x) ⇒ Object
Methods included from AvgTraits
#approx=, #count, #deep_copy, #maximum, #minimum, #offer_many
Constructor Details
#initialize(d = DEFAULT_D) ⇒ ShortTermAvg
Returns a new instance of ShortTermAvg.
9 10 11 12 13 |
# File 'lib/exobasic/stats/short_term_avg.rb', line 9 def initialize(d=DEFAULT_D) @d = d <= 0 ? DEFAULT_D : d @history = [] @meta = AvgMeta.new end |
Instance Attribute Details
#d ⇒ Object
Returns the value of attribute d.
5 6 7 |
# File 'lib/exobasic/stats/short_term_avg.rb', line 5 def d @d end |
#history ⇒ Object (readonly)
Returns the value of attribute history.
5 6 7 |
# File 'lib/exobasic/stats/short_term_avg.rb', line 5 def history @history end |
#meta ⇒ Object (readonly)
Returns the value of attribute meta.
5 6 7 |
# File 'lib/exobasic/stats/short_term_avg.rb', line 5 def @meta end |
Instance Method Details
#==(other) ⇒ Object
35 36 37 38 39 |
# File 'lib/exobasic/stats/short_term_avg.rb', line 35 def ==(other) @d == other.d && (@history <=> other.history) == 0 && @meta == other. end |
#avg ⇒ Object
20 21 22 |
# File 'lib/exobasic/stats/short_term_avg.rb', line 20 def avg history.empty? ? 0.0 : history.sum / history.length.to_f end |
#offer(x) ⇒ Object
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/exobasic/stats/short_term_avg.rb', line 24 def offer(x) siz = history.length if siz < @d history.push(x) else history = history[1..-2].push(x) end @meta = @meta.offer(x, self.avg, siz) end |