bnp

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

commit fc7f6f60687fddc60226bf597fee63222246f016
parent dd80190895cd11da9b0a0d7ea4a836e7d4c89772
Author: Jared Tobin <jared@jtobin.ca>
Date:   Fri, 18 Mar 2016 08:55:48 +1300

Misc fixes.

Diffstat:
Mfinite-gaussian-mixture/src/fmm_multivariate_conditional_collapsed.r | 16+++++++++++-----
Mfinite-gaussian-mixture/src/fmm_multivariate_generative.r | 1+
Mfinite-gaussian-mixture/src/sim_fmm_1d_conditional_collapsed.r | 5++---
Mfinite-gaussian-mixture/src/sim_fmm_2d_conditional.r | 6+++---
Mfinite-gaussian-mixture/src/sim_fmm_2d_conditional_collapsed.r | 19+++++++++----------
5 files changed, 26 insertions(+), 21 deletions(-)

diff --git a/finite-gaussian-mixture/src/fmm_multivariate_conditional_collapsed.r b/finite-gaussian-mixture/src/fmm_multivariate_conditional_collapsed.r @@ -1,11 +1,17 @@ require(mvtnorm) cluster_statistics = function(cluster, l, b, w) { - mclust = as.matrix(cluster) + mclust = + # R, seriously? + if (is.null(dim(cluster))) { + matrix(cluster, ncol = ncol(w)) + } else { + as.matrix(cluster) + } m = ncol(mclust) n = nrow(mclust) ybar = colMeans(mclust) - centered = as.matrix(mclust) - ybar + centered = mclust - ybar ss = t(centered) %*% centered ln = (l + n * ybar) / (1 + n) tn = @@ -25,7 +31,7 @@ cluster_statistics = function(cluster, l, b, w) { ) } -conditional_label_model = function(y, k, z, a, l, r, b, w) { +conditional_label_model = function(y, k, z, a, l, b, w) { cluster_labels = seq(k) rows = seq(nrow(y)) m = ncol(y) @@ -79,9 +85,9 @@ conditional_label_model = function(y, k, z, a, l, r, b, w) { sapply(rows, relabel) } -inverse_model = function(n, k, y, a, l, r, b, w) { +inverse_model = function(n, k, y, a, l, b, w) { gibbs = function(z0) { - list(z = conditional_label_model(y, k, z0, a, l, r, b, w)) + list(z = conditional_label_model(y, k, z0, a, l, b, w)) } params = list(z = sample(seq(k), size = nrow(y), replace = T)) acc = params diff --git a/finite-gaussian-mixture/src/fmm_multivariate_generative.r b/finite-gaussian-mixture/src/fmm_multivariate_generative.r @@ -1,4 +1,5 @@ require(mvtnorm) +require(plyr) source('fmm_utils.r') diff --git a/finite-gaussian-mixture/src/sim_fmm_1d_conditional_collapsed.r b/finite-gaussian-mixture/src/sim_fmm_1d_conditional_collapsed.r @@ -11,8 +11,7 @@ config = list( , m = dimension , a = 1 , l = rep(0, dimension) - , r = diag(0.05, dimension) - , b = 2 + , b = dimension , w = diag(1, dimension) , n = 50 ) @@ -34,7 +33,7 @@ set.seed(990909) params = inverse_model( config$n, config$k, as.matrix(m[, c('x')]) , config$a - , config$l, config$r + , config$l , config$b, config$w ) diff --git a/finite-gaussian-mixture/src/sim_fmm_2d_conditional.r b/finite-gaussian-mixture/src/sim_fmm_2d_conditional.r @@ -20,9 +20,9 @@ config = list( set.seed(222) d = list( - t(replicate(250, rnorm(2, c(5, 5)))) - , t(replicate(250, rnorm(2, c(-5, -5)))) - , t(replicate(500, rnorm(2)))) + t(replicate(250, rnorm(dimension, c(5, 5)))) + , t(replicate(250, rnorm(dimension, c(-5, -5)))) + , t(replicate(500, rnorm(dimension)))) dn = lapply(d, function(j) { data.frame(x = j[,1], y = j[,2]) }) m = melt(dn, id.vars = c('x', 'y')) diff --git a/finite-gaussian-mixture/src/sim_fmm_2d_conditional_collapsed.r b/finite-gaussian-mixture/src/sim_fmm_2d_conditional_collapsed.r @@ -11,27 +11,26 @@ config = list( , m = dimension , a = 1 , l = rep(0, dimension) - , r = diag(0.05, dimension) - , b = 2 - , w = diag(1, dimension) - , n = 50 + , b = dimension + , w = diag(0.05, dimension) + , n = 25 ) set.seed(222) d = list( - t(replicate(100, rnorm(config$m, c(5, 5)))) - , t(replicate(100, rnorm(config$m, c(-5, -5)))) - , t(replicate(200, rnorm(config$m)))) + t(replicate(250, rnorm(config$m, c(5, 5)))) + , t(replicate(250, rnorm(config$m, c(-5, -5)))) + , t(replicate(500, rnorm(config$m)))) dn = lapply(d, function(j) { data.frame(x = j[,1], y = j[,2]) }) m = melt(dn, id.vars = c('x', 'y')) -set.seed(990909) +set.seed(222) params = inverse_model( - config$n, config$k, m[, c('x', 'y')] + config$n, config$k, as.matrix(m[, c('x', 'y')]) , config$a - , config$l, config$r + , config$l , config$b, config$w )