def main(): seed = 2 random = numpy.random.RandomState(seed) sigma = 0.1 ntimes = 20 tspan = numpy.linspace(0, 40, ntimes) solver = pysb.integrate.Solver(model, tspan) solver.run() ydata = solver.y * (random.randn(*solver.y.shape) * sigma + 1) ysim_max = solver.y.max(0) ydata_norm = ydata / ysim_max opts = bayessb.MCMCOpts() opts.model = model opts.tspan = tspan # estimate rates only (not initial conditions) from wild guesses opts.estimate_params = [ p for p in model.parameters if p.name.startswith('k') ] opts.initial_values = [1e-4, 1e3, 1e6] opts.nsteps = 10000 opts.likelihood_fn = functools.partial(likelihood, data=ydata_norm, scale_factor=ysim_max, sigma=sigma) opts.prior_fn = prior opts.step_fn = step opts.use_hessian = True opts.hessian_period = opts.nsteps / 10 opts.seed = seed mcmc = bayessb.MCMC(opts) mcmc.run() mixed_nsteps = opts.nsteps / 2 mixed_positions = mcmc.positions[-mixed_nsteps:] mixed_accepts = mcmc.accepts[-mixed_nsteps:] mixed_accept_positions = mixed_positions[mixed_accepts] marginal_mean_pos = numpy.mean(mixed_accept_positions, 0) # position is far from marginal mean, but posterior is good (determined by # trial and error and some interactive plotting) interesting_step = 8830 print "\nGenerating figures..." # show scatter plot scatter(mcmc, opts.nsteps / 2, mcmc.positions[interesting_step], marginal_mean_pos) # show prediction for C trajectory, which was not fit to prediction(mcmc, opts.nsteps / 2, 2, ysim_max[2], sigma, plot_samples=True) plt.title("Prediction for C") # show "true" trajectories and noisy data data(mcmc, ydata_norm, ysim_max, [0, 1]) plt.title("True trajectories and noisy data") # show all plots at once plt.show()
def do_fit(iteration): """Runs MCMC on the globally defined model.""" def likelihood(mcmc, position): yout = mcmc.simulate(position, observables=True) err = np.sum((synthetic_data['A_'] - yout['A_'])**2 / (2 * sigma**2)) return err # Initialize the MCMC arguments opts = bayessb.MCMCOpts() opts.model = model opts.tspan = tspan # Because there is a degradation reaction, there is a __source_0 # parameter in the model that we need to ignore opts.estimate_params = [ p for p in model.parameters if p.name != '__source_0' ] # Choose MCMC start values randomly from [0, 10) opts.initial_values = np.random.uniform(0, 10, 2) opts.nsteps = nsteps opts.likelihood_fn = likelihood opts.step_fn = step opts.use_hessian = True opts.hessian_period = opts.nsteps / 10 opts.seed = random_seed mcmc = bayessb.MCMC(opts) mcmc.run() # Pickle it! basename = 'convergence_test' output_basename = '%s_%d_steps_seed_%d_iter_%d' % \ (basename, opts.nsteps, random_seed, iteration) mcmc.options.likelihood_fn = None output_file = open('%s.pck' % output_basename, 'w') pickle.dump(mcmc, output_file) output_file.close() # Show best fit params mcmc.position = mcmc.positions[np.argmin(mcmc.likelihoods)] best_fit_params = mcmc.cur_params(position=mcmc.position) p_name_vals = zip([p.name for p in model.parameters], best_fit_params) print('\n'.join([ '%s: %g' % (p_name_vals[i][0], p_name_vals[i][1]) for i in range(0, len(p_name_vals)) ])) return mcmc
ydata = numpy.load(data_filename1) yvar = numpy.load(data_filename2) print ydata[:,1:4] ydata_norm = normalize(ydata[:,1:4]) print ydata_norm exp_var = normalize_var(ydata[:,1:4], yvar[:,0:3]) #Zinkel's data is averaged with n=3. # There are too few time points. Therefore, the solver will be inaccurate. # Instead, I will allow the model to run 4320 time points per every 36 hours (1000 per 30000s). timepoints = ydata[:,0]*3600 #[hrs] t_end = timepoints.max(0) tspan = numpy.linspace(0, t_end, 3000) obs_names = ['Obs_pC3'] opts = bayessb.MCMCOpts() opts.model = model opts.tspan = tspan opts.protien_conctrations = initial_concentrations opts.experim_conditions = {'WT':[], 'BIDKO':['Bid_0'], 'DKO':['Bax_0', 'Bak_0'], 'TKO':['Bid_0', 'Bak_0', 'Bax_0']} # values for prior calculation opts.nsteps = 10000 opts.likelihood_fn = likelihood opts.prior_fn = prior opts.step_fn = step opts.seed = 1 opts.atol=1e-6 opts.rtol=1e-3
def build_opts(): global parp_initial, exp_ecrp, cparp_sim_norm, exp_ecrp_var, plateau_idx global prior_mean, prior_var # instantiate an instance of our model model = Model() # EC-RP trajectory (experimental data, arbitrary units) exp_ecrp = np.array([ 0.4820, 0.4540, 0.4500, 0.4430, 0.4440, 0.4420, 0.4430, 0.4470, 0.4390, 0.4490, 0.4450, 0.4450, 0.4390, 0.4410, 0.4370, 0.4450, 0.4470, 0.4390, 0.4430, 0.4410, 0.4390, 0.4410, 0.4420, 0.4430, 0.4490, 0.4420, 0.4460, 0.4480, 0.4500, 0.4420, 0.4460, 0.4450, 0.4520, 0.4480, 0.4520, 0.4460, 0.4470, 0.4500, 0.4500, 0.4470, 0.4490, 0.4570, 0.4500, 0.4530, 0.4550, 0.4510, 0.4560, 0.4560, 0.4600, 0.4530, 0.4540, 0.4510, 0.4590, 0.4570, 0.4490, 0.4510, 0.4640, 0.4580, 0.4560, 0.4610, 0.4620, 0.4600, 0.4590, 0.4640, 0.4640, 0.4650, 0.4750, 0.4760, 0.4760, 0.4780, 0.4760, 0.4810, 0.4830, 0.4930, 0.4870, 0.4890, 0.4890, 0.5180, 0.6120, 0.7280, 0.8050, 0.9120, 0.9570, 0.9980, 1.025, 1.025, 1.068, 1.074, 1.061, 1.056, 1.049, 1.064, 1.092, 1.086, 1.044, 1.023, 1.062, 1.058, 1.077, 1.081, 1.068, 1.066, 1.078, 1.103, 1.116, 1.099, 1.118, 1.087, 1.086, 1.066, 1.076, 1.075, 1.069, 1.080, 1.077, 1.086, 1.079, 1.088, 1.084, 1.092, 1.088, 1.099, 1.093, 1.108, 1.101, 1.104, 1.106, 1.099, 1.110, 1.102, 1.104, 1.111, 1.113, 1.118, 1.117, 1.118, 1.114, 1.131, 1.140, 1.131, 1.135, 1.143, 1.144, 1.146, 1.145, 1.146, 1.137, 1.149, 1.164, 1.157, 1.150, 1.148, 1.151, 1.164, 1.148, 1.157, 1.163, 1.141, 1.152, 1.151, 1.159, 1.164, 1.154, 1.152, 1.141, 1.143, 1.163, 1.158, 1.152, 1.144, 1.151, 1.156, 1.154, 1.151, 1.150, 1.148, 1.150, 1.150, 1.147, 1.148, 1.161, 1.154, 1.160, 1.158, 1.160, 1.140, 1.149, 1.152, 1.161, 1.152, 1.158, 1.156, 1.154, 1.156, 1.157, 1.148, 1.156, 1.159, 1.160, 1.160, 1.150, 1.153, 1.163, 1.156, 1.171, 1.147, 1.166, 1.155, 1.159, 1.163, 1.157, 1.154, 1.154, 1.159, 1.151, 1.162, 1.167, 1.166, 1.159, 1.160, 1.170, 1.165, 1.171, 1.179, 1.164, 1.176, 1.175, 1.178, 1.175, 1.170, 1.178, 1.173, 1.168, 1.168, 1.169, 1.174, 1.177, 1.178, 1.179, 1.181, 1.182 ]) # index for beginning and end of MOMP (where the EC-RP signal spikes) momp_start_idx = 75 momp_end_idx = 86 # data was collected at evenly-spaced points over 12 hours tspan = np.linspace(0, 12 * 3600, len(exp_ecrp)) # select the forward/reverse/catalytic parameters for estimation kf = filter_params(model, 'kf') kr = filter_params(model, 'kr') kc = filter_params(model, 'kc') estimate_params = sort_params(model, kf + kr + kc) # grab the initial amount of parp, for normalizing CPARP trajectories parp_initial = [p.value for p in model.parameters if p.name == 'PARP_0'][0] prior_mean, prior_var = calculate_prior_stats(estimate_params, kf, kr, kc) # clean up the noise in the latter part of the EC-RP signal and rescale to 0-1 # ---------- # shift the range down to begin at 0 exp_ecrp -= exp_ecrp.min() # take discrete difference ecrp_dd = np.diff(exp_ecrp) # get first index after momp start where the signal drops (i.e. dd is negative) plateau_idx = [ i for i in range(momp_start_idx, len(ecrp_dd)) if ecrp_dd[i] < 0 ][0] # rescale the portion before that index to a max of 1 exp_ecrp[:plateau_idx] /= exp_ecrp[plateau_idx] # clamp the latter portion directly to 1 exp_ecrp[plateau_idx:] = 1.0 # set up a vector of variance values for the ecrp signal exp_ecrp_var = np.empty_like(tspan) # start with a single value for the variance at all time points exp_ecrp_var[:] = 0.0272 # use a higher value for the switching window to reflect less certainty there exp_ecrp_var[momp_start_idx:momp_end_idx + 1] = 0.1179 # set up our MCMC options opts = bayessb.MCMCOpts() opts.model = model opts.tspan = tspan opts.nsteps = 200 opts.likelihood_fn = likelihood opts.prior_fn = prior opts.step_fn = step opts.estimate_params = estimate_params # the specific value of seed isn't important, it just makes the run reproducible opts.seed = 1 # note that solver tolerance values are set in earm_1_3_standalone.py return opts