{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x7f8443806920>]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 22548 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from scipy.interpolate import interp1d\n",
    "\n",
    "# Given lists of quantiles and their corresponding probabilities\n",
    "quantiles = np.array([-0.23013, -0.19831, -0.15217, -0.13654,\n",
    "                      0.011687, 0.015129, 0.043187, 0.047704])\n",
    "probs = np.array([0.025, 0.05, 0.1, 0.15, 0.85, 0.9, 0.95, 0.975])\n",
    "\n",
    "# scatter plot of the quantiles vs probabilities\n",
    "plt.scatter(quantiles, probs)\n",
    "\n",
    "# cubic spline interpolation of the CDF\n",
    "f = interp1d(quantiles, probs, kind='cubic', bounds_error=False, fill_value=\"extrapolate\")\n",
    "\n",
    "# define a range for the x values\n",
    "x = np.linspace(min(quantiles), max(quantiles), 1000)\n",
    "\n",
    "# plot the interpolation (approximated CDF)\n",
    "plt.plot(x, f(x), label='Interpolated CDF')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 20868 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from scipy.interpolate import interp1d\n",
    "\n",
    "# Given lists of quantiles and their corresponding probabilities\n",
    "quantiles = np.array([-0.23013, -0.19831, -0.15217, -0.13654, -0.05726,\n",
    "                      0.011687, 0.015129, 0.043187, 0.047704])\n",
    "probs = np.array([0.025, 0.05, 0.1, 0.15, 0.5, 0.85, 0.9, 0.95, 0.975])\n",
    "\n",
    "# Interpolate the inverse CDF\n",
    "inverse_cdf = interp1d(probs, quantiles, kind='quadratic', bounds_error=False, fill_value=\"extrapolate\")\n",
    "\n",
    "# Generate uniform random numbers\n",
    "uniform_random_numbers = np.random.rand(10000)\n",
    "\n",
    "# Apply the inverse CDF to the uniform random numbers\n",
    "samples = inverse_cdf(uniform_random_numbers)\n",
    "\n",
    "# Plot the histogram of the sampled values\n",
    "plt.hist(samples, bins=50, density=True, alpha=0.75)\n",
    "plt.title('Histogram of Sampled Values')\n",
    "plt.xlabel('Value')\n",
    "plt.ylabel('Frequency')\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "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.10.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
