26
23

More than 5 years have passed since last update.

重み付きランダム抽出:Walker's Alias Method

Last updated at Posted at 2016-01-04

重み付きランダム復元抽出

のこと。

のようになります。

バイナリサーチなら準備にO(n)、抽選1回毎にO(log n)

Walker's Alias Method

(GAとか粒子フィルタとか。)
ググればわかりやすい説明が出てきます。

これを下のようなリストに並べ替えたとする(閾値リストと別名リストを生成する)と、

1.1を引いた場合、整数部が1なので閾値リストの要素1:p[1]をみて、

2.5を引いた場合、整数部が2なので閾値リストの要素1:p[2]をみて、

適当な実装(C#)

``````public class WalkersAliasMethod
{
private double[] probList;
private int[] aliasList;
private double[] weightList;
private Random rnd;

public WalkersAliasMethod()
{
rnd = new Random();
}

public WalkersAliasMethod(int seed)
{
rnd = new Random(seed);
}

//準備
public void UpdateList(double[] weightList)
{
probList = new double[weightList.Length];
aliasList = new int[weightList.Length];
this.weightList = weightList;
int size = weightList.Length;
double[] norWeightList = new double[size];
weightList.CopyTo(norWeightList, 0);
double sum = weightList.Sum();
double[] v = new double[size];//0~要素数で正規化された確率リスト
for (int i = 0; i < size; i++)
{
norWeightList[i] /= sum;
v[i] = norWeightList[i] * size;
}

List<int> small = new List<int>();
List<int> large = new List<int>();

for (int i = 0; i < size; i++)
{

if (v[i] < 1)
else
}

int g, l;
while (small.Count > 0 && large.Count > 0)
{
l = small[0];
g = large[0];
small.RemoveAt(0);
large.RemoveAt(0);

probList[l] = v[l];
aliasList[l] = g;
v[g] += -1.0 + v[l];
if (v[g] < 1)
else
}
while (large.Count > 0)
{
g = large[0];
large.RemoveAt(0);
probList[g] = 1;
}
while (small.Count > 0)
{
l = small[0];
small.RemoveAt(0);
probList[l] = 1;
}
}

//重みを元にランダムに復元抽出してインデックスを返す
public int Resampling()
{
double v = rnd.NextDouble() * (double)weightList.Length;
int k = (int)v;
double u = 1 + k - v;
if (u < probList[k])
{
return k;
}
return aliasList[k];
}
}

``````
26
23
1

Register as a new user and use Qiita more conveniently

1. You get articles that match your needs
2. You can efficiently read back useful information
3. You can use dark theme
What you can do with signing up
26
23