程序代写代做 In [1]:

In [1]:
import numpy as np
import matplotlib.pyplot as plt
plt.style.use(‘seaborn-darkgrid’)

# Initialize random number generator
np.random.seed(123)

# True parameter values
alpha, sigma = 1, 1
beta = [1, 2.5]

# Size of dataset
size = 100

# Predictor variable
X1 = np.random.randn(size)
X2 = np.random.randn(size) * 0.2

# Simulate outcome variable
Y = alpha + beta[0]*X1 + beta[1]*X2 + np.random.randn(size)*sigma
In [2]:
%matplotlib inline

fig, axes = plt.subplots(1, 2, sharex=True, figsize=(10,4))
axes[0].scatter(X1, Y)
axes[1].scatter(X2, Y)
axes[0].set_ylabel(‘Y’); axes[0].set_xlabel(‘X1’); axes[1].set_xlabel(‘X2’);


In [3]:
import pymc3 as pm
print(‘Running on PyMC3 v{}’.format(pm.__version__))

—————————————————————————
ModuleNotFoundError Traceback (most recent call last)
in
—-> 1 import pymc3 as pm
2 print(‘Running on PyMC3 v{}’.format(pm.__version__))

ModuleNotFoundError: No module named ‘pymc3’
In [ ]:
basic_model = pm.Model()

with basic_model:

# Priors for unknown model parameters
alpha = pm.Normal(‘alpha’, mu=0, sd=10)
beta = pm.Normal(‘beta’, mu=0, sd=10, shape=2)
sigma = pm.HalfNormal(‘sigma’, sd=1)

# Expected value of outcome
mu = alpha + beta[0]*X1 + beta[1]*X2

# Likelihood (sampling distribution) of observations
Y_obs = pm.Normal(‘Y_obs’, mu=mu, sd=sigma, observed=Y)
In [ ]:
help(pm.Normal)
In [ ]:
map_estimate = pm.find_MAP(model=basic_model)

map_estimate
In [ ]:
with basic_model:
# draw 500 posterior samples
trace = pm.sample(500)
In [ ]:
trace[‘alpha’][-5:]
In [ ]:
pm.traceplot(trace);
In [ ]:
pm.summary(trace).round(2)