Advertisement
Guest User

Untitled

a guest
Oct 21st, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. import pymc3 as pm
  2. import matplotlib.pyplot as plt
  3. import numpy as np
  4. import theano
  5.  
  6. size = 100
  7. true_intercept = 1
  8. true_slope = 2
  9.  
  10. x = np.linspace(0, 1, size)
  11. # y = a + b*x
  12. true_regression_line = true_intercept + true_slope * x
  13. # add noise
  14. y = true_regression_line + np.random.normal(scale=.5, size=size)
  15.  
  16. # Add outliers
  17. x_out = np.append(x, [.1, .15, .2])
  18. y_out = np.append(y, [8, 6, 9])
  19.  
  20. data = dict(x=x_out, y=y_out)
  21.  
  22. # fig = plt.figure(figsize=(7, 7))
  23. # ax = fig.add_subplot(111, xlabel='x', ylabel='y', title='Generated data and underlying model')
  24. # ax.plot(x_out, y_out, 'x', label='sampled data')
  25. # ax.plot(x, true_regression_line, label='true regression line', lw=2.)
  26. # plt.legend(loc=0)
  27.  
  28. with pm.Model() as model_robust:
  29. family = pm.glm.families.StudentT()
  30. pm.glm.glm('y ~ x', data, family=family)
  31. start = pm.find_MAP()
  32. step = pm.NUTS(scaling=start)
  33. trace_robust = pm.sample(300, step, progressbar=True)
  34.  
  35. pm.traceplot(trace_robust)
  36. plt.figure(figsize=(5, 5))
  37. plt.plot(x_out, y_out, 'x')
  38. pm.glm.plot_posterior_predictive(trace_robust,
  39. label='posterior predictive regression lines')
  40. plt.plot(x, true_regression_line,
  41. label='true regression line', lw=3., c='y')
  42. plt.legend()
  43. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement