Created
September 15, 2014 17:20
-
-
Save darribas/282415df2312c808cff0 to your computer and use it in GitHub Desktop.
Predictions in a probit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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