1
+ """
2
+ prior_probs(model::Model, parm_grid)
3
+
4
+ Computes a grid of prior probabilities over model parameters.
5
+
6
+ - `model::Model`: a model object
7
+ - `parm_grid`: a grid of parameter values
8
+ """
1
9
function prior_probs (model:: Model , parm_grid)
2
10
return prior_probs (model. prior, parm_grid)
3
11
end
4
12
13
+ """
14
+ prior_probs(prior, parm_grid)
15
+
16
+ Computes a grid of prior probabilities over model parameters.
17
+
18
+ - `prior`: a vector of `Distribution` objects, one for each parameter
19
+ - `parm_grid`: a grid of parameter values
20
+ """
5
21
function prior_probs (prior, parm_grid)
6
22
dens = [mapreduce ((θ,d)-> pdf (d, θ), * , g, prior) for g in parm_grid]
7
23
return dens/ sum (dens)
8
24
end
9
25
26
+ """
27
+ prior_probs(prior, parm_grid)
28
+
29
+ Computes a grid of uniform prior probabilities over model parameters.
30
+
31
+ - `prior::Nothing`: a vector of `Distribution` objects, one for each parameter
32
+ - `parm_grid`: a grid of parameter values
33
+ """
10
34
function prior_probs (prior:: Nothing , parm_grid)
11
35
return fill (1 / length (parm_grid), size (parm_grid))
12
36
end
13
37
38
+ """
39
+ loglikelihood(model::Model, design_grid, parm_grid, data_grid, model_type, model_state)
40
+
41
+ Computes a three dimensional grid of log likelihoods where the first dimension is the model parameters, the second
42
+ dimension is the design parameters, and the third dimension is the data values.
43
+
44
+ - `model::Model`: a model object containing a log likelihood function and prior distributions
45
+ - `parm_grid`: a grid of model parameters
46
+ - `design_grid`: a grid of design parameters
47
+ - `data_grid`: a grid of data values
48
+ - `model_type`: model type can be `Static` or `Dynamic`
49
+ - `model_state`: model state variables that are updated for `Dynamic` models
50
+ """
14
51
function loglikelihood (model:: Model , design_grid, parm_grid, data_grid, model_type, model_state)
15
52
return loglikelihood (model. loglike, design_grid, parm_grid, data_grid)
16
53
end
17
54
55
+ """
56
+ loglikelihood(model::Model, design_grid, parm_grid, data_grid, model_type, model_state)
57
+
58
+ Computes a three dimensional grid of log likelihoods where the first dimension is the model parameters, the second
59
+ dimension is the design parameters, and the third dimension is the data values.
60
+
61
+ - `model::Model`: a model object containing a log likelihood function and prior distributions
62
+ - `parm_grid`: a grid of model parameters
63
+ - `design_grid`: a grid of design parameters
64
+ - `data_grid`: a grid of data values
65
+ - `model_type`: model type can be `Static` or `Dynamic`
66
+ - `model_state`: model state variables that are updated for `Dynamic` models
67
+ """
18
68
function loglikelihood (model:: Model , design_grid, parm_grid, data_grid, model_type:: Dyn , model_state)
19
69
return loglikelihood (model. loglike, design_grid, parm_grid, data_grid, model_state)
20
70
end
21
71
72
+ """
73
+ loglikelihood(model::Model, design_grid, parm_grid, data_grid, model_type, model_state)
74
+
75
+ Computes a three dimensional grid of log likelihoods where the first dimension is the model parameters, the second
76
+ dimension is the design parameters, and the third dimension is the data values.
77
+
78
+ - `loglike`: a model object containing a log likelihood function and prior distributions
79
+ - `parm_grid`: a grid of model parameters
80
+ - `design_grid`: a grid of design parameters
81
+ - `data_grid`: a grid of data values
82
+ - `model_type`: model type can be `Static` or `Dynamic`
83
+ - `model_state`: model state variables that are updated for `Dynamic` models
84
+ """
22
85
function loglikelihood (loglike, design_grid, parm_grid, data_grid)
23
86
LLs = zeros (length (parm_grid), length (design_grid), length (data_grid))
24
87
for (d, data) in enumerate (data_grid)
@@ -31,25 +94,61 @@ function loglikelihood(loglike, design_grid, parm_grid, data_grid)
31
94
return LLs
32
95
end
33
96
97
+ """
98
+ loglikelihood(model::Model, design_grid, parm_grid, data_grid, model_type, model_state)
99
+
100
+ Computes a three dimensional grid of log likelihoods for a dynamic model where the first dimension is the model parameters,
101
+ the second dimension is the design parameters, and the third dimension is the data values.
102
+
103
+ - `loglike`: a model object containing a log likelihood function and prior distributions
104
+ - `parm_grid`: a grid of model parameters
105
+ - `design_grid`: a grid of design parameters
106
+ - `data_grid`: a grid of data values
107
+ - `model_type`: model type can be `Static` or `Dynamic`
108
+ - `model_state`: model state variables that are updated for `Dynamic` models
109
+ """
34
110
function loglikelihood (loglike, design_grid, parm_grid, data_grid, model_state)
35
111
LLs = zeros (length (parm_grid), length (design_grid), length (data_grid))
36
112
i = 0
37
113
for (d, data) in enumerate (data_grid)
38
114
for (k,design) in enumerate (design_grid)
39
115
for (p,parms) in enumerate (parm_grid)
40
116
i += 1
41
- LLs[p,k,d] = loglike (parms... , design... , data... , model_state[i])
117
+ state = deepcopy (model_state[i])
118
+ LLs[p,k,d] = loglike (parms... , design... , data... , state)
42
119
end
43
120
end
44
121
end
45
122
return LLs
46
123
end
47
124
125
+ """
126
+ loglikelihood(optimizer::Optimizer)
127
+
128
+ Computes a three dimensional grid of log likelihoods for a dynamic model where the first dimension is the model parameters,
129
+ the second dimension is the design parameters, and the third dimension is the data values.
130
+
131
+ - `optimizer`: an optimizer object
132
+
133
+ """
48
134
function loglikelihood! (optimizer)
49
135
@unpack model, log_like, design_grid, parm_grid, data_grid, model_state = optimizer
50
136
return loglikelihood! (model. loglike, log_like, design_grid, parm_grid, data_grid, model_state)
51
137
end
52
138
139
+ """
140
+ loglikelihood(optimizer::Optimizer)
141
+
142
+ Computes a three dimensional grid of log likelihoods where the first dimension is the model parameters, the second
143
+ dimension is the design parameters, and the third dimension is the data values.
144
+
145
+ - `loglike`: a model object containing a log likelihood function and prior distributions
146
+ - `parm_grid`: a grid of model parameters
147
+ - `design_grid`: a grid of design parameters
148
+ - `data_grid`: a grid of data values
149
+ - `model_type`: model type can be `Static` or `Dynamic`
150
+ - `model_state`: model state variables that are updated for `Dynamic` models
151
+ """
53
152
function loglikelihood! (loglike, log_like, design_grid, parm_grid, data_grid, model_state)
54
153
i = 0
55
154
for (d, data) in enumerate (data_grid)
@@ -64,18 +163,48 @@ function loglikelihood!(loglike, log_like, design_grid, parm_grid, data_grid, mo
64
163
return nothing
65
164
end
66
165
166
+ """
167
+ marginal_log_like!(optimizer)
168
+
169
+ Marginalizes the log likelihoods over model parameters, resulting in a three dimensional array where the first dimension is length 1,
170
+ the second dimension is the design parameters, and the third dimension is the data values.
171
+
172
+ - `optimizer`: an optimizer object
173
+ """
67
174
function marginal_log_like! (optimizer)
68
175
@unpack marg_log_like,log_like,log_post = optimizer
69
176
marg_log_like .= marginal_log_like (log_post, log_like)
70
177
end
71
178
179
+ """
180
+ marginal_log_like(optimizer)
181
+
182
+ Marginalizes the log likelihoods over model parameters, resulting in a three dimensional array where the first dimension is length 1,
183
+ the second dimension is the design parameters, and the third dimension is the data values.
184
+
185
+ - `log_post`: a vector of posterior log likelihood values for the model parameters
186
+ - `log_like`: a three dimensional vector of log likelihood values where the first dimension corresponds to the model parameters,
187
+ the second dimension corresponds to the design parameters, and the third dimension corresponds to the data values
188
+ """
72
189
function marginal_log_like (log_post, log_like)
73
190
return logsumexp (log_post .+ log_like, dims= 1 )
74
191
end
75
192
76
- function marginal_posterior (optimizer)
77
- @unpack posteriors = optimizer
78
- return map (d-> sum (posterior, dims= d), ndims (posterior): - 1 : 1 )
193
+ # """
194
+ # marginal_posterior(optimizer)
195
+
196
+ # Computes a three dimensional grid of log likelihoods where the first dimension is the model parameters, the second
197
+ # dimension is the design parameters, and the third dimension is the data values.
198
+
199
+ # - `optimizer`: an optimizer object
200
+ # """
201
+ # function marginal_posterior(optimizer)
202
+ # @unpack posteriors = optimizer
203
+ # return map(d->sum(posterior, dims=d), ndims(posterior):-1:1)
204
+ # end
205
+
206
+ function compute_entropy (log_like)
207
+ return - 1 * sum (exp .(log_like) .* log_like, dims= 3 )[:,:]
79
208
end
80
209
81
210
function conditional_entropy (entropy, post)
@@ -88,10 +217,6 @@ function conditional_entropy!(optimizer)
88
217
cond_entropy .= conditional_entropy (entropy, post)
89
218
end
90
219
91
- function compute_entropy (log_like)
92
- return - 1 * sum (exp .(log_like) .* log_like, dims= 3 )[:,:]
93
- end
94
-
95
220
function marginal_entropy! (optimizer:: Optimizer )
96
221
@unpack marg_entropy,marg_log_like = optimizer
97
222
marg_entropy .= marginal_entropy (marg_log_like)
@@ -101,10 +226,25 @@ function marginal_entropy(marg_log_like)
101
226
return - sum (exp .(marg_log_like).* marg_log_like, dims= 3 )[:]
102
227
end
103
228
229
+ """
230
+ mutual_information(optimizer)
231
+
232
+ Computes a vector of the mutual information values where each element corresponds to a design parameter.
233
+
234
+ - `marg_entropy`: a vector of marginal entropy values where each element corresponds to a design parameter
235
+ - `cond_entropy`: a vector of conditional entropy values where each element corresponds to a design parameter
236
+ """
104
237
function mutual_information (marg_entropy, cond_entropy)
105
238
return marg_entropy .- cond_entropy
106
239
end
107
240
241
+ """
242
+ mutual_information!(optimizer)
243
+
244
+ Computes a vector of the mutual information values where each element corresponds to a design parameter.
245
+
246
+ - `optimizer`: an optimizer object
247
+ """
108
248
function mutual_information! (optimizer)
109
249
@unpack mutual_info,marg_entropy,cond_entropy = optimizer
110
250
mutual_info .= mutual_information (marg_entropy, cond_entropy)
249
389
250
390
function create_state (model_type:: Stat , T, dims, args... ; kwargs... )
251
391
return nothing
252
- end
392
+ end
393
+
394
+ # function update(θ1, θ2, p, data)
395
+ # #like = pdf.(Binomial.(n, x), k)
396
+ # like = pdf.(Bernoulli.(x), d)
397
+ # for d in data
398
+ # θ1 .*= like
399
+ # θ2 .*= like
400
+ # end
401
+
402
+ # # e1 = θ1'*like
403
+ # # e2 = θ2'*like
404
+
405
+ # # θ1 .= (θ1 .* like)/e1
406
+ # # θ2 .= (θ2 .* like)/e2
407
+ # e1 = sum(θ1)
408
+ # e2 = sum(θ2)
409
+ # θ1 ./= e1
410
+ # θ2 ./= e2
411
+
412
+ # post_odds = (p * e1) / ((1 - p) * e2)
413
+ # p = post_odds/(1 + post_odds)
414
+ # return p, θ1, θ2
415
+ # end
416
+
417
+ # # function update_model_posterior(ps, es)
418
+ # # map(i->ps[i]./(sum(ps.*(es./es[i]))),1:length(ps))
419
+ # # end
420
+
421
+ # using Distributions
422
+ # p = .5
423
+ # x = .001:.001:.999
424
+ # θ1 = pdf.(Beta(10,1), x)
425
+ # θ1 /= sum(θ1)
426
+
427
+ # θ2 = pdf.(Beta(1,1), x)
428
+ # θ2 /= sum(θ2)
429
+
430
+ # p, θ1, θ2 = update(θ1, θ2, p, [1])
0 commit comments