s LearnSqueak.com

SMALLTALK LAB

Measure rank disagreement with Kendall distance

Count the pairs of items that change order between two rankings.

6 tested checkpointsNative Squeak screenshotsNative evaluation replay

Before you begin

Open a Workspace in native Squeak. Select one complete checkpoint and choose Print it.

These are native Squeak 6.1 examples. Each checkpoint is independent: open the download as text, copy one complete expression into Workspace and choose Print it. Do not file in the checkpoint collection as a class definition.

Expected values are Smalltalk expressions. The printer may display equivalent Smalltalk literals with different spaces or delimiters.

01 - Assign positions in the second ranking

Index the second ranking once. A occurs third and B occurs first, even if the first ranking has A ahead of B.

Independent Workspace expression - select all, then Print it
| b pos |
b := #(b d a c).
pos := Dictionary new.
b withIndexDo: [:item :i | pos at: item put: i].
{pos at: #a. pos at: #b}

Expected: #(3 1)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 1: Assign positions in the second ranking
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Look up #d; it is second.

Download this checkpoint

02 - Translate the first ranking

Replace each item in the first ranking with its position in the second. A descending pair now signals disagreement.

Independent Workspace expression - select all, then Print it
| pos |
pos := Dictionary new.
#(b d a c) withIndexDo: [:item :i | pos at: item put: i].
#(a b c d) collect: [:item | pos at: item]

Expected: #(3 1 4 2)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 2: Translate the first ranking
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Swap #a and #b in the first array to see positions 1 and 3 switch.

Download this checkpoint

03 - List the reversed pairs

Record the positions of the pairs instead of just counting them. Each Point here stores two indices, not a geometric coordinate.

Independent Workspace expression - select all, then Print it
| hits |
hits := OrderedCollection new.
1 to: 4 do: [:i | i + 1 to: 4 do: [:j |
 ((#(3 1 4 2) at: i) > (#(3 1 4 2) at: j))
   ifTrue: [hits add: i@j]]].
hits asArray

Expected: {1@2. 1@4. 3@4}

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 3: List the reversed pairs
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Look up those pairs in #(a b c d).

Download this checkpoint

04 - Count the reversed pairs

Compare each pair exactly once. The reversals are (a,b), (a,d), and (c,d).

Independent Workspace expression - select all, then Print it
| distance |
distance := [:a :b |
 | pos count n |
 n := a size.
 ((a asSet size = n) and: [b size = n and: [a asSet = b asSet]])
   ifFalse: [Error signal: 'Use two strict rankings of the same items'].
 pos := Dictionary new.
 b withIndexDo: [:item :i | pos at: item put: i].
 count := 0.
 1 to: n do: [:i |
   i + 1 to: n do: [:j |
     ((pos at: (a at: i)) > (pos at: (a at: j)))
       ifTrue: [count := count + 1]]].
 count].
distance value: #(a b c d) value: #(b d a c)

Expected: 3

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 4: Count the reversed pairs
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Swap the two arguments. The count remains 3.

Download this checkpoint

05 - Try the two extremes

Two identical rankings disagree on no pair. Reversing a four-item ranking changes all six pairs.

Independent Workspace expression - select all, then Print it
| distance |
distance := [:a :b |
 | pos count n |
 n := a size.
 ((a asSet size = n) and: [b size = n and: [a asSet = b asSet]])
   ifFalse: [Error signal: 'Use two strict rankings of the same items'].
 pos := Dictionary new.
 b withIndexDo: [:item :i | pos at: item put: i].
 count := 0.
 1 to: n do: [:i |
   i + 1 to: n do: [:j |
     ((pos at: (a at: i)) > (pos at: (a at: j)))
       ifTrue: [count := count + 1]]].
 count].
{distance value: #(a b c d) value: #(a b c d).
 distance value: #(a b c d) value: #(d c b a)}

Expected: #(0 6)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 5: Try the two extremes
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Try reverse order with three items; expect 3.

Download this checkpoint

06 - Catch a duplicate

Repeated labels make the position lookup ambiguous, so stop before indexing.

Independent Workspace expression - select all, then Print it
| distance |
distance := [:a :b |
 | pos count n |
 n := a size.
 ((a asSet size = n) and: [b size = n and: [a asSet = b asSet]])
   ifFalse: [Error signal: 'Use two strict rankings of the same items'].
 pos := Dictionary new.
 b withIndexDo: [:item :i | pos at: item put: i].
 count := 0.
 1 to: n do: [:i |
   i + 1 to: n do: [:j |
     ((pos at: (a at: i)) > (pos at: (a at: j)))
       ifTrue: [count := count + 1]]].
 count].
[distance value: #(a a b) value: #(a b c)]
 on: Error do: [:e | #rejected]

Expected: #rejected

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 6: Catch a duplicate
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Try a missing item instead; it is also rejected.

Download this checkpoint

Replay the checkpoints

NATIVE SQUEAK CAPTURE
Measure rank disagreement with Kendall distance native Workspace replay
Replay of successive native Squeak Workspace evaluations, not an interaction recording. The static checkpoints appear above.

Reason about correctness and limits

This version requires strict rankings of the same distinct items. It counts discordant pairs, not the Kendall tau coefficient.

The position lookup needs O(n) storage. The nested loops compare n(n−1)/2 pairs.

Three practice challenges

Predict one intermediate value

Swap #a and #b in the first array to see positions 1 and 3 switch.

Show solution

The translated positions become #(1 3 4 2).

Work one small variation

Swap the two arguments. The count remains 3.

Show solution

Every pair where the first ranking disagrees with the second also disagrees when their roles are reversed. The count remains 3.

Check the edge of the model

What would go wrong if two items shared the same label?

Show solution

This version requires strict rankings of the same distinct items. It counts discordant pairs, not the Kendall tau coefficient.

Continue learning

Sorting Visualiser | First Sorted

Environment reference: official Squeak documentation. This lesson and its examples are original. Verified using native Squeak 6.1 build 23976 on Linux.