public class BernoulliDistribution : DiscreteDistribution
{
private double p;
public double ProbabilityOfSuccess => this.p;
public override double Mean => this.p;
public override double Variance => this.p * (1.0 - this.p);
public override double Skewness => (1.0 - 2.0 * this.p) / Math.Sqrt(this.p * (1.0 - this.p));
public override double Kurtosis => 1.0 / (1.0 - this.p) + 1.0 / this.p - 6.0;
public static int GetRandomVariate(System.Random random, double probability)
{
if (random == null)
{
ThrowException.ArgumentNull("random");
}
if (probability < 0.0 || probability > 1.0)
{
ThrowException.ArgumentOutOfRange("probability");
}
return new BernoulliDistribution(probability).GetRandomVariate(random);
}
public BernoulliDistribution(double probability)
{
if (probability < 0.0 || probability > 1.0)
{
ThrowException.ArgumentOutOfRange("probability");
}
this.p = probability;
}
public override double Probability(int n)
{
return n switch
{
0 => 1.0 - this.p,
1 => this.p,
_ => 0.0,
};
}
public override int InverseDistributionFunction(double probability)
{
if (probability < 0.0 || probability > 1.0)
{
ThrowException.ArgumentOutOfRange("probability");
}
if (probability <= 1.0 - this.p)
{
return 0;
}
return 1;
}
public override double DistributionFunction(int n)
{
if (n < 0)
{
return 0.0;
}
if (n == 0)
{
return 1.0 - this.p;
}
return 1.0;
}
public override int GetRandomVariate(System.Random random)
{
if (random == null)
{
ThrowException.ArgumentNull("random");
}
if (!(random.NextDouble() <= this.p))
{
return 0;
}
return 1;
}
}
如果对您有帮忙,非常感谢您支持一下创造者的付出!
感谢支持技术分享,请扫码点赞支持:
技术合作交流qq:2401315930