Adapters¶
Concrete implementations of the ports. Optional dependencies are imported lazily inside the methods that need them.
Inbound¶
Facade¶
wayfault.adapters.inbound.api ¶
Thin Python facade over the WrongWayRiskService.
estimate_wwr ¶
estimate_wwr(exposure: ExposureSource, credit: CreditCurveSource, model: DependenceModel, discount: ndarray | None = None, pfe_quantile: float = 0.95, sink: ResultSink | None = None) -> WWRResult
Estimate Wrong-Way Risk for one counterparty.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exposure
|
ExposureSource
|
The outbound-port collaborators. |
required |
credit
|
ExposureSource
|
The outbound-port collaborators. |
required |
model
|
ExposureSource
|
The outbound-port collaborators. |
required |
discount
|
ndarray | None
|
Optional discount factors on the grid. |
None
|
pfe_quantile
|
float
|
Quantile for the PFE profile. |
0.95
|
sink
|
ResultSink | None
|
Optional result sink; if given, the result is also written to it. |
None
|
Returns:
| Type | Description |
|---|---|
WWRResult
|
The full result object. |
Source code in src/wayfault/adapters/inbound/api.py
CLI¶
wayfault.adapters.inbound.cli ¶
argparse CLI: python -m wayfault (stdlib only).
main ¶
CLI entry point. Returns a process exit code.
Source code in src/wayfault/adapters/inbound/cli.py
Outbound — Exposure sources¶
wayfault.adapters.outbound.exposure_inmemory ¶
In-memory exposure source (no extras).
InMemoryExposureSource ¶
Wraps an in-memory array (or EE profile) as an :class:ExposureSource.
Source code in src/wayfault/adapters/outbound/exposure_inmemory.py
from_ee_profile
classmethod
¶
Build a degenerate source from a precomputed EE profile.
Source code in src/wayfault/adapters/outbound/exposure_inmemory.py
wayfault.adapters.outbound.exposure_csv ¶
CSV/Parquet exposure source ([io] extra, lazy import).
CsvExposureSource ¶
Loads an exposure cube from CSV or Parquet via pandas/pyarrow.
The file's column headers are interpreted as tenor year-fractions and each row as one Monte-Carlo scenario.
Source code in src/wayfault/adapters/outbound/exposure_csv.py
load ¶
Read the file lazily and build an :class:ExposureCube.
Source code in src/wayfault/adapters/outbound/exposure_csv.py
Outbound — Credit sources¶
wayfault.adapters.outbound.credit_flat ¶
Flat-hazard credit-curve source (no extras).
FlatHazardCreditCurveSource ¶
Supplies a flat-hazard :class:CreditCurve.
Source code in src/wayfault/adapters/outbound/credit_flat.py
wayfault.adapters.outbound.credit_piecewise ¶
Piecewise-constant-hazard credit-curve source (no extras).
PiecewiseHazardCreditCurveSource ¶
Supplies a piecewise-constant-hazard :class:CreditCurve.
Source code in src/wayfault/adapters/outbound/credit_piecewise.py
wayfault.adapters.outbound.credit_csv ¶
CSV credit-curve source ([io] extra, lazy import).
Expects a CSV with columns knot and hazard (one row per segment). The
recovery rate is supplied at construction.
CsvCreditCurveSource ¶
Loads a piecewise-constant-hazard curve from CSV via pandas.
Source code in src/wayfault/adapters/outbound/credit_csv.py
load ¶
Read the curve lazily and build a :class:CreditCurve.
Source code in src/wayfault/adapters/outbound/credit_csv.py
Outbound — Dependence models¶
wayfault.adapters.outbound.dependence_independent ¶
wayfault.adapters.outbound.dependence_hullwhite ¶
Hull-White stochastic-hazard dependence model (FR-WWR-022).
Canonical WWR formulation following Hull & White (2012):
.. math::
\lambda(t) = \exp(a(t) + b\, V(t))
The intensity offset a(t) is solved per tenor so that, integrated over the
exposure distribution, the model reproduces the input curve's marginal PDs
(arbitrage consistency). b is the wrong-way-risk knob: b > 0 is
wrong-way, b < 0 right-way, b = 0 independence.
HullWhiteHazardModel ¶
Stochastic-hazard WWR model with intensity exp(a(t) + b*V(t)).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
b
|
float
|
The wrong-way-risk coupling. |
0.0
|
Source code in src/wayfault/adapters/outbound/dependence_hullwhite.py
calibrate_to_curve ¶
Record the target marginal PDs; a(t) is solved at re-weighting.
The per-tenor offset cancels in the normalised conditional expectation, so calibration only needs the target marginal PDs, which are recovered from the curve on demand.
Source code in src/wayfault/adapters/outbound/dependence_hullwhite.py
conditional_ee ¶
Conditional EE by re-weighting scenarios toward their default risk.
Fully vectorised: every tenor column is re-weighted in a single numpy expression via the shared column-wise softmax core.
Source code in src/wayfault/adapters/outbound/dependence_hullwhite.py
implied_marginal_pd ¶
Model-implied marginal PDs integrated over the exposure distribution.
By construction this reproduces curve.marginal_pd(grid) (criterion
4): the per-scenario conditional PD is the target PD scaled by the
normalised exposure weight, whose scenario mean is the target.
Source code in src/wayfault/adapters/outbound/dependence_hullwhite.py
dependence_param ¶
wayfault.adapters.outbound.dependence_copula ¶
Gaussian-copula dependence model (FR-WWR-023).
Couples a credit latent factor to a portfolio/market factor with correlation
rho via a one-factor Gaussian copula. The conditional default probability
given the (rank-normalised) portfolio value re-weights scenarios to produce the
conditional EE. rho > 0 is wrong-way, rho < 0 right-way.
Fully vectorised across tenors via the shared column-wise re-weighting core.
GaussianCopulaModel ¶
One-factor Gaussian-copula WWR model with correlation rho.
Source code in src/wayfault/adapters/outbound/dependence_copula.py
calibrate_to_curve ¶
No eager state: marginal PDs are read from the curve on demand.
conditional_ee ¶
Conditional EE via Gaussian-copula default re-weighting (vectorised).
Source code in src/wayfault/adapters/outbound/dependence_copula.py
dependence_param ¶
wayfault.adapters.outbound.dependence_archimedean ¶
Advanced Archimedean-copula dependence models (numpy-only).
These couple a credit margin to the portfolio/market margin through an Archimedean copula, using the copula's closed-form h-function (conditional CDF) as the per-scenario default re-weight. Unlike the Gaussian copula they capture asymmetric tail dependence, which is the realistic shape of wrong-way risk: defaults cluster precisely in the high-exposure tail.
- :class:
ClaytonCopulaModel— lower-tail dependence;theta > 0is wrong-way (andtheta -> 0recovers independence). Pure powers, no special functions. - :class:
FrankCopulaModel— symmetric, no tail dependence; the sign ofthetaflips the direction (theta > 0wrong-way,theta < 0right-way). Uses onlyexp.
Both are fully vectorised across tenors via the shared re-weighting core. The
market margin is oriented so that high exposure maps to the lower tail
(v = 1 - empirical_grade(V)), making a positive theta wrong-way.
ClaytonCopulaModel ¶
Clayton-copula WWR model with lower-tail dependence.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
theta
|
float
|
Dependence strength |
1.0
|
Source code in src/wayfault/adapters/outbound/dependence_archimedean.py
calibrate_to_curve ¶
conditional_ee ¶
Conditional EE via the Clayton h-function re-weighting (vectorised).
Source code in src/wayfault/adapters/outbound/dependence_archimedean.py
dependence_param ¶
FrankCopulaModel ¶
Frank-copula WWR model (symmetric, sign-controlled direction).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
theta
|
float
|
Non-zero dependence parameter. |
2.0
|
Source code in src/wayfault/adapters/outbound/dependence_archimedean.py
calibrate_to_curve ¶
conditional_ee ¶
Conditional EE via the Frank h-function re-weighting (vectorised).
Source code in src/wayfault/adapters/outbound/dependence_archimedean.py
dependence_param ¶
Outbound — Calibrators¶
wayfault.adapters.outbound.calibrator_regression ¶
Numpy-only regression calibrator (FR-WWR-041).
RegressionCalibrator ¶
Estimate the Hull-White b by least-squares regression.
The Hull-White intensity is lambda = exp(a + b*V), so log lambda is
linear in the portfolio value V with slope b. The credit_factor
samples are interpreted as realised hazard rates; their logs are regressed
on the portfolio value to recover b (and the intercept a).
fit ¶
Return {"a": intercept, "b": slope} from an OLS fit.
Source code in src/wayfault/adapters/outbound/calibrator_regression.py
wayfault.adapters.outbound.calibrator_sklearn ¶
scikit-learn covariate-hazard calibrator ([ml] extra, lazy import).
SklearnSurvivalCalibrator ¶
Fit a covariate-driven (log-)hazard model and reduce it to b.
Uses a gradient-boosting regressor on log(credit_factor) against the
portfolio value, then linearises the fitted response to a single
Hull-White b slope that a :class:DependenceModel can consume. Raises
:class:MissingDependencyError if scikit-learn is absent.
Source code in src/wayfault/adapters/outbound/calibrator_sklearn.py
fit ¶
Fit the survival surrogate and return {"b": slope}.
Source code in src/wayfault/adapters/outbound/calibrator_sklearn.py
Outbound — Sinks¶
wayfault.adapters.outbound.sinks ¶
Result sinks: in-memory dict and JSON report writer (stdlib json).
DictResultSink ¶
Captures the latest result as a plain dictionary in memory.
Source code in src/wayfault/adapters/outbound/sinks.py
Outbound — Visualization¶
The plotting API is documented on the dedicated Visualization page.