public class ContinuousUniformDistribution : ContinuousDistribution
{
private double lowerBound;
private double upperBound;
public static readonly ContinuousUniformDistribution Standard = new ContinuousUniformDistribution();
public override double Mean => (this.upperBound + this.lowerBound) / 2.0;
public override double Variance
{
get
{
double num;
num = this.upperBound - this.lowerBound;
return num * num / 12.0;
}
}
public override double Skewness => 0.0;
public override double Kurtosis => -1.2;
public override bool IsSymmetrical => true;
public double UpperBound => this.upperBound;
public double LowerBound => this.lowerBound;
public static double GetRandomVariate(System.Random random, double upperBound)
{
if (random == null)
{
ThrowException.ArgumentNull("random");
}
return random.NextDouble() * upperBound;
}
public static double GetRandomVariate(System.Random random, double lowerBound, double upperBound)
{
if (random == null)
{
ThrowException.ArgumentNull("random");
}
return lowerBound + random.NextDouble() * (upperBound - lowerBound);
}
public ContinuousUniformDistribution()
: this(0.0, 1.0)
{
}
public ContinuousUniformDistribution(double upperBound)
: this(0.0, upperBound)
{
}
public ContinuousUniformDistribution(double lowerBound, double upperBound)
{
if (upperBound <= lowerBound)
{
ThrowException.ArgumentOutOfRange("upperBound");
}
this.lowerBound = lowerBound;
this.upperBound = upperBound;
}
public override double DistributionFunction(double x)
{
if (x <= this.lowerBound)
{
return 0.0;
}
if (x >= this.upperBound)
{
return 1.0;
}
return (x - this.lowerBound) / (this.upperBound - this.lowerBound);
}
public override double ProbabilityDensityFunction(double x)
{
if (x < this.lowerBound || x >= this.upperBound)
{
return 0.0;
}
return 1.0 / (this.upperBound - this.lowerBound);
}
public override double MomentFunction(int order, double x)
{
if (order < 0)
{
ThrowException.ArgumentOutOfRange("order");
}
double num;
num = this.lowerBound;
double num2;
num2 = this.upperBound;
if (0.0 <= num)
{
if (x <= num)
{
return 0.0;
}
num2 = Math.Min(num2, x);
}
else if (num2 <= 0.0)
{
if (x >= num2)
{
return 0.0;
}
double num3;
num3 = num2;
num2 = Math.Max(num, x);
num = num3;
}
else if (x >= 0.0)
{
num = 0.0;
num2 = Math.Min(num2, x);
}
else
{
num2 = 0.0;
num = Math.Max(num, x);
}
double num4;
num4 = ElementaryFunctions.Pow(num, order + 1);
return (ElementaryFunctions.Pow(num2, order + 1) - num4) / (double)(order + 1) / (this.upperBound - this.lowerBound);
}
public override double InverseDistributionFunction(double probability)
{
if (probability < 0.0 || probability > 1.0)
{
ThrowException.ArgumentOutOfRange("probability");
}
if (probability == 0.0)
{
return this.lowerBound;
}
if (probability == 1.0)
{
return this.upperBound;
}
return this.lowerBound + probability * (this.upperBound - this.lowerBound);
}
public override double GetRandomVariate(System.Random random)
{
if (random == null)
{
ThrowException.ArgumentNull("random");
}
return this.lowerBound + random.NextDouble() * (this.upperBound - this.lowerBound);
}
}
如果对您有帮忙,非常感谢您支持一下创造者的付出!
感谢支持技术分享,请扫码点赞支持:
技术合作交流qq:2401315930