1
+ function test_score_mean (D:: Type{<:Distribution} ; n:: Int = 10 ^ 7 , seed:: Int = 10 ,
2
+ atol:: Float64 = 1e-3 , rtol:: Float64 = 1e-3 )
3
+ Random. seed! (seed)
4
+ dist = D ()
5
+ pars = [params (dist)... ]
6
+ avg = zeros (SDM. num_params (D))
7
+ for i = 1 : n
8
+ avg += SDM. score (rand (D (pars... )), D, pars)
9
+ end
10
+ avg ./= n
11
+ @test avg ≈ zeros (SDM. num_params (D)) atol = atol rtol = rtol
12
+ end
13
+
14
+ function test_loglik (D:: Type{<:Distribution} ; atol:: Float64 = 1e-3 , rtol:: Float64 = 1e-3 ,
15
+ seed:: Int = 13 , n:: Int = 100 )
16
+ Random. seed! (seed)
17
+ dist = D ()
18
+ y = rand (dist, n)
19
+ pars = [vcat (params (dist)... ) for _ in 1 : n]
20
+ log_lik = SDM. log_likelihood (D, y, pars, n)
21
+ @test log_lik ≈ - loglikelihood (dist, y) atol = atol rtol = rtol
22
+ return
23
+ end
24
+
25
+ function test_dynamic (sigma:: Float64 , lags:: Int ; seed:: Int = 12 , atol:: Float64 = 1e-1 , rtol:: Float64 = 1e-3 , n:: Int = 100 )
26
+ Random. seed! (seed)
27
+ dist = Normal (0 , sigma^ 2 )
28
+ obs_dynamic = kron (ones (n), collect (1 : lags)) + rand (dist, n* lags)
29
+ gas_lag_lag = GAS (lags, lags, Normal, 0.0 )
30
+ initial_params = dynamic_initial_params (obs_dynamic, gas_lag_lag)
31
+ for i in eachindex (initial_params)
32
+ @test initial_params[i][1 ] ≈ i atol = atol rtol = rtol
33
+ @test initial_params[i][2 ] ≈ sigma atol = atol rtol = rtol
34
+ end
35
+ end
36
+
37
+ function simulate_GAS_1_1 (D:: Type{<:Distribution} , scaling:: Float64 , ω:: Vector{T} , A:: Matrix{T} ,
38
+ B:: Matrix{T} , seed:: Int ) where T
39
+ Random. seed! (seed)
40
+ gas = GAS (1 , 1 , D, scaling)
41
+
42
+ gas. ω = ω
43
+ gas. A[1 ] = A
44
+ gas. B[1 ] = B
45
+ series, param = simulate (gas, 5000 )
46
+
47
+ return series
48
+ end
49
+
50
+ function simulate_GAS_1_12 (D:: Type{<:Distribution} , scaling:: Float64 , seed:: Int )
51
+ Random. seed! (seed)
52
+ v = [0.1 , 0.1 ]
53
+ gas = GAS ([1 , 12 ], [1 , 12 ], D, scaling)
54
+
55
+ gas. ω = v
56
+ gas. A[1 ] = convert (Matrix{Float64}, Diagonal (3 * v))
57
+ gas. A[12 ] = convert (Matrix{Float64}, Diagonal (- 3 * v))
58
+ gas. B[1 ] = convert (Matrix{Float64}, Diagonal (3 * v))
59
+ gas. B[12 ] = convert (Matrix{Float64}, Diagonal (- 3 * v))
60
+
61
+ series, param = simulate (gas, 5000 )
62
+
63
+ return series
64
+ end
65
+
66
+ function test_coefficients_GAS_1_1 (gas:: GAS{D, T} , ω:: Vector{T} , A:: Matrix{T} , B:: Matrix{T} ;
67
+ atol = 1e-1 , rtol = 1e-1 ) where {D <: Distribution , T}
68
+ @test gas. ω[1 ] ≈ ω[1 ] atol = atol rtol = rtol
69
+ @test gas. ω[2 ] ≈ ω[2 ] atol = atol rtol = rtol
70
+ @test gas. A[1 ][1 , 1 ] ≈ A[1 , 1 ] atol = atol rtol = rtol
71
+ @test gas. A[1 ][2 , 2 ] ≈ A[2 , 2 ] atol = atol rtol = rtol
72
+ @test gas. B[1 ][1 , 1 ] ≈ B[1 , 1 ] atol = atol rtol = rtol
73
+ @test gas. B[1 ][2 , 2 ] ≈ B[2 , 2 ] atol = atol rtol = rtol
74
+ return
75
+ end
76
+
77
+ function test_coefficients_GAS_1_12 (gas:: GAS{D, T} ; atol = 1e-1 , rtol = 1e-1 ) where {D <: Distribution , T}
78
+ @test gas. ω[1 ] ≈ 0.1 atol = atol rtol = rtol
79
+ @test gas. ω[2 ] ≈ 0.1 atol = atol rtol = rtol
80
+ @test gas. A[1 ][1 , 1 ] ≈ 0.3 atol = atol rtol = rtol
81
+ @test gas. A[1 ][2 , 2 ] ≈ 0.3 atol = atol rtol = rtol
82
+ @test gas. A[12 ][1 , 1 ] ≈ - 0.3 atol = atol rtol = rtol
83
+ @test gas. A[12 ][2 , 2 ] ≈ - 0.3 atol = atol rtol = rtol
84
+ @test gas. B[1 ][1 , 1 ] ≈ 0.3 atol = atol rtol = rtol
85
+ @test gas. B[1 ][2 , 2 ] ≈ 0.3 atol = atol rtol = rtol
86
+ @test gas. B[12 ][1 , 1 ] ≈ - 0.3 atol = atol rtol = rtol
87
+ @test gas. B[12 ][2 , 2 ] ≈ - 0.3 atol = atol rtol = rtol
88
+ return
89
+ end
90
+
91
+ function normality_quantile_and_pearson_residuals (D, n:: Int , lags:: Int ; seed:: Int = 11 )
92
+ Random. seed! (seed)
93
+
94
+ gas = GAS (1 , 1 , D, 0.0 )
95
+ gas. ω .= [0.0 ; 0.0 ]
96
+ gas. A[1 ][[1 ; 4 ]] .= 0.2
97
+ gas. B[1 ][[1 ; 4 ]] .= 0.2
98
+
99
+ y, params = simulate (gas, n)
100
+ quant_res = quantile_residuals (y, gas, [params[1 ]])
101
+ pearson = pearson_residuals (y, gas, [params[1 ]])
102
+
103
+ # quantile residuals
104
+ jb = JarqueBeraTest (quant_res)
105
+ @test pvalue (jb) >= 0.05
106
+ Ljb = LjungBoxTest (quant_res, lags)
107
+ @test pvalue (Ljb) >= 0.05
108
+
109
+ # pearson
110
+ Ljb = LjungBoxTest (pearson, lags)
111
+ @test pvalue (Ljb) >= 0.05
112
+ return
113
+ end
0 commit comments