# 用 MQL5 表示统计概率分布

2 一月 2014, 06:30
0
902

(Leonid Sukhorukov)

### 2. 理论概率分布

2.1.1 正态分布

• X 是从正态分布 Nor 选择的随机变量；
• μ 是平均值参数 (-∞ ≤ μ ≤ +∞)；
• σ 是方差参数 (0<σ)。

```//+------------------------------------------------------------------+
//|                Normal Distribution class definition              |
//+------------------------------------------------------------------+
class CNormaldist : CErf // Erf class inheritance
{
public:
double            mu, //mean parameter (μ)
sig;                 //variance parameter (σ)
//+------------------------------------------------------------------+
//| CNormaldist class constructor                                    |
//+------------------------------------------------------------------+
void  CNormaldist()
{
mu=0.0;sig=1.0; //default parameters μ and σ
}
//+------------------------------------------------------------------+
//| Probability density function (pdf)                               |
//+------------------------------------------------------------------+
double pdf(double x)
{
return(0.398942280401432678/sig)*exp(-0.5*pow((x-mu)/sig,2));
}
//+------------------------------------------------------------------+
//| Cumulative distribution function (cdf)                           |
//+------------------------------------------------------------------+
double cdf(double x)
{
return 0.5*erfc(-0.707106781186547524*(x-mu)/sig);
}
//+------------------------------------------------------------------+
//| Inverse cumulative distribution function (invcdf)                |
//| quantile function                                                |
//+------------------------------------------------------------------+
double invcdf(double p)
{
if(!(p>0. && p<1.))
return -1.41421356237309505*sig*inverfc(2.*p)+mu;
}
//+------------------------------------------------------------------+
//| Reliability (survival) function (sf)                             |
//+------------------------------------------------------------------+
double sf(double x)
{
return 1-cdf(x);
}
};

//+------------------------------------------------------------------+```

```//+------------------------------------------------------------------+
//|                Error Function class definition                   |
//+------------------------------------------------------------------+
class CErf
{
public:
int               ncof;    // coefficient array size
double            cof[28]; // Chebyshev coefficient array
//+------------------------------------------------------------------+
//| CErf class constructor                                           |
//+------------------------------------------------------------------+
void CErf()
{
int Ncof=28;
double Cof[28]=//Chebyshev coefficients
{
-1.3026537197817094,6.4196979235649026e-1,
1.9476473204185836e-2,-9.561514786808631e-3,-9.46595344482036e-4,
3.66839497852761e-4,4.2523324806907e-5,-2.0278578112534e-5,
-1.624290004647e-6,1.303655835580e-6,1.5626441722e-8,-8.5238095915e-8,
6.529054439e-9,5.059343495e-9,-9.91364156e-10,-2.27365122e-10,
9.6467911e-11, 2.394038e-12,-6.886027e-12,8.94487e-13, 3.13092e-13,
-1.12708e-13,3.81e-16,7.106e-15,-1.523e-15,-9.4e-17,1.21e-16,-2.8e-17
};
setCErf(Ncof,Cof);
};
//+------------------------------------------------------------------+
//| Set-method for ncof                                              |
//+------------------------------------------------------------------+
void setCErf(int Ncof,double &Cof[])
{
ncof=Ncof;
ArrayCopy(cof,Cof);
};
//+------------------------------------------------------------------+
//| CErf class destructor                                            |
//+------------------------------------------------------------------+
void ~CErf(){};
//+------------------------------------------------------------------+
//| Error function                                                   |
//+------------------------------------------------------------------+
double erf(double x)
{
if(x>=0.0) return 1.0-erfccheb(x);
else return erfccheb(-x)-1.0;
}
//+------------------------------------------------------------------+
//| Complementary error function                                     |
//+------------------------------------------------------------------+
double erfc(double x)
{
if(x>=0.0) return erfccheb(x);
else return 2.0-erfccheb(-x);
}
//+------------------------------------------------------------------+
//| Chebyshev approximations for the error function                  |
//+------------------------------------------------------------------+
double erfccheb(double z)
{
int j;
double t,ty,tmp,d=0.0,dd=0.0;
t=2.0/(2.0+z);
ty=4.0*t-2.0;
for(j=ncof-1;j>0;j--)
{
tmp=d;
d=ty*d-dd+cof[j];
dd=tmp;
}
return t*exp(-z*z+0.5*(cof[0]+ty*d)-dd);
}
//+------------------------------------------------------------------+
//| Inverse complementary error function                             |
//+------------------------------------------------------------------+
double inverfc(double p)
{
double x,err,t,pp;
if(p >= 2.0) return -100.0;
if(p <= 0.0) return 100.0;
pp=(p<1.0)? p : 2.0-p;
t = sqrt(-2.*log(pp/2.0));
x = -0.70711*((2.30753+t*0.27061)/(1.0+t*(0.99229+t*0.04481)) - t);
for(int j=0;j<2;j++)
{
err=erfc(x)-pp;
x+=err/(M_2_SQRTPI*exp(-pow(x,2))-x*err);
}
return(p<1.0? x : -x);
}
//+------------------------------------------------------------------+
//| Inverse error function                                           |
//+------------------------------------------------------------------+
double inverf(double p)
{return inverfc(1.0-p);}
};
//+------------------------------------------------------------------+
double erfcc(const double x)
/*
complementary error function erfc(x) with
a relative error of 1.2 * 10^(-7)
*/
{
double t,z=fabs(x),ans;
t=2./(2.0+z);
ans=t*exp(-z*z-1.26551223+t*(1.00002368+t*(0.37409196+t*(0.09678418+
t*(-0.18628806+t*(0.27886807+t*(-1.13520398+t*(1.48851587+
t*(-0.82215223+t*0.17087277)))))))));
return(x>=0.0 ? ans : 2.0-ans);
}
//+------------------------------------------------------------------+```

