bnp

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

sim_fmm_1d_conditional.r (1758B)


      1 require(ggplot2)
      2 require(gridExtra)
      3 require(reshape2)
      4 
      5 source('fmm_conditional.r')
      6 
      7 config = list(
      8     k = 3
      9   , a = 1
     10   , l = 0
     11   , r = 0.01
     12   , b = 1
     13   , w = 1
     14   , n = 1000
     15   )
     16 
     17 set.seed(990909)
     18 
     19 d = data.frame(
     20   value = c(rnorm(250, -3, 0.25), rnorm(500, 0, 0.25), rnorm(250, 3, 0.25)))
     21 
     22 set.seed(990909)
     23 
     24 params = inverse_model(
     25     config$n, config$k, d$value
     26   , config$a, config$l, config$r
     27   , config$b, config$w
     28   )
     29 
     30 dp = melt(as.data.frame(params$p))
     31 dm = melt(as.data.frame(params$m))
     32 ds = melt(as.data.frame(params$s))
     33 dl = melt(as.data.frame(params$l))
     34 
     35 py = ggplot(d, aes(value)) + geom_histogram(alpha = 0.5, fill = 'darkblue')
     36 
     37 pp = ggplot(dp, aes(x = seq_along(value), y = value, colour = variable)) +
     38        geom_line()
     39 
     40 pm = ggplot(dm, aes(x = seq_along(value), y = value, colour = variable)) +
     41        geom_line()
     42 
     43 ps = ggplot(ds, aes(x = seq_along(value), y = log(value), colour = variable)) +
     44        geom_line()
     45 
     46 pl = ggplot(dl, aes(x = seq_along(value), y = value)) +
     47        geom_line(colour = 'darkblue')
     48 
     49 early = data.frame(value = d$value, variable = params$z[1,])
     50 mid   = data.frame(value = d$value, variable = params$z[round(config$n / 2),])
     51 late  = data.frame(value = d$value, variable = params$z[config$n - 1,])
     52 
     53 p_early =
     54   ggplot(early, aes(value, colour = factor(variable), fill = factor(variable))) +
     55     geom_histogram(alpha = 0.5)
     56 
     57 p_mid =
     58   ggplot(mid, aes(value, colour = factor(variable), fill = factor(variable))) +
     59     geom_histogram(alpha = 0.5)
     60 
     61 p_late =
     62   ggplot(late, aes(value, colour = factor(variable), fill = factor(variable))) +
     63     geom_histogram(alpha = 0.5)
     64 
     65 chain_plots    = grid.arrange(py, pp, pm, ps, nrow = 2, ncol = 2)
     66 
     67 inferred_plots = grid.arrange(py, p_early, p_mid, p_late, nrow = 2, ncol = 2)
     68