In [ ]:
import matplotlib.pyplot as plt
import numpy as np
from scipy import stats
import seaborn as sns
import pymc3 as pm
import pandas as pd
%matplotlib inline
sns.set(font_scale=1.5)
In [ ]:
np.random.seed(314)
N = 100
alfa_real = 2.5
beta_real = 0.9
eps_real = np.random.normal(0, 0.5, size=N)
x = np.random.normal(10, 1, N)
y_real = alfa_real + beta_real * x
y = y_real + eps_real
plt.figure(figsize=(10,5))
plt.subplot(1,2,1)
plt.plot(x, y, 'b.')
plt.xlabel('$x$', )
plt.ylabel('$y$', rotation=0)
plt.plot(x, y_real, 'k')
plt.subplot(1,2,2)
sns.distplot(y)
plt.xlabel('$y$')
In [ ]:
with pm.Model() as model:
alpha = pm.Normal('alpha', mu=0, sd=10)
beta = pm.Normal('beta', mu=0, sd=1)
epsilon = pm.HalfCauchy('epsilon', 5)
# mu = pm.Deterministic('mu', alpha + beta * x)
# y_pred = pm.Normal('y_pred', mu=mu, sd=epsilon, observed=y)
y_pred = pm.Normal('y_pred', mu= alpha + beta * x, sd=epsilon, observed=y)
step = pm.Metropolis()
trace = pm.sample(10000, step=step, chains=1)
In [ ]:
pm.traceplot(trace, compact=True);
In [ ]:
pm.summary(trace, var_names=('alpha', 'beta', 'epsilon'))
In [ ]:
pm.plot_posterior(trace, var_names=('alpha', 'beta', 'epsilon'));
In [ ]:
pm.autocorrplot(trace, var_names=('alpha', 'beta', 'epsilon'));
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]: