Skip to content

Instantly share code, notes, and snippets.

@fakestarbaby
Created September 3, 2014 13:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fakestarbaby/46f9e98526e1f69221f0 to your computer and use it in GitHub Desktop.
Save fakestarbaby/46f9e98526e1f69221f0 to your computer and use it in GitHub Desktop.
多項分布からサンプリングする
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