Summary: this tutorial demonstrates how to determine ranks of a collection
Consider a collection of values. The rank of a number is its size relative to other values in a sequence. There are four methods how to handle ties:
let mySequence = [|1.0; -2.0; 0.0; 1.0|]
ranks = [3,1,2,4]
ranks = [3,1,2,3]
ranks = [4,1,2,4]
ranks = [3.5,1,2,3.5]
If nans are present in the collection, there are several ways to treat them. In general nan <> nan
so that each occurence will receive its unique rank.
(for infinity and -infinity the equality check returns true).
nan, -infinity, -100., 0., 100, infinity
open FSharp.Stats
let collection = [|2.;-infinity;infinity;infinity;nan;0|]
Rank.RankFirst() collection
// result: [|3.0; 1.0; 4.0; 5.0; nan; 2.0|]
Rank.RankMin() collection
// result: [|3.0; 1.0; 4.0; 4.0; nan; 2.0|]
Rank.RankMax() collection
// result: [|3.0; 1.0; 5.0; 5.0; nan; 2.0|]
Rank.RankAverage() collection
// result: [|3.0; 1.0; 4.5; 4.5; nan; 2.0|]
If you want to preserve the true ranks of nans but sort them to the back you can use:
Rank.RankFirst(RankNanWithNan=false) collection
// result: [|3.0; 1.0; 4.0; 5.0; 6.0; 2.0|]
If you want to preserve the true ranks of nans AND sort them to the beginning you can use:
Rank.RankFirst(NanIsMaximum=false,RankNanWithNan=false) collection
// result: [|4.0; 2.0; 5.0; 6.0; 1.0; 3.0|]