RiskEngineV2 adds a logistic-regression PD model over a normalized agent feature vector (reputation, accuracy, dispute rate, experience, revenue, stake, category risk). train() fits weights by deterministic, L2-regularized batch gradient descent on realized default/repay outcomes; score() returns pd + ml_score + rules_score + a convex blended_score, so v2 augments the v1 rules policy rather than replacing it. A domain-calibrated prior makes it useful before any training data. 5 new tests, full suite 212 green. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
44 lines
2.2 KiB
Markdown
44 lines
2.2 KiB
Markdown
# p7 — ML risk-engine v2
|
||
|
||
> **Learn creditworthiness from realized outcomes, not just rules.** Add a trained
|
||
> probability-of-default model on top of the hand-tuned v1 policy, and blend the two
|
||
> so credit decisions improve as the protocol accumulates repayment/default history.
|
||
|
||
## Why
|
||
The v1 risk policy (`risk_policy.ts`) is a transparent rules engine — great for
|
||
launch and auditability, but it can't *learn*. As Cred402 accrues realized credit
|
||
outcomes (who repaid, who defaulted), the cheapest accuracy gain is a model fit to
|
||
that history. Keeping the rules score alongside the learned score preserves
|
||
explainability and gives a safe fallback.
|
||
|
||
## Scope
|
||
- **Feature model.** A stable, normalized feature vector per agent: reputation,
|
||
accuracy, dispute rate, experience (job count), recent revenue, stake, and
|
||
service-category risk.
|
||
- **Learned PD model.** A logistic-regression probability-of-default classifier,
|
||
fit by batch gradient descent on labeled outcomes. Deterministic and dependency-
|
||
free (no external ML runtime), so it is reproducible and the weights are
|
||
inspectable/auditable.
|
||
- **Blend, don't replace.** `blended_score = α·ml_score + (1−α)·rules_score`, so
|
||
v2 augments v1 rather than overriding it. A hand-calibrated prior makes the model
|
||
useful before any training data exists.
|
||
|
||
## Deliverables (shipped)
|
||
- `lib/services/risk_engine_v2.ts` — `RiskEngineV2`:
|
||
- `extractFeatures(agent)` → normalized `[0,1]` `FeatureVector`.
|
||
- `predictPd(features)` → probability of default.
|
||
- `train(samples, opts)` → fits weights by gradient descent on log-loss
|
||
(deterministic; L2-regularized).
|
||
- `score(agentId)` → `{ pd, ml_score, rules_score, blended_score, risk_band }`.
|
||
- `PRIOR_WEIGHTS` — domain-calibrated prior; `weights()` exposes the model.
|
||
- `test/risk_engine_v2.test.ts` — prior ranking, training reduces log-loss + learns
|
||
signals, blended score, normalization, determinism.
|
||
|
||
## Success metric
|
||
The blended score out-predicts the rules-only score on held-out realized outcomes,
|
||
while every decision remains explainable (rules score + inspectable model weights).
|
||
|
||
## Later (p8+)
|
||
zk/light-client receipt proofs (trust-ladder Stage 4); enterprise RealFi GA; new
|
||
service verticals (compute/inference/data markets).
|