pyAssetCorr 0.1.1__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.
@@ -0,0 +1,33 @@
1
+ # Python bytecode
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+
6
+ # Distribution / packaging
7
+ build/
8
+ dist/
9
+ *.egg-info/
10
+ *.egg
11
+ .eggs/
12
+
13
+ # Virtual environments
14
+ .venv/
15
+ venv/
16
+ env/
17
+
18
+ # Test / coverage caches
19
+ .pytest_cache/
20
+ .coverage
21
+ htmlcov/
22
+ .tox/
23
+ .nox/
24
+ .mypy_cache/
25
+ .ruff_cache/
26
+
27
+ # Editor / OS
28
+ .DS_Store
29
+ .idea/
30
+ .vscode/
31
+
32
+ # Agent workspace
33
+ .claude/
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 pyAssetCorr contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,161 @@
1
+ Metadata-Version: 2.4
2
+ Name: pyAssetCorr
3
+ Version: 0.1.1
4
+ Summary: Estimate asset correlation from historical default data under the Vasicek credit-portfolio model.
5
+ Project-URL: Homepage, https://github.com/ZigzagLi/pyAssetCorr
6
+ Project-URL: Documentation, https://github.com/ZigzagLi/pyAssetCorr/blob/main/docs/USER_GUIDE.md
7
+ Project-URL: Source, https://github.com/ZigzagLi/pyAssetCorr
8
+ Author-email: Likai Li <li.likai.1990@gmail.com>
9
+ License: MIT
10
+ License-File: LICENSE
11
+ Keywords: Vasicek,asset correlation,credit risk,default correlation,maximum likelihood,method of moments
12
+ Classifier: Development Status :: 4 - Beta
13
+ Classifier: Intended Audience :: Financial and Insurance Industry
14
+ Classifier: Intended Audience :: Science/Research
15
+ Classifier: License :: OSI Approved :: MIT License
16
+ Classifier: Programming Language :: Python :: 3
17
+ Classifier: Topic :: Scientific/Engineering :: Mathematics
18
+ Requires-Python: >=3.9
19
+ Requires-Dist: numpy>=1.22
20
+ Requires-Dist: scipy>=1.8
21
+ Provides-Extra: accel
22
+ Requires-Dist: numba>=0.57; extra == 'accel'
23
+ Provides-Extra: dev
24
+ Requires-Dist: pytest>=7.0; extra == 'dev'
25
+ Description-Content-Type: text/markdown
26
+
27
+ # pyAssetCorr
28
+
29
+ Estimate **asset correlation** from historical default data under the Vasicek
30
+ credit-portfolio model.
31
+
32
+ A clean-room Python implementation of the most-used functionality of the R
33
+ package [`AssetCorr`](https://cran.r-project.org/package=AssetCorr): method-of-moments
34
+ and maximum-likelihood estimators for intra- and inter-cohort asset correlation,
35
+ with standard errors, information criteria, likelihood-ratio tests, and tools
36
+ for overlapping multi-year cohorts.
37
+
38
+ See [`docs/USER_GUIDE.md`](docs/USER_GUIDE.md) for the full guide.
39
+
40
+ ## Why asset correlation?
41
+
42
+ In the Vasicek single-factor model an obligor's latent asset return is
43
+
44
+ ```
45
+ A = sqrt(rho) * X + sqrt(1 - rho) * eps
46
+ ```
47
+
48
+ with a systematic factor `X` shared across obligors and idiosyncratic `eps`.
49
+ The obligor defaults when `A` falls below `Phi^{-1}(PD)`. The **asset
50
+ correlation** `rho` controls how strongly defaults cluster: it drives the tail
51
+ of the portfolio loss distribution and hence economic and regulatory capital.
52
+ `pyAssetCorr` estimates `rho` (and, for several cohorts, the inter-cohort
53
+ coupling) from observed default-count time series.
54
+
55
+ ## Installation
56
+
57
+ ```bash
58
+ pip install pyAssetCorr # core: numpy + scipy
59
+ pip install "pyAssetCorr[accel]" # optional numba acceleration
60
+ ```
61
+
62
+ From a checkout:
63
+
64
+ ```bash
65
+ pip install -e ".[dev]" # editable install with pytest
66
+ ```
67
+
68
+ Requires Python >= 3.9. The optional `numba` JIT speeds up the hot
69
+ log-likelihood kernel; a pure-NumPy fallback runs transparently when numba is
70
+ absent or incompatible with your interpreter.
71
+
72
+ ## Quickstart
73
+
74
+ Data are two equal-length arrays: `d` = defaults per period, `n` = obligors per
75
+ period.
76
+
77
+ ```python
78
+ import numpy as np
79
+ from pyassetcorr import intra_fmm, intra_mle, simulate_default_series
80
+
81
+ # Simulate a 10-year history with rho = 0.15, PD = 3%, 2000 obligors/year.
82
+ d, n = simulate_default_series(rho=0.15, pd=0.03, n=2000, T=10, seed=0)
83
+
84
+ # Method of moments (finite-sample corrected) with a confidence interval.
85
+ mom = intra_fmm(d, n, ci=True)
86
+ print(mom) # MomentResult(method='fmm', rho=..., se=..., ...)
87
+ print(mom.rho, mom.ci)
88
+
89
+ # Maximum likelihood, jointly estimating PD, with AIC/BIC and SE.
90
+ mle = intra_mle(d, n, pd="joint", ci=True)
91
+ print(mle.params["rho"], mle.params["pd"])
92
+ print(mle.aic, mle.bic, mle.se["rho"])
93
+
94
+ # The MLE integral is adaptive; pass `nodes` to check quadrature convergence,
95
+ # e.g. intra_mle(d, n, nodes=64). See the User Guide, "Checking quadrature
96
+ # convergence".
97
+ ```
98
+
99
+ ### Several cohorts at once
100
+
101
+ ```python
102
+ from pyassetcorr import multi_cohort_mle, lr_test, multi_cohort_single_factor
103
+
104
+ # d, n are now (T x K): T periods, K cohorts.
105
+ fit = multi_cohort_mle(d_mat, n_mat) # per-cohort rho + free gamma
106
+ print(fit.params["rho"], fit.params["gamma"])
107
+ print(fit.inter_corr) # inter-cohort asset-corr matrix
108
+
109
+ # Is a single common factor (gamma = 1) enough?
110
+ restricted = multi_cohort_single_factor(d_mat, n_mat)
111
+ print(lr_test(fit, restricted)) # likelihood-ratio test
112
+ ```
113
+
114
+ The multi-cohort model nests cohorts under a single global factor `Z` plus
115
+ per-cohort factors `w_k`, controlled by one coupling parameter `gamma`:
116
+ intra-cohort correlation stays `rho_k`, inter-cohort correlation is
117
+ `gamma * sqrt(rho_i * rho_j)`. `gamma = 1` is a single common factor,
118
+ `gamma = 0` independent cohorts.
119
+
120
+ ### Overlapping multi-year cohorts
121
+
122
+ Annual cohorts measured over an `h`-year horizon share calendar shocks, so the
123
+ default series is autocorrelated and naive standard errors are too small. Use a
124
+ HAC lag or non-overlapping subsample averaging:
125
+
126
+ ```python
127
+ from pyassetcorr import intra_fmm, group_average
128
+
129
+ intra_fmm(d, n, lag=h - 1) # autocorrelation-robust (Frei-Wunsch) SE
130
+ group_average(d, n, horizon=h) # average over non-overlapping subsamples
131
+ ```
132
+
133
+ ## Estimators
134
+
135
+ | Function | Kind | Description |
136
+ |---|---|---|
137
+ | `intra_amm` | MoM | Asymptotic method of moments (Gordy 2000) |
138
+ | `intra_fmm` | MoM | Finite-sample corrected method of moments |
139
+ | `intra_jdp1` | MoM | Unbiased joint-default-probability matching (Lucas 1995) |
140
+ | `intra_jdp2` | MoM | Biased JDP matching (literature parity) |
141
+ | `intra_mle` | MLE | Single-cohort Vasicek-Binomial MLE |
142
+ | `multi_cohort_mle` | MLE | Generalized multi-cohort MLE (nested two-level factor) |
143
+
144
+ AssetCorr-style aliases (`intraAMM`, `intraFMM`, `intraJDP1`, `intraJDP2`,
145
+ `intraMLE`) are provided for discoverability.
146
+
147
+ ## License
148
+
149
+ MIT. This is a clean-room implementation written from the published equations
150
+ (see references below), not a translation of the GPL-3 R source, so it is free
151
+ to use in both academic and commercial/financial settings.
152
+
153
+ ## References
154
+
155
+ - Vasicek, O. (2002). *The Distribution of Loan Portfolio Value.* Risk.
156
+ - Gordy, M. (2000). *A comparative anatomy of credit risk models.* J. Banking & Finance.
157
+ - Lucas, D. (1995). *Default correlation and credit analysis.* J. Fixed Income.
158
+ - Gordy, M. & Heitfield, E. (2010). *Small-sample estimation of models of portfolio credit risk.*
159
+ - Duellmann, K. & Gehde-Trapp, M. (2004). *Probability of default estimation.*
160
+ - Bluhm, C. & Overbeck, L. (2003). *Systematic risk in homogeneous credit portfolios.*
161
+ - Frei, C. & Wunsch, M. (2018). *Moment Estimators for Autocorrelated Time Series and Default Correlations.* J. Credit Risk.
@@ -0,0 +1,135 @@
1
+ # pyAssetCorr
2
+
3
+ Estimate **asset correlation** from historical default data under the Vasicek
4
+ credit-portfolio model.
5
+
6
+ A clean-room Python implementation of the most-used functionality of the R
7
+ package [`AssetCorr`](https://cran.r-project.org/package=AssetCorr): method-of-moments
8
+ and maximum-likelihood estimators for intra- and inter-cohort asset correlation,
9
+ with standard errors, information criteria, likelihood-ratio tests, and tools
10
+ for overlapping multi-year cohorts.
11
+
12
+ See [`docs/USER_GUIDE.md`](docs/USER_GUIDE.md) for the full guide.
13
+
14
+ ## Why asset correlation?
15
+
16
+ In the Vasicek single-factor model an obligor's latent asset return is
17
+
18
+ ```
19
+ A = sqrt(rho) * X + sqrt(1 - rho) * eps
20
+ ```
21
+
22
+ with a systematic factor `X` shared across obligors and idiosyncratic `eps`.
23
+ The obligor defaults when `A` falls below `Phi^{-1}(PD)`. The **asset
24
+ correlation** `rho` controls how strongly defaults cluster: it drives the tail
25
+ of the portfolio loss distribution and hence economic and regulatory capital.
26
+ `pyAssetCorr` estimates `rho` (and, for several cohorts, the inter-cohort
27
+ coupling) from observed default-count time series.
28
+
29
+ ## Installation
30
+
31
+ ```bash
32
+ pip install pyAssetCorr # core: numpy + scipy
33
+ pip install "pyAssetCorr[accel]" # optional numba acceleration
34
+ ```
35
+
36
+ From a checkout:
37
+
38
+ ```bash
39
+ pip install -e ".[dev]" # editable install with pytest
40
+ ```
41
+
42
+ Requires Python >= 3.9. The optional `numba` JIT speeds up the hot
43
+ log-likelihood kernel; a pure-NumPy fallback runs transparently when numba is
44
+ absent or incompatible with your interpreter.
45
+
46
+ ## Quickstart
47
+
48
+ Data are two equal-length arrays: `d` = defaults per period, `n` = obligors per
49
+ period.
50
+
51
+ ```python
52
+ import numpy as np
53
+ from pyassetcorr import intra_fmm, intra_mle, simulate_default_series
54
+
55
+ # Simulate a 10-year history with rho = 0.15, PD = 3%, 2000 obligors/year.
56
+ d, n = simulate_default_series(rho=0.15, pd=0.03, n=2000, T=10, seed=0)
57
+
58
+ # Method of moments (finite-sample corrected) with a confidence interval.
59
+ mom = intra_fmm(d, n, ci=True)
60
+ print(mom) # MomentResult(method='fmm', rho=..., se=..., ...)
61
+ print(mom.rho, mom.ci)
62
+
63
+ # Maximum likelihood, jointly estimating PD, with AIC/BIC and SE.
64
+ mle = intra_mle(d, n, pd="joint", ci=True)
65
+ print(mle.params["rho"], mle.params["pd"])
66
+ print(mle.aic, mle.bic, mle.se["rho"])
67
+
68
+ # The MLE integral is adaptive; pass `nodes` to check quadrature convergence,
69
+ # e.g. intra_mle(d, n, nodes=64). See the User Guide, "Checking quadrature
70
+ # convergence".
71
+ ```
72
+
73
+ ### Several cohorts at once
74
+
75
+ ```python
76
+ from pyassetcorr import multi_cohort_mle, lr_test, multi_cohort_single_factor
77
+
78
+ # d, n are now (T x K): T periods, K cohorts.
79
+ fit = multi_cohort_mle(d_mat, n_mat) # per-cohort rho + free gamma
80
+ print(fit.params["rho"], fit.params["gamma"])
81
+ print(fit.inter_corr) # inter-cohort asset-corr matrix
82
+
83
+ # Is a single common factor (gamma = 1) enough?
84
+ restricted = multi_cohort_single_factor(d_mat, n_mat)
85
+ print(lr_test(fit, restricted)) # likelihood-ratio test
86
+ ```
87
+
88
+ The multi-cohort model nests cohorts under a single global factor `Z` plus
89
+ per-cohort factors `w_k`, controlled by one coupling parameter `gamma`:
90
+ intra-cohort correlation stays `rho_k`, inter-cohort correlation is
91
+ `gamma * sqrt(rho_i * rho_j)`. `gamma = 1` is a single common factor,
92
+ `gamma = 0` independent cohorts.
93
+
94
+ ### Overlapping multi-year cohorts
95
+
96
+ Annual cohorts measured over an `h`-year horizon share calendar shocks, so the
97
+ default series is autocorrelated and naive standard errors are too small. Use a
98
+ HAC lag or non-overlapping subsample averaging:
99
+
100
+ ```python
101
+ from pyassetcorr import intra_fmm, group_average
102
+
103
+ intra_fmm(d, n, lag=h - 1) # autocorrelation-robust (Frei-Wunsch) SE
104
+ group_average(d, n, horizon=h) # average over non-overlapping subsamples
105
+ ```
106
+
107
+ ## Estimators
108
+
109
+ | Function | Kind | Description |
110
+ |---|---|---|
111
+ | `intra_amm` | MoM | Asymptotic method of moments (Gordy 2000) |
112
+ | `intra_fmm` | MoM | Finite-sample corrected method of moments |
113
+ | `intra_jdp1` | MoM | Unbiased joint-default-probability matching (Lucas 1995) |
114
+ | `intra_jdp2` | MoM | Biased JDP matching (literature parity) |
115
+ | `intra_mle` | MLE | Single-cohort Vasicek-Binomial MLE |
116
+ | `multi_cohort_mle` | MLE | Generalized multi-cohort MLE (nested two-level factor) |
117
+
118
+ AssetCorr-style aliases (`intraAMM`, `intraFMM`, `intraJDP1`, `intraJDP2`,
119
+ `intraMLE`) are provided for discoverability.
120
+
121
+ ## License
122
+
123
+ MIT. This is a clean-room implementation written from the published equations
124
+ (see references below), not a translation of the GPL-3 R source, so it is free
125
+ to use in both academic and commercial/financial settings.
126
+
127
+ ## References
128
+
129
+ - Vasicek, O. (2002). *The Distribution of Loan Portfolio Value.* Risk.
130
+ - Gordy, M. (2000). *A comparative anatomy of credit risk models.* J. Banking & Finance.
131
+ - Lucas, D. (1995). *Default correlation and credit analysis.* J. Fixed Income.
132
+ - Gordy, M. & Heitfield, E. (2010). *Small-sample estimation of models of portfolio credit risk.*
133
+ - Duellmann, K. & Gehde-Trapp, M. (2004). *Probability of default estimation.*
134
+ - Bluhm, C. & Overbeck, L. (2003). *Systematic risk in homogeneous credit portfolios.*
135
+ - Frei, C. & Wunsch, M. (2018). *Moment Estimators for Autocorrelated Time Series and Default Correlations.* J. Credit Risk.