Skip to content

【创新应用】基于振幅估计的量子风险分析:VaR 与预期损失计算 - #37

Open
CREVIOS wants to merge 1 commit into
OriginQ:developfrom
CREVIOS:feature/quantum-risk-analysis
Open

【创新应用】基于振幅估计的量子风险分析:VaR 与预期损失计算#37
CREVIOS wants to merge 1 commit into
OriginQ:developfrom
CREVIOS:feature/quantum-risk-analysis

Conversation

@CREVIOS

@CREVIOS CREVIOS commented Aug 3, 2026

Copy link
Copy Markdown

关联 Issue:#13 (【本源杯项目】优化/新增算法、开发创新应用)
参赛队伍:DU_Fanta
目录:contest2/OriginQCup_DU_Fanta/


一、应用简介

在险价值(VaR)与预期损失(ES)是巴塞尔框架下最核心的两个风险指标,银行通常用蒙特卡洛模拟计算:将误差降到 eps 需要 O(1/eps^2) 次采样。

本应用把仓库中已有的两个组件组合成一条完整的风险计量流水线:

组件 作用
QCmp.int_comparator 构造可逆判定电路 L >= t
QAE.IQAE 估计判定成立的概率,即尾部概率 P(L >= t)

迭代振幅估计只需 O(1/eps) 次 oracle 查询。

二、核心设计

VaR 与 ES 都可以只用尾部概率表示,因此一个量子原语就足够,不需要为 ES 另行设计带线性幅度旋转的电路。对整数取值的损失 L

E[(L - t)^+] = sum_{k > t} P(L >= k)
ES_alpha     = VaR_alpha + E[(L - VaR_alpha)^+] / P(L >= VaR_alpha)

由此:

  • VaR:对阈值二分搜索,只需 O(log N) 次振幅估计,而非线性扫描的 O(N) 次;
  • ES:VaR 之上若干尾部概率求和。

两个恒等式均在测试中对照精确值验证。

LossDistribution.from_credit_portfolio 通过卷积构造独立债务人组合的精确损失分布,测试中与穷举所有违约组合的结果逐点比对(最大误差 1e-17)。

三、实测结果

演示组合:5 个债务人,总敞口 90 万元;损失分布 4 个量子比特(16 个取值),加比较器共 8 个量子比特

置信度 eps VaR(量子) VaR(精确) ES(量子) ES(精确) ES 误差 振幅估计次数
0.90 0.0100 3 3 3.3785 3.4459 0.0673 18
0.95 0.0100 3 3 3.4660 3.4459 0.0201 18
0.99 0.0020 5 5 5.2986 5.3256 0.0271 16

三个置信度下 VaR 均与精确值完全一致。

尾部概率估计(eps = 0.01)最大绝对误差 0.00913,在目标精度之内。

四、一个必须说明的精度条件

VaR 的二分搜索需判断 P(L >= t+1) <= 1 - alpha。若估计精度与 1 - alpha 相当,该比较将被估计噪声主导。

开发中在 alpha=0.99, eps=0.01 下确实观察到:真实 P(L >= 5) = 0.01056,与判定阈值 0.01 的差距小于 eps,VaR 被低估为 4。

因此模块提供 recommended_epsilon(alpha) = 0.2 * (1 - alpha),并在 eps 过粗时发出 RuntimeWarning。上表 alpha=0.99 使用 eps=0.002 后结果正确。此条件已在 README 与测试中明确记录。

五、文件清单

文件 内容
quantum_risk.py 核心模块:LossDistributionQuantumRiskAnalyzer
example_credit_risk.py 信贷组合完整演示
Test_quantum_risk.py 26 个单元测试
README.md 中英双语文档

六、测试

$ python -m pytest Test_quantum_risk.py
26 passed

$ python -m doctest quantum_risk.py -v
14 passed and 0 failed

$ python example_credit_risk.py
(正常退出)

