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

Inherits:
Object
  • Object
show all
Includes:
GSL::MultiMin, Statsample::TimeSeries
Defined in:
lib/bio-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

Returns a new instance of KalmanFilter.



11
12
13
14
15
16
17
# File 'lib/bio-statsample-timeseries/arima/kalman.rb', line 11

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)

Returns the value of attribute ar.



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

def ar
  @ar
end

#iObject

Returns the value of attribute i.



9
10
11
# File 'lib/bio-statsample-timeseries/arima/kalman.rb', line 9

def i
  @i
end

#maObject (readonly)

Returns the value of attribute ma.



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

def ma
  @ma
end

#pObject

Returns the value of attribute p.



9
10
11
# File 'lib/bio-statsample-timeseries/arima/kalman.rb', line 9

def p
  @p
end

#qObject

Returns the value of attribute q.



9
10
11
# File 'lib/bio-statsample-timeseries/arima/kalman.rb', line 9

def q
  @q
end

#tsObject

Returns the value of attribute ts.



9
10
11
# File 'lib/bio-statsample-timeseries/arima/kalman.rb', line 9

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



100
101
102
# File 'lib/bio-statsample-timeseries/arima/kalman.rb', line 100

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 Parameters: -timeseries: timeseries object, against which the ARMA params has to be estimated -p: order of AR -q: order of MA Usage:

  • ts = (1..100).to_a.to_ts

  • KalmanFilter.ks(ts, 3, 1)

NOTE: Suceptible to syntactical change later. Can be called directly on timeseries. NOTE: Return parameters



35
36
37
38
39
40
41
42
43
44
45
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
# File 'lib/bio-statsample-timeseries/arima/kalman.rb', line 35

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



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

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