Created
March 18, 2012 04:09
-
-
Save echen/2068776 to your computer and use it in GitHub Desktop.
Chinese Restaurant Process
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
# Generate table assignments for `num_customers` customers, according to | |
# a Chinese Restaurant Process with dispersion parameter `alpha`. | |
# | |
# returns an array of integer table assignments | |
def chinese_restaurant_process(num_customers, alpha) | |
return [] if num_customers <= 0 | |
table_assignments = [1] # first customer sits at table 1 | |
next_open_table = 2 # index of the next empty table | |
# Now generate table assignments for the rest of the customers. | |
1.upto(num_customers - 1) do |i| | |
r = rand | |
if r < alpha.to_f / (alpha + i) | |
# Customer sits at new table. | |
table_assignments << next_open_table | |
next_open_table += 1 | |
else | |
# Customer sits at an existing table. | |
# He chooses which table to sit at by giving equal weight to each | |
# customer already sitting at a table. | |
which_table = table_assignments[rand(table_assignments.size)] | |
table_assignments << which_table | |
end | |
end | |
table_assignments | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment