Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
29 changes: 29 additions & 0 deletions LeanPool/MathFin.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/-
Copyright (c) 2026 Raphael Coelho. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Raphael Coelho
-/
module

-- Ported from Formal MathFin v2.0.0 for Lean Pool.

public import LeanPool.MathFin.Pricing.FTAP.MarketFTAP
public import LeanPool.MathFin.Models.Binomial.Market
public import LeanPool.MathFin.Models.ModelFree.RoperConverse
public import LeanPool.MathFin.Models.Rates.VasicekMarket
public import LeanPool.MathFin.Risk.AcceptanceSet
public import LeanPool.MathFin.Risk.WorstCaseRisk

/-!
# Pricing measures, market models and coherent risk

Source: arxiv:2606.01356, url:https://github.com/formal-applied-math/formal-mathfin/tree/55e29a93e75f10b40f61f45c78a6058b2938045f
Authors: Raphael Coelho, Robert Martin, Yang Ye, Alfredo Garcia, Aleksey Salkutsan
Status: verified
Main declarations: `MathFin.Market.noArbitrage_iff_exists_isPricingMeasure_of_finite`,
`MathFin.Contracts.Binomial.price_pay_eq_binomialPrice_of_isPricingMeasure`,
`MathFin.exists_market_of_isNormalizedCallPrice`, `MathFin.Rates.price_pay_vasicekMarket`,
`MathFin.coherentRisk_isLUB`
Tags: mathematical-finance, discrete-time-martingales, asset-pricing, risk-measures
MSC: 91G20, 91G70, 60G44
-/
135 changes: 135 additions & 0 deletions LeanPool/MathFin/Choice/Performance/BetSizeSigmoid.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/-
Copyright (c) 2026 Yang Ye. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yang Ye, Raphael Coelho
-/
module

-- Ported from Formal MathFin v2.0.0 for Lean Pool.

public import LeanPool.MathFin.Choice.Performance.Ratios

/-!
# Sigmoid bet-sizing function

López de Prado (*Advances in Financial Machine Learning*, Wiley 2018, ch. 10) sizes
a bet from the divergence `x = f - p` between the forecast price `f` and the
current market price `p` via the sigmoid `m(x) = x / √(w + x²)` (`w > 0` a width
parameter), rather than a hard threshold.

This file proves the three structural properties that justify using `m` as a
bet-sizing map: it is odd, bounded in `(-1,1)`, and strictly increasing in `x`
whenever `w > 0`. It also ties `m` to the Kelly criterion: fed the z-statistic
`z = (p − ½)/√(p(1 − p))` of a predicted win probability `p` (scaled by `√w`), the
sigmoid returns exactly `2p − 1`, the Kelly fraction of an even-money bet
(`kellyFraction p 1`, `Choice/Performance/Ratios.lean`). López de Prado's probability-based size
`2Φ(z) − 1` (ch. 10) is a different map and is not treated here.

## Main definitions

* `betSize`: the bet-sizing sigmoid `m(x) = x / √(w + x²)` with width parameter `w`.

## Main results

* `betSize_odd`: `m w (-x) = -m w x`.
* `betSize_mem_Ioo`: `m w x ∈ (-1, 1)` for `w > 0`.
* `betSize_strictMono`: `w > 0 → StrictMono (m w ·)`.
* `betSize_sqrt_mul_zScore`: `m w (√w · z) = kellyFraction p 1 = 2p − 1` for `0 < p < 1`.

## References

* [J. L. Kelly, *A new interpretation of information rate*][Kelly1956]
* [López de Prado, *Advances in Financial Machine Learning*][LopezDePrado2018], ch. 10

## Tags

bet sizing, López de Prado, sigmoid, Kelly criterion, machine learning for finance, MSC 91G10,
JEL G11
-/

@[expose] public section

namespace MathFin

/-- The bet-sizing sigmoid `m(x) = x / √(w + x²)`. -/
noncomputable def betSize (w x : ℝ) : ℝ := x / Real.sqrt (w + x ^ 2)

theorem betSize_odd (w x : ℝ) : betSize w (-x) = -betSize w x := by
simp [betSize, neg_div]

