<jupyter_start><jupyter_text>Generate artificial data<jupyter_code># Generate some synthetic data from the model.
N = 50
x = np.sort(10 * np.random.rand(N))
yerr = 0.1 + 0.6 * np.random.rand(N)
y = m_true * x + b_true
# y += np.abs(f_true * y) * np.random.randn(N)
y += yerr * np.random.randn(N)

plt.errorbar(x, y, yerr=yerr, fmt=".k", capsize=0)
x0 = np.linspace(0, 10, 50)
plt.plot(x0, results_det['y'], "k", alpha=0.3, lw=3)
plt.xlim(0, 10)
plt.xlabel('x')
plt.ylabel('y');<jupyter_output><empty_output><jupyter_text>Create Pandas dataframes as required by bioscrape inference<jupyter_code>import pandas as pd
exp_data = pd.DataFrame()
exp_data['x'] = x0
exp_data['y'] = y
exp_data<jupyter_output><empty_output><jupyter_text>Using bioscrape inference<jupyter_code># from bioscrape.pid_interfaces import 
from bioscrape.inference import py_inference

# Import data from CSV
# Import a CSV file for each experiment run
prior = {'m' : ['gaussian', m_true, 500],'b' : ['gaussian', b_true, 1000]}
sampler, pid = py_inference(Model = M, exp_data = exp_data, measurements = ['y'],
                     time_column = ['x'], params_to_estimate = ['m','b'],
                      nwalkers = 32, nsteps = 2000, init_seed = 1e-4, prior = prior,
                      sim_type = 'deterministic', plot_show = False)

# Recommended to simply use sampler object/mcmc_results.csv and generate your own custom plots
truth_list, uncertainty_list = pid.plot_mcmc_results(sampler);<jupyter_output>C:\Users\apand\AppData\Local\Continuum\anaconda3\lib\site-packages\emcee\ensemble.py:258: RuntimeWarning: Initial state is not linearly independent and it will not allow a full exploration of parameter space
  category=RuntimeWarning,
100%|█████████████████████████████████████████████████████████████████████████████| 2000/2000 [00:09<00:00, 209.92it/s]<jupyter_text>Model simulations using parameter samples from the posterior distribution compared along side original model simulation<jupyter_code>flat_samples = sampler.get_chain(discard=100, thin=15, flat=True)
inds = np.random.randint(len(flat_samples), size=100)
for ind in inds:
    sample = flat_samples[ind]
    plt.plot(x0, np.dot(np.vander(x0, 2), sample[:2]), "C1", alpha=0.1)
plt.errorbar(x, y, yerr=yerr, fmt=".k", capsize=0)
plt.plot(x0, m_true * x0 + b_true, "k", label="original model")
plt.legend(fontsize=14)
plt.xlim(0, 10)
plt.xlabel("x")
plt.ylabel("y");<jupyter_output><empty_output><jupyter_text>Other helpful utilities:* Cost progress plots: You can look at the exploration of the log-likelihood by plotting cost_progress attribute of the inference object.* The `plot_mcmc_results` returns the uncertainties around parameter values - this could indicate the accuracy of parameter inference predictions.* You can use autocorrelation_time attribute of your inference object (pid.autocorrelation_time in the above example) to look at the autocorrelation time that it took for the chain to converge. A rule of thumb is that this value should be atleast 50 times lesser than the total number of steps for which you ran the MCMC chain. Higher autocorrelation time indicates that the chain was ended before it could converge to a distribution, whereas very low autocorrelation times could also be potentially something to look out for as it would indicate independence of all samples irrespective of the data.* Check out Python emcee documentation on [**how to parallelize**](https://emcee.readthedocs.io/en/stable/tutorials/parallel/) the package using multiple CPU/GPU cores.<jupyter_code># rcParams['agg.path.chunksize'] = 10000
plt.close()
plt.figure()
plt.plot(pid.cost_progress)
plt.title('Log-likelihood progress')
plt.ylabel('Normed log-likelihood')
plt.xlabel('Number of steps in all chains')
plt.show()
pid.autocorrelation_time<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13073094052921141, 'fraction_numerical': 0.02515064186533927, 'mean_word_length': 5.515358361774744, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 20, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '45549', 'license': 'MIT', 'n_tokens_mistral': 1410, 'n_tokens_neox': 1247, 'n_words': 373, 'path': 'inference examples/individual examples/Linear Model.ipynb', 'repo_name': 'biocircuits/bioscrape', 'size': '3817'}