ルーレット選択; Roulette Selection Method
概要
ルーレット選択アルゴリズムは離散確率分布に従う確率変数の生成を一般化するために必要になる。
$$ P[X=k] = p_k $$
ソースコード
namespace ExRandom.Discrete {
public class RouletteRandom : Random{
readonly MT19937 mt;
readonly int[] indexs;
readonly double[] probs;
public RouletteRandom(MT19937 mt, params double[] probs) {
if(mt == null) {
throw new ArgumentNullException();
}
double sum_prob = 0;
for(int i = 0; i < probs.Length; i++){
if(!(probs[i] >= 0)) {
throw new ArgumentException();
}
sum_prob += probs[i];
}
if(!(sum_prob > 0)) {
throw new ArgumentException();
}
this.mt = mt;
this.indexs = new int[probs.Length];
this.probs = new double[probs.Length];
for(int i = 0; i < probs.Length; i++) {
this.indexs[i] = i;
this.probs[i] = probs[i] / sum_prob;
}
Array.Sort(this.probs, this.indexs);
}
public override int Next() {
double r = mt.NextDouble_OpenInterval1();
for(int i = probs.Length - 1; i >= 0; i--) {
r -= probs[i];
if(r <= 0) {
return indexs[i];
}
}
return indexs[probs.Length - 1];
}
}
}
関連項目
メルセンヌ・ツイスタ
各種確率分布サンプリング基本クラス