sourcelibs/Statistics/demos/heights.xtl
1⍝!/usr/bin/env xetal
2⍝# Heights and weights of twelve people: a summary of each, how they
3⍝# relate, the line through them, and who is unusual.
4⍝# Run with "just demo Statistics heights".
5
6ˢˣ⁼u̲se< "Statistics"
7ˢ⁼u̲se< "Stats"
8height ← 158 162 165 168 170 171 173 175 178 180 183 191 ⍝ cm
9weight ← 52 58 61 60 66 70 68 72 75 80 79 95 ⍝ kg
10
11ˢˣf̲ive height ⍝ least, quartiles, median, greatest
12ˢm̲ean weight
13height ˢˣc̲orrelation weight ⍝ strongly related
14f ← height ˢˣf̲it weight ⍝ kg = intercept + slope * cm
15f
16(f̲irst f) + 175.0 × f̲irst 1 d̲rop f ⍝ the expected weight at 175 cm
17
18⍝ Unusual: more than 1.5 standard deviations from the mean.
19z ← ˢˣz̲scores weight
20((a̲bs z) > 1.5) r̲eplicate weight
21
22⍝ The mean height of those over 100 kg: nobody here, and the mean of
23⍝ nothing divides by zero. A guard evaluates only the result it
24⍝ chooses, so the mean is never taken of an empty list (a comparison
25⍝ used as a number, (n > 0) * mean, would take it and stop with an
26⍝ error).
27ᵘm̲eanOr0 ← { v → 0 = t̲ally v ? 0.0◆ ˢm̲ean v }
28ᵘm̲eanOr0 (weight > 100) r̲eplicate height