2.1.2 对数正态分布

• X 是从对数正态分布 Logn 选择的随机变量；
• μ 是位置参数 (0<μ )；
• σ 是比例参数 (0<σ)。

```//+------------------------------------------------------------------+
//|                Lognormal Distribution class definition           |
//+------------------------------------------------------------------+
class CLognormaldist : CErf // Erf class inheritance
{
public:
double            mu, //location parameter (μ)
sig;                 //scale parameter (σ)
//+------------------------------------------------------------------+
//| CLognormaldist class constructor                                 |
//+------------------------------------------------------------------+
void  CLognormaldist()
{
mu=0.0;sig=1.0; //default parameters μ and σ
}
//+------------------------------------------------------------------+
//| Probability density function (pdf)                               |
//+------------------------------------------------------------------+
double pdf(double x)
{
if(x==0.) return 0.;
return(0.398942280401432678/(sig*x))*exp(-0.5*pow((log(x)-mu)/sig,2));
}
//+------------------------------------------------------------------+
//| Cumulative distribution function (cdf)                           |
//+------------------------------------------------------------------+
double cdf(double x)
{
if(x==0.) return 0.;
return 0.5*erfc(-0.707106781186547524*(log(x)-mu)/sig);
}
//+------------------------------------------------------------------+
//| Inverse cumulative distribution function (invcdf)(quantile)      |
//+------------------------------------------------------------------+
double invcdf(double p)
{
if(!(p>0. && p<1.))
return exp(-1.41421356237309505*sig*inverfc(2.*p)+mu);
}
//+------------------------------------------------------------------+
//| Reliability (survival) function (sf)                             |
//+------------------------------------------------------------------+
double sf(double x)
{
return 1-cdf(x);
}
};
//+------------------------------------------------------------------+```

2.1.3 柯西分布

• X 是从柯西分布 Cau 选择的随机变量；
• μ 是位置参数 (-∞ ≤ μ ≤ +∞)；
• σ 是比例参数 (0<σ)。

