Rank-turbulence divergence¶
The rank-sensitive comparison at the centre of keyflux.
keyflux.divergence.rtd.rtd(list1, list2, *, alpha=1.0 / 3.0, normalize=True)
¶
Compute the rank-turbulence divergence between two ranked lists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
list1
|
RankedList
|
The first ranked list. |
required |
list2
|
RankedList
|
The second ranked list. |
required |
alpha
|
float
|
Tuning parameter ( |
1.0 / 3.0
|
normalize
|
bool
|
If True, return |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
An |
RTDResult
|
class: |
RTDResult
|
contributions, each tagged with its shift direction. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Contract
rtd(x, x).divergence == 0(a list never diverges from itself).0 <= divergence <= 1for every input and everyalpha.- Symmetric:
rtd(a, b).divergence == rtd(b, a).divergence. - The per-type contributions sum to
divergence. - Exclusives (present in one list only) are placed at a tied-last rank.
Examples:
>>> from keyflux.ranking.rankedlist import RankedList
>>> r1 = RankedList.from_counts({"a": 20, "e": 14, "c": 8, "b": 7,
... "f": 4, "g": 2, "d": 1})
>>> r2 = RankedList.from_counts({"b": 24, "a": 16, "e": 5, "d": 4,
... "c": 3, "f": 2, "g": 1})
>>> round(rtd(r1, r2, alpha=1.0).divergence, 6)
0.459248
>>> rtd(r1, r1, alpha=1.0).divergence
0.0
Source code in keyflux/divergence/rtd.py
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | |
keyflux.divergence.rtd.RTDResult
dataclass
¶
The result of a rank-turbulence divergence computation.
Attributes:
| Name | Type | Description |
|---|---|---|
divergence |
float
|
The normalised divergence in [0, 1]. |
raw |
float
|
The un-normalised weighted sum (matches the jkbren reference at
|
alpha |
float
|
The tuning parameter used. |
contributions |
tuple[Contribution, ...]
|
Per-type contributions, sorted by contribution descending. |
labels |
tuple[str, str]
|
The two list labels. |
Source code in keyflux/divergence/rtd.py
keyflux.divergence.rtd.Contribution
dataclass
¶
One type's contribution to the total divergence.
Attributes:
| Name | Type | Description |
|---|---|---|
type |
str
|
The type. |
delta |
float
|
The raw per-type divergence term (before normalisation). |
contribution |
float
|
This type's additive share of |
rank1 |
float
|
The type's rank in the first list (tied-last if absent). |
rank2 |
float
|
The type's rank in the second list (tied-last if absent). |
direction |
ShiftDirection
|
Which list the type leans toward (lower rank wins). |