Skip to content

Commit 38f60da

Browse files
Better print (#58)
* better print * tests nust pass through prints
1 parent c9c3870 commit 38f60da

File tree

3 files changed

+22
-15
lines changed

3 files changed

+22
-15
lines changed

src/MLE.jl

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ const DEFAULT_NUM_SEEDS = 3
55
const DEFAULT_VERBOSE = 0
66
const VARIANCE_ZERO = 1e-10
77

8-
struct FittedSDM{T <: AbstractFloat}
8+
struct FittedSDM{D <: Distribution, T <: AbstractFloat}
9+
num_obs::Integer
910
unknowns::UnknownsSDM
1011
aic::T
1112
bic::T
@@ -22,21 +23,22 @@ struct CoefsStatsSDM{T <: AbstractFloat}
2223
p_values::Vector{T}
2324
end
2425

25-
struct EstimationStatsSDM{T <: AbstractFloat}
26+
struct EstimationStatsSDM{D <: Distribution, T <: AbstractFloat}
27+
num_obs::Integer
2628
loglikelihood::T
2729
aic::T
2830
bic::T
2931
np::T
3032
coefs_stats::CoefsStatsSDM{T}
3133
end
3234

33-
function fit_stats(f::FittedSDM{T}) where T
35+
function fit_stats(f::FittedSDM{D, T}) where {D, T}
3436
estim_results = eval_coefs_stats(f)
3537
np = length(f.unknowns)
36-
return EstimationStatsSDM{T}(f.llk, f.aic, f.bic, np, estim_results)
38+
return EstimationStatsSDM{D, T}(f.num_obs, f.llk, f.aic, f.bic, np, estim_results)
3739
end
3840

39-
function eval_coefs_stats(f::FittedSDM{T}) where T
41+
function eval_coefs_stats(f::FittedSDM{D, T}) where {D, T}
4042
np = length(f.unknowns)
4143
inv_H = inv(f.numerical_hessian)
4244
vars = diag(inv_H)
@@ -139,7 +141,7 @@ function fit(sdm::SDM{D, T}, y::Vector{T};
139141
end
140142

141143
println("Finished!")
142-
return FittedSDM{T}(unknowns, aic, bic, best_llk, coefs, num_hessian)
144+
return FittedSDM{D, T}(n, unknowns, aic, bic, best_llk, coefs, num_hessian)
143145
end
144146

145147
function fit!(sdm::SDM{D, T}, y::Vector{T};

src/prints.jl

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
function Base.show(io::IO, est::EstimationStatsSDM)
1+
function Base.show(io::IO, est::EstimationStatsSDM{D, T}) where {D, T}
22
println("--------------------------------------------------------")
3-
println("log-likelihood: ", @sprintf("%.4f", est.loglikelihood))
4-
println(" np: ", Int(est.np))
5-
println(" AIC: ", @sprintf("%.4f", est.aic))
6-
println(" BIC: ", @sprintf("%.4f", est.bic))
3+
println("Distribution: ", D)
4+
println("Number of observations: ", Int(est.num_obs))
5+
println("Number of unknown parameters: ", Int(est.np))
6+
println("Log-likelihood: ", @sprintf("%.4f", est.loglikelihood))
7+
println("AIC: ", @sprintf("%.4f", est.aic))
8+
println("BIC: ", @sprintf("%.4f", est.bic))
79
print_coefs_stats(est.coefs_stats)
810
return nothing
911
end
1012

11-
function print_coefs_stats(coefs_stats)
13+
function print_coefs_stats(coefs_stats::CoefsStatsSDM{T}) where T
1214
println("--------------------------------------------------------")
1315
println("Parameter Estimate Std.Error t stat p-value")
1416
offset = 1
@@ -18,7 +20,8 @@ function print_coefs_stats(coefs_stats)
1820
println(p)
1921
offset += 1
2022
end
21-
for k in sort(collect(keys(coefs_stats.unknowns.A)))
23+
sorted_keys_A = sort(collect(keys(coefs_stats.unknowns.A)))
24+
for k in sorted_keys_A
2225
for i in coefs_stats.unknowns.A[k]
2326
p_c, p_std, p_t_stat, p_p_val = print_coefs_sta(coefs_stats, offset)
2427
ind = round(Int, sqrt(i))
@@ -27,7 +30,8 @@ function print_coefs_stats(coefs_stats)
2730
offset += 1
2831
end
2932
end
30-
for k in sort(collect(keys(coefs_stats.unknowns.B)))
33+
sorted_keys_B = sort(collect(keys(coefs_stats.unknowns.B)))
34+
for k in sorted_keys_B
3135
for i in coefs_stats.unknowns.B[k]
3236
p_c, p_std, p_t_stat, p_p_val = print_coefs_sta(coefs_stats, offset)
3337
ind = round(Int, sqrt(i))

test/utils.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ function test_GARCH_1_1(y, seed::Int, optimizer; atol = 1e-4, rtol = 1e-4)
125125
ub = [1.0; 1.0; 1.0; 1.0]
126126
lb = [-1.0; 0.0; 0.0; 0.0]
127127
gas = GAS(1, 1, Normal, 1.0, time_varying_params = [2])
128-
res = fit!(gas, y; initial_params = ini, verbose = 1, opt_method = optimizer(gas, 10; ub = ub, lb = lb))
128+
f = fit!(gas, y; initial_params = ini, verbose = 1, opt_method = optimizer(gas, 10; ub = ub, lb = lb))
129+
show(stdout, fit_stats(f))
129130

130131
@test gas.ω[1] - -0.00616637237701241 0 atol = atol rtol = rtol
131132
@test gas.ω[2] - 0.010760592759725487 0 atol = atol rtol = rtol

0 commit comments

Comments
 (0)