#### Statistics

This calculates the mean and the standard deviation of data represented by values (Xs) and their frequencies (Ns).

```check_determ
domains
int   = integer
ints  = int*
reals = real*
predicates
test_statistics
statistics(ints,reals,real,real,real)
statistics_1(ints,reals,int,int,real,real,real,real)
clauses

test_statistics:-
Xs = [3.7, 3.8, 3.9, 4.0, 4.1, 4.2, 4.3],
Ns = [  1,   3,   5,   9,   8,   3,   1],
statistics(Ns, Xs,         Mean, StdVarPopulation, StdVarSample),
/* 4.01,    0.1350309,       0.1373392 */
writef("%0.7 %0.7 %0.7\n", Mean, StdVarPopulation, StdVarSample).

statistics(Ns, Xs, Mean, StdVarPopulation, StdVarSample):-
statistics_1(Ns, Xs, 0, S_N, 0, S_NX, 0, S_NXX),
Mean = S_NX / S_N,
StdVarPopulation = sqrt((S_NXX - Mean * Mean * S_N) / S_N),
StdVarSample = sqrt((S_NXX - Mean * Mean * S_N) / (S_N - 1)).

statistics_1([], [], S_N, S_N, S_NX, S_NX, S_NXX, S_NXX).
statistics_1([N|Ns], [X|Xs], S_N0, S_N, S_NX0, S_NX, S_NXX0, S_NXX):-
S_N1 = S_N0 + N,
N_X = N * X,
S_NX1 = S_NX0 + N_X,
S_NXX1 = S_NXX0 + N_X * X,
statistics_1(Ns, Xs, S_N1, S_N, S_NX1, S_NX, S_NXX1, S_NXX).
```