# Statistics

The Statistical Library provides a convenient way of working with the basic statistical distributions.

The library provides 5 functions for each distribution:

1. Calculation of probability density – functions of the form MathProbabilityDensityX()
2. Calculation of probabilities – functions of the form MathCumulativeDistributionX()
3. Calculation of distribution quantiles – functions of the form MathQuantileX()
4. Generation of random numbers with the specified distribution – functions of the form MathRandomX()
5. Calculation of the theoretical moments of the distributions – functions of the form MathMomentsX()

In addition to calculation of values for the individual random variables, the library also provides overloads for the functions, which perform the same calculations for arrays.

Example:

 //+------------------------------------------------------------------+ //|                                    NormalDistributionExample.mq5 | //|                        Copyright 2016, MetaQuotes Software Corp. | //|                                             https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2016, MetaQuotes Software Corp." #property link      "https://www.mql5.com" #property version   "1.00" //--- include the functions for calculating the normal distribution #include  //+------------------------------------------------------------------+ //| Script program start function                                    | //+------------------------------------------------------------------+ void OnStart()   { //--- set the parameters of the normal distribution    double mu=5.0;    double sigma=1.0;    PrintFormat("Normal distribution with parameters mu=%G and sigma=%G, calculation examples:",mu,sigma); //--- set the interval    double x1=mu-sigma;    double x2=mu+sigma; //--- variables for probability calculation    double cdf1,cdf2,probability; //--- variables for error codes    int error_code1,error_code2; //--- calculate the values of distribution functions    cdf1=MathCumulativeDistributionNormal(x1,mu,sigma,error_code1);    cdf2=MathCumulativeDistributionNormal(x2,mu,sigma,error_code2); //--- check the error codes    if(error_code1==ERR_OK && error_code2==ERR_OK)      {       //--- calculate probability of a random variable in the range       probability=cdf2-cdf1;       //--- output the result       PrintFormat("1. Calculate probability of a random variable within the range of %.5f