//+------------------------------------------------------------------+ //| ChiSquare.mqh | //| Copyright 2000-2025, MetaQuotes Ltd. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #include "Math.mqh" #include "Gamma.mqh" //+------------------------------------------------------------------+ //| Chi-Square density function (PDF) | //+------------------------------------------------------------------+ //| The function returns the probability density function | //| of the Chi-Square distribution with parameter nu. | //| | //| Arguments: | //| x : Random variable | //| nu : Degrees of freedom | //| 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 MathProbabilityDensityChiSquare(const double x,const double nu,const bool log_mode,int &error_code) { //--- check arguments if(!MathIsValidNumber(x) || !MathIsValidNumber(nu)) { error_code=ERR_ARGUMENTS_NAN; return QNaN; } //--- nu must be positive integer if(nu<=0 || nu!=MathRound(nu)) { error_code=ERR_ARGUMENTS_INVALID; return QNaN; } error_code=ERR_OK; if(x<=0.0) return TailLog0(true,log_mode); //--- calculate using Gamma density double pdf=MathProbabilityDensityGamma(x,nu*0.5,2.0,error_code); if(log_mode==true) return MathLog(pdf); return pdf; } //+------------------------------------------------------------------+ //| Chi-Square density function (PDF) | //+------------------------------------------------------------------+ //| The function returns the probability density function | //| of the Chi-Square distribution with parameter nu. | //| | //| Arguments: | //| x : Random variable | //| nu : Degrees of freedom | //| error_code : Variable for error code | //| | //| Return value: | //| The probability density evaluated at x. | //+------------------------------------------------------------------+ double MathProbabilityDensityChiSquare(const double x,const double nu,int &error_code) { return MathProbabilityDensityChiSquare(x,nu,false,error_code); } //+------------------------------------------------------------------+ //| Chi-Square density function (PDF) | //+------------------------------------------------------------------+ //| The function calculates the probability density function of the | //| ChiSquare distribution with parameter nu for values in x[] array.| //| | //| Arguments: | //| x : Array with random variables | //| nu : Degrees of freedom | //| log_mode : Logarithm mode flag, if true it returns Log values | //| result : Array with calculated values | //| | //| Return value: | //| true if successful, otherwise false. | //+------------------------------------------------------------------+ bool MathProbabilityDensityChiSquare(const double &x[],const double nu,const bool log_mode,double &result[]) { //--- check arguments if(!MathIsValidNumber(nu)) return false; //--- nu must be positive integer if(nu<=0 || nu!=MathRound(nu)) 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; if(prob==0.0) return 0.0; if(prob==1.0) return QPOSINF; //---- calculate quantile using Gamma distribution return MathQuantileGamma(prob,nu*0.5,2.0,error_code); } //+------------------------------------------------------------------+ //| Chi-Square distribution quantile function (inverse CDF) | //+------------------------------------------------------------------+ //| The function returns the inverse cumulative distribution | //| function of the Chi-Square distribution with parameter nu | //| for the desired probability. | //| | //| Arguments: | //| probability : The desired probability | //| nu : Degrees of freedom | //| error_code : Variable for error code | //| | //| Return value: | //| The value of the inverse cumulative distribution function | //| of the Chi-Square distribution with parameter nu. | //+------------------------------------------------------------------+ double MathQuantileChiSquare(const double probability,const double nu,int &error_code) { return MathQuantileChiSquare(probability,nu,true,false,error_code); } //+------------------------------------------------------------------+ //| Chi-Square distribution quantile function (inverse CDF) | //+------------------------------------------------------------------+ //| The function calculates the inverse cumulative distribution | //| function of the Chi-Square distribution with parameter nu | //| for values from the probability[] array. | //| | //| Arguments: | //| probability : Array with probabilities | //| nu : Degrees of freedom | //| 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 MathQuantileChiSquare(const double &probability[],const double nu,const bool tail,const bool log_mode,double &result[]) { //--- check NaN if(!MathIsValidNumber(nu)) return false; //--- nu must be positive if(nu<=0) return false; //--- nu must be integer if(nu!=MathRound(nu)) 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) result[i]=0.0; else if(prob==1.0) result[i]=QPOSINF; else { //--- calculate using Gamma distribution result[i]=MathQuantileGamma(prob,nu*0.5,2.0,error_code); } } return true; } //+------------------------------------------------------------------+ //| Chi-Square distribution quantile function (inverse CDF) | //+------------------------------------------------------------------+ //| The function calculates the inverse cumulative distribution | //| function of the Chi-Square distribution with parameter nu | //| for values from the probability[] array. | //| | //| Arguments: | //| probability : Array with probabilities | //| nu : Degrees of freedom | //| result : Array with calculated values | //| | //| Return value: | //| true if successful, otherwise false. | //+------------------------------------------------------------------+ bool MathQuantileChiSquare(const double &probability[],const double nu,double &result[]) { return MathQuantileChiSquare(probability,nu,true,false,result); } //+------------------------------------------------------------------+ //| Random variate from the Chi-Square distribution | //+------------------------------------------------------------------+ //| Computes the random variable from the Chi-Square distribution | //| with parameter nu. | //| | //| Arguments: | //| nu : Degrees of freedom | //| error_code : Variable for error code | //| | //| Return value: | //| The random value with Chi-Square distribution. | //+------------------------------------------------------------------+ double MathRandomChiSquare(const double nu,int &error_code) { //--- NaN if(!MathIsValidNumber(nu)) { error_code=ERR_ARGUMENTS_NAN; return QNaN; } //--- nu must be integer if(nu!=MathRound(nu)) { error_code=ERR_ARGUMENTS_INVALID; return QNaN; } //--- nu must be positive if(nu<=0) { error_code=ERR_ARGUMENTS_INVALID; return QNaN; } error_code=ERR_OK; //--- return gamma(nu/2,2) return MathRandomGamma(nu*0.5,2.0,error_code); } //+------------------------------------------------------------------+ //| Random variate from Chi-Square distribution | //+------------------------------------------------------------------+ //| Generates random variables from the Chi-Square distribution | //| with parameter nu. | //| | //| Arguments: | //| nu : Degrees of freedom | //| data_count : Number of values needed | //| result : Output array with random values | //| | //| Return value: | //| true if successful, otherwise false. | //+------------------------------------------------------------------+ bool MathRandomChiSquare(const double nu,const int data_count,double &result[]) { //--- check NaN if(!MathIsValidNumber(nu)) return false; //--- nu must be integer if(nu!=MathRound(nu)) return false; //--- nu must be positive if(nu<=0) return false; int error_code=0; //--- prepare output array and calculate random values ArrayResize(result,data_count); for(int i=0; i