Skip to content

Risk and policy

Three modules collaborate to keep betting strategies within acceptable bounds: Whale.Risk quantifies danger, Whale.Policy declares rules, and Whale.Guards enforces hard limits at the execution boundary.

Risk assessment functions that operate on typed inputs from Whale.Types.

import Whale.Risk exposing (expected_loss, variance_of_return, ruin_probability)
let el = expected_loss fraction odds win_prob
let var = variance_of_return fraction odds win_prob
let ruin = ruin_probability fraction odds win_prob bankroll target_ruin_level
  • expected_loss: the mean loss per unit staked given the sizing and edge.
  • variance_of_return: second moment of per-bet returns, useful for comparing strategies at equal expected value.
  • ruin_probability: the probability that the bankroll reaches a specified floor before doubling, given fixed Kelly-fraction betting.

Declarative rules that constrain which bets a strategy may place. Policies compose: a bet must satisfy every active policy to proceed.

import Whale.Policy exposing (Policy, require)
let rules = [
Policy.min_edge 0.02,
Policy.max_odds (Odds.decimal 20.0),
Policy.max_concurrent 5,
Policy.cooldown_seconds 60,
]
let verdict = Policy.evaluate rules candidate_bet
-- verdict : Result () PolicyViolation

evaluate returns Ok () when all rules pass, or an Err describing the first violated policy. The caller decides whether to reject or log.

Guards are hard limits that act as a final gate before execution. Unlike policies (which advise), guards halt execution unconditionally when breached.

import Whale.Guards exposing (Guard, enforce)
let guards = [
Guard.max_stake (Money.usd 500.00),
Guard.max_daily_loss (Money.usd 1_000.00),
Guard.max_drawdown_pct 0.25,
]
let check = Guard.enforce guards roll candidate_stake
-- check : Result () GuardTripped

When a guard trips, the bet does not execute. The GuardTripped value carries the guard name and the value that breached it, for diagnostics.

A typical execution path:

  1. Compute StakeFraction via Whale.Sizing.
  2. Convert to Money via Whale.BankRoll.stake.
  3. Evaluate Whale.Policy rules against the candidate.
  4. Enforce Whale.Guards on the final stake and bankroll state.
  5. If all pass, record the bet. Otherwise, skip or reduce.