Class: IEma

Inherits:
Object
  • Object
show all
Defined in:
lib/rbma/i_ema.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(g:) ⇒ IEma

Returns a new instance of IEma.



19
20
21
# File 'lib/rbma/i_ema.rb', line 19

def initialize(g:)
  @g = g.to_f
end

Instance Attribute Details

#gObject (readonly)

Returns the value of attribute g.



16
17
18
# File 'lib/rbma/i_ema.rb', line 16

def g
  @g
end

#prevObject (readonly)

where G is the smoothing coeficient, a the rate decay Y_t, an event value at time t Y_n, an event value at the time of event n S_t, the EMA value at time t S_n, the EMA value at the time of the event n t(n), the time of the event n



15
16
17
# File 'lib/rbma/i_ema.rb', line 15

def prev
  @prev
end

#tprevObject (readonly)

Returns the value of attribute tprev.



17
18
19
# File 'lib/rbma/i_ema.rb', line 17

def tprev
  @tprev
end

Instance Method Details

#compute(current:, t:, prev: nil, tprev: nil) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rbma/i_ema.rb', line 23

def compute(current:, t:, prev: nil, tprev: nil)
  if tprev && prev
    alpha = a(t - tprev)
  elsif @tprev && @prev
    alpha = a(t - @tprev)
    prev = @prev
  else
    alpha = 0
    prev = 0
  end
  @tprev = t
  @prev = (1-alpha)*current + alpha*prev
end

#value(t:, tprev: nil, prev: nil) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rbma/i_ema.rb', line 37

def value(t:, tprev: nil, prev: nil)
    # 3) S_t = S_t(n)*exp((t-t(n))/G)
  if tprev && prev
  elsif @tprev && @prev
    tprev = @tprev
    prev = @prev
  else
    raise ArgumentError.new("There is no previous data and you did not provide any")
  end
  prev*a(t - tprev)
end