Wednesday, August 10, 2011

Reservoir sampling

Reservoir sampling, a good algo to know for randomly selecting k items from a list of N. the key observation here is that the replacement is happening with increasing probability i


array R[k];    // result
integer i, j;

// fill the reservoir array
for each i in 1 to k do
    R[i] := S[i]
done;

// replace elements with gradually decreasing probability
for each i in k+1 to length(S) do
    j := random(1, i);   // important: inclusive range
    if j <= k then
        R[j] := S[i]
    fi
done

No comments:

Post a Comment