Skip to main content

ScenarioEngine

Struct ScenarioEngine 

Source
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

Source

pub fn apply_shock(&self, price: f64, shock: &ShockType) -> f64

Apply a single shock to a base price and return the shocked price.

Source

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.

Source

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.

Source

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§

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.