Skip to main content

RiskContribution

Struct RiskContribution 

Source
pub struct RiskContribution;
Expand description

Computes per-position marginal risk contributions from a covariance matrix.

§Example

use fin_primitives::attribution::RiskContribution;

// Two-asset portfolio: equal weights, uncorrelated, same vol
let weights = vec![0.5, 0.5];
let cov = vec![
    vec![0.04, 0.0],  // var(A) = 0.04, cov(A,B) = 0
    vec![0.0,  0.04], // cov(B,A) = 0,  var(B) = 0.04
];
let ids = vec!["A".to_owned(), "B".to_owned()];
let result = RiskContribution::compute(&ids, &weights, &cov).unwrap();
// Each asset contributes 50% of portfolio risk
assert!((result[0].pct_risk_contribution - 50.0).abs() < 0.01);

Implementations§

Source§

impl RiskContribution

Source

pub fn compute( ids: &[String], weights: &[f64], cov: &[Vec<f64>], ) -> Result<Vec<PositionRiskContribution>, FinError>

Compute per-position marginal risk contributions.

§Parameters
  • ids — Position identifiers, length N.
  • weights — Portfolio weights, length N.
  • cov — N×N covariance matrix (row-major).
§Errors

Returns FinError::InvalidInput if:

  • ids, weights, and cov lengths are inconsistent.
  • Portfolio volatility is zero (all-zero weights or flat covariance).
  • Any weight or covariance entry is non-finite.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.