```//+------------------------------------------------------------------+
//|                Cauchy Distribution class definition              |
//+------------------------------------------------------------------+
class CCauchydist //
{
public:
double            mu,//location parameter (μ)
sig;                 //scale parameter (σ)
//+------------------------------------------------------------------+
//| CCauchydist class constructor                                    |
//+------------------------------------------------------------------+
void  CCauchydist()
{
mu=0.0;sig=1.0; //default parameters μ and σ
}
//+------------------------------------------------------------------+
//| Probability density function (pdf)                               |
//+------------------------------------------------------------------+
double pdf(double x)
{
return 0.318309886183790671/(sig*(1.+pow((x-mu)/sig,2)));
}
//+------------------------------------------------------------------+
//| Cumulative distribution function (cdf)                           |
//+------------------------------------------------------------------+
double cdf(double x)
{
return 0.5+0.318309886183790671*atan2(x-mu,sig); //todo
}
//+------------------------------------------------------------------+
//| Inverse cumulative distribution function (invcdf) (quantile func)|
//+------------------------------------------------------------------+
double invcdf(double p)
{
if(!(p>0. && p<1.))
return mu+sig*tan(M_PI*(p-0.5));
}
//+------------------------------------------------------------------+
//| Reliability (survival) function (sf)                             |
//+------------------------------------------------------------------+
double sf(double x)
{
return 1-cdf(x);
}
};
//+------------------------------------------------------------------+```

```double atan2(double y,double x)
/*
Returns the principal value of the arc tangent of y/x,
expressed in radians. To compute the value, the function
uses the sign of both arguments to determine the quadrant.
y - double value representing an y-coordinate.
x - double value representing an x-coordinate.
*/
{
double a;
if(fabs(x)>fabs(y))
a=atan(y/x);
else
{
a=atan(x/y); // pi/4 <= a <= pi/4
if(a<0.)
a=-1.*M_PI_2-a; //a is negative, so we're adding
else
a=M_PI_2-a;
}
if(x<0.)
{
if(y<0.)
a=a-M_PI;
else
a=a+M_PI;
}
return a;
}```

2.1.4 双曲线正割分布

• X 是随机变量；
• μ 是位置参数 (-∞ ≤ μ ≤ +∞)；
• σ 是比例参数 (0<σ)。

```//+------------------------------------------------------------------+
//|        Hyperbolic Secant Distribution class definition           |
//+------------------------------------------------------------------+
class CHypersecdist //
{
public:
double            mu,// location parameter (μ)
sig;                 //scale parameter (σ)
//+------------------------------------------------------------------+
//| CHypersecdist class constructor                                 |
//+------------------------------------------------------------------+
void  CHypersecdist()
{
mu=0.0;sig=1.0; //default parameters μ and σ
}
//+------------------------------------------------------------------+
//| Probability density function (pdf)                               |
//+------------------------------------------------------------------+
double pdf(double x)
{
return sech((M_PI*(x-mu))/(2*sig))/2*sig;
}
//+------------------------------------------------------------------+
//| Cumulative distribution function (cdf)                           |
//+------------------------------------------------------------------+
double cdf(double x)
{
return 2/M_PI*atan(exp((M_PI*(x-mu)/(2*sig))));
}
//+------------------------------------------------------------------+
//| Inverse cumulative distribution function (invcdf) (quantile func)|
//+------------------------------------------------------------------+
double invcdf(double p)
{
if(!(p>0. && p<1.))
return(mu+(2.0*sig/M_PI*log(tan(M_PI/2.0*p))));
}
//+------------------------------------------------------------------+
//| Reliability (survival) function (sf)                             |
//+------------------------------------------------------------------+
double sf(double x)
{
return 1-cdf(x);
}
};
//+------------------------------------------------------------------+```

```//+------------------------------------------------------------------+
//|              Hyperbolic Secant Function                          |
//+------------------------------------------------------------------+
double sech(double x)
// Hyperbolic Secant Function
{
return 2/(pow(M_E,x)+pow(M_E,-x));
}```

2.1.5 学生 t 分布

• t 是从学生 t 分布 Stt 选择的随机变量；
• ν 是形状参数 (ν>0)
• μ 是位置参数 (-∞ ≤ μ ≤ +∞)；
• σ 是比例参数 (0<σ)。

