pdf and cdf for Gamma distribution

.. its quite handy.
This commit is contained in:
Mark Liversedge
2021-09-20 21:27:44 +01:00
parent 8580a45c38
commit fb76ecc33c

View File

@@ -376,6 +376,8 @@ static struct {
{ "cdfnormal", 2 }, // cdfnormal(sigma, x) returns the cumulative density function for value x
{ "pdfbeta", 3 }, // pdfbeta(a,b, x) as above for the beta distribution
{ "cdfbeta", 3 }, // cdfbeta(a,b, x) as above for the beta distribution
{ "pdfgamma", 3 }, // pdfgamma(a,b, x) as above for the gamma distribution
{ "cdfgamma", 3 }, // cdfgamma(a,b, x) as above for the gamma distribution
// add new ones above this line
@@ -4026,6 +4028,48 @@ Result Leaf::eval(DataFilterRuntime *df, Leaf *leaf, const Result &x, long it, R
return returning;
}
if (leaf->function == "pdfgamma") {
Result returning(0);
double a= eval(df, leaf->fparms[0],x, it, m, p, c, s, d).number();
double b= eval(df, leaf->fparms[1],x, it, m, p, c, s, d).number();
Result v= eval(df, leaf->fparms[2],x, it, m, p, c, s, d);
if (v.isVector() && v.isNumber) {
// vector
foreach(double val, v.asNumeric()) {
double f = gsl_ran_gamma_pdf(val, a, b);
returning.asNumeric() << f;
returning.number() += f;
}
} else if (v.isNumber) returning.number() = gsl_ran_gamma_pdf(v.number(), a,b);
return returning;
}
if (leaf->function == "cdfgamma") {
Result returning(0);
double a= eval(df, leaf->fparms[0],x, it, m, p, c, s, d).number();
double b= eval(df, leaf->fparms[1],x, it, m, p, c, s, d).number();
Result v= eval(df, leaf->fparms[2],x, it, m, p, c, s, d);
if (v.isVector() && v.isNumber) {
// vector
foreach(double val, v.asNumeric()) {
double f = gsl_cdf_gamma_P(val, a, b);
returning.asNumeric() << f;
returning.number() += f;
}
} else if (v.isNumber) returning.number() = gsl_cdf_gamma_P(v.number(), a,b);
return returning;
}
if (leaf->function == "cdfnormal") {
Result returning(0);