def get_sim(type, output, dh=None): model = system.get_model() if type == 'h**o': model.collapse(['ii', 'ip']) name = '$G = 1$ (Homogeneous)' dt = 0.2 if type == 'zeta': dhmax = 1.0 / model.params['mu'] model.params['dur'].update( [dh, min(dh * 5, dhmax), min(dh * 30, dhmax)]) # z1 = (1/model.params['dur'].iselect(ii='H',ki='M') - model.params['mu'])/2 # z2 = (1/model.params['dur'].iselect(ii='M',ki='M') - model.params['mu'])/2 # z3 = (1/model.params['dur'].iselect(ii='L',ki='M') - model.params['mu'])/2 # model.params['zeta'].update(z1,ii='H',ip='M') # model.params['zeta'].update(z1,ii='H',ip='L') # model.params['zeta'].update(z2,ii='M',ip='L') # model.params['zeta'].update(z2,ii='M',ip='H') # model.params['zeta'].update(z3,ii='L',ip='M') # model.params['zeta'].update(z3,ii='L',ip='H') model.params['zeta'].update(np.nan) # model.params['pe'].update(np.nan) name = '$G = 3, \\delta_H = {:.03f}$'.format(model.params['dur'][0, 0]) dt = min(dh * 0.8, 0.2) t = system.get_t(dt=dt, tmin=0, tmax=200) sim = system.get_simulation(model, t=t) sim.init_outputs( system.get_outputs( spaces=sim.model.spaces, select=sim.model.select, t=sim.t, names=[output], )) return sim, name
def run_fit(): t = system.get_t(tmax=500) sims = odict([ ('Turnover', get_sim('full', t=t)), ('No Turnover', get_sim('no-turnover', t=t)), ]) for name, sim in sims.items(): sim.init_outputs( system.get_outputs(spaces=sim.model.spaces, select=sim.model.select, t=sim.t, names=['prevalence'])) sim.solve() targets = system.get_targets( sim.model.spaces, sim.model.select, sim.outputs, t=sim.t, ) if config.model == 'mort': sim.model.params['C'].update([25, 10, 5]) # HACK to get low prev > 0 calsim = calibration.CalibrationSim( name, sim=sim, targets=targets, verbose=True, ) calsim.optimize(ftol=1e-3, plot='tmp.png') if config.save: utils.savejson(fname_fit(name), calsim.fitted_params().todict()) else: print(dict(calsim.fitted_params().todict()))
def run_sim(sim, outputs=None): outputs = outputs if outputs is not None else [] sim.init_outputs( system.get_outputs(spaces=sim.model.spaces, select=sim.model.select, t=sim.t, names=outputs)) return sim.solve()
def make_figs(): phis = list(sensitivity.iter_phi()) for label,phi in [ ('low', phis[n]), ('med', phis[int((config.N-1)/2)]), ('high', phis[config.N-n-1]), ('extreme',10), ]: specs = system.get_specs() model = system.get_model() sim = sensitivity.get_sim(phi,0.1) sim.init_outputs(system.get_outputs( spaces = sim.model.spaces, select = sim.model.select, t = sim.t, names = [out] )) if label == 'extreme': sim.update_params(dict(ibeta=0.038)) sim.solve() for name in ['high','low']: make_pie(sim,name,phi) make_tikz(label,phi)
def get_sim(phi, tau, tmax=200): model = system.get_model() dmax = 1.0 / model.params['mu'] dh = 1 / phi model.params['dur'].update(dh, ii=['H']) model.params['dur'].update(dh + 0.30 * (dmax - dh), ii=['M']) model.params['dur'].update(np.nan, ii=['L']) p1 = (1 / model.params['dur'].iselect(ii=['H'], ki=['M']) - model.params['mu']) / 2 model.params['phi'].update(p1, ii=['H'], ip=['M']) model.params['phi'].update(p1, ii=['H'], ip=['L']) model.params['tau'].update(tau) dt = min(dh * 0.8, 0.5) t = system.get_t(dt=dt, tmin=0, tmax=tmax) sim = system.get_simulation(model, t=t) sim.init_outputs( system.get_outputs( spaces=sim.model.spaces, select=sim.model.select, t=sim.t, names=OUTPUTS + ['C', 'X', 'tip'], )) return sim
# define true homogeneous model models['h**o'].collapse(['ii']) # define high zeta model models['zeta'].params['pe'].update(np.nan) models['zeta'].params['dur'].update([0.01, 0.05, 0.8]) models['zeta'].params['zeta'].update(np.nan, ii='H', ip='L') sims = odict([ ('Homogeneous', system.get_simulation(models['h**o'], dt=dt)), ('High Turnover', system.get_simulation(models['zeta'], dt=dt)), ]) for (name, sim), clr in zip(sims.items(), [[1, 0, 0], [0, 0, 1]]): sim.init_outputs( system.get_outputs( spaces=sim.model.spaces, select=sim.model.select, t=system.get_t(dt=dt), names=[output], )) for param in ['pe', 'dur', 'zeta']: print('{} = \n{}'.format(param, np.around(sim.model.params[param], 6)), flush=True) sim.solve(eqfun=lambda s, t: print(t, flush=True)) y = modelutils.taccum(sim.outputs[output].iselect(sim.model.select['all'])) plt.plot(sim.t, y, color=clr) sim = None plt.ylim([0, 0.1]) plt.ylabel('Prevalence (overall)') plt.xlabel('t (years)') plt.legend(sims.keys()) plt.savefig('infinite-zeta-tmp.eps')