eqlx-portfolio 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- eqlx_portfolio-0.1.0/LICENSE +176 -0
- eqlx_portfolio-0.1.0/PKG-INFO +315 -0
- eqlx_portfolio-0.1.0/README.md +292 -0
- eqlx_portfolio-0.1.0/pyproject.toml +86 -0
- eqlx_portfolio-0.1.0/pyproject.toml.orig +63 -0
- eqlx_portfolio-0.1.0/src/eqlx_portfolio/__init__.py +36 -0
- eqlx_portfolio-0.1.0/src/eqlx_portfolio/_bvn.py +61 -0
- eqlx_portfolio-0.1.0/src/eqlx_portfolio/_checks.py +84 -0
- eqlx_portfolio-0.1.0/src/eqlx_portfolio/_version.py +1 -0
- eqlx_portfolio-0.1.0/src/eqlx_portfolio/cli.py +182 -0
- eqlx_portfolio-0.1.0/src/eqlx_portfolio/config.py +253 -0
- eqlx_portfolio-0.1.0/src/eqlx_portfolio/crplus.py +267 -0
- eqlx_portfolio-0.1.0/src/eqlx_portfolio/data.py +247 -0
- eqlx_portfolio-0.1.0/src/eqlx_portfolio/engine.py +488 -0
- eqlx_portfolio-0.1.0/src/eqlx_portfolio/errors.py +5 -0
- eqlx_portfolio-0.1.0/src/eqlx_portfolio/factors.py +101 -0
- eqlx_portfolio-0.1.0/src/eqlx_portfolio/mcp_server.py +276 -0
- eqlx_portfolio-0.1.0/src/eqlx_portfolio/py.typed +0 -0
- eqlx_portfolio-0.1.0/src/eqlx_portfolio/ratings.py +96 -0
- eqlx_portfolio-0.1.0/src/eqlx_portfolio/simulation.py +301 -0
- eqlx_portfolio-0.1.0/src/eqlx_portfolio/synthetic.py +49 -0
- eqlx_portfolio-0.1.0/src/eqlx_portfolio/valuation.py +148 -0
- eqlx_portfolio-0.1.0/src/eqlx_portfolio/vasicek.py +54 -0
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: eqlx-portfolio
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Deterministic credit portfolio risk: loss distribution, value-at-risk, expected shortfall, economic capital and risk contributions, by seeded simulation and by CreditRisk+. Checked against published examples. Read-only MCP server for AI agents.
|
|
5
|
+
Keywords: credit risk,credit portfolio,economic capital,value at risk,expected shortfall,creditmetrics,creditrisk+,concentration risk,risk contributions,mcp
|
|
6
|
+
Author: Can Demir
|
|
7
|
+
Author-email: Can Demir <can.demir@eqlx.ai>
|
|
8
|
+
License-Expression: Apache-2.0
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Classifier: Development Status :: 3 - Alpha
|
|
11
|
+
Classifier: Intended Audience :: Financial and Insurance Industry
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Topic :: Office/Business :: Financial
|
|
15
|
+
Requires-Dist: numpy>=1.26
|
|
16
|
+
Requires-Dist: scipy>=1.11
|
|
17
|
+
Requires-Dist: mcp>=2.3.0 ; extra == 'mcp'
|
|
18
|
+
Requires-Python: >=3.10
|
|
19
|
+
Project-URL: Homepage, https://eqlx.org
|
|
20
|
+
Project-URL: Source, https://github.com/eqlx-ai/eqlx-portfolio
|
|
21
|
+
Provides-Extra: mcp
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
|
|
24
|
+
# eqlx-portfolio
|
|
25
|
+
|
|
26
|
+
Deterministic credit portfolio risk: how much a loan book can lose in a bad year, how much capital
|
|
27
|
+
that takes, and which borrowers and sectors drive it. A Python library, a command line tool and a
|
|
28
|
+
read-only MCP server for AI agents.
|
|
29
|
+
|
|
30
|
+
Loans are assessed one by one, but they fail together: a hundred loans to a hundred industries and a
|
|
31
|
+
hundred loans to one industry look the same loan by loan and are very different portfolios. This
|
|
32
|
+
tool measures that difference.
|
|
33
|
+
|
|
34
|
+
It computes. It does not estimate default probabilities, recoveries or correlations: those are
|
|
35
|
+
inputs. Same input, same output, and every figure comes with the steps that produced it.
|
|
36
|
+
|
|
37
|
+
- **Two methods, one input.** A seeded simulation of the asset value model (the approach of
|
|
38
|
+
CreditMetrics) and the closed-form CreditRisk+ model. Use one to question the other.
|
|
39
|
+
- **Checked against published numbers.** The worked examples of the CreditMetrics Technical
|
|
40
|
+
Document and of the CreditRisk+ document are reproduced in the test-suite; everything else is checked against closed forms, direct
|
|
41
|
+
enumeration and brute-force simulation.
|
|
42
|
+
- **Built for agents.** The agent decides what to look at; the tool returns the number, the band
|
|
43
|
+
around it and the steps taken. The MCP server only reads files in one folder and writes nothing.
|
|
44
|
+
|
|
45
|
+
## Who it is for
|
|
46
|
+
|
|
47
|
+
Credit risk and capital planning teams who want an independent view of economic capital and
|
|
48
|
+
concentration, credit committees asking what one more loan does to the book, validators who need a
|
|
49
|
+
transparent benchmark for a vendor model, and people building AI agents for credit work.
|
|
50
|
+
|
|
51
|
+
## Install
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
pip install eqlx-portfolio # library and command line
|
|
55
|
+
pip install "eqlx-portfolio[mcp]" # with the MCP server
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
Python 3.10 or newer. Depends on NumPy and SciPy only.
|
|
59
|
+
|
|
60
|
+
## A first look
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
eqlx-portfolio sample demo # writes a made-up portfolio: demo/loans.csv, demo/settings.json
|
|
64
|
+
eqlx-portfolio run demo/loans.csv --config demo/settings.json
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
```text
|
|
68
|
+
eqlx-portfolio 0.1.0 method: simulation
|
|
69
|
+
loans 621 borrowers 500 sectors 6 exposure 995,349,000
|
|
70
|
+
|
|
71
|
+
expected loss 12,716,172
|
|
72
|
+
loss std deviation 12,031,533
|
|
73
|
+
at 99.00% confidence
|
|
74
|
+
value-at-risk 57,233,350 95% band 56,635,700 to 57,801,100
|
|
75
|
+
expected shortfall 71,379,023
|
|
76
|
+
economic capital 44,517,178
|
|
77
|
+
at 99.90% confidence
|
|
78
|
+
value-at-risk 91,343,950 95% band 88,673,050 to 93,627,200
|
|
79
|
+
expected shortfall 104,348,278
|
|
80
|
+
economic capital 78,627,778
|
|
81
|
+
|
|
82
|
+
largest borrower 5.5% of exposure, ten largest 28.0%, effective number of borrowers 79
|
|
83
|
+
|
|
84
|
+
sector exposure share risk share
|
|
85
|
+
construction 41.1% 69.1%
|
|
86
|
+
services 19.4% 13.0%
|
|
87
|
+
manufacturing 14.8% 6.8%
|
|
88
|
+
retail 11.7% 4.7%
|
|
89
|
+
tourism 5.6% 3.5%
|
|
90
|
+
energy 7.4% 2.9%
|
|
91
|
+
|
|
92
|
+
borrower exposure share risk share
|
|
93
|
+
B0004 4.0% 12.7%
|
|
94
|
+
B0003 4.7% 10.6%
|
|
95
|
+
B0005 4.2% 8.8%
|
|
96
|
+
B0001 5.5% 7.4%
|
|
97
|
+
B0002 2.4% 3.8%
|
|
98
|
+
B0287 1.8% 3.6%
|
|
99
|
+
B0303 1.1% 2.7%
|
|
100
|
+
B0136 0.7% 2.6%
|
|
101
|
+
B0407 1.0% 2.1%
|
|
102
|
+
B0435 0.5% 2.0%
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
Construction is 41% of the exposure and 69% of the risk: five large borrowers were planted there.
|
|
106
|
+
The band next to each value-at-risk says how far simulation noise can move it. The same run with
|
|
107
|
+
`--json` adds the checks that were made and the steps taken.
|
|
108
|
+
|
|
109
|
+
What does one more loan do? Fifty million to a new construction borrower, then the same loan to an
|
|
110
|
+
energy borrower:
|
|
111
|
+
|
|
112
|
+
```bash
|
|
113
|
+
eqlx-portfolio what-if demo/loans.csv --config demo/settings.json --new new_loan.csv
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
| New loan of 50,000,000 (pd 2%, lgd 45%) | Expected loss | Expected shortfall at 99.9% | Capital per unit lent |
|
|
117
|
+
|---|---|---|---|
|
|
118
|
+
| to construction | +450,000 | +8,530,008 | 17.1% |
|
|
119
|
+
| to energy | +450,000 | +3,824,232 | 7.6% |
|
|
120
|
+
|
|
121
|
+
Same loan, same expected loss, twice the capital: it lands where the book is already heavy. Both
|
|
122
|
+
portfolios are valued on the same scenarios, so the difference is not simulation noise.
|
|
123
|
+
|
|
124
|
+
Other commands: `contributions` (every borrower's share of the risk, as CSV), `describe` (what a
|
|
125
|
+
loan file contains), `pair` (joint default probability of two borrowers).
|
|
126
|
+
|
|
127
|
+
## As a library
|
|
128
|
+
|
|
129
|
+
```python
|
|
130
|
+
import numpy as np
|
|
131
|
+
from eqlx_portfolio import (
|
|
132
|
+
creditriskplus,
|
|
133
|
+
default_thresholds,
|
|
134
|
+
joint_default_probability,
|
|
135
|
+
large_portfolio_loss,
|
|
136
|
+
migration_thresholds,
|
|
137
|
+
sigma_from_asset_correlation,
|
|
138
|
+
simulate,
|
|
139
|
+
single_factor,
|
|
140
|
+
value_stats,
|
|
141
|
+
)
|
|
142
|
+
|
|
143
|
+
# One bond, rated BBB: where can it be in a year, and what is it worth there?
|
|
144
|
+
bbb = [0.0002, 0.0033, 0.0595, 0.8693, 0.0530, 0.0117, 0.0012, 0.0018] # AAA ... CCC, default
|
|
145
|
+
values = [109.37, 109.19, 108.66, 107.55, 102.02, 98.10, 83.64, 51.13]
|
|
146
|
+
s = value_stats(bbb, values)
|
|
147
|
+
round(s.mean, 2), round(s.sd, 2) # (107.09, 2.99)
|
|
148
|
+
np.round(migration_thresholds(bbb), 2) # [ 3.54 2.7 1.53 -1.49 -2.18 -2.75 -2.91]
|
|
149
|
+
|
|
150
|
+
# Two borrowers with 0.18% and 0.06% default probability and 30% asset correlation
|
|
151
|
+
joint_default_probability(0.0018, 0.0006, 0.30)[0] # 1.561e-05, 14.5 times the independent 1.08e-06
|
|
152
|
+
|
|
153
|
+
# 100 loans of 1,000,000, pd 2%, lgd 45%, one common factor with 20% asset correlation
|
|
154
|
+
pd = np.full(100, 0.02)
|
|
155
|
+
amount = np.full(100, 1_000_000 * 0.45)
|
|
156
|
+
sim = simulate(
|
|
157
|
+
single_factor(0.20, 100),
|
|
158
|
+
default_thresholds(pd),
|
|
159
|
+
np.column_stack((np.zeros(100), amount)),
|
|
160
|
+
scenarios=200_000,
|
|
161
|
+
seed=1,
|
|
162
|
+
tail_levels=[0.999],
|
|
163
|
+
)
|
|
164
|
+
tail = sim.tails[0]
|
|
165
|
+
round(tail.value_at_risk), round(tail.expected_shortfall) # (10800000, 13137313)
|
|
166
|
+
round(large_portfolio_loss(amount, pd, 0.20, 0.999)) # 10184076: the infinitely fine limit
|
|
167
|
+
|
|
168
|
+
# The same portfolio in CreditRisk+, with the sector volatility that matches the 20% correlation
|
|
169
|
+
sigma = sigma_from_asset_correlation(0.02, 0.20) # 1.323
|
|
170
|
+
res = creditriskplus(amount, pd, np.ones((100, 1)), [sigma], confidence=[0.999])
|
|
171
|
+
round(res.tails[0].value_at_risk), round(res.tails[0].expected_shortfall) # (9900000, 11375006)
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
## For AI agents (MCP)
|
|
175
|
+
|
|
176
|
+
```bash
|
|
177
|
+
eqlx-portfolio mcp --root /path/to/folder
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
Client configuration (Claude Desktop, Claude Code and other MCP clients):
|
|
181
|
+
|
|
182
|
+
```json
|
|
183
|
+
{"mcpServers": {"eqlx-portfolio": {"command": "eqlx-portfolio", "args": ["mcp", "--root", "/path/to/folder"]}}}
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
| Tool | What it returns |
|
|
187
|
+
|---|---|
|
|
188
|
+
| `list_files` | loan and settings files in the folder |
|
|
189
|
+
| `describe_loans` | loans, borrowers, sectors, exposure, expected loss, largest borrowers |
|
|
190
|
+
| `portfolio_risk` | expected loss, value-at-risk, expected shortfall, economic capital, concentration, steps |
|
|
191
|
+
| `risk_contributions` | borrowers ranked by their share of the risk, next to their share of exposure |
|
|
192
|
+
| `what_if_new_loans` | the portfolio before and after new loans, on the same scenarios |
|
|
193
|
+
| `joint_default` | probability that two borrowers default together |
|
|
194
|
+
| `joint_migration` | joint year-end rating probabilities of two borrowers |
|
|
195
|
+
| `exposure_value` | mean and standard deviation of one exposure's value |
|
|
196
|
+
| `large_portfolio_benchmark` | loss rate of a perfectly fine-grained portfolio in a bad year |
|
|
197
|
+
|
|
198
|
+
The server reads `.csv` and `.json` files inside `--root` and nothing else; `..` and symlinks cannot
|
|
199
|
+
leave the folder. It writes nothing and sends nothing anywhere. When an input is refused the agent
|
|
200
|
+
gets the reason, not a bare error.
|
|
201
|
+
|
|
202
|
+
## Input
|
|
203
|
+
|
|
204
|
+
Loan file (CSV), one row per loan:
|
|
205
|
+
|
|
206
|
+
| Column | Meaning |
|
|
207
|
+
|---|---|
|
|
208
|
+
| `loan_id` | unique |
|
|
209
|
+
| `exposure` | amount at risk if the borrower defaults |
|
|
210
|
+
| `pd` | default probability over the horizon, at least 0 and below 1 |
|
|
211
|
+
| `lgd` | share of the exposure lost in default, 0 to 1 |
|
|
212
|
+
| `borrower_id` | optional; loans of one borrower default together |
|
|
213
|
+
| `sector` | optional; needed for the sector correlation model |
|
|
214
|
+
| `lgd_sd` | optional; makes the loss in default uncertain (simulation only) |
|
|
215
|
+
|
|
216
|
+
Settings (JSON). The correlation block is required: it is the main assumption of a run.
|
|
217
|
+
|
|
218
|
+
```json
|
|
219
|
+
{
|
|
220
|
+
"method": "simulation",
|
|
221
|
+
"confidence": [0.99, 0.999],
|
|
222
|
+
"scenarios": 200000,
|
|
223
|
+
"seed": 1,
|
|
224
|
+
"correlation": {
|
|
225
|
+
"model": "sector",
|
|
226
|
+
"within": {"construction": 0.24, "energy": 0.18},
|
|
227
|
+
"default_within": 0.15,
|
|
228
|
+
"default_between": 0.5,
|
|
229
|
+
"between": {"construction|energy": 0.3}
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
| Correlation model | Meaning |
|
|
235
|
+
|---|---|
|
|
236
|
+
| `single` | one common factor; `rho` is the asset correlation of any two borrowers |
|
|
237
|
+
| `basel_corporate` | one common factor; each borrower's correlation from the Basel corporate formula (12% to 24% by pd) |
|
|
238
|
+
| `sector` | one factor per sector; `within` a sector, and `between` the sector factors |
|
|
239
|
+
|
|
240
|
+
`"method": "creditriskplus"` solves the portfolio in closed form instead. It accepts the `single`
|
|
241
|
+
and `sector` models with independent sectors; the sector volatility is derived from the asset
|
|
242
|
+
correlation unless given in `"creditriskplus": {"sector_sigma": {...}}`.
|
|
243
|
+
|
|
244
|
+
## How it is verified
|
|
245
|
+
|
|
246
|
+
Published figures, from the CreditMetrics Technical Document (J.P. Morgan, 1997):
|
|
247
|
+
|
|
248
|
+
| Example | Published | Reproduced |
|
|
249
|
+
|---|---|---|
|
|
250
|
+
| Value of a BBB bond after one year: mean, standard deviation (Table 2.5) | 107.09, 2.99 | 107.09, 2.99 |
|
|
251
|
+
| The same with uncertain recovery | 3.18 | 3.18 |
|
|
252
|
+
| Revaluation of the bond in each rating from the forward curves (Tables 1.2, 1.4) | 16 values | within 0.02 |
|
|
253
|
+
| Asset return thresholds of BB and single-A borrowers (Tables 8.5, 8.6) | 14 values | all 14 |
|
|
254
|
+
| Joint migration of two borrowers, 0.30 asset correlation (Table 3.2) | 64 cells | within 0.01 point |
|
|
255
|
+
| Joint migration of two borrowers, 0.20 asset correlation (Table 8.7) | 64 cells | within 0.05 point |
|
|
256
|
+
| Scenarios of asset returns mapped to ratings (Tables 10.4, 10.5) | 30 ratings | all 30 |
|
|
257
|
+
| First percentile of a two-bond portfolio | 204.40 | 204.40 |
|
|
258
|
+
| Three-bond portfolio: means and variances (chapter 9) | 7 values | all 7 |
|
|
259
|
+
|
|
260
|
+
Published figures, from the CreditRisk+ document (Credit Suisse First Boston, 1997), example 1A,
|
|
261
|
+
25 obligors in one sector:
|
|
262
|
+
|
|
263
|
+
| Figure | Published | Reproduced |
|
|
264
|
+
|---|---|---|
|
|
265
|
+
| Aggregate exposure, expected loss | 130,513,072 and 14,221,863 | the same |
|
|
266
|
+
| Standard deviation | 12,668,742 | 12,613,315 (0.4% lower) |
|
|
267
|
+
| 99th percentile loss | 55,311,503 | 55,240,000 (0.1% lower) |
|
|
268
|
+
| 99.9th percentile loss | 77,133,478 | 76,990,000 (0.2% lower) |
|
|
269
|
+
|
|
270
|
+
The document's spreadsheet groups exposures into bands by a rounding rule it does not spell out;
|
|
271
|
+
the small gaps are that rounding.
|
|
272
|
+
|
|
273
|
+
Three figures printed in the CreditMetrics document do not follow from its own tables (a misprinted probability, a
|
|
274
|
+
misprinted threshold, and covariances that the model cannot produce). The tests show where each
|
|
275
|
+
comes from; see [LIMITS.md](LIMITS.md).
|
|
276
|
+
|
|
277
|
+
Independent truth for everything else:
|
|
278
|
+
|
|
279
|
+
- the bivariate normal distribution against SciPy, to 12 decimals;
|
|
280
|
+
- simulated mean and standard deviation against exact pairwise formulas;
|
|
281
|
+
- a fine-grained portfolio against the large-portfolio formula behind the Basel risk weights;
|
|
282
|
+
- CreditRisk+ against the negative binomial and Poisson closed forms (to 14 decimals) and against a
|
|
283
|
+
brute-force simulation of the same model, including every borrower's risk contribution;
|
|
284
|
+
- risk contributions add up to the expected shortfall, in both methods;
|
|
285
|
+
- a what-if run reproduces, to the last digit, the separate run of the portfolio without the new
|
|
286
|
+
loans, also when the new loan brings a new sector or an uncertain loss given default;
|
|
287
|
+
- every defect found by an independent review is pinned by a regression test.
|
|
288
|
+
|
|
289
|
+
145 tests. `uv run pytest` runs them in about a minute.
|
|
290
|
+
|
|
291
|
+
## Limits
|
|
292
|
+
|
|
293
|
+
One period, default mode in portfolio runs, inputs are yours, the far tail of a simulation is thin,
|
|
294
|
+
CreditRisk+ sectors are independent. No estimation of correlations, no granularity adjustment, no
|
|
295
|
+
stress scenarios, not regulatory capital. Details in [LIMITS.md](LIMITS.md).
|
|
296
|
+
|
|
297
|
+
## References
|
|
298
|
+
|
|
299
|
+
- Gupton, G. M., Finger, C. C., Bhatia, M. (1997). *CreditMetrics - Technical Document*. J.P. Morgan.
|
|
300
|
+
- Credit Suisse Financial Products (1997). *CreditRisk+: A Credit Risk Management Framework*.
|
|
301
|
+
- Vasicek, O. (2002). The distribution of loan portfolio value. *Risk*, December.
|
|
302
|
+
- Gordy, M. B. (2000). A comparative anatomy of credit risk models. *Journal of Banking & Finance* 24.
|
|
303
|
+
- Gordy, M. B. (2003). A risk-factor model foundation for ratings-based bank capital rules.
|
|
304
|
+
*Journal of Financial Intermediation* 12.
|
|
305
|
+
- Haaf, H., Tasche, D. (2002). Calculating value-at-risk contributions in CreditRisk+. *GARP Risk Review* 7.
|
|
306
|
+
- Haaf, H., Reiss, O., Schoenmakers, J. (2004). Numerically stable computation of CreditRisk+.
|
|
307
|
+
In *CreditRisk+ in the Banking Industry*, Springer.
|
|
308
|
+
- Basel Committee on Banking Supervision (2005). *An Explanatory Note on the Basel II IRB Risk Weight
|
|
309
|
+
Functions*.
|
|
310
|
+
- Drezner, Z., Wesolowsky, G. O. (1990). On the computation of the bivariate normal integral.
|
|
311
|
+
*Journal of Statistical Computation and Simulation* 35.
|
|
312
|
+
|
|
313
|
+
## License
|
|
314
|
+
|
|
315
|
+
Apache-2.0. See [LICENSE](LICENSE) and [NOTICE](NOTICE).
|