//+------------------------------------------------------------------+ //| Logistic.mqh | //| Copyright 2000-2025, MetaQuotes Ltd. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #include "Math.mqh" //+------------------------------------------------------------------+ //| Logistic distribution density function (PDF) | //+------------------------------------------------------------------+ //| The function returns the probability density function of | //| the Logistic distribution with parameters mu and sigma. | //| f(x,mu,sigma)=exp[-(x-mu)/sigma]/(sigma*(exp[-(x-mu)/sigma])^2) | //| | //| Arguments: | //| x : Random variable | //| mu : Mean | //| sigma : Scale parameter | //| log_mode : Logarithm mode, if true it calculates Log values | //| error_code : Variable for error code | //| | //| Return value: | //| The probability density evaluated at x. | //+------------------------------------------------------------------+ double MathProbabilityDensityLogistic(const double x,const double mu,const double sigma,const bool log_mode,int &error_code) { //--- check parameters if(!MathIsValidNumber(x) || !MathIsValidNumber(mu) || !MathIsValidNumber(sigma)) { error_code=ERR_ARGUMENTS_NAN; return QNaN; } //--- check sigma if(sigma<=0.0) { error_code=ERR_ARGUMENTS_INVALID; return QNaN; } //--- prepare argument double y=(x-mu)/sigma; //--- check result if(!MathIsValidNumber(y)) { error_code=ERR_ARGUMENTS_INVALID; return QNaN; } error_code=ERR_OK; //--- calculate exponents double e=MathExp(-y); double e1=(1+e); double pdf=e/(sigma*(e1*e1)); if(log_mode==true) return MathLog(pdf); //--- return logistic density return pdf; } //+------------------------------------------------------------------+ //| Logistic distribution density function (PDF) | //+------------------------------------------------------------------+ //| The function returns the probability density function of | //| the Logistic distribution with parameters mu and sigma. | //| f(x,mu,sigma)=exp[-(x-mu)/sigma]/(sigma*(exp[-(x-mu)/sigma])^2) | //| | //| Arguments: | //| x : Random variable | //| mu : Mean | //| sigma : Scale parameter | //| error_code : Variable for error code | //| | //| Return value: | //| The probability density evaluated at x. | //+------------------------------------------------------------------+ double MathProbabilityDensityLogistic(const double x,const double mu,const double sigma,int &error_code) { return MathProbabilityDensityLogistic(x,mu,sigma,false,error_code); } //+------------------------------------------------------------------+ //| Logistic distribution density function (PDF) | //+------------------------------------------------------------------+ //| The function calculates the probability density function of | //| the Logistic distribution with parameters mu and sigma | //| for values in x[] array. | //| | //| Arguments: | //| x : Array with random variables | //| mu : Mean | //| sigma : Scale parameter | //| log_mode : Logarithm mode flag, if true it returns Log values | //| result : Array with calculated values | //| | //| Return value: | //| true if successful, otherwise false. | //+------------------------------------------------------------------+ bool MathProbabilityDensityLogistic(const double &x[],const double mu,const double sigma,const bool log_mode,double &result[]) { //--- check parameters if(!MathIsValidNumber(mu) || !MathIsValidNumber(sigma)) return false; //--- check sigma if(sigma<=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; } if(prob==0.0 || prob==1.0) { if(sigma==0.0) { error_code=ERR_OK; return mu; } else { error_code=ERR_RESULT_INFINITE; if(prob==0.0) return QNEGINF; else return QPOSINF; } } error_code=ERR_OK; //--- calculate quantile double q=MathLog(prob/(1.0-prob)); //--- return rescaled/shifted quantile return mu+sigma*q; } //+------------------------------------------------------------------+ //| Logistic distribution quantile function (inverse CDF) | //+------------------------------------------------------------------+ //| The function returns the inverse cumulative distribution | //| function of the Logistic distribution with parameters mu | //| and sigma for the desired probability. | //| | //| Arguments: | //| probability : The desired probability | //| mu : Mean | //| sigma : Scale parameter | //| error_code : Variable for error code | //| | //| Return value: | //| The value of the inverse cumulative distribution function | //| of the Logistic distribution with parameters mu and sigma. | //+------------------------------------------------------------------+ double MathQuantileLogistic(const double probability,const double mu,const double sigma,int &error_code) { return MathQuantileLogistic(probability,mu,sigma,true,false,error_code); } //+------------------------------------------------------------------+ //| Logistic distribution quantile function (inverse CDF) | //+------------------------------------------------------------------+ //| The function calculates the inverse cumulative distribution | //| function of the Logistic distribution with parameters mu and | //| sigma for values from the probability[] array. | //| | //| Arguments: | //| probability : Array with probabilities | //| mu : Mean | //| sigma : Scale parameter | //| 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 MathQuantileLogistic(const double &probability[],const double mu,const double sigma,const bool tail,const bool log_mode,double &result[]) { //--- check parameters if(!MathIsValidNumber(mu) || !MathIsValidNumber(sigma)) return false; //--- check sigma if(sigma<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; if(prob==0.0 || prob==1.0) { if(sigma==0.0) result[i]=mu; else { if(prob==0.0) result[i]=QNEGINF; else result[i]=QPOSINF; } } else { //--- calculate quantile double q=MathLog(prob/(1.0-prob)); //--- rescaled/shifted quantile result[i]=mu+sigma*q; } } return true; } //+------------------------------------------------------------------+ //| Logistic distribution quantile function (inverse CDF) | //+------------------------------------------------------------------+ //| The function calculates the inverse cumulative distribution | //| function of the Logistic distribution with parameters mu and | //| sigma for values from the probability[] array. | //| | //| Arguments: | //| probability : Array with probabilities | //| mu : Mean | //| sigma : Scale parameter | //| result : Array with calculated values | //| | //| Return value: | //| true if successful, otherwise false. | //+------------------------------------------------------------------+ bool MathQuantileLogistic(const double &probability[],const double mu,const double sigma,double &result[]) { return MathQuantileLogistic(probability,mu,sigma,true,false,result); } //+------------------------------------------------------------------+ //| Random variate from the Logistic distribution | //+------------------------------------------------------------------+ //| Compute the random variable from the Logistic distribution | //| with parameters mu and sigma. | //| | //| Arguments: | //| mu : Mean | //| sigma : Scale parameter | //| error_code : Variable for error code | //| | //| Return value: | //| The random value with Logistic distribution. | //+------------------------------------------------------------------+ double MathRandomLogistic(const double mu,const double sigma,int &error_code) { //--- check parameters if(!MathIsValidNumber(mu) || !MathIsValidNumber(sigma)) { error_code=ERR_ARGUMENTS_NAN; return QNaN; } //--- check sigma if(sigma<0.0) { error_code=ERR_ARGUMENTS_INVALID; return QNaN; } error_code=ERR_OK; //--- check sigma if(sigma==0.0) return mu; //--- generate random number double rnd=MathRandomNonZero(); //--- return value return mu+sigma*MathLog(rnd/(1.0-rnd)); } //+------------------------------------------------------------------+ //| Random variate from the Logistic distribution | //+------------------------------------------------------------------+ //| Generates random variables from the Logistic distribution | //| with parameters mu and sigma. | //| | //| Arguments: | //| mu : Mean | //| sigma : Scale parameter | //| data_count : Number of values needed | //| result : Output array with random values | //| | //| Return value: | //| true if successful, otherwise false. | //+------------------------------------------------------------------+ bool MathRandomLogistic(const double mu,const double sigma,const int data_count,double &result[]) { //--- check NaN if(!MathIsValidNumber(mu) || !MathIsValidNumber(sigma)) return false; //--- check sigma if(sigma<0.0) return false; //--- prepare output array ArrayResize(result,data_count); //--- check sigma if(sigma==0.0) { for(int i=0; i