theorem betSize_mem_Ioo {w : ℝ} (hw : 0 < w) (x : ℝ) : betSize w x ∈ Set.Ioo (-1 : ℝ) 1 := by
have hpos : 0 < w + x ^ 2 := by positivity
have hsqrt_pos : 0 < Real.sqrt (w + x ^ 2) := Real.sqrt_pos.mpr hpos
have habs : |x| < Real.sqrt (w + x ^ 2) := by
rw [← Real.sqrt_sq_eq_abs]
exact Real.sqrt_lt_sqrt (by positivity) (by linarith)
have hm_abs : |betSize w x| < 1 := by
rw [betSize, abs_div, abs_of_pos hsqrt_pos, div_lt_one hsqrt_pos]
exact habs
exact abs_lt.mp hm_abs

/-- Core monotonicity step: for `0 ≤ p < q` and `w > 0`,
`p · √(w+q²) < q · √(w+p²)`. -/
theorem mul_sqrt_add_sq_lt_of_nonneg {w : ℝ} (hw : 0 < w) {p q : ℝ} (hp : 0 ≤ p) (hpq : p < q) :
p * Real.sqrt (w + q ^ 2) < q * Real.sqrt (w + p ^ 2) := by
have hq : 0 < q := lt_of_le_of_lt hp hpq
have hs1pos : 0 < Real.sqrt (w + p ^ 2) := Real.sqrt_pos.mpr (by positivity)
have hsqlt : (p * Real.sqrt (w + q ^ 2)) ^ 2 < (q * Real.sqrt (w + p ^ 2)) ^ 2 := by
rw [mul_pow, mul_pow, Real.sq_sqrt (by positivity : (0:ℝ) ≤ w + q ^ 2),
Real.sq_sqrt (by positivity : (0:ℝ) ≤ w + p ^ 2)]
have hsq : p ^ 2 < q ^ 2 := sq_lt_sq' (by linarith) hpq
nlinarith [mul_lt_mul_of_pos_right hsq hw]
exact lt_of_pow_lt_pow_left₀ 2 (mul_pos hq hs1pos).le hsqlt

/-- The key cross-multiplied inequality behind strict monotonicity, for any
reals `a < b` (not just nonnegative ones). -/
theorem mul_sqrt_add_sq_lt {w : ℝ} (hw : 0 < w) {a b : ℝ} (hab : a < b) :
a * Real.sqrt (w + b ^ 2) < b * Real.sqrt (w + a ^ 2) := by
rcases le_total 0 a with ha | ha
· exact mul_sqrt_add_sq_lt_of_nonneg hw ha hab
· rcases le_total b 0 with hb | hb
· have := mul_sqrt_add_sq_lt_of_nonneg hw (by linarith : (0:ℝ) ≤ -b) (by linarith : -b < -a)
have ha2 : (-a) ^ 2 = a ^ 2 := by ring
have hb2 : (-b) ^ 2 = b ^ 2 := by ring
rw [ha2, hb2] at this
linarith
· have hs1pos : 0 < Real.sqrt (w + a ^ 2) := Real.sqrt_pos.mpr (by positivity)
have hs2pos : 0 < Real.sqrt (w + b ^ 2) := Real.sqrt_pos.mpr (by positivity)
have h1 : a * Real.sqrt (w + b ^ 2) ≤ 0 := mul_nonpos_of_nonpos_of_nonneg ha hs2pos.le
rcases eq_or_lt_of_le ha with ha0 | ha0
· have hb0 : 0 < b := by rw [ha0] at hab; exact hab
have h2 : 0 < b * Real.sqrt (w + a ^ 2) := mul_pos hb0 hs1pos
linarith
· have h2 : a * Real.sqrt (w + b ^ 2) < 0 := mul_neg_of_neg_of_pos ha0 hs2pos
have h3 : 0 ≤ b * Real.sqrt (w + a ^ 2) := mul_nonneg hb hs1pos.le
linarith

theorem betSize_strictMono {w : ℝ} (hw : 0 < w) : StrictMono (betSize w) := by
intro a b hab
have hs1pos : 0 < Real.sqrt (w + a ^ 2) := Real.sqrt_pos.mpr (by positivity)
have hs2pos : 0 < Real.sqrt (w + b ^ 2) := Real.sqrt_pos.mpr (by positivity)
rw [betSize, betSize, div_lt_div_iff₀ hs1pos hs2pos]
exact mul_sqrt_add_sq_lt hw hab

