Class: Statsample::TimeSeries::ARIMA

Inherits:
Vector
  • Object
show all
Includes:
Statsample::TimeSeries
Defined in:
lib/bio-statsample-timeseries/arima.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Statsample::TimeSeries

arima

Methods inherited from Vector

#squares_of_sum

Class Method Details

.hannan(ts, p, q, k) ⇒ Object

Hannan-Rissanen for ARMA fit



219
220
221
222
223
# File 'lib/bio-statsample-timeseries/arima.rb', line 219

def self.hannan(ts, p, q, k)
  start_params = create_vector(Array.new(p+q+k, 0))
  ts_dup = ts.dup

end

.ks(ts, p, i, q) ⇒ Object

Kalman filter on ARIMA model Params: -ts::timeseries object -p::AR order -i::Integerated part -q::MA order

Usage: ts = (1..100).map { rand }.to_ts k_obj = ARIMA.ks(ts, 2, 1, 1) k_obj.ar #=> AR’s phi coefficients k_obj.ma #=> MA’s theta coefficients

Returns: Kalman filter object



33
34
35
36
37
38
39
40
# File 'lib/bio-statsample-timeseries/arima.rb', line 33

def self.ks(ts, p, i, q)
  #prototype
  if i > 0
    ts = ts.diff(i).reject { |x| x.nil? }.to_ts
  end
  filter = Arima::KalmanFilter.new(ts, p, i, q)
  filter
end

Instance Method Details

#ar(p) ⇒ Object



42
43
44
45
46
47
# File 'lib/bio-statsample-timeseries/arima.rb', line 42

def ar(p)
  #AutoRegressive part of model
  #http://en.wikipedia.org/wiki/Autoregressive_model#Definition
  #For finding parameters(to fit), we will use either Yule-walker
  #or Burg's algorithm(more efficient)
end

#ar_sim(n, phi, sigma) ⇒ Object

Autoregressive Simulator

Simulates an autoregressive AR(p) model with specified number of observations(n), with phi number of values for order p and sigma.

Analysis: ankurgoel.com/blog/2013/07/20/ar-ma-arma-acf-pacf-visualizations/

Parameters: -n::integer, number of observations -phi::array of phi values, e.g: [0.35, 0.213] for p = 2 -sigma::float, sigma value for error generalization

Usage:

ar = ARIMA.new
ar.ar_sim(1500, [0.3, 0.9], 0.12)
  # => AR(2) autoregressive series of 1500 values

Returns: Array of generated autoregressive series against attributes



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/bio-statsample-timeseries/arima.rb', line 92

def ar_sim(n, phi, sigma)
  #using random number generator for inclusion of white noise
  err_nor = Distribution::Normal.rng(0, sigma)
  #creating buffer with 10 random values
  buffer = Array.new(10, err_nor.call())

  x = buffer + Array.new(n, 0)

  #For now "phi" are the known model parameters
  #later we will obtain it by Yule-walker/Burg

  #instead of starting from 0, start from 11
  #and later take away buffer values for failsafe
  11.upto(n+11) do |i|
    if i <= phi.size
      #dependent on previous accumulation of x
      backshifts = create_vector(x[0...i].reverse)
    else
      #dependent on number of phi size/order
      backshifts = create_vector(x[(i - phi.size)...i].reverse)
    end
    parameters = create_vector(phi[0...backshifts.size])

    summation = (backshifts * parameters).inject(:+)
    x[i] = summation + err_nor.call()
  end
  x - buffer
end

#arma_sim(n, p, q, sigma) ⇒ Object

ARMA(Autoregressive and Moving Average) Simulator ARMA is represented by: This simulates the ARMA model against p, q and sigma. If p = 0, then model is pure MA(q), If q = 0, then model is pure AR(p), otherwise, model is ARMA(p, q) represented by above.

Detailed analysis: ankurgoel.com/blog/2013/07/20/ar-ma-arma-acf-pacf-visualizations/

