pub struct ScenarioEngine;Expand description
Engine that applies scenarios to portfolio prices and computes P&L impact.
§Example
use std::collections::HashMap;
use fin_primitives::scenario::{ScenarioEngine, ShockType};
let engine = ScenarioEngine;
let shocked = engine.apply_shock(100.0, &ShockType::RelativeShift(-0.30));
assert!((shocked - 70.0).abs() < 1e-9);Implementations§
Source§impl ScenarioEngine
impl ScenarioEngine
Sourcepub fn apply_shock(&self, price: f64, shock: &ShockType) -> f64
pub fn apply_shock(&self, price: f64, shock: &ShockType) -> f64
Apply a single shock to a base price and return the shocked price.
Sourcepub fn run_scenario(
&self,
portfolio: &HashMap<String, f64>,
scenario: &Scenario,
) -> HashMap<String, f64>
pub fn run_scenario( &self, portfolio: &HashMap<String, f64>, scenario: &Scenario, ) -> HashMap<String, f64>
Apply all shocks in a scenario to every matching asset in the portfolio.
Returns a new HashMap with shocked prices. Assets not mentioned in the
scenario retain their original prices.
Sourcepub fn scenario_pnl(
&self,
original: &HashMap<String, f64>,
shocked: &HashMap<String, f64>,
positions: &HashMap<String, f64>,
) -> f64
pub fn scenario_pnl( &self, original: &HashMap<String, f64>, shocked: &HashMap<String, f64>, positions: &HashMap<String, f64>, ) -> f64
Compute the P&L impact of a scenario on a set of positions.
P&L = Σ positions[asset] * (shocked_price[asset] - original_price[asset])
Assets present in positions but absent from either price map are skipped.
Sourcepub fn worst_case<'a>(
&self,
portfolio: &HashMap<String, f64>,
scenarios: &'a [Scenario],
positions: &HashMap<String, f64>,
) -> (&'a Scenario, f64)
pub fn worst_case<'a>( &self, portfolio: &HashMap<String, f64>, scenarios: &'a [Scenario], positions: &HashMap<String, f64>, ) -> (&'a Scenario, f64)
Find the worst-case scenario (most negative P&L) from a list of scenarios.
Returns a reference to the worst scenario and its P&L.
§Panics
Panics if scenarios is empty.
Auto Trait Implementations§
impl Freeze for ScenarioEngine
impl RefUnwindSafe for ScenarioEngine
impl Send for ScenarioEngine
impl Sync for ScenarioEngine
impl Unpin for ScenarioEngine
impl UnsafeUnpin for ScenarioEngine
impl UnwindSafe for ScenarioEngine
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more