Class: TeaLeaves::SingleExponentialSmoothingForecast
- Defined in:
- lib/tealeaves/single_exponential_smoothing_forecast.rb
Instance Attribute Summary
Attributes inherited from Forecast
Instance Method Summary collapse
-
#initialize(time_series, alpha) ⇒ SingleExponentialSmoothingForecast
constructor
A new instance of SingleExponentialSmoothingForecast.
- #predict(n = nil) ⇒ Object
Methods inherited from Forecast
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 |