Skip to content

Commit

Permalink
Implement test of regime-switching linear regression (all para unfixe…
Browse files Browse the repository at this point in the history
…d) successfully.
  • Loading branch information
chenwilliam77 committed Jan 4, 2021
1 parent 44baf3a commit e084594
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 17 deletions.
29 changes: 17 additions & 12 deletions test/modelsetup.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,24 @@ function setup_linear_model(; regime_switching::Bool = false)
m = GenericModel()

# Set up linear parameters
m <= parameter(:α1, 0., (-1e5, 1e5), (-1e5, 1e5), Untransformed(), Normal(0, 1e3),
prior_para = regime_switching ? 1e1 : 1e3
m <= parameter(:α1, 0., (-1e5, 1e5), (-1e5, 1e5), Untransformed(), Normal(0, prior_para),
fixed = false)
m <= parameter(:β1, 0., (-1e5, 1e5), (-1e5, 1e5), Untransformed(), Normal(0, 1e3),
m <= parameter(:β1, 0., (-1e5, 1e5), (-1e5, 1e5), Untransformed(), Normal(0, prior_para),
fixed = false)
m <= parameter(:σ1, 1., (1e-5, 1e5), (1e-5, 1e5), SquareRoot(), Uniform(0, 1e3),
m <= parameter(:σ1, 1., (1e-5, 1e5), (1e-5, 1e5), SquareRoot(), Uniform(0, prior_para),
fixed = false)
m <= parameter(:α2, 0., (-1e5, 1e5), (-1e5, 1e5), Untransformed(), Normal(0, 1e3),
m <= parameter(:α2, 0., (-1e5, 1e5), (-1e5, 1e5), Untransformed(), Normal(0, prior_para),
fixed = false)
m <= parameter(:β2, 0., (-1e5, 1e5), (-1e5, 1e5), Untransformed(), Normal(0, 1e3),
m <= parameter(:β2, 0., (-1e5, 1e5), (-1e5, 1e5), Untransformed(), Normal(0, prior_para),
fixed = false)
m <= parameter(:σ2, 1., (1e-5, 1e5), (1e-5, 1e5), SquareRoot(), Uniform(0, 1e3),
m <= parameter(:σ2, 1., (1e-5, 1e5), (1e-5, 1e5), SquareRoot(), Uniform(0, prior_para),
fixed = false)
m <= parameter(:α3, 0., (-1e5, 1e5), (-1e5, 1e5), Untransformed(), Normal(0, 1e3),
m <= parameter(:α3, 0., (-1e5, 1e5), (-1e5, 1e5), Untransformed(), Normal(0, prior_para),
fixed = false)
m <= parameter(:β3, 0., (-1e5, 1e5), (-1e5, 1e5), Untransformed(), Normal(0, 1e3),
m <= parameter(:β3, 0., (-1e5, 1e5), (-1e5, 1e5), Untransformed(), Normal(0, prior_para),
fixed = false)
m <= parameter(:σ3, 1., (1e-5, 1e5), (1e-5, 1e5), SquareRoot(), Uniform(0, 1e3),
m <= parameter(:σ3, 1., (1e-5, 1e5), (1e-5, 1e5), SquareRoot(), Uniform(0, prior_para),
fixed = false)
m <= Setting(:n_particles, 400) #-> will get commented eventually
m <= Setting(:n_Φ, 100)
Expand Down Expand Up @@ -55,11 +56,11 @@ function setup_linear_model(; regime_switching::Bool = false)
end
for i in 1:3
ModelConstructors.set_regime_val!(m[Symbol("β$(i)")], 1, .2 * i)
ModelConstructors.set_regime_prior!(m[Symbol("β$(i)")], 1, Normal(0, 1e3)) # regime-switching prior, just to check functionality
ModelConstructors.set_regime_prior!(m[Symbol("β$(i)")], 1, Normal(0, prior_para)) # regime-switching prior, just to check functionality
ModelConstructors.set_regime_val!(m[Symbol("β$(i)")], 2, -.1 * i)
ModelConstructors.set_regime_prior!(m[Symbol("β$(i)")], 2, Normal(0, 1e3))
ModelConstructors.set_regime_prior!(m[Symbol("β$(i)")], 2, Normal(0, prior_para * 1.2))
ModelConstructors.set_regime_val!(m[Symbol("β$(i)")], 3, .1 * i)
ModelConstructors.set_regime_prior!(m[Symbol("β$(i)")], 3, Normal(0, 1e2))
ModelConstructors.set_regime_prior!(m[Symbol("β$(i)")], 3, Normal(0, prior_para * 1.5))
end
end

Expand Down Expand Up @@ -104,6 +105,10 @@ if regenerate_data
write(file, "X", X)
write(file, "Xrs", Xrs)
end
else # Need to define reg1, reg2, and reg3 for regime-switching log likelihood function
reg1 = 1:100
reg2 = 101:200
reg3 = 201:300
end

# Read Predictors from data
Expand Down
Binary file not shown.
12 changes: 7 additions & 5 deletions test/regime_switching_smc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ true_para = [1., 1., 1., # α1, β1, σ1 (regime 1)
3., 4., # β2 regimes = 2-3
3., 3., # α3 regimes = 2-3
4., 5.] # β3 regimes = 2-3
@assert false

if writing_output
jldopen(string("reference/smc_cloud_fix=true_rs=true_version=", ver, ".jld2"), true, true, true, IOStream) do file
write(file, "cloud", test_cloud)
Expand All @@ -64,6 +64,10 @@ saved_W = saved_file["W"]

####################################################################
cloud_fields = fieldnames(typeof(test_cloud))
@testset "Lienar Regression Regime-Switching Parameter Estimates Are Close" begin
@test maximum(abs.(mean_para - true_para)) < .5
end

@testset "ParticleCloud Fields: Linear" begin
@test @test_matrix_approx_eq SMC.get_vals(test_cloud) SMC.get_vals(saved_cloud)
@test @test_matrix_approx_eq SMC.get_loglh(test_cloud) SMC.get_loglh(saved_cloud)
Expand Down Expand Up @@ -95,8 +99,6 @@ end
@test @test_matrix_approx_eq test_W saved_W
end

# TODO: add a check here that the correct parameters are estimated

# Clean output files up
#=rm(rawpath(m, "estimate", "smc_cloud.jld2"))
rm(rawpath(m, "estimate", "smcsave.h5"))=#
rm(rawpath(m, "estimate", "smc_cloud.jld2"))
rm(rawpath(m, "estimate", "smcsave.h5"))

0 comments on commit e084594

Please sign in to comment.