Class: TimeWise::MovingAverage

Inherits:
Object
  • Object
show all
Defined in:
lib/time_wise/moving_average.rb

Overview

Moving average methods for time series analysis

Instance Method Summary collapse

Constructor Details

#initialize(time_series) ⇒ MovingAverage



6
7
8
9
# File 'lib/time_wise/moving_average.rb', line 6

def initialize(time_series)
  @ts = time_series
  @data = @ts.data
end

Instance Method Details

#double_exponential(alpha = 0.2, beta = 0.1) ⇒ TimeWise::Base

Double Exponential Moving Average (Holt’s Linear Method)



44
45
46
47
48
49
# File 'lib/time_wise/moving_average.rb', line 44

def double_exponential(alpha = 0.2, beta = 0.1)
  validate_alpha(alpha)
  validate_alpha(beta, "beta")
  result = calculate_double_exponential(alpha, beta)
  TimeWise.create(result.to_a, @ts.dates)
end

#exponential(alpha = 0.2) ⇒ TimeWise::Base

Exponential Moving Average



23
24
25
26
27
# File 'lib/time_wise/moving_average.rb', line 23

def exponential(alpha = 0.2)
  validate_alpha(alpha)
  result = calculate_exponential_moving_average(alpha)
  TimeWise.create(result.to_a, @ts.dates)
end

#simple(window) ⇒ TimeWise::Base

Simple Moving Average



14
15
16
17
18
# File 'lib/time_wise/moving_average.rb', line 14

def simple(window)
  validate_window(window)
  result = calculate_simple_moving_average(window)
  TimeWise.create(result.to_a, @ts.dates)
end

#triple_exponential(options = {}) ⇒ TimeWise::Base

Triple Exponential Moving Average (Holt-Winters Method) with seasonality

Options Hash (options):

  • :alpha (Float)

    The level smoothing factor (between 0 and 1)

  • :beta (Float)

    The trend smoothing factor (between 0 and 1)

  • :gamma (Float)

    The seasonal smoothing factor (between 0 and 1)

  • :season_length (Integer)

    The length of the seasonal pattern



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/time_wise/moving_average.rb', line 58

def triple_exponential(options = {})
  options = {
    alpha: 0.2,
    beta: 0.1,
    gamma: 0.1,
    season_length: 4
  }.merge(options)

  validate_triple_exponential_params(options)
  result = calculate_triple_exponential(options)
  TimeWise.create(result.to_a, @ts.dates)
end

#weighted(window, weights = nil) ⇒ TimeWise::Base

Weighted Moving Average



33
34
35
36
37
38
# File 'lib/time_wise/moving_average.rb', line 33

def weighted(window, weights = nil)
  validate_window(window)
  weights = prepare_weights(window, weights)
  result = calculate_weighted_moving_average(window, weights)
  TimeWise.create(result.to_a, @ts.dates)
end