This file gives an overview of all functions implemented in Chromathsome - Statistics.
It is intended for people who are not able to use the help file.
If you are using a system with Windows 95 or higher as O.S., use the help file.
This is not intended as a replacement for the help file, just an overview of all the function headers.
Most functions are used in the demonstration program.

Basic Statistical functions
===========================
function Mean(Data: StatVector): extended;
Calculates the mean of the data in Data.

function MinValue(Data: StatVector): extended;
function MaxValue(Data: StatVector): extended;
Calculates the biggest/smallest number in Data.

function SUM(Data: StatVector): extended;
function SumOfSquares(Data: StatVector): extended;
Calculates the sum/sum of the squares of the elements in Data.

function SumOfPowers(Data: StatVector; pwr: cardinal):extended;

This functions calculates the sum of the elements of Data to the power pwr.
Example:
Data = [ 1 2 3 4 5 ]; pwr = 5
-> 1^5  + 2^5 + 3^5 + 4^5 + 5^5 = ...
The pwr variable must be greater than (or equal to) zero.

function SumOfPowerXPowerY(x, y: StatVector; xpwr, ypwr: cardinal):extended;
This functions calculates the sum of products of the elements of x to the power xpwr and
y to the power ypwr.
Example:
x = [ 1 2 3 4 5 ]; xpwr = 2; y = [ 6 7 8 9 1 ]; ypwr = 3

-> 1^2*6^3  + 2^2*7^3 + 3^2*8^3 + 4^2*9^3 + 5^2*1^3 = ...
The xpwr and ypwr variables must be greater than (or equal to) zero.
X and y must have the same dimension.

function TotalVariance(Data: StatVector): extended;
function Variance(Data: StatVector): extended;
function PopnVariance(Data: StatVector): extended;
Calculates the sample variance/population variance for the data in Data.

function Covariance(x, y: StatVector): extended;

function PopnCovariance(x, y: StatVector): extended;
Calculates the (sample) covariance / population covariance for the data in x and y.
X and y must be same dimensional vectors.

function StdDev(Data: StatVector): extended;
Calculates the (sample) standard deviation for the data in Data.

procedure MomentSkewKurtosis(Data: StatVector; var M1, M2, M3, M4, Skew, Kurtosis: extended);
Calculates the core factors of statistical analysis:
the first four moments plus the coefficients of skewness and kurtosis.

Skew reflects symmetry of distribution.
Kurtosis reflects flatness of distribution.
 
function PopnStdDev(Data: StatVector): extended;
Calculates the population standard deviation for the data in Data.


Linear Regression
=================

Type  CurveFittingType = (Linear, Exponential, Logarithmic, Power);

procedure CurveFitting(var a, b, R2: extended; X, Y: StatVector; CurveType: CurveFittingType);
Calculates the cofficients of the equation of a curve that fits the data (in X and Y).
CurveType is either Linear, Exponential, Logarithmic, Power.
- Linear:

  Straight line (Linear Regression) y = a + b*x
- Exponential:
  Exponential curve y = a*e^(b*x)  (a > 0)
- Logarithmic:
  Logarithmic curve y = a + b*ln(x)
- Power:
  Power curve y = a*x^b  (a > 0)	
R2 is the cofficient of determination (= R).
Note: X and Y must be same dimensional vectors.

function BestCurveFit(var a, b, RDet: extended; X, Y: StatVector): CurveFittingType;
Finds the curve that fits the data (in X and Y) best.
Result is both curve (one of the curves in CurveFitting) and the cofficients.

RDet is the cofficient of determination (= R).
Note: X and Y must be same dimensional vectors.

procedure MultiLinearRegression1(var a, b, c, R2: extended; X, Y, T: StatVector);
Multiple linear regression for a linear equation in 2 independent variables.
Calculates the cofficients of the linear equation t = a + b*x + c*y.
R2 is the cofficient of determination (= R).
This function requires atleast 3 data points.
Note: X, Y and T must be same dimensional vectors.


procedure MultiLinearRegression2(var a, b, c, d, R2: extended; X, Y, Z, T: StatVector);

Multiple linear regression for a linear equation in 3 independent variables.
Calculates the cofficients of the linear equation t = a + b*x + c*y + d*z.
R2 is the cofficient of determination (= R).
This function requires atleast 4 data points.
Note: X, Y, Z and T must be same dimensional vectors.

Polynomial regression
=====================

procedure PolynomRegression(var CVector: RealVector; var R: extended; x, y: StatVector; Order: cardinal);

