[python] PyMc primer
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
- import arviz as az
- import matplotlib.pyplot as plt
- import numpy as np
- import pandas as pd
- import pymc as pm
- import xarray as xr
- from pymc import HalfCauchy, Model, Normal, sample
- print(f"Running on PyMC v{pm.__version__}")
- RANDOM_SEED = 8927
- rng = np.random.default_rng(RANDOM_SEED)
- az.style.use("arviz-darkgrid")
- size = 200
- true_intercept = 1
- true_slope = 2
- x = np.linspace(0, 1, size)
- # y = a + b*x
- true_regression_line = true_intercept + true_slope * x
- # add noise
- y = true_regression_line + rng.normal(scale=0.5, size=size)
- data = pd.DataFrame(dict(x=x, y=y))
- fig = plt.figure(figsize=(7, 7))
- ax = fig.add_subplot(111, xlabel="x", ylabel="y", title="Generated data and underlying model")
- ax.plot(x, y, "x", label="sampled data")
- ax.plot(x, true_regression_line, label="true regression line", lw=2.0)
- plt.legend(loc=0);
- plt.savefig('plot1.png')
- print("Finished.")
Editor
You can edit this paste and save as new: