public abstract class DiscreteDistribution : Distribution
{
public abstract double Probability(int n);
public virtual int InverseDistributionFunction(double probability)
{
if (probability < 0.0 || probability > 1.0)
{
ThrowException.ArgumentOutOfRange("probability");
}
double mean;
mean = this.Mean;
double standardDeviation;
standardDeviation = this.StandardDeviation;
double skewness;
skewness = this.Skewness;
double kurtosis;
kurtosis = this.Kurtosis;
double num;
num = NormalDistribution.Standard.InverseDistributionFunction(probability);
double num2;
num2 = Math.Floor(mean + standardDeviation * (num + skewness * (num * num - 1.0) / 6.0 + kurtosis * num * (num * num - 3.0) / 24.0 + skewness * skewness * num * (5.0 - 2.0 * num * num) / 36.0));
int num3;
num3 = ((num2 > 2147483647.0) ? int.MaxValue : ((!(num2 < -2147483648.0)) ? ((int)num2) : int.MinValue));
double num4;
num4 = this.DistributionFunction(num3);
if (num4 > probability)
{
double num5;
num5 = num4;
while (num3 > int.MinValue)
{
num5 -= this.Probability(num3--);
if (num5 < probability)
{
break;
}
}
}
for (num4 = this.DistributionFunction(num3); num4 < probability; num4 += this.Probability(num3))
{
if (num3 >= int.MaxValue)
{
break;
}
num3++;
}
return num3;
}
public virtual double Probability(int lowerBound, int upperBound)
{
if (upperBound <= lowerBound)
{
return 0.0;
}
if (upperBound - lowerBound < 10)
{
double num;
num = 0.0;
for (int i = lowerBound; i < upperBound; i++)
{
num += this.Probability(i);
}
return num;
}
return this.DistributionFunction(upperBound - 1) - this.DistributionFunction(lowerBound - 1);
}
public abstract double DistributionFunction(int n);
public abstract int GetRandomVariate(System.Random random);
public void GetRandomVariates(System.Random random, int[] samples)
{
if (samples == null)
{
ThrowException.ArgumentNull("samples");
}
if (random == null)
{
ThrowException.ArgumentNull("random");
}
this.GetRandomVariates(random, samples, 0, samples.Length);
}
public virtual void GetRandomVariates(System.Random random, int[] samples, int startIndex, int length)
{
if (samples == null)
{
ThrowException.ArgumentNull("samples");
}
if (random == null)
{
ThrowException.ArgumentNull("random");
}
if (startIndex < 0)
{
ThrowException.ArgumentOutOfRange("startIndex");
}
if (length < 0)
{
ThrowException.ArgumentOutOfRange("length");
}
if (startIndex + length > samples.Length)
{
throw new ArgumentException(Resources.GetString("cpyNoSpace"));
}
for (int i = 0; i < length; i++)
{
samples[startIndex + i] = this.GetRandomVariate(random);
}
}
public Histogram GetExpectedHistogram(int lowerBound, int upperBound, double numberOfSamples)
{
Histogram histogram;
histogram = new Histogram(lowerBound, upperBound);
for (int i = 0; i < upperBound; i++)
{
histogram.Increment(i, numberOfSamples * this.Probability(i));
}
return histogram;
}
}
如果对您有帮忙,非常感谢您支持一下创造者的付出!
感谢支持技术分享,请扫码点赞支持:
技术合作交流qq:2401315930