Skip to content

Commit

Permalink
Altered modules hierarchy convention, added more documentation and me…
Browse files Browse the repository at this point in the history
…thods. Fixes #2
  • Loading branch information
AnkurGel committed Aug 31, 2013
1 parent 4010b60 commit a8e3c0d
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 64 deletions.
115 changes: 62 additions & 53 deletions lib/bio-statsample-timeseries/arima.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
module Statsample
# ISSUE: Should be include on module TimeSeries
module TimeSeries

def self.arima
#not passing (ds,p,i,q) elements for now
#will do that once #arima is ready for all modelling
Statsample::TimeSeries::ARIMA.new
end

class ARIMA < Statsample::Vector
include Statsample::TimeSeries
# SUGGESTION: We could use an API similar to R
Expand Down Expand Up @@ -209,64 +216,66 @@ def self.hannan(ts, p, q, k)

# ISSUE: This should be included as a module on
# Statsample::TimeSeries::Arima::KalmanFilter
class KalmanFilter < Statsample::Vector
include Statsample::TimeSeries

#=T
#The coefficient matrix for the state vector in state equation
# It's dimensions is r+k x r+k
#*Parameters*
#-_r_::integer, r is max(p, q+1), where p and q are orders of AR and MA respectively
#-_k_::integer, number of exogeneous variables in ARMA model
#-_q_::integer, The AR coefficient of ARMA model

#*References*: Statsmodels tsa, Durbin and Koopman Section 4.7
def self.T(r, k, p)
arr = Matrix.zero(r)
params_padded = Statsample::Vector.new(Array.new(r, 0), :scale)

params_padded[0...p] = params[k...(p+k)]
intermediate_matrix = (r-1).times.map { Array.new(r, 0) }
#appending an array filled with padded values in beginning
intermediate_matrix[0,0] = [params_padded]

#now generating column matrix for that:
arr = Matrix.columns(intermediate_matrix)
arr_00 = arr[0,0]

#identify matrix substituition in matrix except row[0] and column[0]
r.times do |i|
arr[r,r] = 1
module Arima
class KalmanFilter < Statsample::Vector
include Statsample::TimeSeries

#=T
#The coefficient matrix for the state vector in state equation
# It's dimensions is r+k x r+k
#*Parameters*
#-_r_::integer, r is max(p, q+1), where p and q are orders of AR and MA respectively
#-_k_::integer, number of exogeneous variables in ARMA model
#-_q_::integer, The AR coefficient of ARMA model

#*References*: Statsmodels tsa, Durbin and Koopman Section 4.7
def self.T(r, k, p)
arr = Matrix.zero(r)
params_padded = Statsample::Vector.new(Array.new(r, 0), :scale)

params_padded[0...p] = params[k...(p+k)]
intermediate_matrix = (r-1).times.map { Array.new(r, 0) }
#appending an array filled with padded values in beginning
intermediate_matrix[0,0] = [params_padded]

#now generating column matrix for that:
arr = Matrix.columns(intermediate_matrix)
arr_00 = arr[0,0]

#identify matrix substituition in matrix except row[0] and column[0]
r.times do |i|
arr[r,r] = 1
end
arr[0,0] = arr_00
arr
end
arr[0,0] = arr_00
arr
end


#=R
#The coefficient matrix for the state vector in the observation matrix.
#It's dimension is r+k x 1
#*Parameters*
#-_r_::integer, r is max(p, q+1) where p and q are order of AR and MA respectively
#-_k_::integer, number of exogeneous variables in ARMA model
#-_q_::integer, The MA order in ARMA model
#-_p_::integer, The AR order in ARMA model
#*References*: Statsmodels tsa, Durbin and Koopman
def self.R(r, k, q, p)
arr = Matrix.column_vector(Array.new(r,0.0))
#=R
#The coefficient matrix for the state vector in the observation matrix.
#It's dimension is r+k x 1
#*Parameters*
#-_r_::integer, r is max(p, q+1) where p and q are order of AR and MA respectively
#-_k_::integer, number of exogeneous variables in ARMA model
#-_q_::integer, The MA order in ARMA model
#-_p_::integer, The AR order in ARMA model
#*References*: Statsmodels tsa, Durbin and Koopman
def self.R(r, k, q, p)
arr = Matrix.column_vector(Array.new(r,0.0))

#pending - in kind of difficult end here;
end
#pending - in kind of difficult end here;
end

#=Z
#The Z selector matrix
#*Parameters*
#-_r_::integer, max(p, q+1)
#Returns: vector
def self.Z(r)
arr = Statsample::Vector.new(Array.new(r, 0.0), :scale)
arr[0] = 1.0
return arr
#=Z
#The Z selector matrix
#*Parameters*
#-_r_::integer, max(p, q+1)
#Returns: vector
def self.Z(r)
arr = Statsample::Vector.new(Array.new(r, 0.0), :scale)
arr[0] = 1.0
return arr
end
end
end
end
Expand Down
3 changes: 2 additions & 1 deletion lib/bio-statsample-timeseries/timeseries.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ def pacf(max_lags = nil, method = :yw)
#Array constituting estimated AR series.
#
def ar(n = 1500, k = 1)
series = Statsample::ARIMA::ARIMA.new
series = Statsample::TimeSeries.arima
#series = Statsample::TimeSeries::ARIMA.new
series.yule_walker(self, n, k)
end

Expand Down
19 changes: 9 additions & 10 deletions test/test_arima_simulators.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ def generate_pacf(simulation)
ts.pacf
end
context("AR(1) simulations") do
include Statsample::ARIMA
include Statsample

setup do
@series = ARIMA.new
@series = TimeSeries.arima
@ar_1_positive = @series.ar_sim(1500, [0.9], 2)
@ar_1_negative = @series.ar_sim(1500, [-0.9], 2)

Expand Down Expand Up @@ -73,10 +73,10 @@ def generate_pacf(simulation)
end

context("AR(p) simulations") do
include Statsample::ARIMA
include Statsample

setup do
@series = ARIMA.new
@series = TimeSeries.arima
@ar_p_positive = @series.ar_sim(1500, [0.3, 0.5], 2)
@ar_p_negative = @series.ar_sim(1500, [-0.3, -0.5], 2)
end
Expand Down Expand Up @@ -120,9 +120,9 @@ def generate_pacf(simulation)


context("MA(1) simulations") do
include Statsample::ARIMA
include Statsample
setup do
@series = ARIMA.new
@series = TimeSeries.arima
@ma_positive = @series.ar_sim(1500, [0.5], 2)
@ma_negative = @series.ar_sim(1500, [-0.5], 2)
end
Expand Down Expand Up @@ -153,9 +153,9 @@ def generate_pacf(simulation)
end

context("MA(q) simulations") do
include Statsample::ARIMA
include Statsample
setup do
@series = ARIMA.new
@series = TimeSeries.arima
@ma_positive = @series.ar_sim(1500, [0.5, 0.3, 0.2], 2)
@ma_negative = @series.ar_sim(1500, [-0.5], 2)
end
Expand All @@ -174,8 +174,7 @@ def generate_pacf(simulation)
end

context("Yule walker estimations") do
include Statsample::ARIMA
include Statsample::TimeSeries
include Statsample

setup do
@timeseries = 100.times.map { rand }.to_ts
Expand Down

0 comments on commit a8e3c0d

Please sign in to comment.