Created
September 3, 2014 13:50
-
-
Save fakestarbaby/46f9e98526e1f69221f0 to your computer and use it in GitHub Desktop.
多項分布からサンプリングする
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local function samplingFromMultinomialDistribution(probabilities) | |
local accumulateProbability = 0 | |
local accumulateProbabilities = {} | |
for i = 1, #probabilities do | |
accumulateProbability = accumulateProbability + probabilities[i] | |
table.insert(accumulateProbabilities, accumulateProbability) | |
end | |
local random = math.random() | |
for i = 1, #accumulateProbabilities do | |
if random < accumulateProbabilities[i] then | |
return i | |
end | |
end | |
end | |
local probabilities = { 0.6, 0.2, 0.1, 0.05, 0.04, 0.01 } | |
local count = { 0, 0, 0, 0, 0, 0 } | |
for i = 1, 10000 do | |
local index = samplingFromMultinomialDistribution(probabilities) | |
count[index] = count[index] + 1 | |
end | |
for i = 1, #count do | |
print(count[i]) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment