//+------------------------------------------------------------------+ //| Exponential.mqh | //| Copyright 2000-2025, MetaQuotes Ltd. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #include "Math.mqh" //+------------------------------------------------------------------+ //| Exponential density function (PDF) | //+------------------------------------------------------------------+ //| The function returns the probability density function of | //| the Exponential distribution with parameter mu. | //| f(x,mu)=(1/mu)*exp(-x/mu) | //| Arguments: | //| x : Random variable | //| mu : Mean | //| log_mode : Logarithm mode flag, if true it returns Log values | //| error_code : Variable for error code | //| | //| Return value: | //| The probability density evaluated at x. | //+------------------------------------------------------------------+ double MathProbabilityDensityExponential(const double x,const double mu,const bool log_mode,int &error_code) { //--- check parameters if(!MathIsValidNumber(x) || !MathIsValidNumber(mu)) { error_code=ERR_ARGUMENTS_NAN; return QNaN; } //--- mu must be positive if(mu<=0.0) { error_code=ERR_ARGUMENTS_INVALID; return QNaN; } error_code=ERR_OK; //--- check x if(x<0.0) return TailLog0(true,log_mode); //--- calculate lambda; double lambda=1.0/mu; if(log_mode==true) return MathLog(lambda*MathExp(-x*lambda)); //--- return density return lambda*MathExp(-x*lambda); } //+------------------------------------------------------------------+ //| Exponential density function (PDF) | //+------------------------------------------------------------------+ //| The function returns the probability density function of | //| the Exponential distribution with parameter mu. | //| f(x,mu)=(1/mu)*exp(-x/mu) | //| Arguments: | //| x : Random variable | //| mu : Mean | //| error_code : Variable for error code | //| | //| Return value: | //| The probability density evaluated at x. | //+------------------------------------------------------------------+ double MathProbabilityDensityExponential(const double x,const double mu,int &error_code) { return MathProbabilityDensityExponential(x,mu,false,error_code); } //+------------------------------------------------------------------+ //| Exponential density function (PDF) | //+------------------------------------------------------------------+ //| The function calculates the probability density function of | //| the Exponential distribution with parameter mu for values in x. | //| | //| Arguments: | //| x : Array with random variables | //| mu : Mean | //| log_mode : Logarithm mode flag, if true it returns Log values | //| result : Array with calculated values | //| | //| Return value: | //| true if successful, otherwise false. | //+------------------------------------------------------------------+ bool MathProbabilityDensityExponential(const double &x[],const double mu,const bool log_mode,double &result[]) { //--- check parameters if(!MathIsValidNumber(mu)) return false; //--- mu must be positive if(mu<=0.0) return false; int data_count=ArraySize(x); if(data_count==0) return false; int error_code=0; ArrayResize(result,data_count); for(int i=0; i1.0) { error_code=ERR_ARGUMENTS_INVALID; return QNaN; } error_code=ERR_OK; //--- check zero probability case if(prob==0.0) return 0.0; else if(prob==1.0) return QPOSINF; //--- return quantile return -mu*MathLog(1.0-prob); } //+------------------------------------------------------------------+ //| Exponential distribution quantile function (inverse CDF) | //+------------------------------------------------------------------+ //| The function returns the inverse cumulative distribution | //| function of Exponential distribution with parameter mu | //| for the desired probability. | //| | //| Arguments: | //| probability : The desired probability | //| mu : Mean | //| error_code : Variable for error code | //| | //| Return value: | //| The value of the inverse cumulative distribution function | //| of the Exponential distribution with parameter mu. | //+------------------------------------------------------------------+ double MathQuantileExponential(const double probability,const double mu,int &error_code) { return MathQuantileExponential(probability,mu,true,false,error_code); } //+------------------------------------------------------------------+ //| Exponential distribution quantile function (inverse CDF) | //+------------------------------------------------------------------+ //| The function calculates the inverse cumulative distribution | //| function of the Exponential distribution with parameter mu | //| for values from the probability[] array. | //| | //| Arguments: | //| probability : Array with probabilities | //| mu : Mean | //| tail : Flag to calculate lower tail | //| log_mode : Logarithm mode, if true it calculates Log values | //| result : Array with calculated values | //| | //| Return value: | //| true if successful, otherwise false. | //+------------------------------------------------------------------+ bool MathQuantileExponential(const double &probability[],const double mu,const bool tail,const bool log_mode,double &result[]) { //--- check parameters if(!MathIsValidNumber(mu)) return false; //--- mu must be positive if(mu<=0.0) return false; int data_count=ArraySize(probability); if(data_count==0) return false; int error_code=0; ArrayResize(result,data_count); for(int i=0; i1.0) return false; //--- check zero probability case if(prob==0.0) result[i]=0.0; else if(prob==1.0) result[i]=QPOSINF; else result[i]=-mu*MathLog(1.0-prob); } return true; } //+------------------------------------------------------------------+ //| Exponential distribution quantile function (inverse CDF) | //+------------------------------------------------------------------+ //| The function calculates the inverse cumulative distribution | //| function of the Exponential distribution with parameter mu | //| for values from the probability[] array. | //| | //| Arguments: | //| probability : Array with probabilities | //| mu : Mean | //| result : Array with calculated values | //| | //| Return value: | //| true if successful, otherwise false. | //+------------------------------------------------------------------+ bool MathQuantileExponential(const double &probability[],const double mu,double &result[]) { return MathQuantileExponential(probability,mu,true,false,result); } //+------------------------------------------------------------------+ //| Random variate from the Exponential distribution | //+------------------------------------------------------------------+ //| Compute the random variable from the Exponential distribution | //| with parameter mu using simple inversion method. | //| | //| Arguments: | //| mu : Mean | //| error_code : Variable for error code | //| | //| Return value: | //| The random value with Exponential distribution. | //| | //| Reference: | //| Devroye L. "Non-uniform random variate generation",Springer,1986.| //+------------------------------------------------------------------+ double MathRandomExponential(const double mu,int &error_code) { //--- check mu if(!MathIsValidNumber(mu)) { error_code=ERR_ARGUMENTS_NAN; return QNaN; } //--- mu must be positive if(mu<=0.0) { error_code=ERR_ARGUMENTS_INVALID; return QNaN; } error_code=ERR_OK; //--- generate random number double rnd=MathRandomNonZero(); //--- return variate using quantile return -mu*MathLog(1.0-rnd); } //+------------------------------------------------------------------+ //| Random variate from the Exponential distribution | //+------------------------------------------------------------------+ //| Generates random variables from the Exponential distribution | //| with parameter mu. | //| | //| Arguments: | //| mu : Mean | //| data_count : Number of values needed | //| result : Output array with random values | //| | //| Return value: | //| true if successful, otherwise false. | //+------------------------------------------------------------------+ bool MathRandomExponential(const double mu,const int data_count,double &result[]) { //--- check mu if(!MathIsValidNumber(mu)) return false; //--- mu must be positive if(mu<=0.0) return false; //--- prepare output array and calculate random values ArrayResize(result,data_count); for(int i=0; i