/-- **The sigmoid of the probability z-score is the even-money Kelly fraction.** For a
predicted win probability `0 < p < 1`, let `z = (p − ½)/√(p(1 − p))` be the z-statistic of
`p` against `½`. For every width `w > 0`, `m w (√w · z) = 2p − 1 = kellyFraction p 1`, the
Kelly fraction of an even-money bet (`Choice/Performance/Ratios.lean`). The width only rescales
the argument, and `w + w z² = w / (4 p (1 − p))`, so `√w·z / √(w + w z²) = 2p − 1`. -/
theorem betSize_sqrt_mul_zScore {w p : ℝ} (hw : 0 < w) (hp₀ : 0 < p) (hp₁ : p < 1) :
betSize w (√w * ((p - 1 / 2) / √(p * (1 - p)))) = kellyFraction p 1 := by
have hpq : 0 < p * (1 - p) := mul_pos hp₀ (sub_pos.2 hp₁)
have hp0 : p ≠ 0 := hp₀.ne'
have hq0 : 1 - p ≠ 0 := (sub_pos.2 hp₁).ne'
have hs0 : √(p * (1 - p)) ≠ 0 := (Real.sqrt_pos.2 hpq).ne'
have hw0 : √w ≠ 0 := (Real.sqrt_pos.2 hw).ne'
have hrad : w + (√w * ((p - 1 / 2) / √(p * (1 - p)))) ^ 2 =
(√w / (2 * √(p * (1 - p)))) ^ 2 := by
rw [mul_pow, div_pow, div_pow, mul_pow, Real.sq_sqrt hw.le, Real.sq_sqrt hpq.le]
field_simp
ring
rw [betSize, hrad, Real.sqrt_sq (by positivity), kellyFraction]
field_simp
ring

end MathFin
211 changes: 211 additions & 0 deletions LeanPool/MathFin/Choice/Performance/DownsideMetrics.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
/-
Copyright (c) 2026 Raphael Coelho. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Aleksey Salkutsan, Yang Ye
-/
module

-- Ported from Formal MathFin v2.0.0 for Lean Pool.

public import LeanPool.MathFin.Choice.Performance.Ratios

/-!
# Downside performance metrics

Finite-state Omega ratio, maximum drawdown of a finite price path, and the
positive-scaling invariance of the Calmar ratio.

## Main definitions

* `FiniteReturnModel`: a finite return model, nonnegative probability weights of total mass one.
* `finiteExpectedReturn`: the expected return of a finite return model.
* `omegaUpside`, `omegaDownside`: the expected gain above and the expected shortfall below a
threshold.
* `omegaRatio`: the Omega ratio at a threshold in a finite state space.
* `maximumDrawdown`: the largest decline of a finite price path below its running peak.
* `calmarRatio`: the annual return divided by the maximum drawdown.

## Main results

* `omega_upside_sub_downside`: expected upside minus expected downside is the mean excess return.
* `omegaRatio_nonneg`: the finite-state Omega ratio is nonnegative.
* `omegaRatio_threshold_identity`: when the expected downside is nonzero,
`omegaRatio − 1 = (𝔼[R] − threshold) / omegaDownside`.
* `sub_le_maximumDrawdown`: every chronological peak-to-trough decline `E s − E t`, `s ≤ t`, is at
most `maximumDrawdown E`.
* `maximumDrawdown_nonneg`, `maximumDrawdown_scale`: nonnegativity, and positive homogeneity for
scaling by `c ≥ 0`.
* `calmarRatio_scale_invariant`: the Calmar ratio is unchanged by a common scaling `c > 0`.
* `downsideMetrics_bundle`: one conjunction collecting the Omega, drawdown and Calmar facts.

## References

* [Keating and Shadwick (2002)][KeatingShadwick2002]
* [Young (1991)][Young1991]

## Tags

Omega ratio, maximum drawdown, Calmar ratio, downside risk, performance measurement, MSC 91G10,
JEL G11
-/

@[expose] public section

namespace MathFin

open scoped BigOperators

/-- A finite return model with nonnegative probability weights of total mass one. -/
structure FiniteReturnModel (ι : Type*) [Fintype ι] where
/-- Probability weight assigned to a state. -/
weight : ι → ℝ
/-- Return realized in a state. -/
outcome : ι → ℝ
weight_nonneg : ∀ i, 0 ≤ weight i
weight_sum_one : ∑ i, weight i = 1