```//+------------------------------------------------------------------+
//|                Student's t-distribution class definition         |
//+------------------------------------------------------------------+
class CStudenttdist : CBeta // CBeta class inheritance
{
public:
int               nu;     // shape parameter (ν)
double            mu,     // location parameter (μ)
sig,                     // scale parameter (σ)
np,                      // 1/2*(ν+1)
fac;                     // Г(1/2*(ν+1))-Г(1/2*ν)
//+------------------------------------------------------------------+
//| CStudenttdist class constructor                                  |
//+------------------------------------------------------------------+
void  CStudenttdist()
{
int Nu=1;double Mu=0.0,Sig=1.0; //default parameters ν, μ and σ
setCStudenttdist(Nu,Mu,Sig);
}
void setCStudenttdist(int Nu,double Mu,double Sig)
{
nu=Nu;
mu=Mu;
sig=Sig;
np=0.5*(nu+1.);
fac=gammln(np)-gammln(0.5*nu);
}
//+------------------------------------------------------------------+
//| Probability density function (pdf)                               |
//+------------------------------------------------------------------+
double pdf(double x)
{
return exp(-np*log(1.+pow((x-mu)/sig,2.)/nu)+fac)/(sqrt(M_PI*nu)*sig);
}
//+------------------------------------------------------------------+
//| Cumulative distribution function (cdf)                           |
//+------------------------------------------------------------------+
double cdf(double t)
{
double p=0.5*betai(0.5*nu,0.5,nu/(nu+pow((t-mu)/sig,2)));
if(t>=mu) return 1.-p;
else return p;
}
//+------------------------------------------------------------------+
//| Inverse cumulative distribution function (invcdf) (quantile func)|
//+------------------------------------------------------------------+
double invcdf(double p)
{
double x=invbetai(2.*fmin(p,1.-p),0.5*nu,0.5);
x=sig*sqrt(nu*(1.-x)/x);
return(p>=0.5? mu+x : mu-x);
}
//+------------------------------------------------------------------+
//| Reliability (survival) function (sf)                             |
//+------------------------------------------------------------------+
double sf(double x)
{
return 1-cdf(x);
}
//+------------------------------------------------------------------+
//| Two-tailed cumulative distribution function (aa) A(t|ν)          |
//+------------------------------------------------------------------+
double aa(double t)
{
return 1.-betai(0.5*nu,0.5,nu/(nu+pow(t,2.)));
}
//+------------------------------------------------------------------+
//| Inverse two-tailed cumulative distribution function (invaa)      |
//| p=A(t|ν)                                                         |
//+------------------------------------------------------------------+
double invaa(double p)
{
double x=invbetai(1.-p,0.5*nu,0.5);
return sqrt(nu*(1.-x)/x);
}
};
//+------------------------------------------------------------------+```

CStudenttdist 类的清单显示 CBeta 是基类，该类描述不完整的贝塔函数

CBeta 类看起来如下所示：

```//+------------------------------------------------------------------+
//|            Incomplete Beta Function class definition             |
//+------------------------------------------------------------------+
class CBeta : public CGauleg18
{
private:
int               Switch;                     //when to use the quadrature method
double            Eps,Fpmin;
public:
//+------------------------------------------------------------------+
//| CBeta class constructor                                          |
//+------------------------------------------------------------------+
void CBeta()
{
int swi=3000;
setCBeta(swi,EPS,FPMIN);
};
//+------------------------------------------------------------------+
//| CBeta class set-method                                           |
//+------------------------------------------------------------------+
void setCBeta(int swi,double eps,double fpmin)
{
Switch=swi;
Eps=eps;
Fpmin=fpmin;
};
double            betai(const double a,const double b,const double x); //incomplete beta function Ix(a,b)
double            betacf(const double a,const double b,const double x);//continued fraction for incomplete beta function
double            betaiapprox(double a,double b,double x); //Incomplete beta by quadrature
double            invbetai(double p,double a,double b);    //Inverse of incomplete beta function
};```

2.1.6 逻辑分布

• X 是随机变量；
• α 是位置参数 (-∞ ≤ α ≤ +∞ )；
• β 是比例参数 (0<β)。

CLogisticdist 类是上述分布的实施：