每个量子估计值都与同一分布上的经典精确值对照,测试验证的是流水线的正确性而不仅是可运行性。

七、不作主张的内容

  • 不主张端到端量子加速:平方级优势仅存在于尾部概率子过程的查询复杂度;整条流水线仍包含经典的分布构造与二分搜索,在模拟器上经典计算更快。
  • 不主张可用于生产:演示规模为 4 个损失比特,真实组合需要更多比特、相关性违约模型与噪声硬件上的误差缓解。
  • 未在真实量子硬件运行QAE.IQAE 目前仅支持 CPU 模拟器后端。

English summary

A complete Value at Risk / Expected Shortfall workflow assembled from two components that already exist in this repository: QCmp.int_comparator provides the reversible predicate L >= t, and QAE.IQAE estimates the probability it holds, giving the tail probability in O(1/eps) oracle queries instead of O(1/eps^2) classical Monte Carlo samples.

The design point is that both risk measures reduce to tail probabilities, so a single quantum primitive suffices — Value at Risk becomes a bisection costing O(log N) amplitude estimations rather than an O(N) scan, and Expected Shortfall a short sum. Both identities are verified against exact values.

On the demo portfolio (5 obligors, 8 qubits) VaR matches the exact value at all three confidence levels and ES is within 0.07 loss levels, using 16–18 amplitude estimations.

An accuracy condition is documented explicitly: the VaR bisection compares tail probabilities against 1 - alpha, so epsilon must be small relative to that gap. This was observed concretely at alpha=0.99, eps=0.01; recommended_epsilon() and a RuntimeWarning now guard it.

No end-to-end quantum speedup, production readiness, or hardware run is claimed.

Tests: 26 unit tests + 14 doctests, all passing; every quantum estimate checked against the exact classical value.

Adds a complete Value at Risk and Expected Shortfall workflow built from
two components that already exist in the library: QCmp.int_comparator
supplies the reversible predicate L >= t, and QAE.IQAE estimates the
probability that it holds. The tail probability P(L >= t) therefore costs
O(1/eps) oracle queries instead of the O(1/eps^2) samples a classical
Monte Carlo simulation needs.

The design point is that both risk measures reduce to tail probabilities,
so no second estimation primitive is required. For an integer-valued loss

    E[(L - t)^+] = sum_{k > t} P(L >= k)
    ES_alpha     = VaR_alpha + E[(L - VaR_alpha)^+] / P(L >= VaR_alpha)

which makes Value at Risk a bisection over tail probabilities, costing
O(log N) amplitude estimations rather than the O(N) of a linear scan, and
Expected Shortfall a short sum of them. Both identities are verified
against exact values in the tests.

LossDistribution.from_credit_portfolio builds the exact loss distribution
of a portfolio of independent obligors by convolution; the test suite
checks it against brute-force enumeration of all default combinations.

On the demo portfolio (five obligors, 8 qubits) the estimated Value at
Risk matches the exact value at confidence levels 0.90, 0.95 and 0.99,
and Expected Shortfall is within 0.07 loss levels, using 16 to 18
amplitude estimations.

The VaR bisection compares tail probabilities against 1 - alpha, so the
estimation accuracy has to be small relative to that gap. At alpha=0.99
with eps=0.01 the true P(L >= 5) = 0.01056 lies within eps of the 0.01
decision threshold and the level is under-estimated. recommended_epsilon
returns 0.2 * (1 - alpha) and value_at_risk warns when eps is too coarse.

Contents:
- quantum_risk.py         core module
- example_credit_risk.py  credit portfolio demonstration
- Test_quantum_risk.py    26 unit tests, all against exact classical values
- README.md               bilingual documentation

No end-to-end quantum speedup is claimed: the quadratic advantage applies
to the query complexity of the tail-probability subroutine only. QAE.IQAE
currently supports the CPU simulator backend, so no hardware run is
included.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant