{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ridge Quantile Regression\n",
    "\n",
    "[![Slides](https://img.shields.io/badge/🦌-ReHLine-blueviolet)](https://rehline-python.readthedocs.io/en/latest/)\n",
    "\n",
    "The regularized 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",
    "where $\\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.\n",
    "\n",
    "> **Note.** Since the check loss is a plq function, thus we can solve it by `rehline.plqERM_Ridge`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "## simulate data\n",
    "from sklearn.datasets import make_regression\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "import numpy as np\n",
    "\n",
    "scaler = StandardScaler()\n",
    "\n",
    "n, d = 10000, 5\n",
    "X, y = make_regression(n_samples=n, n_features=d, noise=1.0)\n",
    "X = scaler.fit_transform(X)\n",
    "## add intercept\n",
    "X = np.hstack((X,np.ones((n,1))))\n",
    "y = y/y.std()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "## solve QR with different `qt` via `plqERM_Ridge`\n",
    "from rehline import plqERM_Ridge\n",
    "\n",
    "clf5 = plqERM_Ridge(loss={'name': 'QR', 'qt': 0.05}, C=10.0/n)\n",
    "clf5.fit(X=X, y=y)\n",
    "\n",
    "clf95 = plqERM_Ridge(loss={'name': 'QR', 'qt': 0.95}, C=10.0/n)\n",
    "clf95.fit(X=X, y=y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 39848 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 warnings\n",
    "import matplotlib.pyplot as plt\n",
    "warnings.filterwarnings(\"ignore\", \"is_categorical_dtype\")\n",
    "\n",
    "n_sample = 50\n",
    "X_sample, y_sample = X[:n_sample], y[:n_sample]\n",
    "q05_sample = clf5.decision_function(X_sample)\n",
    "q95_sample = clf95.decision_function(X_sample)\n",
    "\n",
    "df = pd.DataFrame({'x0': X_sample[:,0], 'real_y': y_sample, 'q05': q05_sample, 'q95': q95_sample})\n",
    "df = df.melt(id_vars='x0')\n",
    "\n",
    "sns.scatterplot(data=df, x='x0', y='value', hue='variable')\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "colab": {
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python 3",
   "name": "python3"
  },
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