/-- Expected return of a finite return model. -/
def finiteExpectedReturn {ι : Type*} [Fintype ι] (m : FiniteReturnModel ι) : ℝ :=
∑ i, m.weight i * m.outcome i

/-- Expected gain above a threshold. -/
def omegaUpside {ι : Type*} [Fintype ι] (m : FiniteReturnModel ι) (threshold : ℝ) : ℝ :=
∑ i, m.weight i * max (m.outcome i - threshold) 0

/-- Expected shortfall below a threshold. -/
def omegaDownside {ι : Type*} [Fintype ι]
(m : FiniteReturnModel ι) (threshold : ℝ) : ℝ :=
∑ i, m.weight i * max (threshold - m.outcome i) 0

/-- Omega ratio at a threshold in a finite state space. -/
noncomputable def omegaRatio {ι : Type*} [Fintype ι]
(m : FiniteReturnModel ι) (threshold : ℝ) : ℝ :=
omegaUpside m threshold / omegaDownside m threshold

/-- Expected upside minus expected downside equals mean excess return. -/
theorem omega_upside_sub_downside {ι : Type*} [Fintype ι]
(m : FiniteReturnModel ι) (threshold : ℝ) :
omegaUpside m threshold - omegaDownside m threshold =
finiteExpectedReturn m - threshold := by
unfold omegaUpside omegaDownside finiteExpectedReturn
rw [← Finset.sum_sub_distrib]
calc
(∑ i, (m.weight i * max (m.outcome i - threshold) 0 -
m.weight i * max (threshold - m.outcome i) 0)) =
∑ i, m.weight i * (m.outcome i - threshold) := by
apply Finset.sum_congr rfl
intro i _
rw [← mul_sub]
congr 1
simpa only [neg_sub] using max_zero_sub_max_neg_zero_eq_self (m.outcome i - threshold)
_ = ∑ i, (m.weight i * m.outcome i - threshold * m.weight i) := by
apply Finset.sum_congr rfl
intro i _
ring
_ = (∑ i, m.weight i * m.outcome i) - threshold * (∑ i, m.weight i) := by
rw [Finset.sum_sub_distrib, Finset.mul_sum]
_ = (∑ i, m.weight i * m.outcome i) - threshold := by
rw [m.weight_sum_one, mul_one]

/-- The finite-state Omega ratio is nonnegative. -/
theorem omegaRatio_nonneg {ι : Type*} [Fintype ι]
(m : FiniteReturnModel ι) (threshold : ℝ) :
0 ≤ omegaRatio m threshold := by
unfold omegaRatio omegaUpside omegaDownside
apply div_nonneg
· exact Finset.sum_nonneg fun i _ =>
mul_nonneg (m.weight_nonneg i) (le_max_right _ _)
· exact Finset.sum_nonneg fun i _ =>
mul_nonneg (m.weight_nonneg i) (le_max_right _ _)

/-- Threshold identity for the Omega ratio when expected downside is nonzero. -/
theorem omegaRatio_threshold_identity {ι : Type*} [Fintype ι]
(m : FiniteReturnModel ι) (threshold : ℝ)
(hdown : omegaDownside m threshold ≠ 0) :
omegaRatio m threshold - 1 =
(finiteExpectedReturn m - threshold) / omegaDownside m threshold := by
calc
omegaRatio m threshold - 1 =
(omegaUpside m threshold - omegaDownside m threshold) /
omegaDownside m threshold := by
unfold omegaRatio
field_simp [hdown]
_ = (finiteExpectedReturn m - threshold) / omegaDownside m threshold := by
rw [omega_upside_sub_downside]

/-- The running peak is positively homogeneous: multiplication by `c > 0` is an order
isomorphism, so it commutes with the finite suprema defining `partialSups`. This is the
group-with-zero version of Mathlib's `[Group α]` lemma `partialSups_const_mul`. -/
theorem partialSups_const_mul₀ {ι : Type*} [Preorder ι] [LocallyFiniteOrderBot ι]
{G₀ : Type*} [GroupWithZero G₀] [SemilatticeSup G₀] [PosMulReflectLT G₀] {c : G₀}
(hc : 0 < c) (E : ι → G₀) (t : ι) :
partialSups (fun s ↦ c * E s) t = c * partialSups E t :=
map_partialSups (OrderIso.mulLeft₀ c hc) E t

