C#, Numerical Calculation - Calculation Method and Source Code of Chisq Distribution

 

using System;

namespace Legalsoft.Truffer
{
    public class Chisqdist : Gamma
    {
        private double nu { get; set; }
        private double fac { get; set; }

        public Chisqdist(double nnu)
        {
            this.nu = nnu;
            if (nu <= 0.0)
            {
                throw new Exception("bad nu in Chisqdist");
            }
            fac = 0.693147180559945309 * (0.5 * nu) + Globals.gammln(0.5 * nu);
        }

        public double p(double x2)
        {
            if (x2 <= 0.0)
            {
                throw new Exception("bad x2 in Chisqdist");
            }
            return Math.Exp(-0.5 * (x2 - (nu - 2.0) * Math.Log(x2)) - fac);
        }

        public double cdf(double x2)
        {
            if (x2 < 0.0)
            {
                throw new Exception("bad x2 in Chisqdist");
            }
            return gammp(0.5 * nu, 0.5 * x2);
        }

        public double invcdf(double p)
        {
            if (p < 0.0 || p >= 1.0)
            {
                throw new Exception("bad p in Chisqdist");
            }
            return 2.0 * invgammp(p, 0.5 * nu);
        }
    }
}

Guess you like

Origin blog.csdn.net/beijinghorn/article/details/131670422