【创新应用】基于振幅估计的量子风险分析:VaR 与预期损失计算 - #37
Open
CREVIOS wants to merge 1 commit into
Open
Conversation
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.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
关联 Issue:#13 (【本源杯项目】优化/新增算法、开发创新应用)
参赛队伍:DU_Fanta
目录:
contest2/OriginQCup_DU_Fanta/一、应用简介
在险价值(VaR)与预期损失(ES)是巴塞尔框架下最核心的两个风险指标,银行通常用蒙特卡洛模拟计算:将误差降到
eps需要O(1/eps^2)次采样。本应用把仓库中已有的两个组件组合成一条完整的风险计量流水线:
QCmp.int_comparatorL >= tQAE.IQAEP(L >= t)迭代振幅估计只需
O(1/eps)次 oracle 查询。二、核心设计
VaR 与 ES 都可以只用尾部概率表示,因此一个量子原语就足够,不需要为 ES 另行设计带线性幅度旋转的电路。对整数取值的损失
L:由此:
O(log N)次振幅估计,而非线性扫描的O(N)次;两个恒等式均在测试中对照精确值验证。
LossDistribution.from_credit_portfolio通过卷积构造独立债务人组合的精确损失分布,测试中与穷举所有违约组合的结果逐点比对(最大误差 1e-17)。三、实测结果
演示组合:5 个债务人,总敞口 90 万元;损失分布 4 个量子比特(16 个取值),加比较器共 8 个量子比特。
三个置信度下 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.pyLossDistribution、QuantumRiskAnalyzerexample_credit_risk.pyTest_quantum_risk.pyREADME.md六、测试
每个量子估计值都与同一分布上的经典精确值对照,测试验证的是流水线的正确性而不仅是可运行性。
七、不作主张的内容
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_comparatorprovides the reversible predicateL >= t, andQAE.IQAEestimates the probability it holds, giving the tail probability inO(1/eps)oracle queries instead ofO(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 anO(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, soepsilonmust be small relative to that gap. This was observed concretely atalpha=0.99, eps=0.01;recommended_epsilon()and aRuntimeWarningnow 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.