#### Statistics

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

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),
fwrite(f, 10, 2, Mean), % 4.01
fwrite(f, 10, 7, StdVarPopulation), % 0.1350309
fwrite(f, 10, 7, StdVarSample), nl. % 0.1373392
statistics(Ns, Xs, Mean, StdVarPopulation, StdVarSample):-
statistics_1(Ns, Xs, 0, S_N, 0, S_NX, 0, S_NXX),
Mean is S_NX / S_N,
StdVarPopulation is sqrt((S_NXX - Mean * Mean * S_N) / S_N),
StdVarSample is 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 is S_N0 + N,
N_X is N * X,
S_NX1 is S_NX0 + N_X,
S_NXX1 is S_NXX0 + N_X * X,
statistics_1(Ns, Xs, S_N1, S_N, S_NX1, S_NX, S_NXX1, S_NXX).

LPA Index
Home Page