forked from AnkurGel/statsample-timeseries
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest_arima_simulators.rb
203 lines (173 loc) · 6.46 KB
/
test_arima_simulators.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
81
82
83
84
85
86
87
88
89
90
91
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
require(File.expand_path(File.dirname(__FILE__)+'/helper.rb'))
class StatsampleArimaSimulatorsTest < MiniTest::Test
def setup
Daru.lazy_update = true
end
def teardown
Daru.lazy_update = false
end
def generate_acf(simulation)
ts = Daru::Vector.new(simulation)
ts.acf
end
def generate_pacf(simulation)
ts = Daru::Vector.new(simulation)
ts.pacf
end
context("AR(1) simulations") do
include Statsample
def self.generate_acf(simulation)
ts = Daru::Vector.new(simulation)
ts.acf
end
def self.generate_pacf(simulation)
ts = Daru::Vector.new(simulation)
ts.pacf
end
# TODO: Try to speed this up.
@@series = TimeSeries.arima
@@ar_1_positive = @@series.ar_sim(1500, [0.9], 2)
@@ar_1_negative = @@series.ar_sim(1500, [-0.9], 2)
#generating acf
@@positive_acf = generate_acf(@@ar_1_positive)
@@negative_acf = generate_acf(@@ar_1_negative)
#generating pacf
@@positive_pacf = generate_pacf(@@ar_1_positive)
@@negative_pacf = generate_pacf(@@ar_1_negative)
should "have exponential decay of acf on positive side with phi > 0" do
acf = @@positive_acf
assert_equal acf[0], 1.0
assert_operator acf[1], :>=, 0.7
assert_operator acf[acf.size - 1], :<=, 0.2
#visualization:
#https://dl.dropboxusercontent.com/u/102071534/sciruby/AR%281%29_positive_phi_acf.png
#https://dl.dropboxusercontent.com/u/102071534/sciruby/AR%281%29_positive_phi_acf_line.png
end
should "have series with alternating sign on acf starting on negative side with phi < 0" do
acf = @@negative_acf
assert_equal acf[0], 1.0
#testing for alternating series
assert_operator acf[1], :<, 0
assert_operator acf[2], :>, 0
assert_operator acf[3], :<, 0
assert_operator acf[4], :>, 0
#visualization:
#https://dl.dropboxusercontent.com/u/102071534/sciruby/AR%281%29_negative_phi_acf.png
#https://dl.dropboxusercontent.com/u/102071534/sciruby/AR%281%29_negative_phi_acf_line.png
end
should "have positive spike on pacf at lag 1 for phi > 0" do
pacf = @@positive_pacf
assert_operator pacf[1], :>=, 0.7
assert_operator pacf[2], :<=, 0.2
assert_operator pacf[3], :<=, 0.14
#visualization:
#https://dl.dropboxusercontent.com/u/102071534/sciruby/AR%281%29_postive_phi_pacf.png
#https://dl.dropboxusercontent.com/u/102071534/sciruby/AR%281%29_postive_phi_pacf_line.png
end
should "have negative spike on pacf at lag 1 for phi < 0" do
pacf = @@negative_pacf
assert_operator pacf[1], :<=, 0
assert_operator pacf[1], :<=, -0.5
assert_operator pacf[2], :>=, -0.5
#visualizaton:
#https://dl.dropboxusercontent.com/u/102071534/sciruby/AR%281%29_negative_phi_pacf.png
#[hided @pacf[0] = 1 to convey accurate picture]
end
end
context("AR(p) simulations") do
include Statsample
setup do
Daru.lazy_update = true
@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
teardown do
Daru.lazy_update = false
end
should "have damped sine wave starting on positive side on acf" do
@acf = generate_acf(@ar_p_positive)
assert_operator @acf[0], :>=, @acf[1]
assert_operator @acf[1], :>=, 0.0
#caution: sine curve can split on cartesian plane,
#visualization:
#https://dl.dropboxusercontent.com/u/102071534/sciruby/AR(p)_positive_phi_sine_wave.png
end
should "have damped sine wave starting on negative side on acf" do
@acf = generate_acf(@ar_p_negative)
assert_operator @acf[0], :>=, @acf[1]
assert_operator @acf[1], :<=, 0.0
assert_operator @acf[1], :>=, @acf[2]
#caution: sine curve can split on cartesian plane,
#visualization:
#https://dl.dropboxusercontent.com/u/102071534/sciruby/AR%28p%29_negative_phi_acf_sine_wave.png
end
should "have spikes from 1 to p for pacf" do
#here p = 2
@pacf = generate_pacf(@ar_p_positive)
assert_equal @pacf[0], 1.0
assert_operator @pacf[1], :>, @pacf[3]
assert_operator @pacf[1], :>, @pacf[4]
assert_operator @pacf[1], :>, @pacf[5]
assert_operator @pacf[2], :>, @pacf[3]
assert_operator @pacf[2], :>, @pacf[4]
#visualization:
#https://dl.dropboxusercontent.com/u/102071534/sciruby/AR(p)_positive_phi_pacf_spikes.png
end
end
context("MA(1) simulations") do
include Statsample
setup do
@series = TimeSeries.arima
@ma_positive = @series.ar_sim(1500, [0.5], 2)
@ma_negative = @series.ar_sim(1500, [-0.5], 2)
end
should "have one positive spike at lag 1 on acf at positive theta" do
@acf = generate_acf(@ma_positive)
assert_equal @acf[0], 1.0
assert_operator @acf[1], :>=, 0 #test if positive
#test if spike
assert_operator @acf[2], :>=, 0.1
assert_operator @acf[3], :<=, 0.2
assert_operator @acf[4], :<=, 0.2
#visualization:
#https://dl.dropboxusercontent.com/u/102071534/sciruby/MA%281%29_postive_acf.png
end
should "have one negative spike at lag 1 on acf at negative theta" do
@acf = generate_acf(@ma_negative)
assert_operator @acf[1], :<, 0
assert_operator @acf[2], :>=, @acf[1]
assert_operator @acf[3], :>=, @acf[1]
#visualization:
#https://dl.dropboxusercontent.com/u/102071534/sciruby/MA%281%29_negative_acf.png
#positive_vs_negative:
#https://dl.dropboxusercontent.com/u/102071534/sciruby/MA%281%29_acf_positive_vs_negative.png
end
end
context("MA(q) simulations") do
include Statsample
setup do
@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
should "have q positive spikes at lag 1 to q on acf at positive thetas" do
@acf = generate_acf(@ma_positive)
assert_operator @acf[1], :>=, @acf[2]
assert_operator @acf[2], :>=, @acf[3]
assert_operator @acf[3], :>=, @acf[4]
#Visualization: http://jsfiddle.net/YeK2c/
end
should "have damped sine wave on pacf at positive thetas" do
#visualization: http://jsfiddle.net/7keHK/2/
end
end
context("Yule walker estimations") do
include Statsample
setup do
@timeseries = Daru::Vector.new(100.times.map { rand })
@arma_simulation =->(n) { @timeseries.ar(n, k)}
end
# TODO: write tests
end
end