Class: TeaLeaves::SingleExponentialSmoothingForecast

Inherits:
Forecast
  • Object
show all
Defined in:
lib/tealeaves/single_exponential_smoothing_forecast.rb

Instance Attribute Summary

Attributes inherited from Forecast

#one_step_ahead_forecasts

Instance Method Summary collapse

Methods inherited from Forecast

#errors, #mean_squared_error

Constructor Details

#initialize(time_series, alpha) ⇒ SingleExponentialSmoothingForecast

Returns a new instance of SingleExponentialSmoothingForecast.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/tealeaves/single_exponential_smoothing_forecast.rb', line 5

def initialize(time_series, alpha)
  @time_series = time_series
  @alpha = alpha

  @one_step_ahead_forecasts = [nil]

  ([@time_series.first] + @time_series).inject do |a,b|
    value = (1 - @alpha) * a + @alpha * b
    @one_step_ahead_forecasts << value
    value
  end

  @prediction = @one_step_ahead_forecasts.pop
end

Instance Method Details

#predict(n = nil) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/tealeaves/single_exponential_smoothing_forecast.rb', line 20

def predict(n=nil)
  if n.nil?
    @prediction
  else
    [@prediction] * n
  end
end