Skip to main content

Risk model liquidation kernels (v2 + v3) — pure compute, no DB access

Project description

risk-kernel

The risk model liquidation kernels as a standalone package — pure compute (numpy + stdlib), zero DB access, no write path. Powers the on-demand LTV↔CRR estimator endpoint in core-api (office #490 / #537).

Two model namespaces, mirrored from core:

  • risk_kernel.v2 — the v2 liquidation-simulation kernel. Operates on in-memory PricePaths / SellOrderbook / BorrowerPosition structures (risk_kernel.v2.liquidator.types).
  • risk_kernel.v3 — the v3 market-clearing kernel: clearing kernel, impact law, CRR stats, liquidation-wall curve math, FHS residuals/simulator. Entry points risk_kernel.v3.kernel.run_clearing_kernel / run_multi_asset_kernel on KernelInputs / MultiAssetKernelInputs.

Loading inputs from a database is the consumer's job, never this package's.

Consumers

  • core-api — installs this package from PyPI (balabs-risk-kernel) and runs the kernels in-process for the estimator endpoint.
  • core — NOT hooked up yet, deliberately out of scope for now. core keeps its own in-tree copies at src/risk_model_v2/ and src/risk_model_v3/ and remains the production source of truth for kernel logic.

Source of truth & sync protocol

Until core itself consumes this package, this repo is a mirror of core's kernels. Per-model pins (the subtrees sync at different cadences):

  • v2 source: blockanalitica/core src/risk_model_v2/liquidator/ commit: c0bb2732e51cbb2338e0db5f49799088529e003c (main, 2026-07-03)
  • v3 source: blockanalitica/core src/risk_model_v3/ commit: 0d0695dda00bafe99ad3294958203a7da98f76fd (PR #78, 2026-07-13)

Kernel changes land in core first (production model), then get pulled here, per model:

git -C ../core diff <v2-pinned-commit>..main -- src/risk_model_v2/liquidator/
git -C ../core diff <v3-pinned-commit>..main -- src/risk_model_v3/

Apply the changes to src/risk_kernel/v2/ / src/risk_kernel/v3/, bump the model's pin above and the package version, then bump the pin in consumers. The divergences below are expected diff noise; everything else must match, e.g.:

diff -r ../core/src/risk_model_v2/liquidator/ src/risk_kernel/v2/liquidator/
diff -r ../core/src/risk_model_v3/ src/risk_kernel/v3/

There is no test guardrail — the pins are the only drift baseline.

Deliberate divergences from core (the ONLY ones allowed)

v2

  1. Persistence stripped from v2/liquidator/pipeline/processors/liquidator.py: the _persist_market_crr / _persist_scenario_paths / _rewrite_market_crr_tiers / _prune_market_crr_snapshot24 methods, their call sites in sync(), the risk_model_v2.models import block, and the imports only they used (asdict, timedelta, MARKET_CRR_SNAPSHOT24_RETENTION_DAYS, CapturedScenarioPaths, select_bands). sync() computes and returns CRRSummary objects, nothing else. pipeline/scenario_paths.py is unused after the strip but kept verbatim so the drift diff stays trivial.
  2. Package renamed: core's risk_model_v2.liquidatorrisk_kernel.v2.liquidator (mechanical import rewrite; tree structure is otherwise 1:1 so the diff above works).
  3. v2/constants.py is a 3-constant subset of core's src/risk_model_v2/constants.py (FORECAST_STEP, N_MC, SEED) — the only values the kernels import from outside the mirrored subtrees.

v3

  1. Impure modules excluded — only the pure compute modules are mirrored: kernel, impact, stats, types, constants, liquidation_wall/{builder,flow,types}, fhs/{residuals,simulator}. Excluded: models, persistence, pipeline/, tasks, calibration, liquidation_wall/{fetcher,sources}, fhs/forecaster. Mirrored modules import only numpy/stdlib/each other (v3 → v2.constants allowed, mirroring core's fhs/simulator.py).
  2. Package renamed: core's risk_model_v3risk_kernel.v3, risk_model_v2risk_kernel.v2 (mechanical import rewrite, applies to docstrings too).

Anything else diverging from core = drift = bug.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

balabs_risk_kernel-0.2.1.tar.gz (69.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

balabs_risk_kernel-0.2.1-py3-none-any.whl (81.2 kB view details)

Uploaded Python 3

File details

Details for the file balabs_risk_kernel-0.2.1.tar.gz.

File metadata

  • Download URL: balabs_risk_kernel-0.2.1.tar.gz
  • Upload date:
  • Size: 69.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for balabs_risk_kernel-0.2.1.tar.gz
Algorithm Hash digest
SHA256 7ba30a7827163ff0e246c1974d2a351c02af3bee48f3263ad6aa8a53709316e5
MD5 cce45b2d81b1177b1571f64387735ef9
BLAKE2b-256 5c9e03dd4b11589f3e95efa632f42d2af74d1d70b9928e4787f7f49e1d7bdc67

See more details on using hashes here.

Provenance

The following attestation bundles were made for balabs_risk_kernel-0.2.1.tar.gz:

Publisher: release.yml on blockanalitica/risk-kernel

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file balabs_risk_kernel-0.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for balabs_risk_kernel-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f2aca58227d2a8f88917c459a33c15b697a6c932019648ebd1f932ea40938db3
MD5 0f1a6adb47686820d05edf6c757261c0
BLAKE2b-256 1e34f7340ee70f507a0a280a5eb836a4f09fc7eae2598f6b3111cf4f3cf00bf6

See more details on using hashes here.

Provenance

The following attestation bundles were made for balabs_risk_kernel-0.2.1-py3-none-any.whl:

Publisher: release.yml on blockanalitica/risk-kernel

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page