statsample-timeseries 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,52 @@
1
+ require(File.expand_path(File.dirname(__FILE__)+'/helper.rb'))
2
+ class StatsampleTimeSeriesPacfTestCase < MiniTest::Unit::TestCase
3
+ context(Statsample::TimeSeries) do
4
+ include Statsample::TimeSeries
5
+ setup do
6
+ @ts = (1..20).map { |x| x * 10 }.to_ts
7
+ #setting up a proc to get a closure for pacf calling with variable lags and methods
8
+ @pacf_proc =->(k, method) { @ts.pacf(k, method) }
9
+ end
10
+
11
+ should "return correct correct pacf size for lags = 5" do
12
+ assert_equal @pacf_proc.call(5, 'yw').size, 6
13
+ assert_equal @pacf_proc.call(5, 'mle').size, 6
14
+ #first element is 1.0
15
+ end
16
+
17
+ should "return correct correct pacf size for lags = 10" do
18
+ assert_equal @pacf_proc.call(10, 'yw').size, 11
19
+ assert_equal @pacf_proc.call(10, 'mle').size, 11
20
+ #first element is 1.0
21
+ end
22
+
23
+ should "have first element as 1.0" do
24
+ assert_equal @pacf_proc.call(10, 'yw')[0], 1.0
25
+ assert_equal @pacf_proc.call(10, 'mle')[0], 1.0
26
+ end
27
+
28
+ should "give correct pacf results for unbiased yule-walker" do
29
+ result_10 = [1.0, 0.8947368421052632, -0.10582010582010604, -0.11350188273265083, -0.12357534824820737, -0.13686534216335522, -0.15470588235294147, -0.17938011883732036, -0.2151192288178601, -0.2707082833133261, -0.3678160919540221]
30
+ result_5 = [1.0, 0.8947368421052632, -0.10582010582010604, -0.11350188273265083, -0.12357534824820737, -0.13686534216335522]
31
+ assert_equal @pacf_proc.call(10, 'yw'), result_10
32
+ assert_equal @pacf_proc.call(5, 'yw'), result_5
33
+
34
+ #Checking for lag = (1..10)
35
+ 1.upto(10) do |i|
36
+ assert_equal @pacf_proc.call(i, 'yw'), result_10[0..i]
37
+ end
38
+ end
39
+
40
+ should "give correct pacf results for mle yule-walker" do
41
+ result_10 = [1.0, 0.85, -0.07566212829370711, -0.07635069706072706, -0.07698628638512295, -0.07747034005560738, -0.0776780981161499, -0.07744984679625189, -0.0765803323191094, -0.07480650005932366, -0.07179435184923755]
42
+ result_5 = [1.0, 0.85, -0.07566212829370711, -0.07635069706072706, -0.07698628638512295, -0.07747034005560738]
43
+ assert_equal @pacf_proc.call(10, 'mle'), result_10
44
+ assert_equal @pacf_proc.call(5, 'mle'), result_5
45
+
46
+ #Checking for lag = (1..10)
47
+ 1.upto(10) do |i|
48
+ assert_equal @pacf_proc.call(i, 'mle'), result_10[0..i]
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,103 @@
1
+ require(File.expand_path(File.dirname(__FILE__)+'/helper.rb'))
2
+
3
+ class StatsampleTestTimeSeries < MiniTest::Unit::TestCase
4
+ include Statsample::Shorthand
5
+
6
+ # All calculations are compared to the output of the equivalent function in R
7
+
8
+ def setup
9
+ # daily closes of iShares XIU on the TSX
10
+ @xiu = Statsample::TimeSeries::Series.new [17.28, 17.45, 17.84, 17.74, 17.82, 17.85, 17.36, 17.3, 17.56, 17.49, 17.46, 17.4, 17.03, 17.01,
11
+ 16.86, 16.86, 16.56, 16.36, 16.66, 16.77], :scale
12
+ end
13
+
14
+ def test_acf
15
+ acf = @xiu.acf
16
+
17
+ assert_equal 14, acf.length
18
+
19
+ # test the first few autocorrelations
20
+ assert_in_delta 1.0, acf[0], 0.0001
21
+ assert_in_delta 0.852, acf[1], 0.001
22
+ assert_in_delta 0.669, acf[2], 0.001
23
+ assert_in_delta 0.486, acf[3], 0.001
24
+ end
25
+
26
+ def test_lag
27
+ #test of default lag
28
+ lag1 = @xiu.lag
29
+
30
+ assert_in_delta 16.66, lag1[lag1.size - 1], 0.001
31
+ assert_in_delta 16.36, lag1[lag1.size - 2], 0.001
32
+
33
+ #test with different lagging unit
34
+ lag2 = @xiu.lag(2)
35
+
36
+ assert_in_delta 16.36, lag2[lag2.size - 1], 0.001
37
+ assert_in_delta 16.56, lag2[lag2.size - 2], 0.001
38
+ end
39
+
40
+ def test_delta
41
+ diff = @xiu.diff
42
+
43
+ assert_in_delta 0.11, diff[@xiu.size - 1], 0.001
44
+ assert_in_delta 0.30, diff[@xiu.size - 2], 0.001
45
+ assert_in_delta -0.20, diff[@xiu.size - 3], 0.001
46
+ end
47
+
48
+ def test_ma
49
+ # test default
50
+ ma10 = @xiu.ma
51
+
52
+ assert_in_delta ma10[-1], 16.897, 0.001
53
+ assert_in_delta ma10[-5], 17.233, 0.001
54
+ assert_in_delta ma10[-10], 17.587, 0.001
55
+
56
+ # test with a different lookback period
57
+ ma5 = @xiu.ma 5
58
+
59
+ assert_in_delta ma5[-1], 16.642, 0.001
60
+ assert_in_delta ma5[-10], 17.434, 0.001
61
+ assert_in_delta ma5[-15], 17.74, 0.001
62
+ end
63
+
64
+ def test_ema
65
+ # test default
66
+ ema10 = @xiu.ema
67
+
68
+ assert_in_delta ema10[-1], 16.87187, 0.00001
69
+ assert_in_delta ema10[-5], 17.19187, 0.00001
70
+ assert_in_delta ema10[-10], 17.54918, 0.00001
71
+
72
+ # test with a different lookback period
73
+ ema5 = @xiu.ema 5
74
+
75
+ assert_in_delta ema5[-1], 16.71299, 0.0001
76
+ assert_in_delta ema5[-10], 17.49079, 0.0001
77
+ assert_in_delta ema5[-15], 17.70067, 0.0001
78
+
79
+ # test with a different smoother
80
+ ema_w = @xiu.ema 10, true
81
+
82
+ assert_in_delta ema_w[-1], 17.08044, 0.00001
83
+ assert_in_delta ema_w[-5], 17.33219, 0.00001
84
+ assert_in_delta ema_w[-10], 17.55810, 0.00001
85
+ end
86
+
87
+ def test_macd
88
+ # MACD uses a lot more data than the other ones, so we need a bigger vector
89
+ data = File.readlines(File.dirname(__FILE__) + "/fixtures/stock_data.csv").map(&:to_f).to_time_series
90
+
91
+ macd, signal = data.macd
92
+
93
+ # check the MACD
94
+ assert_in_delta 3.12e-4, macd[-1], 1e-6
95
+ assert_in_delta -1.07e-2, macd[-10], 1e-4
96
+ assert_in_delta -5.65e-3, macd[-20], 1e-5
97
+
98
+ # check the signal
99
+ assert_in_delta -0.00628, signal[-1], 1e-5
100
+ assert_in_delta -0.00971, signal[-10], 1e-5
101
+ assert_in_delta -0.00338, signal[-20], 1e-5
102
+ end
103
+ end
@@ -0,0 +1,71 @@
1
+ require(File.expand_path(File.dirname(__FILE__)+'/helper.rb'))
2
+
3
+ class StatsampleWaldTest < MiniTest::Unit::TestCase
4
+ # Wald test is useful to test a series of n acf with Chi-square
5
+ # degree of freedom. It is extremely useful to test fit the fit of
6
+ # an ARIMA model to test the residuals.
7
+
8
+ include Statsample::TimeSeries
9
+ include Statsample::Shorthand
10
+ include Distribution
11
+
12
+ def setup
13
+ #create time series to evaluate later
14
+ @wald = 100.times.map { rand(100) }.to_ts
15
+ end
16
+
17
+ def sum_of_squares_of_acf_series(lags)
18
+ #perform sum of squares for a series of acf with specified lags
19
+ acf = @wald.acf(lags)
20
+ return acf.map { |x| x ** 2 }.inject(:+)
21
+ end
22
+
23
+ def chisquare_cdf(sum_of_squares, lags)
24
+ 1 - ChiSquare.cdf(sum_of_squares, lags)
25
+ end
26
+
27
+
28
+ def test_wald_with_5_lags
29
+ #number of lags for acf = 5
30
+ lags = 5
31
+ sum_of_squares = sum_of_squares_of_acf_series(lags)
32
+ assert_in_delta chisquare_cdf(sum_of_squares, lags), 1, 0.05
33
+ assert_equal @wald.acf(lags).size, lags + 1
34
+ end
35
+
36
+
37
+ def test_wald_with_10_lags
38
+ #number of lags for acf = 10
39
+ lags = 10
40
+ sum_of_squares = sum_of_squares_of_acf_series(lags)
41
+ assert_in_delta chisquare_cdf(sum_of_squares, lags), 1, 0.05
42
+ assert_equal @wald.acf(lags).size, lags + 1
43
+ end
44
+
45
+
46
+ def test_wald_with_15_lags
47
+ #number of lags for acf = 15
48
+ lags = 15
49
+ sum_of_squares = sum_of_squares_of_acf_series(lags)
50
+ assert_in_delta chisquare_cdf(sum_of_squares, lags), 1, 0.05
51
+ assert_equal @wald.acf(lags).size, lags + 1
52
+ end
53
+
54
+
55
+ def test_wald_with_20_lags
56
+ #number of lags for acf = 20
57
+ lags = 20
58
+ sum_of_squares = sum_of_squares_of_acf_series(lags)
59
+ assert_in_delta chisquare_cdf(sum_of_squares, lags), 1, 0.05
60
+ assert_equal @wald.acf(lags).size, lags + 1
61
+ end
62
+
63
+
64
+ def test_wald_with_25_lags
65
+ #number of lags for acf = 25
66
+ lags = 25
67
+ sum_of_squares = sum_of_squares_of_acf_series(lags)
68
+ assert_in_delta chisquare_cdf(sum_of_squares, lags), 1, 0.05
69
+ assert_equal @wald.acf(lags).size, lags + 1
70
+ end
71
+ end
metadata ADDED
@@ -0,0 +1,273 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: statsample-timeseries
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ankur Goel
9
+ - Claudio Bustos
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2013-09-25 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: statsample
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - '='
21
+ - !ruby/object:Gem::Version
22
+ version: 1.2.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - '='
29
+ - !ruby/object:Gem::Version
30
+ version: 1.2.0
31
+ - !ruby/object:Gem::Dependency
32
+ name: activesupport
33
+ requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - '='
37
+ - !ruby/object:Gem::Version
38
+ version: 3.2.10
39
+ type: :runtime
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - '='
45
+ - !ruby/object:Gem::Version
46
+ version: 3.2.10
47
+ - !ruby/object:Gem::Dependency
48
+ name: shoulda
49
+ requirement: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ - !ruby/object:Gem::Dependency
64
+ name: rdoc
65
+ requirement: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ~>
69
+ - !ruby/object:Gem::Version
70
+ version: '3.12'
71
+ type: :development
72
+ prerelease: false
73
+ version_requirements: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ~>
77
+ - !ruby/object:Gem::Version
78
+ version: '3.12'
79
+ - !ruby/object:Gem::Dependency
80
+ name: minitest
81
+ requirement: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ~>
85
+ - !ruby/object:Gem::Version
86
+ version: 4.7.5
87
+ type: :development
88
+ prerelease: false
89
+ version_requirements: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ~>
93
+ - !ruby/object:Gem::Version
94
+ version: 4.7.5
95
+ - !ruby/object:Gem::Dependency
96
+ name: cucumber
97
+ requirement: !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ! '>='
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ type: :development
104
+ prerelease: false
105
+ version_requirements: !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - ! '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: bundler
113
+ requirement: !ruby/object:Gem::Requirement
114
+ none: false
115
+ requirements:
116
+ - - ~>
117
+ - !ruby/object:Gem::Version
118
+ version: 1.3.5
119
+ type: :development
120
+ prerelease: false
121
+ version_requirements: !ruby/object:Gem::Requirement
122
+ none: false
123
+ requirements:
124
+ - - ~>
125
+ - !ruby/object:Gem::Version
126
+ version: 1.3.5
127
+ - !ruby/object:Gem::Dependency
128
+ name: jeweler
129
+ requirement: !ruby/object:Gem::Requirement
130
+ none: false
131
+ requirements:
132
+ - - ~>
133
+ - !ruby/object:Gem::Version
134
+ version: 1.8.4
135
+ type: :development
136
+ prerelease: false
137
+ version_requirements: !ruby/object:Gem::Requirement
138
+ none: false
139
+ requirements:
140
+ - - ~>
141
+ - !ruby/object:Gem::Version
142
+ version: 1.8.4
143
+ - !ruby/object:Gem::Dependency
144
+ name: bio
145
+ requirement: !ruby/object:Gem::Requirement
146
+ none: false
147
+ requirements:
148
+ - - ! '>='
149
+ - !ruby/object:Gem::Version
150
+ version: 1.4.2
151
+ type: :development
152
+ prerelease: false
153
+ version_requirements: !ruby/object:Gem::Requirement
154
+ none: false
155
+ requirements:
156
+ - - ! '>='
157
+ - !ruby/object:Gem::Version
158
+ version: 1.4.2
159
+ - !ruby/object:Gem::Dependency
160
+ name: rdoc
161
+ requirement: !ruby/object:Gem::Requirement
162
+ none: false
163
+ requirements:
164
+ - - ~>
165
+ - !ruby/object:Gem::Version
166
+ version: '3.12'
167
+ type: :development
168
+ prerelease: false
169
+ version_requirements: !ruby/object:Gem::Requirement
170
+ none: false
171
+ requirements:
172
+ - - ~>
173
+ - !ruby/object:Gem::Version
174
+ version: '3.12'
175
+ - !ruby/object:Gem::Dependency
176
+ name: mocha
177
+ requirement: !ruby/object:Gem::Requirement
178
+ none: false
179
+ requirements:
180
+ - - ~>
181
+ - !ruby/object:Gem::Version
182
+ version: 0.14.0
183
+ type: :development
184
+ prerelease: false
185
+ version_requirements: !ruby/object:Gem::Requirement
186
+ none: false
187
+ requirements:
188
+ - - ~>
189
+ - !ruby/object:Gem::Version
190
+ version: 0.14.0
191
+ - !ruby/object:Gem::Dependency
192
+ name: gsl
193
+ requirement: !ruby/object:Gem::Requirement
194
+ none: false
195
+ requirements:
196
+ - - ! '>='
197
+ - !ruby/object:Gem::Version
198
+ version: '0'
199
+ type: :development
200
+ prerelease: false
201
+ version_requirements: !ruby/object:Gem::Requirement
202
+ none: false
203
+ requirements:
204
+ - - ! '>='
205
+ - !ruby/object:Gem::Version
206
+ version: '0'
207
+ description: Statsample-timeseries is an extension to Statsample. It incorporates
208
+ helpful timeseries functions and modules like ARMA, ARIMA, acf, pacf, lags etc.
209
+ email: ankurgel@gmail.com
210
+ executables:
211
+ - bio-statsample-timeseries
212
+ extensions: []
213
+ extra_rdoc_files:
214
+ - LICENSE.txt
215
+ - README.rdoc
216
+ files:
217
+ - .document
218
+ - .travis.yml
219
+ - Gemfile
220
+ - LICENSE.txt
221
+ - README.rdoc
222
+ - Rakefile
223
+ - VERSION
224
+ - bin/bio-statsample-timeseries
225
+ - features/acf.feature
226
+ - features/pacf.feature
227
+ - features/step_definitions/bio-statsample-timeseries_steps.rb
228
+ - features/step_definitions/step_definitions.rb
229
+ - features/step_definitions/step_definitions_acf.rb
230
+ - features/support/env.rb
231
+ - lib/statsample-timeseries.rb
232
+ - lib/statsample-timeseries/arima.rb
233
+ - lib/statsample-timeseries/arima/kalman.rb
234
+ - lib/statsample-timeseries/arima/likelihood.rb
235
+ - lib/statsample-timeseries/timeseries.rb
236
+ - lib/statsample-timeseries/timeseries/pacf.rb
237
+ - lib/statsample-timeseries/utility.rb
238
+ - test/fixtures/stock_data.csv
239
+ - test/helper.rb
240
+ - test/test_arima_ks.rb
241
+ - test/test_arima_simulators.rb
242
+ - test/test_matrix.rb
243
+ - test/test_pacf.rb
244
+ - test/test_tseries.rb
245
+ - test/test_wald.rb
246
+ homepage: http://github.com/AnkurGel/statsample-timeseries
247
+ licenses: []
248
+ post_install_message:
249
+ rdoc_options: []
250
+ require_paths:
251
+ - lib
252
+ required_ruby_version: !ruby/object:Gem::Requirement
253
+ none: false
254
+ requirements:
255
+ - - ! '>='
256
+ - !ruby/object:Gem::Version
257
+ version: '0'
258
+ segments:
259
+ - 0
260
+ hash: -237853649
261
+ required_rubygems_version: !ruby/object:Gem::Requirement
262
+ none: false
263
+ requirements:
264
+ - - ! '>='
265
+ - !ruby/object:Gem::Version
266
+ version: '0'
267
+ requirements: []
268
+ rubyforge_project:
269
+ rubygems_version: 1.8.25
270
+ signing_key:
271
+ specification_version: 3
272
+ summary: TimeSeries modules for Statsample
273
+ test_files: []