1
{ ******************************************************************
2
Statistical distribution and Histogram
3
****************************************************************** }
11
N = 30; { Number of values }
12
Alpha = 0.05; { Significance level }
14
{ Hemoglobin concentrations in men }
15
const HbM : array[1..N] of Float =
16
(141, 144, 146, 148, 149, 150, 150, 151, 153, 153,
17
153, 154, 155, 156, 156, 160, 160, 160, 163, 164,
18
164, 165, 166, 168, 168, 170, 172, 172, 176, 179);
21
M, S : Float; { Mean and standard deviation }
23
function PltFunc(X : Float) : Float;
24
{ ------------------------------------------------------------------
25
Function to be plotted (density of normal distribution)
26
------------------------------------------------------------------ }
29
PltFunc := DNorm((X - M) / S) / S;
32
procedure WriteResults(C : PStatClassVector;
38
{ ------------------------------------------------------------------
39
Writes results to screen
40
------------------------------------------------------------------ }
43
Line1 = '-----------------------------';
50
Writeln('Statistical distribution');
53
Writeln(' Inf Sup N Ncalc');
59
Writeln(C^[I].Inf:8:0, C^[I].Sup:7:0, C^[I].N:7, Calc^[I]:7:2);
60
Sum := Sum + Calc^[I];
64
Writeln('Total ', N:5, Sum:7:2);
68
Writeln('Comparison with normal distribution:');
71
Writeln('Pearson''s Khi-2 = ', Khi2:10:4, ' (', DoF, ' DoF)');
72
Writeln('Woolf''s G = ', G:10:4, ' (', DoF, ' DoF)');
73
Writeln('Critical value (p = ', Alpha:4:2, ') = ', K2c:10:4);
76
procedure PlotGraph(C : PStatClassVector;
78
Xmin , Xmax, Xstep : Float);
79
{ ------------------------------------------------------------------
80
Plots histogram and normal curve
81
------------------------------------------------------------------ }
84
Ymin, Ymax, Ystep : Float; { Oy scale }
85
Npts : Integer; { Number of points }
86
X, Y : PVector; { Point coordinates }
87
I, J : Integer; { Loop variables }
90
if not InitGraphics(9, 2, 'c:\tp\bgi') then { 640x480 16 color }
92
Writeln('Unable to set graphic mode');
96
SetWindow(15, 85, 15, 85, True);
98
{ The histogram is plotted as a continuous curve.
99
Each bar of the histogram is defined by 4 points }
106
for I := 1 to Ncls do
109
J := 4 * (I - 1) + 1; X^[J] := Inf; { Y^[J] := 0 }
110
Inc(J); X^[J] := Inf; Y^[J] := D;
111
Inc(J); X^[J] := Sup; Y^[J] := D;
112
Inc(J); X^[J] := Sup; { Y^[J] := 0 }
115
{ Set scale on Oy, making sure that it starts from 0 }
117
AutoScale(Y, 1, Ncls, LinScale, Ymin, Ymax, Ystep);
119
if Ymin <> 0.0 then Ymin := 0.0;
120
Ymax := Ymax + Ystep;
122
SetOxScale(LinScale, Xmin, Xmax, Xstep);
123
SetOyScale(LinScale, Ymin, Ymax, Ystep);
125
SetGraphTitle('Statistical Distribution');
127
SetOyTitle('Frequency Density');
136
{ Plot histogram and normal curve }
138
SetPointParam(1, 0, 0, 0); { Don't show points on histogram }
139
SetLineParam(1, 1, 3, 1); { Use thick lines }
141
PlotCurve(X, Y, 1, Npts, 1);
143
PlotFunc({$IFDEF FPC}@{$ENDIF}PltFunc, Xmin, Xmax, 2);
153
{ ******************************************************************
155
****************************************************************** }
158
X : PVector; { Data }
159
C : PStatClassVector; { Statistical classes }
160
Ncls : Integer; { Number of classes }
161
Obs : PIntVector; { Observed frequencies }
162
Calc : PVector; { Calculated frequencies }
163
Khi2 : Float; { Pearson's Khi-2 }
164
G : Float; { Woolf's G }
165
K2c : Float; { Theoretical Khi-2 }
166
DoF : Integer; { Degrees of freedom }
167
T : Float; { Standard normal variable }
168
F0, F : Float; { Cumulative probability }
171
XStep : Float; { Scale on Ox }
172
I : Integer; { Loop variable }
180
{ Sort data if necessary }
183
{ Compute an appropriate interval for the set of values }
184
Interval(X^[1], X^[N], 5, 10, XMin, XMax, XStep);
186
{ Compute number of classes and dimension arrays }
187
Ncls := Round((Xmax - Xmin) / XStep);
189
DimStatClassVector(C, Ncls);
190
DimIntVector(Obs, Ncls);
191
DimVector(Calc, Ncls);
193
{ Compute distribution }
194
Distrib(X, 1, N, Xmin, Xmax, XStep, C);
196
{ Compute mean and S.D. }
198
S := StDev(X, 1, N, M);
200
{ Compute theoretical values }
202
for I := 1 to Ncls do
208
T := (C^[I].Sup - M) / S;
211
Calc^[I] := N * (F - F0);
216
{ Perform Khi-2 and Woolf tests }
217
Khi2_Conform(Ncls, 2, Obs, Calc, Khi2, DoF);
218
Woolf_Conform(Ncls, 2, Obs, Calc, G, DoF);
220
{ Compute critical value }
221
K2c := InvKhi2(DoF, 1.0 - Alpha);
224
WriteResults(C, Ncls, Calc, Khi2, G, DoF, K2c);
229
PlotGraph(C, Ncls, Xmin - Xstep, Xmax + Xstep, Xstep);