SynaptraDocs

    Examples

    Example: classifier or detector

    A fraud or abuse detector where the threshold matters as much as the model. A held-out set keeps threshold tuning honest.

    #

    What it registers

    KindNameWhat
    scorerrecall_at_p95Recall at the threshold that gives 95% precision on the tier's labelled set.
    scorerfprFalse-positive rate at the chosen threshold (lower is better).
    datasetlabelled-202620,000 labelled events, split by time, with a held-out slice.
    runnerrunnerpython score_events.py --variant {variant} --tier {tier}

    No action tools: the model is the system. Harnesses don't need tools to be useful.

    #

    How it's scored

    The metric is recall_at_p95 (bar 0.62). Thresholds are chosen on the dev set only; the test set's held-out slice is never used to choose and must pass before adopting. A second harness with metric="fpr" and direction="min" works when you care about false positives first.

    #

    The code

    detector_harness.py
    from sklearn.metrics import precision_recall_curve
    
    h = Harness.create("Fraud detector", metric="recall_at_p95", bar=0.62,
                       url="https://synaptra.my", token=TOKEN)
    h.dataset("labelled-2026", path="gs://risk-data/labelled-2026.parquet", size=20000)
    h.runner("python score_events.py --variant {variant} --tier {tier}")
    
    def recall_at_precision(y, scores, target=0.95):
        p, r, _ = precision_recall_curve(y, scores)
        ok = r[p >= target]
        return float(ok.max()) if ok.size else 0.0
    
    for split in splits_for(tier):                 # time-ordered folds = tasks
        scores = model(variant).predict_proba(split.X)[:, 1]
        rows.append({"task": split.name, "variant": variant, "tier": tier,
                     "metrics": {"recall_at_p95": recall_at_precision(split.y, scores)}})
    h.report(rows, experiment_id=EXP)
    detector_harness.ts
    const h = await Harness.create({ name: "Fraud detector", metric: "recall_at_p95", bar: 0.62,
                                     url: "https://synaptra.my", token: TOKEN });
    await h.dataset("labelled-2026", { path: "gs://risk-data/labelled-2026.parquet", size: 20000 });
    await h.runner("node score_events.js --variant {variant} --tier {tier}");
    
    // The best recall among thresholds whose precision reaches the target
    function recallAtPrecision(y: number[], scores: number[], target = 0.95): number {
      const ranked = scores.map((s, i) => [s, y[i] ?? 0] as const).sort((a, b) => b[0] - a[0]);
      const positives = y.filter((v) => v === 1).length || 1;
      let tp = 0;
      let best = 0;
      ranked.forEach(([, label], i) => {
        tp += label;
        if (tp / (i + 1) >= target) best = Math.max(best, tp / positives);
      });
      return best;
    }
    
    const rows = [];
    for (const split of splitsFor(tier)) {          // time-ordered folds = tasks
      const scores = await model(variant).predictProba(split.X);
      rows.push({ task: split.name, variant, tier, metrics: { recall_at_p95: recallAtPrecision(split.y, scores) } });
    }
    await h.report(rows, { experimentId: EXP });
    #

    A first experiment

    Python
    e = h.propose(
        "Add merchant-velocity features", "velocity-v1",
        {"T1": {"mean_recall_at_p95": 0.66}, "T2": {"mean_recall_at_p95": 0.64}},
        hypothesis="Bursts at one merchant are the missed fraud",
        treatment_axes=["features"],
    )
    TypeScript
    const e = await h.propose({
      title: "Add merchant-velocity features",
      variant: "velocity-v1",
      hypothesis: "Bursts at one merchant are the missed fraud",
      predictions: { T1: { mean_recall_at_p95: 0.66 }, T2: { mean_recall_at_p95: 0.64 } },
      treatmentAxes: ["features"],
    });
    POST /api/harnesses/fraud-detector/experiments
    {"title": "Add merchant-velocity features", "variant": "velocity-v1",
     "hypothesis": "Bursts at one merchant are the missed fraud",
     "treatment_axes": ["features"],
     "predictions": {"T1": {"mean_recall_at_p95": 0.66}, "T2": {"mean_recall_at_p95": 0.64}}}
    #

    What this won't tell you

    • Drift: last quarter's labels may not describe next quarter's fraud. Time-ordered splits show some of it, not all.
    • Label noise: if labels are wrong, a better model can look worse. Audit a sample of disagreements.

    Something unclear or missing? Email support@synaptra.my.