学生分布代码实现

public class StudentTDistribution : ContinuousDistribution
    {
        private double degreesOfFreedom;

        public double DegreesOfFreedom => this.degreesOfFreedom;

        public override bool IsSymmetrical => true;

        public override double Mean => 0.0;

        public override double Variance => this.degreesOfFreedom / (this.degreesOfFreedom - 2.0);

        public override double Skewness => 0.0;

        public override double Kurtosis => 6.0 / (this.degreesOfFreedom - 4.0);

        public static double DistributionFunction(double x, int degreesOfFreedom)
        {
            if (degreesOfFreedom <= 0)
            {
                ThrowException.ArgumentOutOfRange("degreesOfFreedom");
            }
            if (x < 0.0)
            {
                return 0.5 * GammaFunctions.RegularizedBeta(0.5 * (double)degreesOfFreedom, 0.5, (double)degreesOfFreedom / ((double)degreesOfFreedom + x * x));
            }
            return 1.0 - 0.5 * GammaFunctions.RegularizedBeta(0.5 * (double)degreesOfFreedom, 0.5, (double)degreesOfFreedom / ((double)degreesOfFreedom + x * x));
        }

        public static double InverseDistributionFunction(double probability, double degreesOfFreedom)
        {
            if (probability < 0.0 || probability > 1.0)
            {
                ThrowException.ArgumentOutOfRange("probability");
            }
            if (degreesOfFreedom <= 0.0)
            {
                ThrowException.ArgumentOutOfRange("degreesOfFreedom");
            }
            return new StudentTDistribution(degreesOfFreedom).InverseDistributionFunction(probability);
        }

        public static Interval GetConfidenceInterval(double mean, double varianceOfMean, double confidenceLevel, double degreesOfFreedom)
        {
            if (varianceOfMean <= 0.0)
            {
                ThrowException.ArgumentOutOfRange("varianceOfMean");
            }
            if (confidenceLevel < 0.0 || confidenceLevel > 1.0)
            {
                ThrowException.ArgumentOutOfRange("confidenceLevel");
            }
            double num;
            num = varianceOfMean * StudentTDistribution.InverseDistributionFunction((1.0 + confidenceLevel) / 2.0, degreesOfFreedom);
            return new Interval(mean - num, mean + num);
        }

        public StudentTDistribution(double degreesOfFreedom)
        {
            if (degreesOfFreedom <= 0.0)
            {
                ThrowException.ArgumentOutOfRange("degreesOfFreedom");
            }
            this.degreesOfFreedom = degreesOfFreedom;
        }

        public override double DistributionFunction(double x)
        {
            if (x < 0.0)
            {
                return 0.5 * GammaFunctions.RegularizedBeta(0.5 * this.degreesOfFreedom, 0.5, this.degreesOfFreedom / (this.degreesOfFreedom + x * x));
            }
            return 1.0 - 0.5 * GammaFunctions.RegularizedBeta(0.5 * this.degreesOfFreedom, 0.5, this.degreesOfFreedom / (this.degreesOfFreedom + x * x));
        }

        public override double ProbabilityDensityFunction(double x)
        {
            return Math.Pow(1.0 + x * x / this.degreesOfFreedom, -0.5 * (this.degreesOfFreedom + 1.0)) / Math.Sqrt(this.degreesOfFreedom) / GammaFunctions.Beta(0.5 * this.degreesOfFreedom, 0.5);
        }

        public override double MomentFunction(int order, double x)
        {
            if (order < 0)
            {
                ThrowException.ArgumentOutOfRange("order");
            }
            if (order == 0)
            {
                return this.DistributionFunction(x) - 0.5;
            }
            double num;
            num = this.degreesOfFreedom;
            return order switch
            {
                1 => (num + x * x) / (1.0 - this.degreesOfFreedom) * this.ProbabilityDensityFunction(x), 
                3 => (0.0 - (num + x * x)) * (2.0 * num + (num - 1.0) * x * x) / (num - 1.0) / (num - 3.0) * this.ProbabilityDensityFunction(x), 
                _ => base.MomentFunction(order, x), 
            };
        }

        public override double GetRandomVariate(System.Random random)
        {
            if (random == null)
            {
                ThrowException.ArgumentNull("random");
            }
            double num;
            double num3;
            do
            {
                num = 2.0 * random.NextDouble() - 1.0;
                double num2;
                num2 = 2.0 * random.NextDouble() - 1.0;
                num3 = num * num + num2 * num2;
            }
            while (num3 > 1.0);
            return num * Math.Sqrt(this.degreesOfFreedom * (Math.Exp(-2.0 * Math.Log(num3) / this.degreesOfFreedom) - 1.0) / num3);
        }
    }

 如果对您有帮忙,非常感谢您支持一下创造者的付出!

 感谢支持技术分享,请扫码点赞支持:

技术合作交流qq:2401315930

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

合抱阴阳

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值