statsample-timeseries 0.0.3 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,103 +0,0 @@
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