11个版本
0.2.1 | 2020年11月27日 |
---|---|
0.2.0 | 2019年3月2日 |
0.1.4 | 2019年1月18日 |
0.1.0 | 2018年12月29日 |
0.0.5 | 2018年12月29日 |
#849 在 算法
每月 46 次下载
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] |
贷款组合经济资本分配的实用工具
此库具有相对有观点的API,用于创建贷款组合并执行汇总统计(如贷款层级风险贡献和预期值)。
安装
在您的Cargo.toml中添加以下内容
loan_ec= "0.1.4"
使用
完整示例在 credit_faas_demo 中。
创建Loan结构的实例
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
};
然后将它们添加到组合中
//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...
检索组合的(离散化)特征函数
//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);
使用特征函数,获取任何数量的指标,包括预期亏损和风险价值(来自我的 cf_dist_utils 存储库)。
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();
依赖项
~2–3MB
~63K SLoC