2 releases
Uses new Rust 2024
0.1.1 | Apr 3, 2025 |
---|---|
0.1.0 | Mar 28, 2025 |
#954 in Math
181 downloads per month
28KB
371 lines
Credit Portfolio Model
A rust implementaiton of a credit portfolio model.
Features
This is an early implementation of a credit portfolio model that simulates correlated rating migrations in a portfolio. The correlation structure is based on a Merton-type factor model. Rating migrations are based on the asset value $z$ exceeding a certain threshold.
z>c
The random variable z
is standard normally distributed. Hence, the thresholds are calculated from pre-defined migration or default probabilities
using the cumulative normal distribution. The asset value $z$ itself is a linear combination of multiple standard normal random variables
z=\sqrt{r^2}\cdot y+\sqrt{1-r^2}\cdot\left(\sqrt{1-\epsilon}\cdot e_1 + \sqrt{\epsilon}\cdot e_2\right)
Here, the variables are defined as follows:
- $
e_1
$ is the idiosyncratic risk variable only corresponding to the borrower - $
e_2
$ is the idiosyncratic risk variable of the risk group that the borrower belongs to, e.g. linked to other companies through ownership - $
y
$ is the systematic risk, which is defined as a linear combination of the vector $x
$ which is distributed according to a multivariate normal distribution with covariance matrix $\Sigma
$. Besides, the borrower dependency to the risk drives is specified by weights $\phi
$. The resulting random variable is then defined as $y=\frac{\phi\cdot x}{\sqrt{\phi^T\cdot\Sigma\cdot\phi}}
$. - $
r^2
$ and $\epsilon$ specifiy the correlation of the borrowers asset value to the systematic component or the risk group.
The following features are implemented:
- Migration mode
- Risk groups
- Multi-threading
Features that could be implemented, but are not at the moment
- Expected shortfall calculations and other risk measures
- Alternative sampling methods for better tail measure sampling
- Random losses based on random process in case of default
Installation
Install the package via
cargo install credit_portfolio_model
Usage
The crate comes with a binary that reads csv-data with the portfolio information and outputs the loss-distribution. Besides, Mean and quantile information of the loss distribution is provided on stdout.
credit_portfolio_model --input /path/to/read/input/csv/files/from --output /path/to/store/output/csv --num-trials NUMBERTRIALS --chunk-size TRIALSPERTHREAD
The input consists of six files. The files are
correlation_matrix.csv
risk_factor_1
,risk_factor_2
- Number index of column and row, respectively. Starts with 0 and must be continuouscorrelation
- Correlation value. Note that the matrix needs to be symmetric and positive semi-definite
borrower.csv
borrower_id
- Unique identifier of Borrower (string-like) that is used to map with other filesrisk_group
- Unique identifier of Risk Group (string-like). Risk Groups share one of the idiosyncratic risk driversrating
- Current rating, given as index to a vector, i.e. starting at0
and continuous. The last rating class is considered default, although the current implementation does no special treatment of defaultsr2
- Correlation to the systematic risk factor, i.e. $\rho$.eps
- Correlation to the risk group, i.e. $\epsilon$.
risk_factors.csv
borrower_id
- See borrower, must match the other filerisk_factor
- Mapping to risk factor of correlation factorweight
- Borrower dependency to the risk factor (relative to the other risk factors)
transition_probabilities.csv
borrower_id
- See borrower, must match the other filerating
- Resulting rating classprobabiliy
- Probability to migrate into this class, must sum to 100%.
exposure.csv
exposure_id
- Unique identifier of Exposure (string-like) that is used to map with other filesborrower_id
- See borrower, must match the other fileoutstanding
- Current outstanding, not used at the moment
valuations.csv
exposure_id
- See exposure, must match the other filerating
- Resulting rating classvaluation
- Valuation of the exposure given the rating class.
Documentation
Full API documentation is available on docs.rs.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Dependencies
~73MB
~1M SLoC