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