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

Method allocate

crates/openquant/src/hcaa.rs:38–142  ·  view source on GitHub ↗
(
        &mut self,
        asset_names: &[String],
        asset_prices: Option<&DMatrix<f64>>,
        asset_returns: Option<&DMatrix<f64>>,
        covariance_matrix: Option<&DMatrix<f64>>,
      

Source from the content-addressed store, hash-verified

36 Self {
37 weights: Vec::new(),
38 ordered_indices: Vec::new(),
39 clusters: Vec::new(),
40 calculate_expected_returns: calculate_expected_returns.to_string(),
41 }
42 }
43
44 #[allow(clippy::too_many_arguments)]
45 pub fn allocate(
46 &mut self,
47 asset_names: &[String],
48 asset_prices: Option<&DMatrix<f64>>,
49 asset_returns: Option<&DMatrix<f64>>,
50 covariance_matrix: Option<&DMatrix<f64>>,
51 expected_asset_returns: Option<&[f64]>,
52 allocation_metric: &str,
53 confidence_level: f64,
54 optimal_num_clusters: Option<usize>,
55 resample_by: Option<&str>,
56 ) -> Result<(), HcaaError> {
57 if asset_prices.is_none() && asset_returns.is_none() && covariance_matrix.is_none() {
58 return Err(HcaaError::NoData);
59 }
60 if !matches!(
61 allocation_metric,
62 "minimum_variance"
63 | "minimum_standard_deviation"
64 | "sharpe_ratio"
65 | "equal_weighting"
66 | "expected_shortfall"
67 | "conditional_drawdown_risk"
68 ) {
69 return Err(HcaaError::UnknownAllocationMetric(allocation_metric.to_string()));
70 }
71 let n_assets = asset_names.len();
72 if n_assets == 0 {
73 return Err(HcaaError::NoData);
74 }
75
76 let returns_owned = if let Some(r) = asset_returns {
77 r.clone_owned()
78 } else if let Some(p) = asset_prices {
79 let step = freq_step(resample_by);
80 let sampled = resample_prices(p, step);
81 returns_from_prices(&sampled)?
82 } else {
83 DMatrix::zeros(0, n_assets)
84 };
85 if returns_owned.ncols() != n_assets && returns_owned.nrows() > 0 {
86 return Err(HcaaError::DimensionMismatch(
87 "asset_returns columns != asset_names length",
88 ));
89 }
90
91 let covariance_owned = if let Some(cov) = covariance_matrix {
92 cov.clone_owned()
93 } else {
94 covariance(&returns_owned)?
95 };

Callers

nothing calls this directly

Calls 11

mean_expected_returnsFunction · 0.85
recursive_bisectionFunction · 0.85
lenMethod · 0.80
freq_stepFunction · 0.70
resample_pricesFunction · 0.70
returns_from_pricesFunction · 0.70
covarianceFunction · 0.70
cov2corrFunction · 0.70
single_linkage_childrenFunction · 0.70
quasi_diagonalizationFunction · 0.70

Tested by

no test coverage detected