bnp

Some older Bayesian nonparametrics research.
Log | Files | Refs | README | LICENSE

commit dce62753db135a1aa27dc4af62e59cd6f8d7555c
parent 5d6215744119da697bd24bbaa28ae6d62c6a9e2c
Author: Jared Tobin <jared@jtobin.ca>
Date:   Mon, 15 Feb 2016 14:36:18 +1300

Add generative CRP.

Diffstat:
Achinese-restaurant-process/src/crp.r | 28++++++++++++++++++++++++++++
1 file changed, 28 insertions(+), 0 deletions(-)

diff --git a/chinese-restaurant-process/src/crp.r b/chinese-restaurant-process/src/crp.r @@ -0,0 +1,28 @@ +crp = function(n, a) { + restaurant = data.frame(table = 1, customers = 1) + for (j in seq(n - 1)) { + restaurant = arrival(restaurant, a) + } + restaurant + } + +arrival = function(r, a) { + p = 1 - a / (sum(r$customers) + a) + if (rbinom(1, 1, p)) { + join_table(r, a) + } else { + start_table(r) + } + } + +join_table = function(r, a) { + probs = r$customers / sum(r$customers) + table = sample(1:nrow(r), size = 1, prob = probs) + r[table, 'customers'] = r[table, 'customers'] + 1 + r + } + +start_table = function(r) { + rbind(r, c(nrow(r) + 1, 1)) + } +