s LearnSqueak.com

SMALLTALK LAB

Median Absolute Deviation in Squeak: Measure Spread Robustly

Compute a robust measure of spread for a short response-time sample. Work through the median, absolute distances, extreme values and the zero-MAD corner case.

6 tested checkpointsNative Squeak screenshotsComputed visual replay

Before you begin

Open a Squeak Workspace. Use Print it on the complete checkpoint, including its temporary variable declarations.

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 show explicit Symbol markers: #(A B) and #(#A #B) represent the same array of Symbols.

01 - Sort before choosing the center

Distances need a reference point. Sorting places the middle of this odd-length list at index three, giving 12 even with an extreme value at the end.

Independent Workspace expression - select all, then Print it
( #(12 10 99 11 13) sorted ) asArray

Expected: #(10 11 12 13 99)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 1: Sort before choosing the center
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Replace 99 by 999. The middle value stays the same.

Download this checkpoint

02 - Write a median for both lengths

An even list has two middle neighbors. Here they are 3 and 6; averaging with Smalltalk division preserves the exact fraction. The block assumes nonempty input.

Independent Workspace expression - select all, then Print it
| median |
median := [:samples | | ordered middle |
 ordered := samples sorted.
 middle := ordered size // 2.
 ordered size odd
  ifTrue: [ordered at: middle + 1]
  ifFalse: [((ordered at: middle) + (ordered at: middle + 1)) / 2]].
{median value: #(12 10 99 11 13).
 median value: #(3 9 1 6)}

Expected: {12. 9/2}

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 2: Write a median for both lengths
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Try a single value and check that it returns unchanged.

Download this checkpoint

03 - Measure distances from the center

Take absolute distances so values on different sides do not cancel. Keep original order here so the large distance can be associated with 99.

Independent Workspace expression - select all, then Print it
| median sample center |
median := [:samples | | ordered middle |
 ordered := samples sorted.
 middle := ordered size // 2.
 ordered size odd
  ifTrue: [ordered at: middle + 1]
  ifFalse: [((ordered at: middle) + (ordered at: middle + 1)) / 2]].
sample := #(12 10 99 11 13).
center := median value: sample.
sample collect: [:x | (x - center) abs]

Expected: #(0 2 87 1 1)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 3: Measure distances from the center
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: The sample value equal to the median contributes distance zero.

Download this checkpoint

04 - Take the second median

The five distances sort to 0, 1, 1, 2, 87. The median of the distances is 1. This is the raw MAD, in the same units as the sample.

Independent Workspace expression - select all, then Print it
| median summary |
median := [:samples | | ordered middle |
 ordered := samples sorted.
 middle := ordered size // 2.
 ordered size odd
  ifTrue: [ordered at: middle + 1]
  ifFalse: [((ordered at: middle) + (ordered at: middle + 1)) / 2]].
summary := [:samples | | center deviations |
 center := median value: samples.
 deviations := samples collect: [:x | (x - center) abs].
 {center. median value: deviations}].
summary value: #(12 10 99 11 13)

Expected: #(12 1)

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

Try it: MAD means median absolute deviation here, not mean absolute deviation.

Download this checkpoint

05 - Check resilience to a large outlier

A more extreme final observation raises the biggest distance without changing the median or the middle distance. Robust does not mean unchanged under every possible edit; adding many extreme observations can shift the medians.

Independent Workspace expression - select all, then Print it
| median summary |
median := [:samples | | ordered middle |
 ordered := samples sorted.
 middle := ordered size // 2.
 ordered size odd
  ifTrue: [ordered at: middle + 1]
  ifFalse: [((ordered at: middle) + (ordered at: middle + 1)) / 2]].
summary := [:samples | | center deviations |
 center := median value: samples.
 deviations := samples collect: [:x | (x - center) abs].
 {center. median value: deviations}].
{summary value: #(12 10 14 11 13).
 summary value: #(12 10 10000 11 13)}

Expected: #((12 1) (12 1))

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 5: Check resilience to a large outlier
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Replace three observations instead of one. Predict when the middle position moves.

Download this checkpoint

06 - Give a zero result a safe interpretation

A majority at one value can make the MAD zero despite a distant observation. Report the result instead of dividing by zero. Raw MAD is not a standard deviation; under a normal-model assumption a common consistency factor is approximately 1.4826.

Independent Workspace expression - select all, then Print it
| median summary |
median := [:samples | | ordered middle |
 ordered := samples sorted.
 middle := ordered size // 2.
 ordered size odd
  ifTrue: [ordered at: middle + 1]
  ifFalse: [((ordered at: middle) + (ordered at: middle + 1)) / 2]].
summary := [:samples | | center deviations |
 center := median value: samples.
 deviations := samples collect: [:x | (x - center) abs].
 {center. median value: deviations}].
summary value: #(5 5 5 5 50)

Expected: #(5 0)

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

Try it: Look at the count of exact ties and the raw observations before interpreting a zero.

Download this checkpoint

Replay the checkpoints

NATIVE SQUEAK CAPTURE
Median Absolute Deviation in Squeak: Measure Spread Robustly native Workspace replay
Replay of successive native Squeak Workspace evaluations, not an interaction recording. The complete static checkpoints appear above.

Reason about correctness and limits

The 1.4826 scaling is a large-sample normal consistency convention, not a correction that makes MAD identical to standard deviation for arbitrary samples.

This teaching implementation sorts twice, making it O(n log n). Choose explicit input validation and an empty-input policy for reusable code.

Three practice challenges

Constant shift

What changes if every value increases by ten?

Show solution

The center increases by ten; the MAD is unchanged.

Change units

What happens when all milliseconds are converted to microseconds?

Show solution

Both the center and MAD multiply by 1,000.

Find the even-case distance

What is the MAD for 1, 2, 4, 5?

Show solution

Median 3; distances 2, 1, 1, 2, so the median distance is 3/2.

Continue learning

Online Variance | Probability Lab | First Absolute

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