```//+------------------------------------------------------------------+
//|                Logistic Distribution class definition            |
//+------------------------------------------------------------------+
class CLogisticdist
{
public:
double            alph,//location parameter (α)
bet;                   //scale parameter (β)
//+------------------------------------------------------------------+
//| CLogisticdist class constructor                                  |
//+------------------------------------------------------------------+
void  CLogisticdist()
{
alph=0.0;bet=1.0; //default parameters μ and σ
}
//+------------------------------------------------------------------+
//| Probability density function (pdf)                               |
//+------------------------------------------------------------------+
double pdf(double x)
{
return exp(-(x-alph)/bet)/(bet*pow(1.+exp(-(x-alph)/bet),2));
}
//+------------------------------------------------------------------+
//| Cumulative distribution function (cdf)                           |
//+------------------------------------------------------------------+
double cdf(double x)
{
double et=exp(-1.*fabs(1.81379936423421785*(x-alph)/bet));
if(x>=alph) return 1./(1.+et);
else return et/(1.+et);
}
//+------------------------------------------------------------------+
//| Inverse cumulative distribution function (invcdf) (quantile func)|
//+------------------------------------------------------------------+
double invcdf(double p)
{
return alph+0.551328895421792049*bet*log(p/(1.-p));
}
//+------------------------------------------------------------------+
//| Reliability (survival) function (sf)                             |
//+------------------------------------------------------------------+
double sf(double x)
{
return 1-cdf(x);
}
};
//+------------------------------------------------------------------+```

2.1.7 指数分布

• X 是随机变量；
• λ比例参数 (λ>0)。

```//+------------------------------------------------------------------+
//|                Exponential Distribution class definition         |
//+------------------------------------------------------------------+
class CExpondist
{
public:
double            lambda;   //scale parameter (λ)
//+------------------------------------------------------------------+
//| CExpondist class constructor                                     |
//+------------------------------------------------------------------+
void  CExpondist()
{
lambda=1.0;              //default parameter λ
}
//+------------------------------------------------------------------+
//| Probability density function (pdf)                               |
//+------------------------------------------------------------------+
double pdf(double x)
{
return lambda*exp(-lambda*x);
}
//+------------------------------------------------------------------+
//| Cumulative distribution function (cdf)                           |
//+------------------------------------------------------------------+
double cdf(double x)
{
return 1.-exp(-lambda*x);
}
//+------------------------------------------------------------------+
//| Inverse cumulative distribution function (invcdf) (quantile func)|
//+------------------------------------------------------------------+
double invcdf(double p)
{
return -log(1.-p)/lambda;
}
//+------------------------------------------------------------------+
//| Reliability (survival) function (sf)                             |
//+------------------------------------------------------------------+
double sf(double x)
{
return 1-cdf(x);
}
};
//+------------------------------------------------------------------+```

2.1.8 伽玛分布

• X 是随机变量；
• α 是形状参数 (0<α)；
• β 是比例参数 (0<β)。

```//+------------------------------------------------------------------+
//|                Gamma Distribution class definition               |
//+------------------------------------------------------------------+
class CGammadist : CGamma // CGamma class inheritance
{
public:
double            alph,//continuous shape parameter (α>0)
bet,                   //continuous scale parameter  (β>0)
fac;                   //factor
//+------------------------------------------------------------------+
//| CGammaldist class constructor                                    |
//+------------------------------------------------------------------+
{
}
void setCGammadist(double Alph=1.0,double Bet=1.0)//default parameters α and β
{
alph=Alph; bet=Bet;
fac=alph*log(bet)-gammln(alph);
}
//+------------------------------------------------------------------+
//| Probability density function (pdf)                               |
//+------------------------------------------------------------------+
double pdf(double x)
{
return exp(-bet*x+(alph-1.)*log(x)+fac);
}
//+------------------------------------------------------------------+
//| Cumulative distribution function (cdf)                           |
//+------------------------------------------------------------------+
double cdf(double x)
{
return gammp(alph,bet*x);
}
//+------------------------------------------------------------------+
//| Inverse cumulative distribution function (invcdf) (quantile func)|
//+------------------------------------------------------------------+
double invcdf(double p)
{
return invgammp(p,alph)/bet;
}
//+------------------------------------------------------------------+
//| Reliability (survival) function (sf)                             |
//+------------------------------------------------------------------+
double sf(double x)
{
return 1-cdf(x);
}
};
//+------------------------------------------------------------------+```

CGamma 类的定义如下所示：

