Skip to content

Commit bfb7e39

Browse files
improve tests, fix scaling call (#32)
1 parent 2874e10 commit bfb7e39

File tree

7 files changed

+97
-37
lines changed

7 files changed

+97
-37
lines changed

docs/src/examples.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ y = Vector{Float64}(vec(inflow'))
1010

1111
# Specify GAS model: here we use lag 1 for trend characterization and
1212
# lag 12 for seasonality characterization
13-
gas = GAS_Sarima([1, 12], [1, 12], LogNormal, 0.0)
13+
gas = GAS([1, 12], [1, 12], LogNormal, 0.0)
1414

1515
# Estimate the model via MLE
1616
estimate!(gas, y)

examples/inflow_lognormal.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ inflow = [
4848
y = Vector{Float64}(vec(inflow'))
4949

5050
# Specify GAS model: here we use lag 1 for trend characterization and lag 12 for seasonality characterization
51-
gas = GAS_Sarima([1, 12], [1, 12], LogNormal, 0.0)
51+
gas = GAS([1, 12], [1, 12], LogNormal, 0.0)
5252

5353
# Estimate the model via MLE
5454
estimate!(gas, y)

src/MLE.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ function estimate!(sdm::SDM{D, T}, y::Vector{T};
3737
push!(psi, optseed.minimizer)
3838
push!(optseeds, optseed)
3939
println("seed $i of $nseeds - $(-optseed.minimum)")
40-
catch
40+
catch err
41+
println(err)
4142
println("seed $i diverged")
4243
end
4344
end

src/gas/gas.jl

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ mutable struct GAS{D <: Distribution, T <: AbstractFloat} <: SDM{D, T}
55
A::Dict{Int, Matrix{T}}
66
B::Dict{Int, Matrix{T}}
77
scaling::Real
8-
9-
function GAS{D, T}::Vector{Float64}, A::Dict{Int, Matrix{T}}, B::Dict{Int, Matrix{T}}, scaling::Real) where {D <: Distribution, T <: AbstractFloat}
10-
return new(ω, A, B, scaling)
11-
end
128
end
139

1410
function create_ω(num_params::Int)

src/score.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ function score_tilde(y::T, D::Type{<:Distribution}, param::Vector{T}, param_tild
1717
end
1818

1919
# Scalings
20-
function scaling_invsqrt(jac::Matrix{T}, D::Type{Distribution}, param::Vector{T}) where T
20+
function scaling_invsqrt(jac::AbstractMatrix{T}, D::Type{<:Distribution}, param::Vector{T}) where T
2121
#TODO improve performace and check if this is right
2222
# chol = cholesky(fisher_information(dist, param))
2323
# cholmat = Matrix(chol)
2424
error("check how to do this properly")
2525
return jac'*inv(cholmat)*jac
2626
end
2727

28-
function scaling_inv(jac::Matrix{T}, D::Type{Distribution}, param::Vector{T}) where T
28+
function scaling_inv(jac::AbstractMatrix{T}, D::Type{<:Distribution}, param::Vector{T}) where T
2929
#TODO improve performace
3030
return jac'*inv(fisher_information(D, param))*jac
3131
end

test/test_distributions.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ end
3434
end
3535
end
3636

37-
@testset "Log Likelihood" begin
37+
@testset "Log-likelihood" begin
3838
for dist in SDM.DISTS
3939
@testset "$dist" begin
4040
test_loglik(dist)

test/test_estimate.jl

Lines changed: 90 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,105 @@
1-
function simulate_GAS_Sarima_1_1(D::Type{Beta}, scaling::Float64)
2-
# Create model
3-
Random.seed!(13)
4-
vec = [0.1 ; 0.1]
5-
1+
function simulate_GAS_1_1(D::Type{<:Distribution}, scaling::Float64, ω::Vector{T}, A::Matrix{T},
2+
B::Matrix{T}, seed::Int) where T
3+
Random.seed!(seed)
64
gas = GAS(1, 1, D, scaling)
75

8-
gas.ω = vec
9-
gas.A[1] = convert(Matrix{Float64}, Diagonal(5*vec))
10-
gas.B[1] = convert(Matrix{Float64}, Diagonal(5*vec))
6+
gas.ω = ω
7+
gas.A[1] = A
8+
gas.B[1] = B
9+
series, param = simulate(gas, 1000)
10+
11+
return series
12+
end
13+
14+
function simulate_GAS_1_12(D::Type{<:Distribution}, scaling::Float64)
15+
Random.seed!(123)
16+
v = [0.1, 0.1]
17+
gas = GAS([1, 12], [1, 12], D, scaling)
1118

12-
# Simulate 1000 observations
13-
serie_simulated, param_simulated = simulate(gas, 1000)
19+
gas.ω = v
20+
gas.A[1] = convert(Matrix{Float64}, Diagonal(3*v))
21+
gas.A[12] = convert(Matrix{Float64}, Diagonal(-3*v))
22+
gas.B[1] = convert(Matrix{Float64}, Diagonal(3*v))
23+
gas.B[12] = convert(Matrix{Float64}, Diagonal(-3*v))
1424

15-
return serie_simulated
25+
series, param = simulate(gas, 5000)
26+
27+
return series
1628
end
1729

18-
function test_coefficients_GAS_Sarima_1_1(gas::GAS{Beta, T}; atol = 1e-1, rtol = 1e-1) where T
19-
@test gas.ω[1] 0.1 atol = atol rtol = rtol
20-
@test gas.ω[2] 0.1 atol = atol rtol = rtol
21-
@test gas.A[1][1, 1] 0.5 atol = atol rtol = rtol
22-
@test gas.A[1][2, 2] 0.5 atol = atol rtol = rtol
23-
@test gas.B[1][1, 1] 0.5 atol = atol rtol = rtol
24-
@test gas.B[1][2, 2] 0.5 atol = atol rtol = rtol
30+
function test_coefficients_GAS_1_1(gas::GAS{D, T}, ω::Vector{T}, A::Matrix{T}, B::Matrix{T};
31+
atol = 1e-1, rtol = 1e-1) where {D <: Distribution, T}
32+
@test gas.ω[1] ω[1] atol = atol rtol = rtol
33+
@test gas.ω[2] ω[2] atol = atol rtol = rtol
34+
@test gas.A[1][1, 1] A[1, 1] atol = atol rtol = rtol
35+
@test gas.A[1][2, 2] A[2, 2] atol = atol rtol = rtol
36+
@test gas.B[1][1, 1] B[1, 1] atol = atol rtol = rtol
37+
@test gas.B[1][2, 2] B[2, 2] atol = atol rtol = rtol
2538
return
2639
end
2740

28-
function test_estimation_GAS_Sarima_1_1(gas::GAS{D, T}, simulation::Vector{T}) where {D, T}
29-
estimate!(gas, simulation; verbose = 1,
30-
opt_method = ScoreDrivenModels.LBFGS(gas, 5))
31-
32-
test_coefficients_GAS_Sarima_1_1(gas)
33-
return
41+
function test_coefficients_GAS_1_12(gas::GAS{D, T}; atol = 1e-1, rtol = 1e-1) where {D <: Distribution, T}
42+
@test gas.ω[1] 0.1 atol = atol rtol = rtol
43+
@test gas.ω[2] 0.1 atol = atol rtol = rtol
44+
@test gas.A[1][1, 1] 0.3 atol = atol rtol = rtol
45+
@test gas.A[1][2, 2] 0.3 atol = atol rtol = rtol
46+
@test gas.A[12][1, 1] -0.3 atol = atol rtol = rtol
47+
@test gas.A[12][2, 2] -0.3 atol = atol rtol = rtol
48+
@test gas.B[1][1, 1] 0.3 atol = atol rtol = rtol
49+
@test gas.B[1][2, 2] 0.3 atol = atol rtol = rtol
50+
@test gas.B[12][1, 1] -0.3 atol = atol rtol = rtol
51+
@test gas.B[12][2, 2] -0.3 atol = atol rtol = rtol
52+
return
3453
end
3554

3655
@testset "Estimate" begin
3756
@testset "Beta" begin
38-
simulation = simulate_GAS_Sarima_1_1(Beta, 0.0)
39-
gas = GAS(1, 1, Beta, 0.0)
40-
test_estimation_GAS_Sarima_1_1(gas, simulation)
57+
ω = [0.1, 0.1]
58+
A = [0.5 0; 0 0.5]
59+
B = [0.5 0; 0 0.5]
60+
simulation = simulate_GAS_1_1(Beta, 0.0, ω, A, B, 13)
61+
@testset "Estimation by passing number of seeds" begin
62+
gas = GAS(1, 1, Beta, 0.0)
63+
estimate!(gas, simulation; verbose = 1, opt_method = ScoreDrivenModels.LBFGS(gas, 3))
64+
test_coefficients_GAS_1_1(gas, ω, A, B)
65+
end
66+
@testset "Estimation by passing seeds" begin
67+
gas = GAS(1, 1, Beta, 0.0)
68+
seeds = [[0.1, 0.1, 0.5, 0.5, 0.5, 0.5]]
69+
estimate!(gas, simulation; verbose = 1, opt_method = ScoreDrivenModels.LBFGS(gas, seeds))
70+
test_coefficients_GAS_1_1(gas, ω, A, B)
71+
end
72+
end
73+
74+
@testset "Lognormal" begin
75+
@testset "Scaling = 0.0" begin
76+
ω = [0.1, 0.1]
77+
A = [0.5 0; 0 0.5]
78+
B = [0.5 0; 0 0.5]
79+
simulation = simulate_GAS_1_1(LogNormal, 0.0, ω, A, B, 13)
80+
gas = GAS(1, 1, LogNormal, 0.0)
81+
estimate!(gas, simulation; verbose = 1, opt_method = ScoreDrivenModels.LBFGS(gas, 3))
82+
test_coefficients_GAS_1_1(gas, ω, A, B)
83+
end
84+
# @testset "Scaling = 0.5" begin
85+
# gas = GAS(1, 1, LogNormal, 0.5)
86+
# estimate!(gas, simulation; verbose = 1, opt_method = ScoreDrivenModels.LBFGS(gas, 3))
87+
# test_coefficients_GAS_1_1(gas)
88+
# end
89+
# @testset "Scaling = 1.0" begin
90+
# ω = [0.5, 0.5]
91+
# A = [0.1 0; 0 0.1]
92+
# B = [0.1 0; 0 0.1]
93+
# simulation = simulate_GAS_1_1(LogNormal, 1.0, ω, A, B, 123)
94+
# gas = GAS(1, 1, LogNormal, 1.0)
95+
# estimate!(gas, simulation; verbose = 1, opt_method = ScoreDrivenModels.LBFGS(gas, 3))
96+
# test_coefficients_GAS_1_1(gas)
97+
# end
98+
@testset "GAS([1, 12], [1, 12])" begin
99+
simulation = simulate_GAS_1_12(LogNormal, 0.0)
100+
gas = GAS([1, 12], [1, 12], LogNormal, 0.0)
101+
estimate!(gas, simulation; verbose = 1, opt_method = ScoreDrivenModels.LBFGS(gas, 3))
102+
test_coefficients_GAS_1_12(gas)
103+
end
41104
end
42105
end

0 commit comments

Comments
 (0)