Documentation

CslibFrontier.Foundations.Data.Finset.KthLargest

The k-th largest value, padded with zeros #

Finset.kthLargest k s h is the k-th largest member of the multiset of values h x, x ∈ s, padded with infinitely many zeros. It is the largest value h x that is reached or exceeded by at least k of the values, or 0 if that is larger. So it is nonnegative, and it is 0 when fewer than k of the values are positive. This is the order statistic rank_k of the detectors in the OpenAI preprint [OpenAI2026LogSpace, Section 4.2].

It is characterized by threshold counts: for 0 < k and 0 ≤ t, the k-th largest value exceeds t exactly when at least k values exceed t (Finset.lt_kthLargest_iff), and for 0 < t it is at least t exactly when at least k values are at least t (Finset.le_kthLargest_iff).

Main definitions #

Main results #

References #

def Finset.kthLargest {α : Type u_1} {R : Type u_2} [LinearOrder R] [Zero R] (k : ℕ) (s : Finset α) (h : α → R) :
R

The k-th largest value of h on s, padded with zeros: the largest value h x, x ∈ s, that at least k values h w, w ∈ s, reach or exceed, or 0 if that is larger.

theorem Finset.kthLargest_nonneg {α : Type u_1} {R : Type u_2} [LinearOrder R] [Zero R] {k : ℕ} {s : Finset α} {h : α → R} :
0 ≤ kthLargest k s h
theorem Finset.kthLargest_le {α : Type u_1} {R : Type u_2} [LinearOrder R] [Zero R] {k : ℕ} {s : Finset α} {h : α → R} {t : R} (ht : 0 ≤ t) (hh : ∀ x ∈ s, h x ≤ t) :
kthLargest k s h ≤ t

The k-th largest value is at most any nonnegative upper bound of the values.

theorem Finset.kthLargest_congr {α : Type u_1} {R : Type u_2} [LinearOrder R] [Zero R] {k : ℕ} {s : Finset α} {f g : α → R} (hfg : ∀ x ∈ s, f x = g x) :
kthLargest k s f = kthLargest k s g

kthLargest depends only on the values of the function on s.

theorem Finset.lt_kthLargest_iff {α : Type u_1} {R : Type u_2} [LinearOrder R] [Zero R] {k : ℕ} {s : Finset α} {h : α → R} {t : R} (hk : 0 < k) (ht : 0 ≤ t) :
t < kthLargest k s h ↔ k ≤ {x ∈ s | t < h x}.card

The k-th largest value exceeds t ≥ 0 exactly when at least k > 0 values do.

theorem Finset.le_kthLargest_iff {α : Type u_1} {R : Type u_2} [LinearOrder R] [Zero R] {k : ℕ} {s : Finset α} {h : α → R} {t : R} (hk : 0 < k) (ht : 0 < t) :
t ≤ kthLargest k s h ↔ k ≤ {x ∈ s | t ≤ h x}.card

The k-th largest value is at least t > 0 exactly when at least k > 0 values are.

theorem Finset.kthLargest_le_of_lt_imp {α : Type u_1} {R : Type u_2} [LinearOrder R] [Zero R] {k : ℕ} {s : Finset α} {t : R} {f g : α → R} {m : ℕ} (hk : 0 < k) (hkm : k ≤ m) (ht : 0 ≤ t) (hfg : ∀ x ∈ s, t < f x → t < g x) (hg : kthLargest k s g ≤ t) :
kthLargest m s f ≤ t

Comparing ranks through a threshold. If every value of f above t ≥ 0 has the value of g above t too, a bound t on the k-th largest value of g bounds the m-th largest value of f for every m ≥ k > 0.

theorem Finset.kthLargest_eq_of_eq_or_gt {α : Type u_1} {R : Type u_2} [LinearOrder R] [Zero R] {k : ℕ} {s : Finset α} {t : R} {f g : α → R} (h : ∀ x ∈ s, f x = g x ∨ t < f x ∧ t < g x) (hg : kthLargest k s g ≤ t) :
kthLargest k s f = kthLargest k s g

Changing only values strictly above a ceiling preserves every rank at most that ceiling. The bound may be an equality, and other values may be negative.

theorem Finset.kthLargest_le_kthLargest_add {α : Type u_1} {R : Type u_3} [AddCommGroup R] [LinearOrder R] [IsOrderedAddMonoid R] {k : ℕ} {s : Finset α} {h₁ h₂ : α → R} {ε : R} (hk : 0 < k) (hε : 0 ≤ ε) (h : ∀ x ∈ s, h₁ x ≤ h₂ x + ε) :
kthLargest k s h₁ ≤ kthLargest k s h₂ + ε

Rank stability, one side [OpenAI2026LogSpace, (5.4)]: if every value of h₁ exceeds the corresponding value of h₂ by at most ε ≥ 0, so does the k-th largest value.

theorem Finset.abs_kthLargest_sub_kthLargest_le {α : Type u_1} {R : Type u_3} [AddCommGroup R] [LinearOrder R] [IsOrderedAddMonoid R] {k : ℕ} {s : Finset α} {h₁ h₂ : α → R} {ε : R} (hk : 0 < k) (hε : 0 ≤ ε) (h : ∀ x ∈ s, |h₁ x - h₂ x| ≤ ε) :
|kthLargest k s h₁ - kthLargest k s h₂| ≤ ε

Rank stability [OpenAI2026LogSpace, (5.4)]: |rank_k h₁ − rank_k h₂| ≤ sup |h₁ − h₂|. If the values differ by at most ε ≥ 0 everywhere on s, so do their k-th largest values.

theorem Finset.abs_le_kthLargest_one {ι : Type u_3} {R' : Type u_4} [AddCommGroup R'] [LinearOrder R'] [IsOrderedAddMonoid R'] (S : Finset ι) (b : ι → R') (y : ι) (hy : y ∈ S) :
|b y| ≤ kthLargest 1 S fun (z : ι) => |b z|

A retained endpoint's magnitude is bounded by the full row maximum, including zero.