Documentation

CslibFrontier.Foundations.Data.List.OrderStatistic

Order statistics of a list #

List.IsOrderStatistic r xs k y says that y is an element of xs of rank k (counting from 0) for a total preorder r: at most k elements of xs lie strictly below y, and more than k lie at or below it. With ties, several elements of xs can have rank k; they are all r-equivalent, and under an antisymmetric order the order statistic is unique and equals the k-th entry of the sorted list.

Main definitions #

Main results #

References #

structure List.IsOrderStatistic {α : Type u_1} (r : α → α → Prop) [DecidableRel r] (xs : List α) (k : ℕ) (y : α) :

y is an element of xs of rank k (counting from 0) for the relation r: at most k elements of xs lie strictly below y, and more than k lie at or below it. For a total preorder r, "strictly below y" means ¬ r y x.

  • mem : y ∈ xs

    The order statistic is an element of the list.

  • countP_lt_le : countP (fun (x : α) => decide ¬r y x) xs ≤ k

    At most k elements lie strictly below y.

  • lt_countP_le : k < countP (fun (x : α) => decide (r x y)) xs

    More than k elements lie at or below y.

theorem List.IsOrderStatistic.lt_length {α : Type u_1} {r : α → α → Prop} [DecidableRel r] {xs : List α} {k : ℕ} {y : α} (h : IsOrderStatistic r xs k y) :
k < xs.length

An order statistic has rank below the length of the list.

theorem List.Pairwise.length_sub_le_countP {α : Type u_1} {r : α → α → Prop} [DecidableRel r] [IsTrans α r] {s : List α} (hs : Pairwise r s) {i : ℕ} (hi : i < s.length) {p : α} (hp : r p s[i]) :
s.length - i ≤ countP (fun (x : α) => decide (r p x)) s

In a list sorted by a transitive relation, an element p below the i-th entry is below all of the last length - i entries.

theorem List.Pairwise.add_one_le_countP {α : Type u_1} {r : α → α → Prop} [DecidableRel r] [IsTrans α r] {s : List α} (hs : Pairwise r s) {i : ℕ} (hi : i < s.length) {p : α} (hp : r s[i] p) :
i + 1 ≤ countP (fun (x : α) => decide (r x p)) s

In a list sorted by a transitive relation, an element p above the i-th entry is above all of the first i + 1 entries.

theorem List.isOrderStatistic_getElem_of_perm {α : Type u_1} {r : α → α → Prop} [DecidableRel r] [Std.Total r] [IsTrans α r] {xs s : List α} (hperm : s.Perm xs) (hs : Pairwise r s) {k : ℕ} (hk : k < s.length) :

The k-th entry of a sorted permutation of xs is an order statistic of rank k.

theorem List.IsOrderStatistic.rel {α : Type u_1} {r : α → α → Prop} [DecidableRel r] [IsTrans α r] {xs : List α} {k : ℕ} {y y' : α} (h : IsOrderStatistic r xs k y) (h' : IsOrderStatistic r xs k y') :
r y y'

Two order statistics of the same rank are related, so under a total preorder they are equivalent.

theorem List.IsOrderStatistic.unique {α : Type u_1} {r : α → α → Prop} [DecidableRel r] [IsTrans α r] [Std.Antisymm r] {xs : List α} {k : ℕ} {y y' : α} (h : IsOrderStatistic r xs k y) (h' : IsOrderStatistic r xs k y') :
y = y'

Under an antisymmetric transitive relation, order statistics are unique.

theorem List.IsOrderStatistic.of_filter {α : Type u_1} {r : α → α → Prop} [DecidableRel r] {q : α → Prop} [DecidablePred q] {xs : List α} {k : ℕ} {y : α} (h : IsOrderStatistic r (filter (fun (x : α) => decide (q x)) xs) k y) (hq : ∀ x ∈ xs, ¬r y x → q x) :

An order statistic of xs.filter q is an order statistic of xs of the same rank when every element of xs strictly below it satisfies q.

theorem List.IsOrderStatistic.of_filter_add {α : Type u_1} {r : α → α → Prop} [DecidableRel r] {q : α → Prop} [DecidablePred q] {xs : List α} {k : ℕ} {y : α} (h : IsOrderStatistic r (filter (fun (x : α) => decide (q x)) xs) k y) (hq : ∀ x ∈ xs, ¬q x → r x y) :
IsOrderStatistic r xs (k + countP (fun (x : α) => decide ¬q x) xs) y

An order statistic of xs.filter q is an order statistic of xs whose rank is shifted by the number of rejected elements, when every rejected element is at or below it.

theorem List.isOrderStatistic_iff_mergeSort {β : Type u_2} [LinearOrder β] {xs : List β} {k : ℕ} {y : β} :
IsOrderStatistic (fun (x1 x2 : β) => x1 ≤ x2) xs k y ↔ (xs.mergeSort fun (a b : β) => decide (a ≤ b))[k]? = some y

In a linear order, the rank-k order statistic of xs is the k-th entry of xs.mergeSort.