' ******************************************************************
' Test of Gaussian random number generator.
'
' This program picks a random sample of size N from a gaussian
' distribution with known mean and standard deviation (SD), estimates
' mean and SD from the sample, and computes a 95% confidence interval
' (CI) for the mean (i.e. an interval which has a probability of 0.95
' to include the true mean).
' ******************************************************************

#INCLUDE "fbmath.bi"

CONST Mu    =  10  ' Mean of Gaussian distribution
CONST Sigma =   2  ' Standard deviation of Gaussian distribution
CONST N     = 100  ' Sample size, must be > 30

DIM AS DOUBLE  X(1 TO N)  ' Sample values
DIM AS DOUBLE  M, S       ' Sample mean & SD
DIM AS DOUBLE  Delta      ' Half-width of CI
DIM AS DOUBLE  M1, M2     ' Bounds of CI
DIM AS INTEGER I          ' Loop variable

' Initialize RNG
RANDOMIZE TIMER

' Pick sample values
FOR I = 1 TO N
  X(I) = RanGauss(Mu, Sigma)
NEXT I

' Estimate mean and SD from sample
M = Mean(X())
S = StDev(X(), M)

' Compute 95% CI, assuming that the sample mean
' is normally distributed.
' This requires N > 30
Delta = 1.96 * S / SQR(N)
M1 = M - Delta
M2 = M + Delta

' Display results
PRINT
PRINT USING "Population mean = ######.####"; Mu
PRINT USING "Population SD   = ######.####"; Sigma
PRINT
PRINT "Sample size     =   "; N
PRINT
PRINT USING "Estimated mean  = ######.####"; M
PRINT USING "Estimated SD    = ######.####"; S
PRINT
PRINT "95% CI of mean  = [";
PRINT USING "######.####"; M1;
PRINT " , ";
PRINT USING "######.####"; M2;
PRINT " ]"

SLEEP
