Skip to content

Instantly share code, notes, and snippets.

@darribas
Created September 15, 2014 17:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save darribas/282415df2312c808cff0 to your computer and use it in GitHub Desktop.
Save darribas/282415df2312c808cff0 to your computer and use it in GitHub Desktop.
Predictions in a probit
import numpy as np
import pandas as pd
import statsmodels.api as sm
from scipy.stats import norm as N
n, k = 1000, 2
x = np.random.random((n, k+1))
x[:, 0] = 1
b = np.ones((k+1, ))
e = np.random.normal(size=(n, ))
y = np.dot(x, b) + e
yb = np.ones((n, ))
yb[np.where(y < 2)] = 0
# OLS
ols = sm.OLS(yb, x).fit()
yh_ols = ols.predict(x)
print 'OLS diff.: ', (np.dot(x, ols.params) != yh_ols).sum()
# Probit
pbt = sm.Probit(yb, x).fit()
yh_pbt = pbt.predict(x)
print 'PBT diff.: ', (N.cdf(np.dot(x, pbt.params)) != yh_pbt).sum()
import matplotlib.pyplot as plt
xa = np.linspace(-4, 4, 100)
plt.figure()
plt.plot(xa, N.pdf(xa))
plt.title("PDF")
plt.figure()
plt.plot(N.cdf(xa), xa)
plt.title("CDF")
plt.show()
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment