forked from fslaborg/FSharp.Stats
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRank.fsx
More file actions
126 lines (81 loc) · 3.22 KB
/
Rank.fsx
File metadata and controls
126 lines (81 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
(**
---
title: Ranking
index: 19
category: Documentation
categoryindex: 0
---
*)
(*** hide ***)
(*** condition: prepare ***)
#r "nuget: FSharpAux.Core, 2.0.0"
#r "nuget: FSharpAux, 2.0.0"
#r "nuget: FSharpAux.IO, 2.0.0"
#r "nuget: OptimizedPriorityQueue, 5.1.0"
#r "nuget: FsMath, 0.0.2"
#I "../src/FSharp.Stats/bin/Release/.net8.0/"
#r "FSharp.Stats.dll"
#r "nuget: Plotly.NET, 4.0.0"
open FsMath
Plotly.NET.Defaults.DefaultDisplayOptions <-
Plotly.NET.DisplayOptions.init (PlotlyJSReference = Plotly.NET.PlotlyJSReference.NoReference)
(*** condition: ipynb ***)
#if IPYNB
#r "nuget: Plotly.NET, 4.0.0"
#r "nuget: Plotly.NET.Interactive, 4.0.0"
#r "nuget: FSharp.Stats"
open Plotly.NET
#endif // IPYNB
open Plotly.NET
open Plotly.NET.StyleParam
open Plotly.NET.LayoutObjects
(**
# Ranking
[](https://mybinder.org/v2/gh/fslaborg/FSharp.Stats/gh-pages?urlpath=/tree/home/jovyan/Rank.ipynb)
[]({{root}}{{fsdocs-source-basename}}.ipynb)
_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|]
```
- **rankFirst**
- Each rank occurs exactly once. If ties are present the first occurence gets the low rank.
- ATTENTION: If there are multiple ties (>20) the sorting of Arrays will not preserve the element order correctly!!
- `ranks = [3,1,2,4]`
- **rankMin**
- If ties are present all tied elements receive the score of the first occurence (min rank)
- `ranks = [3,1,2,3]`
- **rankMax**
- If ties are present all tied elements receive the score of the last occurence (max rank)
- `ranks = [4,1,2,4]`
- **rankAverge**
- If ties are present all tied elements receive their average rank.
- `ranks = [3.5,1,2,3.5]`
## NaN treatment
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).
- Usually nans are sorted to the beginning of the collection: `nan, -infinity, -100., 0., 100, infinity`
- By default in FSharp.Stats.Rank, nans are sorted to the end of the sequence (the sorting can be defined as optional parameter)
- Additionally ranks of nan values are set to nan if not specified othwerwise
*)
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|]