Parameters: -n::integer, number of observations -p::array, contains p number of phi values for AR(p) process -q::array, contains q number of theta values for MA(q) process -sigma::float, sigma value for whitenoise error generation

Usage:

ar = ARIMA.new
ar.arma_sim(1500, [0.3, 0.272], [0.8, 0.317], 0.92)

Returns: array of generated ARMA model values



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/bio-statsample-timeseries/arima.rb', line 183

def arma_sim(n, p, q, sigma)
  #represented by :
  #http://upload.wikimedia.org/math/2/e/d/2ed0485927b4370ae288f1bc1fe2fc8b.png


  whitenoise_gen = Distribution::Normal.rng(0, sigma)
  noise_arr = (n+11).times.map { whitenoise_gen.call() }

  buffer = Array.new(10, whitenoise_gen.call())
  x = buffer + Array.new(n, 0)

  11.upto(n+11) do |i|
    if i <= p.size
      backshifts = create_vector(x[0...i].reverse)
    else
      backshifts = create_vector(x[(i - p.size)...i].reverse)
    end
    parameters = create_vector(p[0...backshifts.size])

    ar_summation = (backshifts * parameters).inject(:+)

    if i <= q.size
      noises = create_vector(noise_arr[0..i].reverse)
    else
      noises = create_vector(noise_arr[(i-q.size)..i].reverse)
    end
    weights = create_vector([1] + q[0...noises.size - 1])

    ma_summation = (weights * noises).inject(:+)

    x[i] = ar_summation + ma_summation
  end
  x - buffer
end

#create_vector(arr) ⇒ Object

Converts a linear array into a vector



50
51
52
# File 'lib/bio-statsample-timeseries/arima.rb', line 50

def create_vector(arr)
  Statsample::Vector.new(arr, :scale)
end

#levinson_durbin(ts, n, k) ⇒ Object



63
64
65
66
67
68
69
70
71
72
# File 'lib/bio-statsample-timeseries/arima.rb', line 63

def levinson_durbin(ts, n, k)
  #parameters;
  #ts: timseries against which to generate phi coefficients
  #n: number of observations for simulation
  #k: order of AR
  intermediate = Pacf::Pacf.levinson_durbin(ts, k)
  phi, sigma = intermediate[1], intermediate[0]
  return phi, sigma
  #return ar_sim(n, phi, sigma)
end

#ma_sim(n, theta, sigma) ⇒ Object

Moving Average Simulator

Simulates a moving average model with specified number of observations(n), with theta values for order k and sigma

Parameters: -n::integer, number of observations -theta::array of floats, e.g: [0.23, 0.732], must be < 1 -sigma::float, sigma value for whitenoise error

Usage:

ar = ARIMA.new
ar.ma_sim(1500, [0.23, 0.732], 0.27)

Returns: Array of generated MA(q) model



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/bio-statsample-timeseries/arima.rb', line 136

def ma_sim(n, theta, sigma)
  #n is number of observations (eg: 1000)
  #theta are the model parameters containting q values
  #q is the order of MA
  mean = theta.to_ts.mean()
  whitenoise_gen = Distribution::Normal.rng(0, sigma)
  x = Array.new(n, 0)
  q = theta.size
  noise_arr = (n+1).times.map { whitenoise_gen.call() }

  1.upto(n) do |i|
    #take care that noise vector doesn't try to index -ve value:
    if i <= q
      noises = create_vector(noise_arr[0..i].reverse)
    else
      noises = create_vector(noise_arr[(i-q)..i].reverse)
    end
    weights = create_vector([1] + theta[0...noises.size - 1])

    summation = (weights * noises).inject(:+)
    x[i] = mean + summation
  end
  x
end

#yule_walker(ts, n, k) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/bio-statsample-timeseries/arima.rb', line 55

def yule_walker(ts, n, k)
  #parameters: timeseries, no of observations, order
  #returns: simulated autoregression with phi parameters and sigma
  phi, sigma = Pacf::Pacf.yule_walker(ts, k)
  return phi, sigma
  #return ar_sim(n, phi, sigma)
end