Skip to content

Instantly share code, notes, and snippets.

@y-mitsui
Created June 11, 2018 11:52
Show Gist options
  • Select an option

  • Save y-mitsui/06b414721af349213a502fd613edeaf1 to your computer and use it in GitHub Desktop.

Select an option

Save y-mitsui/06b414721af349213a502fd613edeaf1 to your computer and use it in GitHub Desktop.
bayesian linear regression in Python
import numpy as np
n_sample = 1000
true_coef = np.array([2.0, 3.0])
u0 = np.array([0.0, 0.0])
A0 = 0.1 * np.eye(2)
sample_X = np.random.normal(size=(n_sample, 2))
sample_y = np.dot(sample_X, true_coef.reshape(-1, 1)).reshape(-1,) + np.random.normal(size=n_sample)
tmp1 = np.linalg.inv(np.dot(sample_X.T, sample_X))
tmp2 = np.dot(sample_X.T, sample_y.reshape(-1, 1))
beta = np.dot(tmp1, tmp2)
tmp1 = np.linalg.inv(np.dot(sample_X.T, sample_X) + A0)
tmp2 = np.dot(np.dot(sample_X.T, sample_X), beta) + np.dot(A0, u0.reshape(-1, 1))
estimation_coef = np.dot(tmp1, tmp2)
print(estimation_coef)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment