from scipy import stats import statsmodels.api as sm from statsmodels.sandbox.regression.quantile_regression import quantilereg sige = 0.1 nobs, k_vars = 1500, 3 x = np.random.uniform(-1, 1, size=nobs) x.sort() exog = np.vander(x, k_vars+1)[:,::-1] mix = 0.1 * stats.norm.pdf(x[:,None], loc=np.linspace(-0.5, 0.75, 4), scale=0.01).sum(1) y = exog.sum(1) + mix + sige * (np.random.randn(nobs)/2 + 1)**3 p = 0.5 x0 = exog[:, 1:] #quantilereg includes constant already! res_qr = quantilereg(y, x0, p) res_qr2 = quantilereg(y, x0, 0.1) res_qr3 = quantilereg(y, x0, 0.75) res_ols = sm.OLS(y, exog).fit() params = [res_ols.params, res_qr2, res_qr, res_qr3] labels = ['ols', 'qr 0.1', 'qr 0.5', 'qr 0.75'] import matplotlib.pyplot as plt plt.figure() plt.plot(x, y, '.', alpha=0.5) for lab, beta in zip(['ols', 'qr 0.1', 'qr 0.5', 'qr 0.75'], params): print('%-8s'%lab, np.round(beta, 4)) fitted = np.dot(exog, beta)
Author: Josef Perktold ''' import numpy as np import statsmodels.api as sm from statsmodels.sandbox.regression.quantile_regression import quantilereg sige = 5 nobs, k_vars = 500, 5 x = np.random.randn(nobs, k_vars) #x[:,0] = 1 y = x.sum(1) + sige * (np.random.randn(nobs)/2 + 1)**3 p = 0.5 res_qr = quantilereg(y,x,p) res_qr2 = quantilereg(y,x,0.25) res_qr3 = quantilereg(y,x,0.75) res_ols = sm.OLS(y, np.column_stack((np.ones(nobs), x))).fit() ##print 'ols ', res_ols.params ##print '0.25', res_qr2 ##print '0.5 ', res_qr ##print '0.75', res_qr3 params = [res_ols.params, res_qr2, res_qr, res_qr3] labels = ['ols', 'qr 0.25', 'qr 0.5', 'qr 0.75'] import matplotlib.pyplot as plt