parameter_optimization.jl 10.3 KB
Newer Older
Peter Jentsch's avatar
Peter Jentsch committed
1
2
using KissABC

3
4
const vaccination_data = [0.0,0.043,0.385,0.424,0.115,0.03,0.005] #by month starting in august
const ymo_vac = [0.255,0.278,0.602]
5
const ymo_attack_rate = [10.376,5.636,7.2]./100
6

Peter Jentsch's avatar
Peter Jentsch committed
7

8
function solve_w_parameters(default_p, p_names, new_p_list)
9

10
11
12
13
14
    new_params = merge(default_p, NamedTuple{p_names}(ntuple(i -> new_p_list[i],length(p_names))))
    out = DebugRecorder(0,default_p.sim_length)
    model = abm(new_params,out)
    return out, model
end
Peter Jentsch's avatar
Peter Jentsch committed
15
16
17
18
19
20
21
22
23
#######
# function fit_pre_inf_behavioural_parameters(p_tuple)
#     samples = 1
#     p_names = (:π_base_y,:π_base_m,:π_base_o)
#     priors = Factored(
#         Uniform(-10.0,2.0),
#         Uniform(-10.0,2.0),
#         Uniform(-10.0,2.0)
#     )
24
    
Peter Jentsch's avatar
Peter Jentsch committed
25
26
27
#     #simulation begins in july
#     #60 days for opinion dynamics to stabilize, then immunization begins in september,
#     #infection is not considered
28
    
Peter Jentsch's avatar
Peter Jentsch committed
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#     sim_length = 180
#     p_tuple_adjust = merge(p_tuple,
#         (
#             sim_length = sim_length,
#             I_0_fraction = 0.000,
#             immunization_begin_day =60, 
#             infection_introduction_day = 180,
#             immunizing = true,
#         )
#     )
#     function cost(p)
#         output,model = solve_w_parameters(p_tuple_adjust, p_names,p)
#         target_ymo_vac = ymo_vac .* sum(vaccination_data[1:4]) .* length.(model.index_vectors)
#         ymo_vaccination_ts = output.daily_immunized_by_age
#         total_preinfection_vaccination = sum.(eachrow(ymo_vaccination_ts))
#         # display(target_ymo_vac)
#         # display(total_preinfection_vaccination)
#         return sum((total_preinfection_vaccination .- target_ymo_vac).^2)    
#     end

#     out = smc(priors,cost; verbose = true, nparticles = 150, parallel = true)#
#     return NamedTuple{p_names}(ntuple(i -> out.P[i].particles,length(p_names)))
# end

# function fit_post_inf_behavioural_parameters(p_tuple)
#     p_names = (:ω,:β_y,:β_m,:β_o)
#     priors = Factored(Uniform(0.0,0.1),Uniform(0.0,0.1),Uniform(0.0,0.1),Uniform(0.0,1.0))
#     #simulation begins in july
#     #60 days for opinion dynamics to stabilize, then immunization begins in september,
#     #infection introduced at beginning of december
#     sim_length = 300
#     p_tuple_adjust = merge(p_tuple,
#         (
#             sim_length = sim_length,
#             I_0_fraction = 0.005,
#             immunization_begin_day =60, 
#             infection_introduction_day = 180,
#             immunizing = true,
#         )
#     )
#     function cost(p)
#         output,model = solve_w_parameters(p_tuple_adjust, p_names,p)
#         target_ymo_vac = ymo_vac .* sum(vaccination_data[1:end]) .* length.(model.index_vectors)
#         ymo_vaccination_ts = output.daily_immunized_by_age
#         total_postinf_vaccination = sum.(eachrow(ymo_vaccination_ts[:,180:end]))

#         final_size = sum.(eachrow(output.daily_cases_by_age))
#         target_final_size = ymo_attack_rate .* length.(model.index_vectors)
#         # display( length.(model.index_vectors))
#         # display((final_size,target_final_size))
#         # display((total_postinf_vaccination,target_ymo_vac))
#         # display((1e-1*sum((total_postinf_vaccination .- target_ymo_vac).^2) , sum((final_size .- target_final_size).^2)))
#         return 1e-2*sum((total_postinf_vaccination .- target_ymo_vac).^2)   + sum((final_size .- target_final_size).^2)
#     end

#     # display(cost([0.000,0.001,0.001,1.0]))
#     out =smc(priors,cost; verbose = true, nparticles = 400, parallel = true)# ABCDE(priors,cost,1e6; verbose=true, nparticles=300,generations=1000,  parallel = true) #this one has better NaN handling
#     return NamedTuple{p_names}(ntuple(i -> out.P[i].particles,length(p_names)))
# end
Peter Jentsch's avatar
Peter Jentsch committed
88

