Skip to content

Commit 1b1c6f6

Browse files
Pearson res (#128)
* Add backtest and time_limit_sec * Add backtest * Add JarqueBera on pearson residuals
1 parent dfae6d5 commit 1b1c6f6

File tree

4 files changed

+22
-19
lines changed

4 files changed

+22
-19
lines changed

Project.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
name = "ScoreDrivenModels"
22
uuid = "4a87933e-d659-11e9-0e65-7f40dedd4a3a"
33
authors = ["guilhermebodin <[email protected]>, raphaelsaavedra <[email protected]>"]
4-
version = "0.1.6"
4+
version = "0.1.7"
55

66
[deps]
77
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
8+
HypothesisTests = "09f84164-cd44-5f33-b23f-e6b0d136a0d5"
89
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
910
Optim = "429524aa-4258-5aef-a3af-852621145aeb"
1011
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
@@ -15,6 +16,7 @@ Distributions = "0.23"
1516
Optim = "0.20, 0.21, 0.22, 1.2"
1617
SpecialFunctions = "0.8"
1718
julia = "1"
19+
HypothesisTests = "0.9"
1820

1921
[extras]
2022
DelimitedFiles = "8bb1440f-4735-579b-a4ab-409b98df4dab"

src/MLE.jl

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ struct Fitted{D <: Distribution, T <: AbstractFloat}
1313
llk::T
1414
coefs::Vector{T}
1515
numerical_hessian::Matrix{T}
16+
pearson_residuals::Vector{T}
1617
end
1718

1819
struct CoefsStats{T <: AbstractFloat}
@@ -29,13 +30,16 @@ struct EstimationStats{D <: Distribution, T <: AbstractFloat}
2930
aic::T
3031
bic::T
3132
np::T
33+
jarquebera_p_value::T
3234
coefs_stats::CoefsStats{T}
3335
end
3436

3537
function results(f::Fitted{D, T}) where {D, T}
3638
estim_results = eval_coefs_stats(f)
3739
np = length(f.unknowns)
38-
return EstimationStats{D, T}(f.num_obs, f.llk, f.aic, f.bic, np, estim_results)
40+
jarquebera_p_value = pvalue(JarqueBeraTest(f.pearson_residuals))
41+
return EstimationStats{D, T}(f.num_obs, f.llk, f.aic, f.bic, np,
42+
jarquebera_p_value, estim_results)
3943
end
4044

4145
function eval_coefs_stats(f::Fitted{D, T}) where {D, T}
@@ -91,13 +95,18 @@ function update_aux_estimation!(aux_est::AuxEstimation{T}, func::Optim.TwiceDiff
9195
return
9296
end
9397

94-
function fit(gas::Model{D, T}, y::Vector{T};
98+
function fit!(gas::Model{D, T}, y::Vector{T};
9599
initial_params::Matrix{T} = DEFAULT_INITIAL_PARAM,
96100
opt_method::AbstractOptimizationMethod = NelderMead(gas, DEFAULT_NUM_SEEDS),
97101
verbose::Int = DEFAULT_VERBOSE,
98102
throw_errors::Bool = false,
99103
time_limit_sec::Int = 10^8) where {D, T}
100104

105+
unknowns = find_unknowns(gas)
106+
# Check if the model has no unknowns
107+
n_unknowns = length(unknowns)
108+
check_model_estimated(n_unknowns) && return gas
109+
101110
verbose in [0, 1, 2, 3] || throw(ErrorException, "verbose argument must be in [0, 1, 2, 3]")
102111
# Number of initial_points and number of params to estimate
103112
n_initial_points = length(opt_method.initial_points)
@@ -150,21 +159,12 @@ function fit(gas::Model{D, T}, y::Vector{T};
150159
println(aux_est.opt_result[best_seed])
151160
end
152161

153-
return Fitted{D, T}(n, unknowns, aic, bic, best_llk, coefs, num_hessian)
154-
end
162+
fill_psitilde!(gas, coefs, unknowns)
155163

156-
function fit!(gas::Model{D, T}, y::Vector{T};
157-
initial_params::Matrix{T} = DEFAULT_INITIAL_PARAM,
158-
opt_method::AbstractOptimizationMethod = NelderMead(gas, DEFAULT_NUM_SEEDS),
159-
verbose::Int = DEFAULT_VERBOSE,
160-
throw_errors::Bool = false) where {D, T}
164+
# Calculate pearson residuals
165+
pearson_res = isnan(initial_params[1]) ?
166+
pearson_residuals(y, gas) :
167+
pearson_residuals(y, gas; initial_params = initial_params)
161168

162-
unknowns = find_unknowns(gas)
163-
# Check if the model has no unknowns
164-
n_unknowns = length(unknowns)
165-
check_model_estimated(n_unknowns) && return gas
166-
167-
f = fit(gas, y; initial_params = initial_params, opt_method = opt_method, verbose = verbose, throw_errors = throw_errors)
168-
fill_psitilde!(gas, f.coefs, unknowns)
169-
return f
169+
return Fitted{D, T}(n, unknowns, aic, bic, best_llk, coefs, num_hessian, pearson_res)
170170
end

src/ScoreDrivenModels.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module ScoreDrivenModels
22

3-
using Distributions, Optim, SpecialFunctions
3+
using Distributions, Optim, SpecialFunctions, HypothesisTests
44
using LinearAlgebra, Printf
55

66
import Base: length, deepcopy, show

src/prints.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ function Base.show(io::IO, est::EstimationStats{D, T}) where {D, T}
66
println(io, "Log-likelihood: ", @sprintf("%.4f", est.loglikelihood))
77
println(io, "AIC: ", @sprintf("%.4f", est.aic))
88
println(io, "BIC: ", @sprintf("%.4f", est.bic))
9+
println(io, "Jarque Bera p value: ", @sprintf("%.4f", est.jarquebera_p_value))
910
print_coefs_stats(est.coefs_stats)
1011
return nothing
1112
end

0 commit comments

Comments
 (0)