MCPcopy Create free account
hub / github.com/Open-Quant/openquant / inverse_variance

Function inverse_variance

crates/openquant/src/portfolio_optimization.rs:282–298  ·  view source on GitHub ↗
(cov: &DMatrix<f64>, bounds: &[(f64, f64)])

Source from the content-addressed store, hash-verified

280}
281
282fn inverse_variance(cov: &DMatrix<f64>, bounds: &[(f64, f64)]) -> Result<Vec<f64>, AllocError> {
283 check_bounds_feasible(bounds)?;
284 let diag = cov.diagonal();
285 if diag.iter().any(|v| *v == 0.0) {
286 return Err(AllocError::OptimizationFailed("covariance contained zero on diagonal"));
287 }
288 let mut ivp: Vec<f64> = diag.iter().map(|v| 1.0 / v).collect();
289 let sum: f64 = ivp.iter().sum();
290 if sum == 0.0 {
291 return Err(AllocError::OptimizationFailed("zero inverse variance sum"));
292 }
293 for v in ivp.iter_mut() {
294 *v /= sum;
295 }
296 project_to_bounds(&mut ivp, bounds)?;
297 Ok(ivp)
298}
299
300fn solve_min_vol(cov: &DMatrix<f64>, bounds: &[(f64, f64)]) -> Result<Vec<f64>, AllocError> {
301 check_bounds_feasible(bounds)?;

Callers 2

allocate_from_inputsFunction · 0.85

Calls 2

check_bounds_feasibleFunction · 0.70
project_to_boundsFunction · 0.70

Tested by

no test coverage detected