Environment.sortedDistance module

sortedDistance: define function to measure of sortedness of permutations of [0..N-1].

Environment.sortedDistance.weightedDistance(choices, weights, n=None)[source]

Relative difference between the best possible weighted choices and the actual choices.

>>> weights = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]
>>> choices = [8, 6, 5, 2]
>>> weightedDistance(choices, weights)  # not a bad choice  # doctest: +ELLIPSIS
0.8333...
>>> choices = [8, 6, 5, 7]
>>> weightedDistance(choices, weights)  # best choice!  # doctest: +ELLIPSIS
1.000...
>>> choices = [3, 2, 1, 0]
>>> weightedDistance(choices, weights)  # worst choice!  # doctest: +ELLIPSIS
0.3333...
Environment.sortedDistance.manhattan(permutation, comp=None)[source]

A certain measure of sortedness for the list A, based on Manhattan distance.

>>> perm = [0, 1, 2, 3, 4]
>>> manhattan(perm)  # sorted  # doctest: +ELLIPSIS
1.0...
>>> perm = [0, 1, 2, 5, 4, 3]
>>> manhattan(perm)  # almost sorted!  # doctest: +ELLIPSIS
0.777...
>>> perm = [2, 9, 6, 4, 0, 3, 1, 7, 8, 5]  # doctest: +ELLIPSIS
>>> manhattan(perm)
0.4
>>> perm = [2, 1, 6, 4, 0, 3, 5, 7, 8, 9]  # better sorted!  # doctest: +ELLIPSIS
>>> manhattan(perm)
0.72
Environment.sortedDistance.kendalltau(permutation, comp=None)[source]

A certain measure of sortedness for the list A, based on Kendall Tau ranking coefficient.

>>> perm = [0, 1, 2, 3, 4]
>>> kendalltau(perm)  # sorted  # doctest: +ELLIPSIS
0.98...
>>> perm = [0, 1, 2, 5, 4, 3]
>>> kendalltau(perm)  # almost sorted!  # doctest: +ELLIPSIS
0.90...
>>> perm = [2, 9, 6, 4, 0, 3, 1, 7, 8, 5]
>>> kendalltau(perm)  # doctest: +ELLIPSIS
0.211...
>>> perm = [2, 1, 6, 4, 0, 3, 5, 7, 8, 9]  # better sorted!
>>> kendalltau(perm)  # doctest: +ELLIPSIS
0.984...
Environment.sortedDistance.spearmanr(permutation, comp=None)[source]

A certain measure of sortedness for the list A, based on Spearman ranking coefficient.

>>> perm = [0, 1, 2, 3, 4]
>>> spearmanr(perm)  # sorted  # doctest: +ELLIPSIS
1.0...
>>> perm = [0, 1, 2, 5, 4, 3]
>>> spearmanr(perm)  # almost sorted!  # doctest: +ELLIPSIS
0.92...
>>> perm = [2, 9, 6, 4, 0, 3, 1, 7, 8, 5]
>>> spearmanr(perm)  # doctest: +ELLIPSIS
0.248...
>>> perm = [2, 1, 6, 4, 0, 3, 5, 7, 8, 9]  # better sorted!
>>> spearmanr(perm)  # doctest: +ELLIPSIS
0.986...
Environment.sortedDistance.gestalt(permutation, comp=None)[source]

A certain measure of sortedness for the list A, based on Gestalt pattern matching.

>>> perm = [0, 1, 2, 3, 4]
>>> gestalt(perm)  # sorted  # doctest: +ELLIPSIS
1.0...
>>> perm = [0, 1, 2, 5, 4, 3]
>>> gestalt(perm)  # almost sorted!  # doctest: +ELLIPSIS
0.666...
>>> perm = [2, 9, 6, 4, 0, 3, 1, 7, 8, 5]
>>> gestalt(perm)  # doctest: +ELLIPSIS
0.4...
>>> perm = [2, 1, 6, 4, 0, 3, 5, 7, 8, 9]  # better sorted!
>>> gestalt(perm)  # doctest: +ELLIPSIS
0.5...
>>> import random
>>> random.seed(0)
>>> ratings = [random.gauss(1200, 200) for i in range(100000)]
>>> gestalt(ratings)  # doctest: +ELLIPSIS
8e-05...
Environment.sortedDistance.meanDistance(permutation, comp=None, methods=(<function manhattan>, <function gestalt>))[source]

A certain measure of sortedness for the list A, based on mean of the 2 distances: manhattan and gestalt.

>>> perm = [0, 1, 2, 3, 4]
>>> meanDistance(perm)  # sorted  # doctest: +ELLIPSIS
1.0
>>> perm = [0, 1, 2, 5, 4, 3]
>>> meanDistance(perm)  # almost sorted!  # doctest: +ELLIPSIS
0.722...
>>> perm = [2, 9, 6, 4, 0, 3, 1, 7, 8, 5]  # doctest: +ELLIPSIS
>>> meanDistance(perm)
0.4
>>> perm = [2, 1, 6, 4, 0, 3, 5, 7, 8, 9]  # better sorted!  # doctest: +ELLIPSIS
>>> meanDistance(perm)
0.61

Warning

I removed kendalltau() and spearmanr() as they were giving 100% for many cases where clearly there were no reason to give 100%…

Environment.sortedDistance.sortedDistance(permutation, comp=None, methods=(<function manhattan>, <function gestalt>))

A certain measure of sortedness for the list A, based on mean of the 2 distances: manhattan and gestalt.

>>> perm = [0, 1, 2, 3, 4]
>>> meanDistance(perm)  # sorted  # doctest: +ELLIPSIS
1.0
>>> perm = [0, 1, 2, 5, 4, 3]
>>> meanDistance(perm)  # almost sorted!  # doctest: +ELLIPSIS
0.722...
>>> perm = [2, 9, 6, 4, 0, 3, 1, 7, 8, 5]  # doctest: +ELLIPSIS
>>> meanDistance(perm)
0.4
>>> perm = [2, 1, 6, 4, 0, 3, 5, 7, 8, 9]  # better sorted!  # doctest: +ELLIPSIS
>>> meanDistance(perm)
0.61

Warning

I removed kendalltau() and spearmanr() as they were giving 100% for many cases where clearly there were no reason to give 100%…