89
function fit_all_parameters(p_tuple)
Peter Jentsch's avatar
Peter Jentsch committed
90
    p_names = (:ω,:β_y,:β_m,:β_o,:π_base_y,:π_base_m,:π_base_o,:α_y,:α_m,:α_o)
91
    priors = Factored(
Peter Jentsch's avatar
Peter Jentsch committed
92
93
        Uniform(0.0,0.01),
        Uniform(0.0,0.2),
94
95
        Uniform(0.0,0.1),
        Uniform(0.0,1.0),
Peter Jentsch's avatar
Peter Jentsch committed
96
97
        Uniform(-5.0,0.0),
        Uniform(-5.0,0.0),
98
        Uniform(-5.0,5.0),
Peter Jentsch's avatar
Peter Jentsch committed
99
100
101
        Uniform(0.0,1.0),
        Uniform(0.0,1.0),
        Uniform(0.0,1.0),
102
103
104
105
106
107
108
109
    )
    #simulation begins in july
    #60 days for opinion dynamics to stabilize, then immunization begins in september,
    #infection introduced at beginning of december
    sim_length = 300
    p_tuple_adjust = merge(p_tuple,
        (
            sim_length = sim_length,
Peter Jentsch's avatar
Peter Jentsch committed
110
            I_0_fraction = 0.005,
111
112
113
114
115
116
117
118
119
            immunization_begin_day =60, 
            infection_introduction_day = 180,
            immunizing = true,
        )
    )
    function cost(p)
        output,model = solve_w_parameters(p_tuple_adjust, p_names,p)
        target_ymo_vac = ymo_vac .* sum(vaccination_data[1:end]) .* length.(model.index_vectors)
        ymo_vaccination_ts = output.daily_immunized_by_age
Peter Jentsch's avatar
Peter Jentsch committed
120
121

        normalize_by_pop(v) = v./length.(model.index_vectors)
122
123
        total_postinf_vaccination = sum.(eachrow(ymo_vaccination_ts[:,180:end]))

Peter Jentsch's avatar
Peter Jentsch committed
124
        final_size = sum.(eachrow(output.daily_unvac_cases_by_age))
125
126
127

        target_final_size = ymo_attack_rate .* length.(model.index_vectors)
        target_ymo_vac = ymo_vac .* sum(vaccination_data[1:4]) .* length.(model.index_vectors)
Peter Jentsch's avatar
Peter Jentsch committed
128

129
130
        ymo_vaccination_ts = output.daily_immunized_by_age
        total_preinfection_vaccination = sum.(eachrow(ymo_vaccination_ts))
Peter Jentsch's avatar
Peter Jentsch committed
131
132
133
        return sum((normalize_by_pop(total_preinfection_vaccination .- target_ymo_vac)).^2)  
        + sum(normalize_by_pop((total_postinf_vaccination .- target_ymo_vac)).^2)  
        + 2*sum(normalize_by_pop((final_size .- target_final_size)).^2)
134
135
    end

Peter Jentsch's avatar
Peter Jentsch committed
136
137
    # display(cost(rand(priors)))
    out = smc(priors,cost; verbose = true, nparticles = 1000, parallel = true)#this one has better NaN handling
138
139
140
    return NamedTuple{p_names}(ntuple(i -> out.P[i].particles,length(p_names)))
end

Peter Jentsch's avatar
Peter Jentsch committed
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# function plot_behavioural_fit(particles,p_tuple)
#     p_names = (:π_base_y,:π_base_m,:π_base_o)
#     sim_length = 210
#     samples = 1
#     p_tuple_adjust = merge(p_tuple,
#         (
#             sim_length = sim_length,
#             I_0_fraction = 0.000,
#             immunization_begin_day =60, 
#             infection_introduction_day = 180,
#             immunizing = true,
#         )
#     )
#     p = map(e -> mode(e.particles),particles.P)
#     display(p)
#     new_params = merge(p_tuple_adjust, NamedTuple{p_names}(ntuple(i -> p[i],length(p_names))))
#     out = mean_solve(samples, new_params ,DebugRecorder)
#     target_cumulative_vac_proportion = 0.33
#     vaccination_data = @SVector [0.0,0.043,0.385,0.424,0.115,0.03,0.005] #by month starting in august
#     ymo_vac = @SVector [0.255,0.278,0.602]
#     ymo_vaccination_ts = mean.(out.daily_immunized_by_age)
#     total_preinfection_vaccination = sum.(eachrow(ymo_vaccination_ts))
#     display(total_preinfection_vaccination)
#     p = [plot(),plot(),plot()]

