2
2
fit!(
3
3
model::StateSpaceModel;
4
4
filter::KalmanFilter=default_filter(model),
5
- optimizer::Optimizer=Optimizer(Optim.LBFGS())
5
+ optimizer::Optimizer=Optimizer(Optim.LBFGS()),
6
+ save_hyperparameter_distribution::Bool=true
6
7
)
7
8
8
9
Estimate the state-space model parameters via maximum likelihood. The resulting optimal
@@ -29,6 +30,7 @@ function fit!(
29
30
model:: StateSpaceModel ;
30
31
filter:: KalmanFilter = default_filter (model),
31
32
optimizer:: Optimizer = default_optimizer (model),
33
+ save_hyperparameter_distribution:: Bool = true
32
34
)
33
35
isfitted (model) && return model
34
36
@assert has_fit_methods (typeof (model))
@@ -45,12 +47,29 @@ function fit!(
45
47
opt_loglikelihood = - opt. minimum * size (model. system. y, 1 )
46
48
opt_hyperparameters = opt. minimizer
47
49
update_model_hyperparameters! (model, opt_hyperparameters)
48
- # TODO
49
- # I leaned that this is not a good way to compute the covariance matrix of paarameters
50
- # we should investigate other methods
51
- numerical_hessian = Optim. hessian! (func, opt_hyperparameters)
52
- std_err = diag (pinv (numerical_hessian))
53
- fill_results! (model, opt_loglikelihood, std_err)
50
+
51
+ if save_hyperparameter_distribution
52
+ numerical_hessian = Optim. hessian! (func, opt_hyperparameters)
53
+ try
54
+ std_err = numerical_hessian |> pinv |> diag .| > sqrt
55
+ fill_results! (model, opt_loglikelihood, std_err)
56
+ catch
57
+ @warn (
58
+ " The optimization process converged but the Hessian matrix is not positive definite. " *
59
+ " This means that StateSpaceModels.jl cannot estimate the distribution of the hyperparameters " *
60
+ " If you are interested in estimates of the distribution of ther hyperparameters we advise you to" *
61
+ " change the optimization algorithm by using the kwarg fit(...; optimizer = " *
62
+ " Optimizer(StateSpaceModels.Optim.THE_METHOD_OF_YOUR_CHOICE()))" *
63
+ " The list of possible algorithms can be found on this link https://julianlsolvers.github.io/Optim.jl/stable/#" *
64
+ " Otherwise you can simply skip this proccess by using fit(...; save_hyperparameter_distribution=false) "
65
+ )
66
+ std_err = fill (NaN , number_hyperparameters (model))
67
+ fill_results! (model, opt_loglikelihood, std_err)
68
+ end
69
+ else
70
+ std_err = fill (NaN , number_hyperparameters (model))
71
+ fill_results! (model, opt_loglikelihood, std_err)
72
+ end
54
73
return model
55
74
end
56
75
@@ -121,12 +140,7 @@ function Results{Fl}() where Fl
121
140
return Results {Fl} (" " , CoefficientTable {Fl} (), Fl (NaN ), Fl (NaN ), Fl (NaN ), Fl (NaN ), 0 , 0 )
122
141
end
123
142
124
- """
125
- results(model::StateSpaceModel)
126
-
127
- Query the results of the optimization called by `fit!`.
128
- """
129
- results (model:: StateSpaceModel ) = model. results
143
+ get_results (model:: StateSpaceModel ) = model. results
130
144
function Base. isempty (results:: Results )
131
145
return isempty (results. coef_table) &&
132
146
isnan (results. llk) &&
0 commit comments