対数分布サンプリング; Logarithmic Distribution Sampling
概要
対数分布とは以下の式で表現される確率分布。
$$ P[X=k] = -\frac{p^k}{k log(1-p)}, \quad k \geq 1 $$
ソースコード
namespace ExRandom.Discrete {
public class LogarithmicRandom : Random{
readonly MT19937 mt;
readonly int max;
readonly double p, f1;
public LogarithmicRandom(MT19937 mt, double p = 0.5, int max = int.MaxValue) {
if(mt == null) {
throw new ArgumentNullException();
}
if(!(p > 0) || p >= 1 || max < 1) {
throw new ArgumentException();
}
this.mt = mt;
this.max = max;
this.p = p;
this.f1 = -p / Math.Log(1 - p);
}
public override int Next() {
int k = 1;
double f = f1, r = mt.NextDouble_OpenInterval1();
for(;;) {
r -= f;
if(r <= 0 || k >= max || f < double.Epsilon) {
return k;
}
f *= p * k / (k + 1);
k++;
}
}
}
}
関連項目
メルセンヌ・ツイスタ
各種確率分布サンプリング基本クラス