fintech-algorithms
Using a coding agent? Give it the skill: npx skills add IslamBaraka90/Fintech-Algorithms-Library What it does →

Cost-Sensitive Threshold Optimization

Install and import#

bash
npm install fintech-algorithms
ts
import { costSensitiveThresholdOptimization } from "fintech-algorithms/model-validation-and-backtesting/classification-and-score-validation/cost-sensitive-threshold-optimization";

Signature#

costSensitiveThresholdOptimization(inputs)

Worked example#

verified This is the worked example published in the article, replayed by the test suite on every run. The output cannot drift.

Input#

inputs
{
  "records": [
    {
      "id": "R01",
      "label": 1,
      "score": 0.95,
      "probability": 0.92,
      "weight": 1,
      "sector": "Banking",
      "country": "Egypt",
      "regime": "Expansion",
      "score_available_at": "2025-01-01T00:00:00Z",
      "label_available_at": "2026-01-01T00:00:00Z"
    },
    {
      "id": "R02",
      "label": 0,
      "score": 0.9,
      "probability": 0.88,
      "weight": 1,
      "sector": "Insurance",
      "country": "Egypt",
      "regime": "Expansion",
      "score_available_at": "2025-01-01T00:00:00Z",
      "label_available_at": "2026-01-01T00:00:00Z"
    },
    {
      "id": "R03",
      "label": 1,
      "score": 0.9,
      "probability": 0.84,
      "weight": 1,
      "sector": "Markets",
      "country": "Saudi Arabia",
      "regime": "Expansion",
      "score_available_at": "2025-01-01T00:00:00Z",
      "label_available_at": "2026-01-01T00:00:00Z"
    }
  ],
  "evaluation_cutoff": "2026-06-30T00:00:00Z",
  "costs": {
    "false_positive": 1,
    "false_negative": 6,
    "true_positive": 0,
    "true_negative": 0
  }
}

Call#

costSensitiveThresholdOptimization(inputs)

Returns#

object with 8 fields: candidates, optimal_threshold, optimal_expected_cost, optimal_selected_rate, optimal_confusion, costs, tie_break, state

{
  "candidates": [
    {
      "threshold": null,
      "true_positive": 0,
      "false_positive": 0,
      "true_negative": 14,
      "false_negative": 10,
      "selected_weight": 0,
      "selected_rate": 0,
      "expected_cost": 2.5
    },
    {
      "threshold": 0.95,
      "true_positive": 1,
      "false_positive": 0,
      "true_negative": 14,
      "false_negative": 9,
      "selected_weight": 1,
      "selected_rate": 0.041666666666666664,
      "expected_cost": 2.25
    },
    {
      "threshold": 0.9,
      "true_positive": 2,
      "false_positive": 1,
      "true_negative": 13,
      "false_negative": 8,
      "selected_weight": 3,
      "selected_rate": 0.125,
      "expected_cost": 2.0416666666666665
    }
  ],
  "optimal_threshold": 0.1,
  "optimal_expected_cost": 0.5,
  "optimal_selected_rate": 0.9166666666666666,
  "optimal_confusion": {
    "true_positive": 10,
    "false_positive": 12,
    "true_negative": 2,
    "false_negative": 0
  },
  "costs": {
    "false_positive": 1,
    "false_negative": 6,
    "true_positive": 0,
    "true_negative": 0
  },
  "tie_break": "minimum-cost-then-lower-selected-weight-then-higher-threshold",
  "state": "threshold-selected"
}

Other exports#

This module also exports rocCurveAndRocAuc, precisionRecallCurveAndPrAuc, brierScore, logLoss, reliabilityDiagramAndExpectedCalibrationError, gainsLiftAndDecileCapture, scoreStabilityAndMigrationMatrix, sliceBasedValidationBySectorCountryAndRegime, rareEventBacktestAndConfidenceBounds, calculate. Every module additionally exports run as an alias of its primary function, and a meta object carrying its catalog id, domain, family, shape and article URL.

Diagrams#

Cost-Sensitive Threshold Optimization — article hero
Cost-Sensitive Threshold Optimization — decision boundaries
Cost-Sensitive Threshold Optimization — method selection
Cost-Sensitive Threshold Optimization — system map

How it works#

This page states the contract — how to call it correctly. The article explains the concept: why it works, and where it breaks.

Read the article →

References#

The rest of the Classification and Score Validation family#