(づ•ᴥ•)づ┬─┬

Notes on math and machine learning.

argsort(argsort(argsort(....)))

Family life has decreased my writing cadence — sorry about that. Should be getting better soon.

Here's a fun thing I learned. If you have a list of numbers in Python, you can use NumPy to get argsort():

import numpy

x = numpy.array([0.3, 0.1, 0.6, 0.4])
x = x.argsort()
print(x)

# array([1, 0, 3, 2])

This gives us the order in which we should arrange the elements if we want the array to be sorted from smallest to largest.

We can apply argsort again to x, now getting the order that would make [1, 0, 3, 2] go from smallest to largest:

x = x.argsort()
print(x)

# array([1, 0, 3, 2])

And look — we get the same sequence! So [1, 0, 3, 2] is a fixed point of the argsort operation.

More generally, applying argsort twice is sometimes known as the "argsort-argsort" trick, because argsort(argsort(x)) gives the ranks of the elements in an array / tensor, assuming the values are distinct. In our particular example, the first argsort happens to equal those ranks, which is why applying argsort again doesn't change it.

Not every permutation has this property. For example,

x = numpy.array([1, 2, 0, 3])
print(x.argsort())

# array([2, 0, 1, 3])

which is different from the original sequence.

Abstraction

Let's switch to mathematical notation, where it is more convenient to index from 1 rather than 0.

Suppose T:SkSk is the argsort operator on permutations of {1,,k}. Given a permutation

x=(x1,,xk),

let

q=Tx

be the permutation satisfying

xq1<xq2<<xqk.

Since x is a permutation of {1,,k}, this means that qj is precisely the position at which the value j occurs in x. In other words,

q=x1.

So argsort, when restricted to permutations, is just the operation of taking the inverse permutation.

Now, when will q=x? Exactly when

x=x1.

Equivalently,

xxj=j

for every j=1,,k.

So the fixed points of argsort are exactly the permutations that are their own inverses.

How long can a trajectory be?

Either

T(x)=x=x1,

in which case x is its own inverse — an involution — or

T(x)=x1x.

But then

T2(x)=T(x1)=(x1)1=x.

So every orbit of T has length either 1 or 2.

Essentially, we are studying a dynamical system on the symmetric group Sk, with

T(x)=x1.

Every element therefore belongs either to an orbit of length 1, when x=x1, or an orbit of length 2,

xx1.

How many fixed points are there?

Sk has k! elements. How many of them are fixed points of T? From our previous condition x=x1, it means we are counting involutions of the group, and that is a known sequence: 1, 2, 4, 10, 26, 76, 232, 764, 2620, 9496, 35696 ...