# 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
0.8333...
>>> choices = [8, 6, 5, 7]
>>> weightedDistance(choices, weights)  # best choice!
1.000...
>>> choices = [3, 2, 1, 0]
>>> weightedDistance(choices, weights)  # worst choice!
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
1.0...

>>> perm = [0, 1, 2, 5, 4, 3]
>>> manhattan(perm)  # almost sorted!
0.777...

>>> perm = [2, 9, 6, 4, 0, 3, 1, 7, 8, 5]
>>> manhattan(perm)
0.4

>>> perm = [2, 1, 6, 4, 0, 3, 5, 7, 8, 9]  # better sorted!
>>> 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
0.98...

>>> perm = [0, 1, 2, 5, 4, 3]
>>> kendalltau(perm)  # almost sorted!
0.90...

>>> perm = [2, 9, 6, 4, 0, 3, 1, 7, 8, 5]
>>> kendalltau(perm)
0.211...

>>> perm = [2, 1, 6, 4, 0, 3, 5, 7, 8, 9]  # better sorted!
>>> kendalltau(perm)
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
1.0...

>>> perm = [0, 1, 2, 5, 4, 3]
>>> spearmanr(perm)  # almost sorted!
0.92...

>>> perm = [2, 9, 6, 4, 0, 3, 1, 7, 8, 5]
>>> spearmanr(perm)
0.248...

>>> perm = [2, 1, 6, 4, 0, 3, 5, 7, 8, 9]  # better sorted!
>>> spearmanr(perm)
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
1.0...

>>> perm = [0, 1, 2, 5, 4, 3]
>>> gestalt(perm)  # almost sorted!
0.666...

>>> perm = [2, 9, 6, 4, 0, 3, 1, 7, 8, 5]
>>> gestalt(perm)
0.4...

>>> perm = [2, 1, 6, 4, 0, 3, 5, 7, 8, 9]  # better sorted!
>>> gestalt(perm)
0.5...

>>> import random
>>> random.seed(0)
>>> ratings = [random.gauss(1200, 200) for i in range(100000)]
>>> gestalt(ratings)
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
1.0

>>> perm = [0, 1, 2, 5, 4, 3]
>>> meanDistance(perm)  # almost sorted!
0.722...

>>> perm = [2, 9, 6, 4, 0, 3, 1, 7, 8, 5]
>>> meanDistance(perm)
0.4

>>> perm = [2, 1, 6, 4, 0, 3, 5, 7, 8, 9]  # better sorted!
>>> 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
1.0

>>> perm = [0, 1, 2, 5, 4, 3]
>>> meanDistance(perm)  # almost sorted!
0.722...

>>> perm = [2, 9, 6, 4, 0, 3, 1, 7, 8, 5]
>>> meanDistance(perm)
0.4

>>> perm = [2, 1, 6, 4, 0, 3, 5, 7, 8, 9]  # better sorted!
>>> 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%…