C#, Numerical Calculation - Calculation method and source program of t-distribution (Student distribution)

In probability theory and statistics, the Student's t-distribution is often used to estimate the mean of a normally distributed population. It is the basis for the Student's t-test, which tests for significance of the difference between two sample means. The t test improves the Z test (en: Z-test), which can be applied regardless of the sample size is large or small. When the number of samples is large (more than 120, etc.), the Z test can be applied, but the Z test will produce a large error when used in a small sample, so the student's t test must be used when the sample is small. When there are more than three groups of data, because the error cannot be suppressed, the student's t test can be replaced by the analysis of variance at this time.
When the standard deviation of the parent population is unknown but needs to be estimated, we can use the Student's t-distribution.
The Student's t-distribution may be referred to simply as the t-distribution. Its derivation was first published by William Gosse in 1908 while he was still working at the Guinness Storehouse in Dublin. Because it cannot be published in his own name, the paper uses the pseudonym Student (Student). The t-test and related theory were later developed by the work of Ronald Fisher, who called this distribution the student distribution.
 

In probability theory and statistics, the t-distribution (t-distribution) is used to estimate the mean of a normally distributed population with unknown variance from a small sample. If the population variance is known (for example, if the sample size is large enough), then the normal distribution should be used to estimate the population mean.
The shape of the t-distribution curve is related to the size of n (specifically, the degree of freedom df). Compared with the standard normal distribution curve, the smaller the degree of freedom df, the flatter the t distribution curve, the lower the middle of the curve, and the higher the tails on both sides of the curve; the larger the degree of freedom df, the closer the t distribution curve is to the normal distribution curve , when the degree of freedom df=∞, the t-distribution curve is a standard normal distribution curve.

using System;

namespace Legalsoft.Truffer
{
    public class Studenttdist : Beta
    {
        private double nu { get; set; }
        private double mu { get; set; }
        private double sig { get; set; }
        private double np { get; set; }
        private double fac { get; set; }

        public Studenttdist(double nnu, double mmu = 0.0, double ssig = 1.0)
        {
            this.nu = nnu;
            this.mu = mmu;
            this.sig = ssig;
            if (sig <= 0.0 || nu <= 0.0)
            {
                throw new Exception("bad sig,nu in Studentdist");
            }
            np = 0.5 * (nu + 1.0);
            fac = Globals.gammln(np) - Globals.gammln(0.5 * nu);
        }
        public double p(double t)
        {
            return Math.Exp(-np * Math.Log(1.0 + Globals.SQR((t - mu) / sig) / nu) + fac) / (Math.Sqrt(3.14159265358979324 * nu) * sig);
        }
        public double cdf(double t)
        {
            double p = 0.5 * betai(0.5 * nu, 0.5, nu / (nu + Globals.SQR((t - mu) / sig)));
            if (t >= mu)
            {
                return 1.0 - p;
            }
            else
            {
                return p;
            }
        }
        public double invcdf(double p)
        {
            if (p <= 0.0 || p >= 1.0)
            {
                throw new Exception("bad p in Studentdist");
            }
            double x = invbetai(2.0 * Math.Min(p, 1.0 - p), 0.5 * nu, 0.5);
            x = sig * Math.Sqrt(nu * (1.0 - x) / x);
            return (p >= 0.5 ? mu + x : mu - x);
        }
        public double aa(double t)
        {
            if (t < 0.0)
            {
                throw new Exception("bad t in Studentdist");
            }
            return 1.0 - betai(0.5 * nu, 0.5, nu / (nu + Globals.SQR(t)));
        }
        public double invaa(double p)
        {
            if (p < 0.0 || p >= 1.0)
            {
                throw new Exception("bad p in Studentdist");
            }
            double x = invbetai(1.0 - p, 0.5 * nu, 0.5);
            return Math.Sqrt(nu * (1.0 - x) / x);
        }
    }
}
 

Guess you like

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