#     p = plot_model(nothing,[nothing],[out],new_params.infection_introduction_day,new_params.immunization_begin_day)
#     savefig(p,"behaviour_fit.pdf")
#     return out
# end
170
171
172
173
174
# outbreak_transmission_dist = CovidAlertVaccinationModel.fit_epi_parameters(default_parameters,0.241) ##outbreak
# serialize(joinpath(PACKAGE_FOLDER,"abm_parameter_fits","outbreak_inf_parameters.dat"),outbreak_transmission_dist)
# plot_max_posterior("outbreak", outbreak_transmission_dist,default_parameters)

function fit_parameters(default_parameters)
Peter Jentsch's avatar
Peter Jentsch committed
175
176
    # pre_inf_behaviour_parameters_path =joinpath(PACKAGE_FOLDER,"abm_parameter_fits","pre_inf_behaviour_parameters.dat")
    # post_inf_behaviour_parameters_path = joinpath(PACKAGE_FOLDER,"abm_parameter_fits","post_inf_behaviour_parameters.dat")
177
    fit_all_parameters_path = joinpath(PACKAGE_FOLDER,"abm_parameter_fits","fit_all_parameters.dat")
178

Peter Jentsch's avatar
Peter Jentsch committed
179
180
    # output = fit_all_parameters(default_parameters)
    # serialize(fit_all_parameters_path,output)
181
182
183

    
    fitted_parameter_tuple = deserialize(fit_all_parameters_path)
Peter Jentsch's avatar
Peter Jentsch committed
184
185
186
187
188
189
    fitted_sol,avg_populations = plot_fitting_posteriors("post_inf_fitting",fitted_parameter_tuple,default_parameters)

    final_vac = sum.(eachrow(mean.(fitted_sol.daily_immunized_by_age)))
    final_size = sum.(eachrow(mean.(fitted_sol.daily_unvac_cases_by_age)))
    display(final_vac./avg_populations)
    display(final_size./avg_populations)
190
    return fitted_sol
191
192
end 

Peter Jentsch's avatar
Peter Jentsch committed
193
194
195
196
197
198
199
200
201
202
203
# function plot_max_posterior(fname,particles,parameters)
#     samples = 5
#     base_transmission = mode(particles.base_transmission_probability)
#     p_tuple_without_vac = merge(parameters,
#         (
#             sim_length = 150,
#             immunization_begin_day = 0,
#             infection_introduction_day = 1,
#             immunizing = false,
#         )
#     )
204
    
Peter Jentsch's avatar
Peter Jentsch committed
205
206
207
208
209
210
211
212
213
#     new_params = merge(p_tuple_without_vac, (base_transmission_probability = base_transmission,))
#     out,_ = mean_solve(samples, new_params ,DebugRecorder)
#     p = plot_model(nothing,[nothing],[out],new_params.infection_introduction_day,new_params.immunization_begin_day)
#     savefig(p,"$fname.pdf")
#     hist = StatsBase.fit(Histogram,particles.base_transmission_probability; nbins = 25)
#     p = plot(hist;legend = false)
#     savefig(p,"$(fname)_posterior.pdf")

# end
214
using PairPlots
215
function plot_fitting_posteriors(fname,particles_tuple,parameters)
Peter Jentsch's avatar
Peter Jentsch committed
216
217
218
    p = merge(parameters,map(mean,particles_tuple))
    # p_adjust = merge(parameters,(β_y = p.β_y*0.1, β_m = p.β_m*0.1 ,β_o = p.β_o)) 
    out,avg_populations = mean_solve(5, p,DebugRecorder)
219
220
221
222
223
    p = plot_model(nothing,[nothing],[out],parameters.infection_introduction_day,parameters.immunization_begin_day)
    savefig(p, "$fname.pdf")
    
    plts = [plot() for i in 1:length(particles_tuple)]
    for (plt,(k,v)) in zip(plts,pairs(particles_tuple))
224
        hist = StatsBase.fit(Histogram,v; nbins = 30)
225
226
        plot!(plt,hist;legend = false,xlabel = k)            
    end
227
    p = plot(plts...; size = (1400,800),bottommargin = 5Plots.mm)
228
    savefig(p,"$(fname)_posteriors.pdf")
Peter Jentsch's avatar
Peter Jentsch committed
229
    return out,avg_populations
230
end
231

232
# function visualize_π_base(particles_tuple)
233

234
235
236
#     param_keys = [:π_base_y,:π_base_m,:π_base_o]
#     # π_bases_array = map(f -> getproperty(particles_tuple,f),param_keys) |> l -> mapreduce(t-> [t...],hcat,zip(l...))
#     # display(π_bases_array)
237

238
239
240
241
242
#     # p = cornerplot(π_bases_array'; labels = string.(param_keys))
#     params = NamedTuple{(param_keys...,)}(particles_tuple)
#     # display(params)
#     p = corner(params)
#     display(p)
243
244


245
246
#     # display(scatter(map(f -> getproperty(particles_tuple,f),param_keys)...))
# end