Scientific programming in Modula-9 — first steps

↖ contents

6 — Simple math and statistics

Stats is the library this project uses for its own science, and its numbers come with a pedigree. Every procedure is gated against numpy and scipy, the p-values digit for digit. The expected values are checked into the repository, because a gate that regenerates what it compares against cannot fail. When the tutorial prints a p-value below, that number has an oracle behind it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
MODULE C6Stats ;

(* Chapter 6 computes statistics with real p-values.  The library's
   own gate verifies them against scipy digit for digit, so a
   tutorial can print them without hedging.  Two synthetic samples:
   y depends on x almost linearly, and the two groups differ.       *)

IMPORT Io ;
IMPORT Faults ;
IMPORT Fmt ;
IMPORT Stats ;

PROCEDURE P (RO label: STR ; v: F64 ; dec: I64) =
BEGIN
  Io.Write (label) ;
  Io.Write (Fmt.Fixed (v, dec)) ;
  Io.WriteLine ('')
EXCEPT
| ValueRange :
    Io.ErrLine ('formatting failed') ;
    Io.Halt (1)
END P ;

VAR
  x, y : ARRAY 8 OF F64 ;
  a, b : ARRAY 6 OF F64 ;
  i    : I64 ;
  reg  : Stats.Reg ;
  tst  : Stats.Test ;

BEGIN
  FOR i := 0 TO 7 DO
    x [i] := F64 (i) ;
    y [i] := 2.0 + 0.5 * F64 (i)
  END ;
  y [3] := y [3] + 0.4 ;             (* one imperfect point *)
  P ('mean y    ', Stats.Mean (y), 4) ;
  P ('std y     ', Stats.Std (y), 4) ;
  P ('median y  ', Stats.Median (y), 4) ;
  P ('p90 y     ', Stats.Percentile (y, 90.0), 4) ;

  reg := Stats.LinReg (x, y) ;
  P ('slope     ', reg.slope, 4) ;
  P ('intercept ', reg.intercept, 4) ;
  P ('r         ', reg.r, 4) ;
  P ('p         ', reg.p, 8) ;

  a [0] := 5.1 ; a [1] := 4.9 ; a [2] := 5.3 ;
  a [3] := 5.0 ; a [4] := 5.2 ; a [5] := 4.8 ;
  FOR i := 0 TO 5 DO
    b [i] := a [i] + 0.6                (* shifted group *)
  END ;
  tst := Stats.TTest2 (a, b) ;
  P ('Welch t   ', tst.t, 4) ;
  P ('p         ', tst.p, 6)
EXCEPT
| Stats.TooFew :
    Io.ErrLine ('sample too small') ; Io.Halt (1)
| Faults.BadArg :
    Io.ErrLine ('bad argument') ; Io.Halt (1)
| ValueRange :
    Io.ErrLine ('a sample that does not vary') ; Io.Halt (1)
| Overflow :
    Io.ErrLine ('overflow') ; Io.Halt (1)
END C6Stats.
C6Stats.m9

expected output:

mean y    3.8000
std y     1.2212
median y  3.9500
p90 y     5.1500
slope     0.4952
intercept 2.0667
r         0.9933
p         0.00000074
Welch t   -5.5549
p         0.000242

The library gives you, in the order a working analysis meets them: moments (Mean, Std, and the population forms), order statistics (Median, Percentile), a normal fit, least-squares regression with scipy.linregress's five numbers, and both t-tests. Percentile follows numpy's linear interpolation rule, so your quartiles match your colleague's notebook. TTest2 is Welch's: it assumes unequal variances, which is the safe default for real measurements. The p-values are real two-sided probabilities computed through the incomplete beta function, not lookup-table approximations.

For reproducible synthetic data, Stats.Seed gives a deterministic Stream with uniform, integer, normal, exponential and log-normal draws. The draws are bit-for-bit reproducible: seed in, same sequence out, on every machine.

The NaN policy, and why it is a policy

Chapter 5 turned a declared gap into NaN. What happens when a NaN reaches a statistic — and when a sample is nothing but gaps?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
MODULE C6Nan ;

(* Chapter 6.  A NaN in a sample is a MISSING VALUE: the mean of
   [1, NaN, 3] is 2, over the two values that are there.  The
   library says how many that was.  A skip nobody can see changes n
   behind your back; a skip with its count beside it is a statement
   about your data.  A sample of nothing but gaps is not answered
   at all: it is refused, by that same count.                      *)

