introduction_en.ipynb 9.7 KB
Notebook
Newer Older
Q
Quleaf 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Credit Risk Analysis\n",
    "\n",
    "*Copyright (c) 2023 Institute for Quantum Computing, Baidu Inc. All Rights Reserved.*"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The task of risk analysis is to access the risk of given assets facing the fluctuations of financial systems, i.e. the Value at Risk (VaR). Such quantity can measure the potent loss of given assets in a financial system. There are many factors determining the Value of Risk of assets, including the favor of risk for different financial organizations, the characteristics of system fluctuations and the risk-related properties of assets. In 2014 Rutkowski and Tarca [1] provided a mathematical model that estimates the risk measure. This model suggests that the whole financial system can be modelled by a Brownian motion process, so that the minimum upper bound of assets' loss can be computed under an appropriate confidence level."
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Credit risk analysis is a direct application of such model, which can evaluate the regulatory capital for credit risk in banking industry i.e. the VaR of credit asset combination held under the current financial . In this model, we denote the given asset combination as $\\{ 0, ..., K-1 \\}$, and the risk measures of the total loss as\n",
    "$$\n",
    "\\mathcal{L} = \\sum_{k=0}^{K - 1} \\lambda_k X_k(Z)\n",
    ".$$\n",
    "Here, $\\lambda_k$ is the loss given default of the $k$-th asset; $Z$ is the standard Gaussian variable that implicitly models the process of financial system; $X_k(Z)$ is the Bernoulli variable that models the default process of the $k$-th asset. In particular, when $Z = z$, the parameter $p_k(z)$ of random variable $X_k(Z)$ i.e. the default probability of the $k$-th asset, is dependent on the basic default probability without the effect of financial system $p_k^{(0)} \\in [0, 1]$, and the asset sensitivity to the system risk $\\rho_k \\in [0, 1]$. After settling the confidence level, the Value at Risk in the problem of credit risk analysis shall be defined as \"the minimum upper bound of credit asset combination under confidence level $\\alpha$\". That is,\n",
    "$$\n",
    "\\textrm{VaR}_\\alpha(\\mathcal{L}) := \\inf \\{ x \\,|\\, \\textrm{Pr}(\\mathcal{L} \\leq x) \\geq \\alpha \\}\n",
    ".$$\n",
    "For example, if the VaR of a combination of credit assets held by a bank happens to be one million under confidence level $99\\%$, then the probability that the total loss given default of such combination is greater than one million is less than $1\\%$. In classical calculations, the VaR in the problem of credit risk analysis can be estimated by classical Monte Carlo and bisection search methods:\n",
    "1. Choose an appropriate VaR guess $\\check{x}$ according to the properties of assets.\n",
    "2. Use classical Monte Carlo method to estimate the probability $p_{\\check{x}} = \\textrm{Pr}(\\mathcal{L} \\leq \\check{x})$.\n",
    "3. Compare the probability with $\\alpha$, and update the VaR guess $\\check{x}$ according to the comparison result and bisection search method.\n",
    "4. If the convergence criteria is met, output $\\check{x} = \\textrm{VaR}_\\alpha(\\mathcal{L})$; otherwise return to step $2$."
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Quantum solution\n",
    "\n",
    "Unlike classical algorithms, quantum computation can use the algorithm of quantum amplitude estimation (QAE) to replace the classical Monte Carlo method in the second step mentioned above, to enhance the efficiency of probability estimation. Through the characteristics of quantum superposition and entanglement, such quantum scheme is expected to obtain the advantage of quadratic acceleration compared with classical schemes [2]. Next, we will show how to use Paddle Quantum to simulate this quantum scheme to complete the VaR computational problem of credit risk analysis."
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Online demonstration"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We have set a parameter that can be used directly for the VaR computation of asset combinations. Just configure it in the configuration file `config.toml` and enter the command \n",
    "`python credit_risk.py --config config.toml`.\n",
    "The VaR of configured assets is then be computed.\n",
    "\n",
    "Here, we give a version of the online demo that can be tested online. First define the contents of the configuration file:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "cra_toml = r\"\"\"\n",
    "# The config for credit risk analysis model\n",
    "# number of assets.\n",
    "num_assets = 4\n",
    "# basic default probabilities.\n",
    "base_default_prob = [0.15, 0.25, 0.39, 0.58]\n",
    "# sensitivity.\n",
    "sensitivity = [0.37, 0.21, 0.32, 0.02]\n",
    "# loss given default.\n",
    "lgd = [5, 1, 3, 4]\n",
    "# confidence level.\n",
    "confidence_level = 0.99\n",
    "# degree of simulation. Higher the degree, preciser the simulation.\n",
    "degree_of_simulation = 4\n",
    "\"\"\""
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The financial module of the Paddle Quantum enables numerical simulation of quantum amplitude estimation scheme. We can import ``CreditRiskAnalyzer`` from the ``paddle_quantum.finance`` module to solve the configured VaR computational problem."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-----------------------------------------------------------------------------------\n",
      "Begin bisection search for VaR with confidence level >= 99.0%.\n",
      "-----------------------------------------------------------------------------------\n",
      "Lower guess: level            Middle guess: level            Upper guess: level    \n",
      "    -1     : 0.000                  6    : 0.691                 13     : 1.000    \n",
      "     6     : 0.691                  9    : 0.941                 13     : 1.000    \n",
      "     9     : 0.941                 11    : 0.962                 13     : 1.000    \n",
      "    11     : 0.962                 12    : 0.990                 13     : 1.000    \n",
      "-----------------------------------------------------------------------------------\n",
      "Estimated VaR is 12 with confidence level 99.0%.\n",
      "-----------------------------------------------------------------------------------\n",
      "The Value at Risk of these assets are 12\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import warnings\n",
    "warnings.filterwarnings(\"ignore\")\n",
    "os.environ['PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION'] = 'python'\n",
    "\n",
    "import toml\n",
    "from paddle_quantum.finance import CreditRiskAnalyzer\n",
    "\n",
    "config = toml.loads(cra_toml)\n",
    "num_assets = config[\"num_assets\"]\n",
    "base_default_prob = config[\"base_default_prob\"]\n",
    "sensitivity = config[\"sensitivity\"]\n",
    "lgd = config[\"lgd\"]\n",
    "confidence_level = config[\"confidence_level\"]\n",
    "degree_of_simulation = config[\"degree_of_simulation\"]\n",
    "\n",
    "estimator = CreditRiskAnalyzer(num_assets, base_default_prob, sensitivity, lgd, confidence_level, degree_of_simulation)\n",
    "print(\"The Value at Risk of these assets are\", estimator.estimate_var())"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "___\n",
    "\n",
    "# Note\n",
    "\n",
    "The model presented here is only intended to solve the credit risk analysis problem of a particular model.\n",
    "\n",
    "# Citation\n",
    "\n",
    "```\n",
    "@article{rutkowski2015regulatory,\n",
    "  title={Regulatory capital modeling for credit risk},\n",
    "  author={Rutkowski, Marek and Tarca, Silvio},\n",
    "  journal={International Journal of Theoretical and Applied Finance},\n",
    "  volume={18},\n",
    "  number={05},\n",
    "  pages={1550034},\n",
    "  year={2015},\n",
    "  publisher={World Scientific}\n",
    "}\n",
    "\n",
    "@article{egger2020credit,\n",
    "  title={Credit risk analysis using quantum computers},\n",
    "  author={Egger, Daniel J and Guti{\\'e}rrez, Ricardo Garc{\\'\\i}a and Mestre, Jordi Cahu{\\'e} and Woerner, Stefan},\n",
    "  journal={IEEE Transactions on Computers},\n",
    "  volume={70},\n",
    "  number={12},\n",
    "  pages={2136--2145},\n",
    "  year={2020},\n",
    "  publisher={IEEE}\n",
    "}\n",
    "```\n",
    "\n",
    "# References\n",
    "\n",
    "[1] Rutkowski, Marek, and Silvio Tarca. \"Regulatory capital modeling for credit risk.\" International Journal of Theoretical and Applied Finance 18.05 (2015): 1550034.\n",
    "\n",
    "[2] Egger, Daniel J., et al. \"Credit risk analysis using quantum computers.\" IEEE Transactions on Computers 70.12 (2020): 2136-2145."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "pq",
   "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.8.13"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}