```//+------------------------------------------------------------------+
//|            Incomplete Gamma Function class definition            |
//+------------------------------------------------------------------+
class CGamma : public CGauleg18
{
private:
int               ASWITCH;
double            Eps,
Fpmin,
gln;
public:
//+------------------------------------------------------------------+
//| CGamma class constructor                                         |
//+------------------------------------------------------------------+
void CGamma()
{
int aswi=100;
setCGamma(aswi,EPS,FPMIN);
};
void setCGamma(int aswi,double eps,double fpmin) //CGamma set-method
{
ASWITCH=aswi;
Eps=eps;
Fpmin=fpmin;
};
double            gammp(const double a,const double x); //incomplete gamma function
double            gammq(const double a,const double x); //incomplete gamma function Q(a,x)
void              gser(double &gamser,double a,double x,double &gln); //incomplete gamma function P(a,x)
double            gcf(const double a,const double x); //incomplete gamma function Q(a,x)
double            gammpapprox(double a,double x,int psig); //incomplete gamma by quadrature
double            invgammp(double p,double a); //inverse of incomplete gamma function
};
//+------------------------------------------------------------------+```

CGamma 类和 CBeta 类都有 CGauleg18 类作为基类。

2.1.9 贝塔分布

• X 是随机变量；
• α 是第一个形状参数 (0<α)；
• β 是第二个形状参数 (0<β)。

```//+------------------------------------------------------------------+
//|                Beta Distribution class definition                |
//+------------------------------------------------------------------+
class CBetadist : CBeta // CBeta class inheritance
{
public:
double            alph,//continuous shape parameter (α>0)
bet,                   //continuous shape parameter (β>0)
fac;                   //factor
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
{
}
void setCBetadist(double Alph=0.5,double Bet=0.5)//default parameters α and β
{
alph=Alph; bet=Bet;
fac=gammln(alph+bet)-gammln(alph)-gammln(bet);
}
//+------------------------------------------------------------------+
//| Probability density function (pdf)                               |
//+------------------------------------------------------------------+
double pdf(double x)
{
return exp((alph-1.)*log(x)+(bet-1.)*log(1.-x)+fac);
}
//+------------------------------------------------------------------+
//| Cumulative distribution function (cdf)                           |
//+------------------------------------------------------------------+
double cdf(double x)
{
return betai(alph,bet,x);
}
//+------------------------------------------------------------------+
//| Inverse cumulative distribution function (invcdf) (quantile func)|
//+------------------------------------------------------------------+
double invcdf(double p)
{
return invbetai(p,alph,bet);
}
//+------------------------------------------------------------------+
//| Reliability (survival) function (sf)                             |
//+------------------------------------------------------------------+
double sf(double x)
{
return 1-cdf(x);
}
};
//+------------------------------------------------------------------+```

2.1.10 拉普拉斯分布

• X 是随机变量；
• α 是位置参数 (-∞ ≤ α ≤ +∞ )；
• β 是比例参数 (0<β)。

```//+------------------------------------------------------------------+
//|                 Laplace Distribution class definition            |
//+------------------------------------------------------------------+
class CLaplacedist
{
public:
double            alph;   //location parameter (α)
double            bet;    //scale parameter (β)
//+------------------------------------------------------------------+
//| CLaplacedist class constructor                                   |
//+------------------------------------------------------------------+
void  CLaplacedist()
{
alph=.0;               //default parameter α
bet=1.;                //default parameter β
}
//+------------------------------------------------------------------+
//| Probability density function (pdf)                               |
//+------------------------------------------------------------------+
double pdf(double x)
{
return exp(-fabs((x-alph)/bet))/2*bet;
}
//+------------------------------------------------------------------+
//| Cumulative distribution function (cdf)                           |
//+------------------------------------------------------------------+
double cdf(double x)
{
double temp;
if(x<0)
temp=0.5*exp(-fabs((x-alph)/bet));
else
temp=1.-0.5*exp(-fabs((x-alph)/bet));
return temp;
}
//+------------------------------------------------------------------+
//| Inverse cumulative distribution function (invcdf) (quantile func)|
//+------------------------------------------------------------------+
double invcdf(double p)
{
double temp;
if(p<0.5)
temp=bet*log(2*p)+alph;
else
temp=-1.*(bet*log(2*(1.-p))+alph);
return temp;
}
//+------------------------------------------------------------------+
//| Reliability (survival) function (sf)                             |
//+------------------------------------------------------------------+
double sf(double x)
{
return 1-cdf(x);
}
};
//+------------------------------------------------------------------+```

