bnp

Some older Bayesian nonparametrics research.
git clone git://git.jtobin.io/bnp.git
Log | Files | Refs | README | LICENSE

sim_fmm_1d_conditional_collapsed.r (1330B)


      1 require(ggplot2)
      2 require(gridExtra)
      3 require(reshape2)
      4 
      5 source('fmm_multivariate_conditional_collapsed.r')
      6 
      7 dimension = 1
      8 
      9 config = list(
     10     k = 4
     11   , m = dimension
     12   , a = 1
     13   , l = rep(0, dimension)
     14   , b = dimension
     15   , w = diag(1, dimension)
     16   , n = 50
     17   )
     18 
     19 set.seed(222)
     20 
     21 d = list(
     22     as.matrix(replicate(100, rnorm(config$m, 5)))
     23   , as.matrix(replicate(100, rnorm(config$m, -5)))
     24   , as.matrix(replicate(100, rnorm(config$m, 10)))
     25   , as.matrix(replicate(200, rnorm(config$m))))
     26 
     27 dn = lapply(d, function(j) { data.frame(x = j) })
     28 
     29 m  = melt(dn, id.vars = c('x'))
     30 
     31 set.seed(990909)
     32 
     33 params = inverse_model(
     34     config$n, config$k, as.matrix(m[, c('x')])
     35   , config$a
     36   , config$l
     37   , config$b, config$w
     38   )
     39 
     40 early = data.frame(x = m$x, variable = params$z[1,])
     41 mid   = data.frame(x = m$x, variable = params$z[round(config$n * 1 / 2),])
     42 late  = data.frame(x = m$x, variable = params$z[config$n - 1,])
     43 
     44 p_early =
     45   ggplot(early, aes(x, colour = factor(variable), fill = factor(variable))) +
     46     geom_histogram(alpha = 0.5)
     47 
     48 p_mid =
     49   ggplot(mid, aes(x, colour = factor(variable), fill = factor(variable))) +
     50     geom_histogram(alpha = 0.5)
     51 
     52 p_late =
     53   ggplot(late, aes(x, colour = factor(variable), fill = factor(variable))) +
     54     geom_histogram(alpha = 0.5)
     55 
     56 inferred_plots = grid.arrange(p_early, p_mid, p_late, ncol = 3)
     57