/-- **Maximum drawdown** of a finite price path: the largest decline `max_{s ≤ t} E s − E t` of
the path below its running peak (Mathlib's `partialSups`), which is the largest chronological
peak-to-trough loss `E s − E t`, `s ≤ t` (`sub_le_maximumDrawdown`). The empty path has drawdown
`0`. -/
noncomputable def maximumDrawdown {n : ℕ} (price : Fin n → ℝ) : ℝ :=
⨆ t, partialSups price t - price t

/-- The decline below the running peak at any date is at most the maximum drawdown. -/
theorem partialSups_sub_le_maximumDrawdown {n : ℕ} (price : Fin n → ℝ) (t : Fin n) :
partialSups price t - price t ≤ maximumDrawdown price :=
le_ciSup (f := fun t ↦ partialSups price t - price t) (Set.finite_range _).bddAbove t

/-- Every chronological peak-to-trough decline `E s − E t` (`s ≤ t`) of a finite path is at
most its `maximumDrawdown`. -/
theorem sub_le_maximumDrawdown {n : ℕ} (E : Fin n → ℝ) {s t : Fin n} (hst : s ≤ t) :
E s - E t ≤ maximumDrawdown E :=
(sub_le_sub_right (le_partialSups_of_le E hst) _).trans (partialSups_sub_le_maximumDrawdown E t)

/-- Maximum drawdown is nonnegative, including for an empty path. -/
theorem maximumDrawdown_nonneg {n : ℕ} (price : Fin n → ℝ) :
0 ≤ maximumDrawdown price :=
Real.iSup_nonneg fun t ↦ sub_nonneg.2 (le_partialSups_of_le price (le_refl t))

/-- Maximum drawdown is positively homogeneous for nonnegative scaling. -/
theorem maximumDrawdown_scale {n : ℕ} (price : Fin n → ℝ) (c : ℝ) (hc : 0 ≤ c) :
maximumDrawdown (fun i => c * price i) = c * maximumDrawdown price := by
rcases hc.eq_or_lt with rfl | hc
· have h0 (t : Fin n) : partialSups (fun _ : Fin n ↦ (0 : ℝ)) t = 0 :=
le_antisymm (partialSups_le _ _ _ fun _ _ ↦ le_rfl)
(le_partialSups_of_le (fun _ : Fin n ↦ (0 : ℝ)) (le_refl t))
simp [maximumDrawdown, h0]
· rw [maximumDrawdown, maximumDrawdown, Real.mul_iSup_of_nonneg hc.le]
refine iSup_congr fun t ↦ ?_
rw [partialSups_const_mul₀ hc]
ring

/-- Calmar ratio: annual return divided by maximum drawdown. -/
noncomputable def calmarRatio (annualReturn maxDrawdown : ℝ) : ℝ :=
annualReturn / maxDrawdown

/-- Calmar ratio is invariant under strictly positive common scaling. -/
theorem calmarRatio_scale_invariant (annualReturn maxDrawdown c : ℝ) (hc : 0 < c) :
calmarRatio (c * annualReturn) (c * maxDrawdown) =
calmarRatio annualReturn maxDrawdown := by
simpa [calmarRatio] using
(diff_div_scale_invariant (c := c) hc.ne' annualReturn 0 maxDrawdown)

/-- One acceptance bundle covering Omega, maximum drawdown, and Calmar. -/
theorem downsideMetrics_bundle {ι : Type*} [Fintype ι]
(m : FiniteReturnModel ι) (threshold : ℝ)
(hdown : omegaDownside m threshold ≠ 0)
{n : ℕ} (price : Fin n → ℝ) (annualReturn c : ℝ) (hc : 0 < c) :
(0 ≤ omegaRatio m threshold ∧
omegaRatio m threshold - 1 =
(finiteExpectedReturn m - threshold) / omegaDownside m threshold) ∧
(0 ≤ maximumDrawdown price ∧
maximumDrawdown (fun i => c * price i) = c * maximumDrawdown price) ∧
calmarRatio (c * annualReturn) (c * maximumDrawdown price) =
calmarRatio annualReturn (maximumDrawdown price) := by
exact
⟨⟨omegaRatio_nonneg m threshold,
omegaRatio_threshold_identity m threshold hdown⟩,
⟨maximumDrawdown_nonneg price,
maximumDrawdown_scale price c hc.le⟩,
calmarRatio_scale_invariant annualReturn (maximumDrawdown price) c hc⟩

end MathFin
Loading
Loading