2.2.1 二项分布

• k 是随机变量；
• n 是试验次数 (0 ≤ n);
• p 是成功概率 (0 ≤ p ≤1)。

```//+------------------------------------------------------------------+
//|               Binomial Distribution class definition             |
//+------------------------------------------------------------------+
class CBinomialdist : CBeta // CBeta class inheritance
{
public:
int               n;      //number of trials
double            pe,     //success probability
fac;                      //factor
//+------------------------------------------------------------------+
//| CBinomialdist class constructor                                  |
//+------------------------------------------------------------------+
void              CBinomialdist()
{
setCBinomialdist();
}
void setCBinomialdist(int N=100,double Pe=0.5)//default parameters n and pe
{
n=N; pe=Pe;
fac=gammln(n+1.);
}
//+------------------------------------------------------------------+
//| Probability density function (pdf)                               |
//+------------------------------------------------------------------+
double pdf(int k)
{
if(k>n) return 0.;
return exp(k*log(pe)+(n-k)*log(1.-pe)+fac-gammln(k+1.)-gammln(n-k+1.));
}
//+------------------------------------------------------------------+
//| Cumulative distribution function (cdf)                           |
//+------------------------------------------------------------------+
double cdf(int k)
{
if(k==0) return 0.;
if(k>n) return 1.;
return 1.-betai((double)k,n-k+1.,pe);
}
//+------------------------------------------------------------------+
//| Inverse cumulative distribution function (invcdf) (quantile func)|
//+------------------------------------------------------------------+
int invcdf(double p)
{
int k,kl,ku,inc=1;
k=fmax(0,fmin(n,(int)(n*pe)));
if(p<cdf(k))
{
do
{
k=fmax(k-inc,0);
inc*=2;
}
while(p<cdf(k));
kl=k; ku=k+inc/2;
} else {
do
{
k=fmin(k+inc,n+1);
inc*=2;
}
while(p>cdf(k));
ku=k; kl=k-inc/2;
}
while(ku-kl>1)
{
k=(kl+ku)/2;
if(p<cdf(k)) ku=k;
else kl=k;
}
return kl;
}
//+------------------------------------------------------------------+
//| Reliability (survival) function (sf)                             |
//+------------------------------------------------------------------+
double sf(int k)
{
return 1.-cdf(k);
}
};
//+------------------------------------------------------------------+```

2.2.2 泊松分布

• k 是随机变量；
• λ 是位置参数 (0 < λ)。

CPoissondist 类将用于这个分布：

```//+------------------------------------------------------------------+
//|               Poisson Distribution class definition              |
//+------------------------------------------------------------------+
class CPoissondist : CGamma // CGamma class inheritance
{
public:
double            lambda;   //location parameter (λ)
//+------------------------------------------------------------------+
//| CPoissondist class constructor                                   |
//+------------------------------------------------------------------+
void CPoissondist()
{
lambda=15.;
}
//+------------------------------------------------------------------+
//| Probability density function (pdf)                               |
//+------------------------------------------------------------------+
double pdf(int n)
{
return exp(-lambda+n*log(lambda)-gammln(n+1.));
}
//+------------------------------------------------------------------+
//| Cumulative distribution function (cdf)                           |
//+------------------------------------------------------------------+
double cdf(int n)
{
if(n==0) return 0.;
return gammq((double)n,lambda);
}
//+------------------------------------------------------------------+
//| Inverse cumulative distribution function (invcdf) (quantile func)|
//+------------------------------------------------------------------+
int invcdf(double p)
{
int n,nl,nu,inc=1;
if(p<exp(-lambda)) return 0;
n=(int)fmax(sqrt(lambda),5.);
if(p<cdf(n))
{
do
{
n=fmax(n-inc,0);
inc*=2;
}
while(p<cdf(n));
nl=n; nu=n+inc/2;
} else {
do
{
n+=inc;
inc*=2;
}
while(p>cdf(n));
nu=n; nl=n-inc/2;
}
while(nu-nl>1)
{
n=(nl+nu)/2;
if(p<cdf(n)) nu=n;
else nl=n;
}
return nl;
}
//+------------------------------------------------------------------+
//| Reliability (survival) function (sf)                             |
//+------------------------------------------------------------------+
double sf(int n)
{
return 1.-cdf(n);
}
};
//+=====================================================================+```