Tries to fit the datapoints (Xi, Yi) into a polynom of order Order.

Order must be larger than (or equal to) 2 and lower than (or equal to) 10.
In other words the polynom must have atleast degree 2 (a parabolic curve) and have
a maximum degree of 10.
If you want to fit the data into a straight line, use procedure CurveFitting.
X and y must be same dimensional.
Note that the dimension of x (and y), or the number of datapoints, must be
atleast Order + 1.
CVector is a vector with elements the coefficients of the resulting polynom.

R is the coefficient of determination.

Note:
ChroMathsome - Statistics builds a statistical matrix upon the elements of x and y.
It then tries to solve a system of linear equations to find the coefficients of the polynom.
To solve this system, it needs to calculate the determinant of the statistical matrix.
If this determinant is almost equal to 0 (abs(determinant) < 1E-20), it halts and shows a message box with the message
"Determinant is zero!".
The determinant of the statistical matrix depends on:
a. the numerical values of the elements of x and y;
b. the order of the polynom ( -> statistical matrix is a (order + 1)*(order + 1) matrix).
The exact explanation why this occurs is beyond our scope (study the source code of ChroMathsome - Statistics). 
In the unlikely event that you get the "determinant is zero" message, try to find a polynom with a degree 1 lower 
than the order at which the message was shown. 

procedure BestPolynomFit(var CVector: RealVector; var CDet: extended; x, y: StatVector; MaxOrder: cardinal);

Finds the best fitting polynom (2 =< polynom degree =< MaxOrder) for the datapoints in x and y.
X and y must be same dimensional.
Note that the dimension of x (and y), or the number of datapoints, must be
atleast 1 more than  MaxOrder  (and atleast 3 for the parabolic curve).
CVector is a vector with elements the coefficients of the resulting polynom.
CDet is the coefficient of determination.
MaxOrder must be so that 2 =< MaxOrder =< 10.
See also Note above.

Hint:

If you want the best fitting curve over all possible curves (linear regression and polynomial regression), 
do the following:
- fit the data in a curve (linear regression) via BestCurveFit
- fit the data in a polynom via BestPolynomFit
- compare the coefficients of determination of both
- the more the absolute value of the coefficient of determination is equal to 1, the better the curve/polynom fit.

How to interprete the result from PolynomRegression (and BestPolynomFit)?

*************************************************************************

Result is returned as a real vector that contains the coefficients of the polynom.
The dimension variable of this vector (automatically adjusted by Statistc.dcu) gives the degree of the 
polynom (polynom degree = vector.dimension - 1).
Read the result as:
V.Vector[1] + V.Vector[2]*x + V.Vector[3]*x + ... + V.Vector[n]*x^(n-1) = y.

Without going too far into the implementation part of Statistc.dcu, here's some additional info.
Vectors are implemented in ChroMathsome - Statistics as a record variable, namely:

RealVectorType = array[1..11] of extended;
       
       RealVector = record
             Vector: RealVectorType;
             Dimension: integer;
             end;

The array (RealVector.Vector) is limited to 11 extended values (just enough to hold all coefficients of a polynom of 
degree 10).
The dimension variable (RealVector.Dimension) defines the number of significant values the vector holds (the other 
elements have a value of 0).

ChroMathsome - Statistics will give this variable a useful value.

There's no need to give this variable a value yourself.
Examples that show how to use the vector datatype : see demonstration program.

As you can see (look at the code of / run the demo program), nowhere in the code the vector V has been given value(s)
from an "external" source.
ChroMathsome - Statistics does this all by itself, for you.
It is, therefore, advised that you use the vectors as shown in the examples and do not worry too much about what's going 
on in the background.

Implementing vectors this way has the great benefit that one does not need to know the
dimension of this vector at forehand (look at the examples in the demonstration program).

Although the (extra) memory use of this vector datatype may be ignored,
you can use pointers ( var V: ^RealVector; ) in your code.
If so, remember to use a try ... finally block to make sure the pointers get freed always. 

Note:
A real vector (RealVector) is not the same as a statistical vector (StatVector).

A statistical vector "grows" dynamically while a real vector has a static dimension of 11 elements.
A statistical vector can have, for example, 1000's of elements.

Errors in Regression Coefficients
=================================

The coefficints found using the regressions functions are not always 100% correct because these functions use 
the least squares method to find a curve that fits the data points.

The following procedures enable you to find the deviation (error) in every point.
The error is calculated using the coefficients of the curve (as found by the regression functions) and testing this result 
with the value it should be (see example, lower).


