Search code examples
pythonpython-3.xstatsmodelsquantile-regression

Quantile Regression in Python for Multiple Quantiles Simultaneously


I'd like to perform quantile regression of multiple quantiles simultaneously. Statsmodels API offers quantile regression for a single quantile. Is there some way I can use it for multiple quantiles simultaneously?

More specifically, I'd like to optimize the following equations:

enter image description here


Solution

  • I don't think I have an optimum solution, but I may be close. Based on that cost function, it seems like you are trying to fit one coefficient matrix (beta) and several intercepts (b_k). I would do this by first fitting a quantile regression line to the median (q = 0.5), then fitting the other quantile regression lines to the residuals. I understand this isn't "simultaneously", but perhaps its close enough. see below:

    X = np.random.randn(100)
    Y = X + 0.1*np.random.randn(100)
    data = pd.DataFrame(dict(X1=X, Y=Y))
    mod = smf.quantreg('Y ~ X1', data)
    original_model = mod.fit(q=0.5)
    resids = pd.DataFrame(dict(resid=original_model.resid)) 
    residual_model = smf.quantreg('resid ~ 1', resids)
    quantiles = {q:residual_model.fit(q=q).params for q in np.arange(0.1,1.,0.1)}
    prediction_q0.1 = original_model.predict(data) + quantiles[0.1]
    

    prediction_q0.1 yields the prediction for the 10th quantile

    The resulting pretty quantile plot:

    enter image description here

    for quantiles 0.1:0.9. Notice how they are all parallel, as the only differentiating feature is the bias term.