11 releases

0.2.1 Nov 27, 2020
0.2.0 Mar 2, 2019
0.1.4 Jan 18, 2019
0.1.0 Dec 29, 2018
0.0.5 Dec 29, 2018

#273 in Science

Download history 9/week @ 2023-10-19 26/week @ 2023-10-26 11/week @ 2023-11-02 20/week @ 2023-11-09 11/week @ 2023-11-16 19/week @ 2023-11-23 39/week @ 2023-11-30 28/week @ 2023-12-07 19/week @ 2023-12-14 31/week @ 2023-12-21 5/week @ 2023-12-28 16/week @ 2024-01-04 8/week @ 2024-01-11 17/week @ 2024-01-18 17/week @ 2024-01-25 16/week @ 2024-02-01

59 downloads per month

MIT license

74KB
1K SLoC

[lin-badge]: https://github.com/danielhstahl/loan_ec/workflows/Rust/badge.svg [cov-badge]: https://codecov.io/gh/danielhstahl/loan_ec/branch/master/graph/badge.svg

Linux Codecov
![lin-badge] ![cov-badge]

Utilities for economic capital assignments for a loan portfolio

This library has a relatively opinionated API for creating a portfolio of loans and performing aggregate statistics (such as loan level risk contributions and expected values).

Install

Add the following to your Cargo.toml:

loan_ec = "0.1.4"

Use

A full example is in the credit_faas_demo.

Create instances of the Loan struct:

extern crate loan_ec;
//crate is needed for computing the complex domain
extern crate fang_oost;
let loan=loan_ec::Loan{
    balance:1000.0, //dollar exposure
    pd:0.03, //annualized probability of default
    lgd:0.5,//expected value of loss given default
    weight:vec![0.4, 0.6],//must add to one, represents exposure to macro variables
    r:0.5, //loss in a liquidity event, as a fraction of the balance
    lgd_variance:0.3,//variance of the loss given default
    num:1000.0//number of loans that have these attributes
};

Then add to the portfolio:

//the higher this number, the more accurate the numerical approximation, but the slower it will run
let num_u:usize=256;
//the truncation of the distribution for numerical purposes
let x_min=-100000.0;
let x_max=0.0;//the maximum of the distribution
let mut ec=loan_ec::EconomicCapitalAttributes::new(
    num_u, 
    weight.len()
);
let u_domain:Vec<Complex<f64>>=fang_oost::get_u_domain(
    num_u, x_min, x_max
).collect();

//the characteristic function for the random variable for LGD...in this case, degenerate (a constant)
let lgd_fn=|u:&Complex<f64>, l:f64, _lgd_v:f64|(-u*l).exp();
        
//cf enhancement for ec
let liquid_fn=loan_ec::get_liquidity_risk_fn(lambda, q);

let log_lpm_cf=loan_ec::get_log_lpm_cf(&lgd_fn, &liquid_fn);
ec.process_loan(&loan, &u_domain, &log_lpm_cf);
//keep adding until there are no more loans left...

Retrieve the (discretized) characteristic function for the portfolio:

//variance of macro variables
let variance=vec![0.3, 0.4]; //must have same length as the weight vector
//in this example, macro variables are Gamma distributed
let v_mgf=|u_weights:&[Complex<f64>]|->Complex<f64>{
    u_weights.iter().zip(&variance).map(|(u, v)|{
        -(1.0-v*u).ln()/v
    }).sum::<Complex<f64>>().exp()
};
let final_cf:Vec<Complex<f64>>=ec.get_full_cf(&v_mgf);

Using the characteristic function, obtain any number of metrics including expected shortfall and value at risk (from my cf_dist_utils repository).

let quantile=0.01;
let (
    expected_shortfall, 
    value_at_risk
)=cf_dist_utils::get_expected_shortfall_and_value_at_risk_discrete_cf(
    quantile, 
    x_min,
    x_max,
    max_iterations,
    tolerance,
    &final_cf
).unwrap();

Dependencies

~2–3MB
~63K SLoC