[python] PyMc primer

Viewer

copydownloadembedprintName: PyMc primer
  1. import arviz as az
  2. import matplotlib.pyplot as plt
  3. import numpy as np
  4. import pandas as pd
  5. import pymc as pm
  6. import xarray as xr
  7.  
  8. from pymc import HalfCauchy, Model, Normal, sample
  9.  
  10. print(f"Running on PyMC v{pm.__version__}")
  11. RANDOM_SEED = 8927
  12. rng = np.random.default_rng(RANDOM_SEED)
  13.  
  14. az.style.use("arviz-darkgrid")
  15.  
  16. size = 200
  17. true_intercept = 1
  18. true_slope = 2
  19.  
  20. = np.linspace(0, 1, size)
  21. # y = a + b*x
  22. true_regression_line = true_intercept + true_slope * x
  23. # add noise
  24. = true_regression_line + rng.normal(scale=0.5, size=size)
  25.  
  26. data = pd.DataFrame(dict(x=x, y=y))
  27.  
  28. fig = plt.figure(figsize=(7, 7))
  29. ax = fig.add_subplot(111, xlabel="x", ylabel="y", title="Generated data and underlying model")
  30. ax.plot(x, y, "x", label="sampled data")
  31. ax.plot(x, true_regression_line, label="true regression line", lw=2.0)
  32. plt.legend(loc=0);
  33. plt.savefig('plot1.png')
  34.  
  35. print("Finished.")

Editor

You can edit this paste and save as new:


File Description
  • PyMc primer
  • Paste Code
  • 16 Apr-2024
  • 908 Bytes
You can Share it: