Class: Statsample::TimeSeries::Arima::KalmanFilter

Inherits:
Object
  • Object
show all
Includes:
GSL::MultiMin, Statsample::TimeSeries
Defined in:
lib/statsample-timeseries/arima/kalman.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Statsample::TimeSeries

arima

Constructor Details

#initialize(ts = [].to_ts, p = 0, i = 0, q = 0) ⇒ KalmanFilter

Creates a new KalmanFilter object and computes the likelihood


25
26
27
28
29
30
31
# File 'lib/statsample-timeseries/arima/kalman.rb', line 25

def initialize(ts=[].to_ts, p=0, i=0, q=0)
  @ts = ts
  @p = p
  @i = i
  @q = q
  ks #call the filter
end

Instance Attribute Details

#arObject (readonly)

Autoregressive coefficients


20
21
22
# File 'lib/statsample-timeseries/arima/kalman.rb', line 20

def ar
  @ar
end

#iObject

Integerated part order


15
16
17
# File 'lib/statsample-timeseries/arima/kalman.rb', line 15

def i
  @i
end

#maObject (readonly)

Moving average coefficients


22
23
24
# File 'lib/statsample-timeseries/arima/kalman.rb', line 22

def ma
  @ma
end

#pObject

Autoregressive order


13
14
15
# File 'lib/statsample-timeseries/arima/kalman.rb', line 13

def p
  @p
end

#qObject

Moving average order


17
18
19
# File 'lib/statsample-timeseries/arima/kalman.rb', line 17

def q
  @q
end

#tsObject

timeseries object


11
12
13
# File 'lib/statsample-timeseries/arima/kalman.rb', line 11

def ts
  @ts
end

Class Method Details

.log_likelihood(params, timeseries, p, q) ⇒ Object

Log Likelihood

Computes Log likelihood on given parameters, ARMA order and timeseries

params

  • params: array of floats, contains phi/theta parameters

  • timeseries: timeseries object

  • p: integer, AR(p) order

  • q: integer, MA(q) order

Returns

LogLikelihood object

Usage

s = (1..100).map { rand }.to_ts p, q = 1, 0 ll = KalmanFilter.log_likelihood(, s, p, q) ll.log_likelihood #=> -22.66 ll.sigma #=> 0.232


111
112
113
# File 'lib/statsample-timeseries/arima/kalman.rb', line 111

def self.log_likelihood(params, timeseries, p, q)
  Arima::KF::LogLikelihood.new(params, timeseries, p, q)
end

Instance Method Details

#ksObject

Kalman Filter

Function which minimizes KalmanFilter.ll iteratively for initial parameters

Usage

@s = [-1.16025577,0.64758021,0.77158601,0.14989543,2.31358162,3.49213868,1.14826956,0.58169457,-0.30813868,-0.34741084,-1.41175595,0.06040081, -0.78230232,0.86734837,0.95015787,-0.49781397,0.53247330,1.56495187,0.30936619,0.09750217,1.09698829,-0.81315490,-0.79425607,-0.64568547,-1.06460320,1.24647894,0.66695937,1.50284551,1.17631218,1.64082872,1.61462736,0.06443761,-0.17583741,0.83918339,0.46610988,-0.54915270,-0.56417108,-1.27696654,0.89460084,1.49970338,0.24520493,0.26249138,-1.33744834,-0.57725961,1.55819543,1.62143157,0.44421891,-0.74000084 ,0.57866347,3.51189333,2.39135077,1.73046244,1.81783890,0.21454040,0.43520890,-1.42443856,-2.72124685,-2.51313877,-1.20243091,-1.44268002 ,-0.16777305,0.05780661,2.03533992,0.39187242,0.54987983,0.57865693,-0.96592469,-0.93278473,-0.75962671,-0.63216906,1.06776183, 0.17476059 ,0.06635860,0.94906227,2.44498583,-1.04990407,-0.88440073,-1.99838258,-1.12955558,-0.62654882,-1.36589161,-2.67456821,-0.97187696, -0.84431782 ,-0.10051809,0.54239549,1.34622861,1.25598105,0.19707759,3.29286114,3.52423499,1.69146333,-0.10150024,0.45222903,-0.01730516, -0.49828727, -1.18484684,-1.09531773,-1.17190808,0.30207662].to_ts
@kf=Statsample::TimeSeries::ARIMA.ks(@s,1,0,0)
#=> ks is implictly called in above operation
@kf.ar
#=> AR coefficients

46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/statsample-timeseries/arima/kalman.rb', line 46

def ks
  initial = Array.new((@p+@q), 0.0)

  my_f = Proc.new{ |x, params|
    #In rb-gsl, params remain idle, x is varied upon
    #In R code, initial parameters varied in each iteration
    #my_func.set_params([(1..100).to_a.to_ts, p_value, q_value])
    timeseries = params[0]
    p,q = params[1], params[2]
    params = x
    #puts x
    -Arima::KF::LogLikelihood.new(x.to_a, timeseries, p, q).ll
    #KalmanFilter.ll(x.to_a, timeseries, p, q)
  }
  np = @p + @q
  my_func = Function.alloc(my_f, np)
  my_func.set_params([@ts, @p, @q])
  x = GSL::Vector.alloc(initial)
  ss = GSL::Vector.alloc(np)
  ss.set_all(0.1)

  minimizer = FMinimizer.alloc("nmsimplex", np)
  minimizer.set(my_func, x, ss)
  status = GSL::CONTINUE
  iter = 0
  while status == GSL::CONTINUE && iter < 100
    iter += 1
    begin
      status = minimizer.iterate()
      status = minimizer.test_size(1e-2)
      x = minimizer.x
    rescue
      break
    end
  #  printf("%5d ", iter)
  #  for i in 0...np do
  #    puts "#{x[i]}.to_f"
  #    #printf("%10.3e ", x[i].to_f)
  #  end
  #  printf("f() = %7.3f size = %.3f\n", minimizer.fval, minimizer.size)
  end
  #
  @ar = (p > 0) ? x.to_a[0...p] : []
  @ma = (q > 0) ? x.to_a[p...(p+q)] : []
  x.to_a
end

#to_sObject


33
34
35
36
# File 'lib/statsample-timeseries/arima/kalman.rb', line 33

def to_s
  sprintf("ARIMA model(p = %d, i = %d, q = %d) on series(%d elements) - [%s]",
          @p, @i, @q, @ts.size, @ts.to_a.join(','))
end