{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ridge Composite Quantile Regression\n",
    "\n",
    "[![Slides](https://img.shields.io/badge/🦌-ReHLine-blueviolet)](https://rehline-python.readthedocs.io/en/latest/)\n",
    "\n",
    "The regularized composite quantile regression solves the following optimization problem:\n",
    "\n",
    "<!-- $$ -->\n",
    "<!-- min_{\\beta \\in \\mathbb{R}^{d}} \\ C \\sum_{i=1}^n  \\rho_\\kappa ( y_i - x^\\intercal_i \\beta ) + \\frac{1}{2} \\| \\beta \\|^2, -->\n",
    "<!-- $$ -->\n",
    "$$\n",
    "min_{\\beta \\in \\mathbb{R}^{d}, \\alpha \\in \\mathbb{R}^{K}} \\ C  \\sum_{k=1}^K \\sum_{i=1}^n \\rho_\\kappa ( y_i - x^\\intercal_i \\beta - \\alpha_k) + \\frac{1}{2} (\\| \\beta \\|^2 + \\| \\alpha \\|^2),\n",
    "$$\n",
    "\n",
    "\n",
    "where $\\alpha_k$ is the intercept associated with k-th quantile, $\\rho_\\kappa(u) = u\\cdot(\\kappa - \\mathbf{1}(u < 0))$ is the check loss,\n",
    "$x_i \\in \\mathbb{R}^d$ is a feature vector, $y_i \\in \\mathbb{R}$ is the response variable."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "## simulate data\n",
    "from sklearn.datasets import make_regression\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "\n",
    "scaler = StandardScaler()\n",
    "\n",
    "n, d = 1000, 1\n",
    "X, y, coef = make_regression(n_samples=n, n_features=d, noise=50.0, coef=True)\n",
    "X = scaler.fit_transform(X)\n",
    "y = y/y.std()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "## fit model\n",
    "from rehline import CQR_Ridge\n",
    "\n",
    "cqr = CQR_Ridge(quantiles = [0.05, 0.5, 0.95])\n",
    "cqr.fit(X, y)\n",
    "y_pred = cqr.predict(X) # 3 columns, each represents the prediction for a quantile"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 76580 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "## plot QR results\n",
    "import pandas as pd\n",
    "import seaborn as sns\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "df = pd.DataFrame({'X': X.squeeze(), 'real_y': y, 'q05': y_pred[:, 0], 'q50': y_pred[:, 1], 'q95': y_pred[:, 2]})\n",
    "\n",
    "## scatter plot of the data\n",
    "sns.scatterplot(df, x = 'X', y = 'real_y', color = 'blue')\n",
    "\n",
    "## plot the quantile regression lines\n",
    "sns.lineplot(df, x = 'X', y = 'q05', color = 'red', label = 'Quantile: 0.05')\n",
    "sns.lineplot(df, x = 'X', y = 'q50', color = 'orange', label = 'Quantile: 0.50')\n",
    "sns.lineplot(df, x = 'X', y = 'q95', color = 'purple', label = 'Quantile: 0.95')\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "cqr",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