procedure LinearRegressionDeviations(var Z: Statvector; a, b: extended; X, Y: StatVector; CurveType: CurveFittingType);
Finds the deviation in every point for the coefficients found with LinRegression.
Coefficients a and b must be provided as well as Statvectors X and Y.
Returns StatVector Z with as elements the deviation in every point.
Z[i] corresponds to values X[i] and Y[i].    
 
procedure MultiLinearRegression1Deviations(var Z: Statvector; a, b, c: extended; X, Y, T: StatVector);

procedure MultiLinearRegression2Deviations(var Z1: Statvector; a, b, c, d: extended; X, Y, Z, T: StatVector);
Same, now for the multi linear regression functions.

procedure PolynomRegressionDeviations(var Z: Statvector; X, Y: StatVector; CVector: RealVector);
Finds the deviations in every point for the coefficients found with PolynomRegression.
Coefficients (and degree of polynom) defined by CVector.
Returns StatVector Z with as elements the deviations in every point.

Z[i] corresponds to values X[i] and Y[i].    

Note:
In all the above procedures, ALL statvectors must be same dimensional.
If not an exception will be raised.

The following example will clearify the above very much.

If we take the data points from the demo program to find a logarithmic curve and calculate the regression cofficients 
we find:
X = [ 3 4 6 10 12 ]; Y = [ 1.5 9.3 23.4 45.8 60.1 ]
a:= -47,0211978419916
b:= 41,3944676307222
or Y = a + b*ln(X) = -47,0211978419916 + 41,3944676307222*ln(X).

If we, now, substitute the X[i] value in this equation, Y will not always be exact Y[i] but a (small) error will be present.
By using LinearRegressionDeviations for a logarithmic curve we get a statvector V with the following elements:
V[1] = 0,0447270200058524
V[2] = 1,06371921603848
V[3] = 3,74773150900918
V[4] = 2,49308625693398
V[5] = -4,25980996197578

This means that, for example, the error for X[2] is 1.0637192....
In other words, using the coefficients a and b, a + b*ln(4) = 10.363719216 = 9.3 + 1.0637192

(the error is always calculated using the absolute values)

Miscellaneous Functions
========================

procedure NormalizeData(var Data: StatVector);
Normalizes the data in Data.
Every element in data will be divided by the sum of all elements.

function Percent(x,y: extended):extended;
Calculates x percent of y.
Example: x = 15, y = 300 -> Percent(x, y) = 45.

function Faculty(i:cardinal):extended;
Calculates i!.
Example: 6! = 6*5*4*3*2*1 = 720.
If i > 1754, an overflow will occur.
( 1754! = 1,97926189010501E4930)


function Combinations(i1,i2:cardinal):extended;

function Permutations(i1,i2:cardinal):extended;
Permutations:
Given i1 objects, the number of ways to select and arrange i2 of these objects in different order.
Formula: i1!/(i1-i2)!.
Note: i1 > i2.
If not -> exception.
Combinations:
Combinations ignore the order in the i2 objects chosen.
Formula: i1!/(i2!*(i1-i2)!).
Note: i1 > i2.
If not -> exception.

function RandG(Mean, StdDev: extended): extended;

RandG produces random numbers with Gaussian distribution about the mean.
Useful for simulating data with sampling errors.

Distribution Functions
======================
  function BetaDistribution(x, n, w: extended):extended;
  function FDistribution(x, n, w: extended): extended;
  function StudentsTDistribution(x, n: extended):extended;
  function ChiSquareDistribution(x, n: extended):extended;
  function NormalDistribution(X, CMean, CSDeviation: extended): extended;
  function PoissonDistribution(X: LongInt; CMean: extended): extended;
  function WeibullDistribution(X, b, c, q: extended): extended;
  function CauchyDistribution(X, h, q: extended): extended;
  function ExponentialDistribution(X, l: extended): extended;
  function ExtremeValueDistribution(X, a, b: extended): extended;
  function GeometricDistribution(X, p: extended): extended;
  function LaplaceDistribution(X, a, b: extended): extended;
  function LogisticDistribution(X, a, b: extended): extended;
  function LogNormalDistribution(X, m, s: extended): extended;
  function ParetoDistribution(X, c: extended): extended;
  function RaleighDistribution(X, b: extended): extended;
  function BinomialDistribution(X, p, n: longint): extended;

Test Statistics
===============
function PairedtStatistic(X, Y: StatVector):extended;
function tStatistic(X, Y: StatVector; d: extended): extended;
function ChiSquareStatistic(X, Y: StatVector):extended;




