Quantile Regression Analysis
Quantile Regression Analysis
HEMANT THAPA
Where:
The objective of quantile regression is to minimize a loss function that differs from the
least squares loss used in ordinary regression. The quantile loss function ρ τ (e) is defined
as:
τe if e ≥ 0,
ρτ (e) = {
(τ − 1)e if e < 0,
Where:
In [ ]: import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import statsmodels.api as sm
from statsmodels.regression.quantile_regression import QuantReg
from sklearn.metrics import mean_absolute_error, mean_squared_error
# Metrics Calculation
mse = mean_squared_error(data['y'], y_pred)
mae = mean_absolute_error(data['y'], y_pred)
Deviancemodel
2
R = 1 −
pseudo Deviancenull
2
^
Deviancemodel = ∑ (Y − Y )
Deviance null
: The deviance for a null model, which typically uses the median value
as the prediction for all Y . The null model assumes that the best predictor for Y is
simply the median of Y .
~ 2
Deviancenull = ∑ (Y − Y )
~
Where Y is the median of Y (the constant prediction in the null model).
A value close to 1 indicates that the model explains much of the variability in Y ,
similar to traditional R . 2
A value close to 0 indicates that the model provides little explanatory power
compared to the null model.
In [ ]: for q in quantiles:
print(f"Quantile: {q}")
print(metrics[q])
print()
Quantile: 0.1
{'Pseudo-R²': 0.7328, 'MAE': 2.642, 'MSE': 9.8555}
Quantile: 0.5
{'Pseudo-R²': 0.8988, 'MAE': 1.5335, 'MSE': 3.7315}
Quantile: 0.9
{'Pseudo-R²': 0.7159, 'MAE': 2.7772, 'MSE': 10.476}
for i, q in enumerate(quantiles):
axs[i].scatter(data['X'], residuals[q], alpha=0.6, color='black', marker='o'
axs[i].set_title(f'Residuals for {int(q*100)}th Quantile', fontsize=14)
axs[i].set_xlabel('X', fontsize=12)
axs[i].set_ylabel('Residuals', fontsize=12)
axs[i].grid(True, axis='y', ls='--', alpha=0.2)
plt.tight_layout()
plt.show()
In [ ]: # Residual histogram for each quantile regression
fig, axs = plt.subplots(3, 1, figsize=(6, 8))
for i, q in enumerate(quantiles):
axs[i].hist(residuals[q], bins=30, color='white', alpha=0.7, edgecolor='blac
axs[i].set_title(f'Residuals Histogram for {int(q*100)}th Quantile', fontsiz
axs[i].set_xlabel('Residuals', fontsize=12)
axs[i].set_ylabel('Frequency', fontsize=12)
axs[i].grid(True, axis='y', ls='--', alpha=0.2)
plt.tight_layout()
plt.show()