| 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 creditfaasdemo.

Create instances of the Loan struct:

rust 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:

```rust //the higher this number, the more accurate the numerical approximation, but the slower it will run let numu:usize=256; //the truncation of the distribution for numerical purposes let xmin=-100000.0; let xmax=0.0;//the maximum of the distribution let mut ec=loanec::EconomicCapitalAttributes::new( numu, weight.len() ); let udomain:Vec>=fangoost::getudomain( numu, xmin, xmax ).collect();

//the characteristic function for the random variable for LGD...in this case, degenerate (a constant) let lgdfn=|u:&Complex, l:f64, _lgdv:f64|(-u*l).exp();

//cf enhancement for ec let liquidfn=loanec::getliquidityrisk_fn(lambda, q);

let loglpmcf=loanec::getloglpmcf(&lgdfn, &liquidfn); ec.processloan(&loan, &udomain, &loglpmcf); //keep adding until there are no more loans left... ```

Retrieve the (discretized) characteristic function for the portfolio:

rust //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 cfdistutils repository).

rust 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();