### 3. 创建分布图

```//+------------------------------------------------------------------+
//|           Distribution Figure class definition                   |
//+------------------------------------------------------------------+
class CDistributionFigure
{
private:
Dist_type         type;  //distribution type
Dist_mode         mode;  //distribution mode
double            x;     //step start
double            x11;   //left side limit
double            x12;   //right side limit
int               d;     //number of points
double            st;    //step

public:
double            xAr[]; //array of random variables
double            p1[];  //array of probabilities
void              CDistributionFigure();  //constructor
void              setDistribution(Dist_type Type,Dist_mode Mode,double X11,double X12,double St); //set-method
void              calculateDistribution(double nn,double mm,double ss); //distribution parameter calculation
void              filesave(); //saving distribution parameters
};
//+------------------------------------------------------------------+```

```//+------------------------------------------------------------------+
//|                     Input variables                              |
//+------------------------------------------------------------------+

input Dist_type dist;  //Distribution Type
input Dist_mode distM; //Distribution Mode
input int nn=1;        //Nu
input double mm=0.,    //Mu
ss=1.;    //Sigma
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
{
//(Normal #0,Lognormal #1,Cauchy #2,Hypersec #3,Studentt #4,Logistic #5,Exponential #6,Gamma #7,Beta #8 , Laplace #9)
double Xx1,      //left side limit
Xx2,     //right side limit
st=0.05; //step
if(dist==0) //Normal
{
Xx1=mm-5.0*ss/1.25;
Xx2=mm+5.0*ss/1.25;
}
if(dist==2 || dist==4 || dist==5) //Cauchy,Studentt,Logistic
{
Xx1=mm-5.0*ss/0.35;
Xx2=mm+5.0*ss/0.35;
}
else if(dist==1 || dist==6 || dist==7) //Lognormal,Exponential,Gamma
{
Xx1=0.001;
Xx2=7.75;
}
else if(dist==8) //Beta
{
Xx1=0.0001;
Xx2=0.9999;
st=0.001;
}
else
{
Xx1=mm-5.0*ss;
Xx2=mm+5.0*ss;
}
//---
CDistributionFigure F;        //creation of the CDistributionFigure class instance
F.setDistribution(dist,distM,Xx1,Xx2,st);
F.calculateDistribution(nn,mm,ss);
F.filesave();
string path=TerminalInfoString(TERMINAL_DATA_PATH)+"\\MQL5\\Files\\Distribution_function.htm";
ShellExecuteW(NULL,"open",path,NULL,NULL,1);
}
//+------------------------------------------------------------------+```

### 总结

#

1
Distribution_class.mqh
2  DistributionFigure_class.mqh
分布的图形显示类
4
discreteDistribution.mq5
5
6
Distribution_function.htm
7  Distribution_function_discr.htm
8  exporting.js
9  highcharts.js

1. K. Krishnamoorthy. Handbook of Statistical Distributions with Applications, Chapman and Hall/CRC 2006.
2. W.H. Press, et al. Numerical Recipes:The Art of Scientific Computing, Third Edition, Cambridge University Press:2007. - 1256 pp.
3. S.V. Bulashev Statistics for Traders.- M.:Kompania Sputnik +, 2003. - 245 pp.
4. I. Gaidyshev Data Analysis and Processing:Special Reference Guide - SPb:Piter, 2001. - 752 pp.:ill.
5. A.I. Kibzun, E.R. Goryainova — Probability Theory and Mathematical Statistics.Basic Course with Examples and Problems
6. N.Sh. Kremer Probability Theory and Mathematical Statistics.M.:Unity-Dana, 2004. — 573 pp.

MQL5 傻瓜式向导