Best way to run many replicates while drawing parameters from a prior distribution? #1834
Replies: 3 comments
-
One option would be to make the code a function that takes the parameters and outputs the statistics of interest:
and then do e.g.
|
Beta Was this translation helpful? Give feedback.
-
Maybe not exactly an answer to the question but I have used the "multiprocessing" python library as follows: as @petrelharp suggested define a function to simulate your model and compute the statistics, e.g.
where input_a is the parameter value I am computing over ( note, can be multi.dimensional) :
the set it up as follows:
this works on a laptop/desktop with several cpus with the imports
|
Beta Was this translation helpful? Give feedback.
-
The wrapper function pattern is also appropriate when doing optimisation over the parameters, eg. using def run_sims(modern_size, ancestral_size, split_time):
# As above.
...
def loss(sim_out, observed=3.14152):
# E.g. minimise squared distance to empirical data.
return (sim_out - observed)**2
def opt_func(x):
modern_size, ancestral_size, split_time = x
out = run_sims(modern_size, ancestral_size, split_time)
return loss(out)
import scipy.optimize
x0 = 300, 1000, 500 # initial guess for params
res = scipy.optimize.minimize(opt_func, x0)
if res.success:
# Best fit params are now in `res.x`. Note: this might be a local optima!
print(res.x)
else:
print("optimisation failed") |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Let's say I want to run a model across a range of parameter values (eg for ABC inference). Modifying an example from the manual:
let's say I want to run this model and extract statistics either across a grid of parameter values, or by drawing the parameters from a prior. For instance:
What's the best way to do this?
Beta Was this translation helpful? Give feedback.
All reactions