Skip to content

Commit e6080ad

Browse files
committed
Update model tuning to use a Tree Parzen estimator
1 parent 0ee241d commit e6080ad

File tree

3 files changed

+27
-40
lines changed

3 files changed

+27
-40
lines changed

Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@ ProximalOperators = "a725b495-10eb-56fe-b38b-717eba820537"
2222
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
2323
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
2424
StatsAPI = "82ae8749-77ed-4fe6-ae5f-f523153014b0"
25+
TreeParzen = "eb66a70c-a255-11e9-03ea-7ba6b2f22006"
2526

2627
[compat]
2728
DifferentiationInterface = "0.6.53"
2829
FiniteDiff = "2.27.0"
30+
TreeParzen = "0.3.4"
2931
julia = "1.9"
3032

3133
[extras]

src/DynamicFactorModels.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ using DifferentiationInterface: AutoFiniteDiff
3434
using ProximalCore
3535
using ProximalOperators: NormL1, NormL21, TotalVariation1D
3636
using ProximalAlgorithms
37+
using TreeParzen
3738

3839
using ProgressMeter
3940

src/interface.jl

Lines changed: 24 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -264,66 +264,50 @@ function fit!(model::DynamicFactorModel;
264264
end
265265

266266
"""
267-
model_tuning_ic!(model, regularizers; ic = :bic, parallel = false, verbose = false,
267+
model_tuning_ic!(model, space, regularizer; trials = 100, ic = :bic, verbose = false,
268268
kwargs...) -> (model_opt, index_opt)
269269
270-
Search for the optimal regularizer in `regularizers` for the dynamic factor model `model`
271-
using information criterion `ic`. If `parallel` is true, the search is performed in
272-
parallel. If `verbose` is true, a summary of model tuning and progress of the search is
273-
printed. Additional keyword arguments `kwargs` are passed to the `fit!` function.
270+
Search for the optimal regularizer in search space `space` for the dynamic factor model
271+
`model` using information criterion `ic` and a Tree Parzen estimator performing number of
272+
trials given by `trials`, where `regularizer` is a function that creates the regularizer
273+
from a dictionary of hyperparameters. If `verbose` is true, a summary of model tuning and
274+
progress of the search is printed. Additional keyword arguments `kwargs` are passed to the
275+
`fit!` function.
274276
"""
275-
function model_tuning_ic!(model::DynamicFactorModel, regularizers::AbstractArray;
276-
ic::Symbol = :bic, parallel::Bool = false, verbose::Bool = false,
277+
function model_tuning_ic!(model::DynamicFactorModel, space::Dict, regularizer::Function;
278+
trials::Integer = 100, ic::Symbol = :bic, verbose::Bool = false,
277279
kwargs...)
278280
ic (:aic, :aicc, :bic) && error("Information criterion $ic not supported.")
279281

280282
if verbose
281283
println("Model tuning summary")
282284
println("====================")
283-
println("Number of regularizers: $(length(regularizers))")
285+
println("Number of trials: $trials")
284286
println("Information criterion: $ic")
285-
println("Parallel: $(parallel ? "yes" : "no")")
286287
println("====================")
287288
end
288289

289-
# model tuning
290-
map_func = parallel ? verbose ? progress_pmap : pmap : verbose ? progress_map : map
291-
θ0 = params(model)
292-
f0 = copy(factors(model))
293-
θ = map_func(regularizers) do regularizer
294-
try
295-
params!(model, θ0)
296-
factors(model) .= f0
297-
fit!(model, regularizer = regularizer; kwargs...)
298-
params(model)
299-
catch
300-
missing
301-
end
302-
end
303-
ic_values = map(θ) do θi
304-
if all(ismissing.(θi))
305-
missing
306-
else
307-
params!(model, θi)
308-
eval(ic)(model)
309-
end
310-
end
311-
(ic_opt, index_opt) = findmin(x -> isnan(x) ? Inf : x, skipmissing(ic_values))
312-
params!(model, θ[index_opt])
313-
(α, _, _) = smoother(model)
314-
for (t, αt) in pairs(α)
315-
factors(model)[:, t] = αt
290+
# objective function
291+
function objective(params)
292+
fit!(model, regularizer = regularizer(params))
293+
294+
return eval(ic)(model)
316295
end
317296

297+
# model tuning
298+
best = fmin(objective, space, trials)
299+
300+
# refit
301+
fit!(model, regularizer = regularizer(best))
302+
318303
if verbose
319304
println("====================")
320-
println("Optimal regularizer index: $(index_opt)")
321-
println("Optimal information criterion: $(ic_opt)")
322-
println("Failed fits: $(sum(ismissing.(ic_values)))")
305+
println("Optimal regularizer: $best")
306+
println("Optimal information criterion: $(eval(ic)(model))")
323307
println("====================")
324308
end
325309

326-
return (model, index_opt)
310+
return (model, best)
327311
end
328312

329313
"""

0 commit comments

Comments
 (0)