IMPORT Io ;
IMPORT Stats ;

VAR
  xs, gaps : ARRAY 3 OF F64 ;
  m : F64 ;
  i : I64 ;

BEGIN
  xs [0] := 1.0 ;
  xs [1] := NaN ;                   (* a gap, as data has *)
  xs [2] := 3.0 ;
  m := Stats.Mean (xs) ;
  Io.WriteLine ($'mean = ${m:.3} over ${Stats.Count (xs)}'
                + $' of ${LEN (xs)} values') ;
  FOR i := 0 TO 2 DO gaps [i] := NaN END ;
  m := Stats.Mean (gaps) ;
  Io.WriteLine ($'mean = ${m:.3}')
EXCEPT
| Stats.TooFew (got, need) :
    Io.WriteLine ($'nothing but gaps: ${got} values, '
                  + $'and a mean needs ${need}')
| ValueRange :
    Io.WriteLine ('formatting failed')
END C6Nan.
C6Nan.m9

expected output:

mean = 2.000 over 2 of 3 values
nothing but gaps: 0 values, and a mean needs 1

(The lines are written as interpolation literals: $'...' is a string with holes, ${m:.3} is m with three decimals and ${LEN (xs)} an integer as it is. Chapter 12 has the whole of it. The chapters before this one build such a line from + and Fmt calls, which is what a hole stands for.)

The gap is skipped, and counted out. A library can give two answers here that someone regrets. One is a NaN mean (correct IEEE, useless science). The other is the silently "helpful" skip, which changes n without telling you. It turns "a third of my sample is missing" into a confident narrow confidence interval. Stats gives neither. It takes every statistic over the values that are there. Measured series have gaps, and a mean that refuses them is only ever called behind a filter someone wrote by hand. And it puts the count in your hands: Stats.Count (xs) beside a mean, n in the answer of a regression, a fit or a test, Stats.RollingCount beside a rolling mean. For two samples taken together, a *pair* counts when both its values are present. And when nothing is left to count, the answer is not a NaN but a refusal by name, TooFew, carrying the number it found. The second line above shows it.

So the rule for your own code is one line long: print n beside every statistic of measured data. The library will always tell you what it was.

NaN in the program above is a name the language gives you, as it gives you STR: the quiet NaN, an F64 or an F32 wherever one is wanted. Its bits are the ones numpy, Python and C's NAN use. 0.0 / 0.0 still makes a NaN, but on an x86 machine it is a NaN with its sign bit set, a different bit pattern from numpy's, which shows when a file M9 wrote is compared with one Python wrote.

A name for NaN invites the test everybody writes first:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
MODULE X6NanEq ;

(* EXPECT-ERROR: no '=' with NaN *)
(* Chapter 6 shows a program that must NOT compile.  Everybody
   writes this test first for a missing value: is x NaN?  Written
   with '=' it is never TRUE.  A NaN is unequal to everything, itself
   included, so the gap it was meant to catch goes straight through.
   The checker refuses the comparison and names the test that works. *)

IMPORT Io ;

VAR x : F64 ;

BEGIN
  x := NaN ;
  IF x = NaN THEN Io.WriteLine ('a gap') END
END X6NanEq.
X6NanEq.m9

the compiler refuses:

X6NanEq.m9:16:8: X6NanEq body: no '=' with NaN: no comparison with NaN is ever TRUE, except '#', which always is; Math.IsNaN (x) asks (par 2.1)

A NaN is unequal to everything, itself included, so x = NaN is never TRUE. The gap it was written to catch would go straight through. The checker refuses every comparison with NaN and names the question that works: Math.IsNaN (x). (x # x is TRUE exactly when x is a NaN, which is the same test written as a riddle.)

*(Until 0.14 a NaN in a sample raised ValueRange, and skipping was the caller's loop. Chapter 5's filter is exactly that loop, and it still works. The rule changed because the loop hid the very thing the refusal was meant to keep in view. The count keeps it in view now.)*

One more time: a checked build runs within a few percent of the same code with every check stripped. A language does not have to choose between honest and fast.

← Previous: reading and writing data · Next: timeseries →