Scenario

class quantsupport.risk.Scenario(shocks: Sequence[Shock])

A combination of temporary market shocks used in with context, scenario:.

Parameters:

shocks (sequence of Shock) – Typed market shocks, applied in their supplied order. Each Shock supplies a target selector, value and ScenarioType. Required keyword argument; an empty sequence is allowed and adds no shocks of its own.

Notes

Enter after a PricingContext. Nested scenarios compose shocks in entry order. Overlapping shocks also compose without deduplication; fully qualified targets are still token selectors and can select more than one quote. The base context retains its market inputs and constructed objects. Requested curves and volatility markets have independent AD state. Leaving the scope, including through an exception, discards the temporary view. Reusable. The Scenario object and active-scope stacks are thread-confined; enter and exit on the owning thread, exiting before the enclosing PricingContext. Do not retain AD-backed shocked market objects for use after scenario exit.

Source declaration

Methods

__enter__() → Self
Activate shocks on the innermost context entered on this thread.
Returns:

This scenario. It retains the selected context until exit, validates the combined shocks, and constructs an independent market cache on demand. Distinct nested scenarios compose in entry order without mutating base inputs.

Return type:

Scenario

Raises:
  • RuntimeError – If no PricingContext is active on this thread, or this Scenario object is already active. Calling context.initialize() alone does not enter a scope.

  • QuantSupportError – If the selected context is uninitialized or the shocks fail native validation.

__exit__(_exc_type: Any = ..., _exc_value: Any = ..., _traceback: Any = ...) → bool
Discard the temporary market view and release its context reference.
Parameters:
  • _exc_type (type or None, optional) – Exception class supplied by the with statement; defaults to None. Ignored.

  • _exc_value (BaseException or None, optional) – Exception instance supplied by the with statement; defaults to None. Ignored.

  • _traceback (traceback or None, optional) – Exception traceback supplied by the with statement; defaults to None. Ignored.

Returns:

Always False, so exceptions propagate. The prior scenario or base view is restored and this scenario may be entered again after exit.

Return type:

bool

Raises:

RuntimeError – If this scenario is inactive or scenarios exit out of reverse entry order.

Properties

property shocks: list[Shock]

Read-only ordered shocks, returned as a fresh list of shock copies.