-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
248 changed files
with
22,876 additions
and
8,726 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# The config for credit risk analysis model | ||
# number of assets. | ||
num_assets = 4 | ||
# basic default probabilities. | ||
base_default_prob = [0.15, 0.25, 0.39, 0.58] | ||
# sensitivity. | ||
sensitivity = [0.37, 0.21, 0.32, 0.02] | ||
# loss given default. | ||
lgd = [5, 1, 3, 4] | ||
# confidence level. | ||
confidence_level = 0.99 | ||
# degree of simulation. Higher the degree, preciser the simulation. | ||
degree_of_simulation = 4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# !/usr/bin/env python3 | ||
# Copyright (c) 2020 Institute for Quantum Computing, Baidu Inc. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import argparse | ||
import os | ||
import warnings | ||
import logging | ||
import time | ||
from typing import Dict | ||
|
||
warnings.filterwarnings('ignore') | ||
os.environ['PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION'] = 'python' | ||
|
||
import toml | ||
from paddle_quantum.finance import CreditRiskAnalyzer | ||
|
||
|
||
def main(args): | ||
time_start = time.strftime("%Y%m%d-%H:%M:%S", time.localtime()) | ||
logging.info(f"Job start at {time_start:s}") | ||
|
||
parsed_configs: Dict = toml.load(args.config) | ||
|
||
# input credit portfolio settings | ||
num_assets = parsed_configs["num_assets"] | ||
base_default_prob = parsed_configs["base_default_prob"] | ||
sensitivity = parsed_configs["sensitivity"] | ||
lgd = parsed_configs["lgd"] | ||
confidence_level = parsed_configs["confidence_level"] | ||
degree_of_simulation = parsed_configs["degree_of_simulation"] | ||
|
||
estimator = CreditRiskAnalyzer(num_assets, base_default_prob, sensitivity, lgd, | ||
confidence_level, degree_of_simulation) | ||
print("The Value at Risk of these assets are", estimator.estimate_var()) | ||
|
||
|
||
if __name__ == '__main__': | ||
parser = argparse.ArgumentParser(description="Credit risk analysis with paddle quantum.") | ||
parser.add_argument("--config", type=str, help="Input the config file with toml format.") | ||
main(parser.parse_args()) |
214 changes: 214 additions & 0 deletions
214
applications/credit_risk_analysis/introduction_cn.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,214 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"attachments": {}, | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# 信贷风险分析\n", | ||
"\n", | ||
"*Copyright (c) 2023 Institute for Quantum Computing, Baidu Inc. All Rights Reserved.*" | ||
] | ||
}, | ||
{ | ||
"attachments": {}, | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"风险分析的任务是评估给定资产在面临金融系统波动时的风险,即在险价值 Value at Risk(VaR)。该价值可以衡量在金融系统下给定资产的潜在损失。决定资产在险价值的因素有很多,包括金融机构对于风险的不同偏好、金融系统的波动特征和资产本身的含风险性质。2014 年 Rutkowski 与 Tarca [1] 给出了一个计算风险资本的数学模型。该模型认为整体金融系统的波动应该遵循高斯布朗运动,从而能够计算出较高可信度下处于该系统中资产损失上界的最小值。" | ||
] | ||
}, | ||
{ | ||
"attachments": {}, | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"当应用于信贷风险分析问题时,该模型可以用于评估银行的信贷风险监管资本,即银行所持有信贷资产组合在当前金融系统下的在险价值。在该模型中,我们记给定的信贷资产组合为 $\\{ 0, ..., K-1 \\}$, 且决定资产组合总损失的随机变量为\n", | ||
"$$\n", | ||
"\\mathcal{L} = \\sum_{k=0}^{K - 1} \\lambda_k X_k(Z)\n", | ||
"。$$\n", | ||
"这里,$\\lambda_k$ 为第 $k$ 个信贷资产的违约损失(loss given default);$Z$ 为描述金融系统波动过程的潜在随机变量(latent random variable),且其概率分布默认为标准正态分布;$X_k(Z)$ 为描述金融系统下资产违约过程的伯努利随机变量。具体地,当 $Z = z$ 时,随机变量 $X_k(Z)$ 的参数 $p_k(z)$,即第 $k$ 个资产的违约概率,取决于该资产对于系统风险的敏感度(sensitivity)$\\rho_k \\in [0, 1]$ 以及无系统风险下该资产的基础违约概率(base default probability)$p_k^{(0)} \\in [0, 1]$。在标定置信度(confidence level)之后,信贷资产组合的在险价值应定义为『于置信度 $\\alpha$ 下,信贷资产损失上界的最小值』,即\n", | ||
"$$\n", | ||
"\\textrm{VaR}_\\alpha(\\mathcal{L}) := \\inf \\{ x \\,|\\, \\textrm{Pr}(\\mathcal{L} \\leq x) \\geq \\alpha \\}\n", | ||
"。$$\n", | ||
"例如,若银行持有的一组信贷资产在置信度 $99\\%$ 下的在险价值正好为 $100$ 万,那么当该资产组合出现违约情况时,由于违约而造成的资产损失超过 $100$ 万的概率不高于 $1\\%$。在经典计算中,信贷风险分析问题中的在险价值可以通过经典蒙特卡罗(classical Monte Carlo)方法和二分搜索(bisection search)方法估算:\n", | ||
"1. 根据资产组合性质,选定初始 VaR 猜测值 $\\check{x}$。\n", | ||
"2. 使用经典蒙特卡罗算法估算概率值 $p_{\\check{x}} = \\textrm{Pr}(\\mathcal{L} \\leq \\check{x})$。\n", | ||
"3. 将概率值 $p_{\\check{x}}$ 与 $\\alpha$ 比较,并根据比较结果和二分搜索方法更新猜测值 $\\check{x}$。\n", | ||
"4. 若更新值达到收敛标准,输出 $\\check{x} = \\textrm{VaR}_\\alpha(\\mathcal{L})$;否则返回第 $2$ 步。" | ||
] | ||
}, | ||
{ | ||
"attachments": {}, | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## 量子解决方案\n", | ||
"\n", | ||
"不同于经典算法,量子计算可以使用量子振幅估计(quantum amplitude estimation)算法替换上述第 2 步中使用的经典蒙特卡罗算法,来提高概率值 $p_{\\check{x}}$ 的估算效率。通过量子叠加和纠缠的特性,量子方案与经典方案相比有望在采样次数上获取平方加速的优势 [2]。接下来我们展示如何使用量桨来模拟该量子方案,从而完成信贷风险分析的在险价值计算问题。" | ||
] | ||
}, | ||
{ | ||
"attachments": {}, | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### 在线演示" | ||
] | ||
}, | ||
{ | ||
"attachments": {}, | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"我们已经给出了一个设置好的参数,可以直接用于风险资产组合的在险价值计算。只需要在 `config.toml` 这个配置文件中进行对应的配置,然后输入命令 \n", | ||
"`python credit_risk.py --config config.toml`\n", | ||
"即可对配置好的资产组合进行在险价值计算。\n", | ||
"\n", | ||
"这里,我们给出一个在线演示的版本,可以在线进行测试。首先定义配置文件的内容:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"cra_toml = r\"\"\"\n", | ||
"# 用于信贷风险分析模型的整体配置文件。\n", | ||
"# 资产数量。\n", | ||
"num_assets = 4\n", | ||
"# 基础违约概率。\n", | ||
"base_default_prob = [0.15, 0.25, 0.39, 0.58]\n", | ||
"# 敏感度。\n", | ||
"sensitivity = [0.37, 0.21, 0.32, 0.02]\n", | ||
"# 违约损失。\n", | ||
"lgd = [5, 1, 3, 4]\n", | ||
"# 置信度。\n", | ||
"confidence_level = 0.99\n", | ||
"# 估计精度系数。系数越高,则估计的结果越精确。\n", | ||
"degree_of_simulation = 4\n", | ||
"\"\"\"" | ||
] | ||
}, | ||
{ | ||
"attachments": {}, | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"量桨的金融模块实现了量子解决方案的数值模拟。我们可以从 ``paddle_quantum.finance`` 模块里导入 ``CreditRiskAnalyzer`` 来解决配置好的在险价值计算问题。" | ||
] | ||
}, | ||
{ | ||
"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", | ||
"该资产组合的在险价值大约为 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(\"该资产组合的在险价值大约为\", estimator.estimate_var())" | ||
] | ||
}, | ||
{ | ||
"attachments": {}, | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"___\n", | ||
"\n", | ||
"# 注意事项\n", | ||
"\n", | ||
"这里提供的模型只用于特定模型的信贷风险分析问题。\n", | ||
"\n", | ||
"# 引用信息\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", | ||
"# 参考文献\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 | ||
} |
Oops, something went wrong.