commit 22c63244aa68fcf0cc8d577dc248ca9a87d82aa5
parent ca36a3f2f866e4876afe3dbe029776a2edb9c6f2
Author: Jared Tobin <jared@jtobin.ca>
Date: Wed, 17 Feb 2016 17:22:31 +1300
IBP skeleton.
Diffstat:
1 file changed, 38 insertions(+), 0 deletions(-)
diff --git a/indian-buffet-process/src/ibp.r b/indian-buffet-process/src/ibp.r
@@ -0,0 +1,38 @@
+# FIXME handle sampled zero values
+
+ibp = function(n, a) {
+ dishes = rpois(1, a)
+ diners = data.frame(dish = seq(dishes), diners = rep(1, dishes))
+ buffet = list(buffet = diners, choices = list(seq(dishes)))
+
+ for (j in seq(n - 1)) {
+ buffet = arrival(buffet, a)
+ }
+ buffet
+ }
+
+arrival = function(b, a) {
+
+ config = b$buffet
+ choices = b$choices
+
+ j = length(choices) + 1
+ n = nrow(config)
+ probs = sapply(config$diners, function(n) { n / j })
+ selection = rbinom(n, 1, prob = probs) == 1
+
+ existing_diners = config[selection, 'diners']
+ new_diners = with(config, replace(diners, selection, diners[selection] + 1))
+
+ existing_dishes = data.frame(dish = config$dish, diners = new_diners)
+ num_new_dishes = rpois(1, a / j )
+ new_dishes = data.frame(
+ dish = (n + 1):(n + num_new_dishes)
+ , diners = rep(1, num_new_dishes)
+ )
+
+ list(
+ buffet = rbind(existing_dishes, new_dishes)
+ , choices = list(choices, rep(TRUE, num_new_dishes)) # FIXME
+ )
+ }