Last updated: 2022-09-27

Checks: 7 0

Knit directory: gsmash/

This reproducible R Markdown analysis was created with workflowr (version 1.7.0). The Checks tab describes the reproducibility checks that were applied when the results were created. The Past versions tab lists the development history.


Great! Since the R Markdown file has been committed to the Git repository, you know the exact version of the code that produced these results.

Great job! The global environment was empty. Objects defined in the global environment can affect the analysis in your R Markdown file in unknown ways. For reproduciblity it’s best to always run the code in an empty environment.

The command set.seed(20220606) was run prior to running the code in the R Markdown file. Setting a seed ensures that any results that rely on randomness, e.g. subsampling or permutations, are reproducible.

Great job! Recording the operating system, R version, and package versions is critical for reproducibility.

Nice! There were no cached chunks for this analysis, so you can be confident that you successfully produced the results during this run.

Great job! Using relative paths to the files within your workflowr project makes it easier to run your code on other machines.

Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility.

The results in this page were generated with repository version a3db677. See the Past versions tab to see a history of the changes made to the R Markdown and HTML files.

Note that you need to be careful to ensure that all relevant files for the analysis have been committed to Git prior to generating the results (you can use wflow_publish or wflow_git_commit). workflowr only checks the R Markdown file, but you know if there are other scripts or data files that it depends on. Below is the status of the Git repository when the results were generated:


Ignored files:
    Ignored:    .Rhistory
    Ignored:    .Rproj.user/

Untracked files:
    Untracked:  code/poisson_mean/neg_binom_mean_lower_bound.R
    Untracked:  code/poisson_mean/neg_binom_mean_polya_gamma.R
    Untracked:  code/poisson_mean/pois_mean_GG.R
    Untracked:  code/poisson_mean/pois_mean_GG_opt.R
    Untracked:  code/poisson_mean/pois_mean_GMG.R
    Untracked:  code/poisson_mean/pois_mean_GMGM.R
    Untracked:  code/poisson_mean/test_example.R
    Untracked:  code/poisson_smooth/

Unstaged changes:
    Modified:   analysis/index.Rmd
    Modified:   code/poisson_mean/pois_mean_penalized.R

Note that any generated files, e.g. HTML, png, CSS, etc., are not included in this status report because it is ok for generated content to have uncommitted changes.


These are the previous versions of the repository in which changes were made to the R Markdown (analysis/neg_binom_mean_polya_gamma.Rmd) and HTML (docs/neg_binom_mean_polya_gamma.html) files. If you’ve configured a remote Git repository (see ?wflow_git_remote), click on the hyperlinks in the table below to view the files as they were in that past version.

File Version Author Date Message
Rmd a3db677 Dongyue Xie 2022-09-27 wflow_publish("analysis/neg_binom_mean_polya_gamma.Rmd")

Model

Consider the model

\[\begin{equation} \begin{split} &y_i\sim NB(r,p_i), \\ &\log\frac{p_i}{1-p_i} = \mu_i\sim g(\cdot), \end{split} \end{equation}\] where \(p(y;r,p)\propto p^y(1-p)^r\).

We use PG augmentation proposed in Polson et al.(2013) to perform posterior inference.

source('code/poisson_mean/neg_binom_mean_polya_gamma.R')
set.seed(12345)
n = 1000 
w = 0.2
mu = c(rep(0.1,n*(1-w)),rep(10,n*w))
x = rnbinom(n, size = 10, mu=mu)

When r is known.

Assume r is given, estimate p.

point_laplace, mean = estimate

out = nb_mean_polya_gamma(x=x,r=10,
                          maxiter = 2000,
                          tol=1e-8,
                          ebnm_params=list(mode='estimate',prior_family='point_laplace'))
plot(out$obj,type='l',ylab='ELBO',xlab='iteration')

out$ebnm_res$fitted_g
$pi
[1] 0.7049466 0.2950534

$mean
[1] -3.925342 -3.925342

$scale
[1] 0.000000 2.630529

attr(,"class")
[1] "laplacemix"
attr(,"row.names")
[1] 1 2
plot(x,main='',col='grey80',pch=20)
lines(mu,col='grey50')
lines(out$mean_est,col=4)
legend('topleft',c('data','true mean','estimated mean'),pch=c(20,NA,NA),lty=c(NA,1,1),col=c('grey80','grey50',4))

point_laplace, mean = 0

out = nb_mean_polya_gamma(x=x,r=10,
                          maxiter = 2000,
                          tol=1e-8,
                          ebnm_params=list(mode=0,prior_family='point_laplace'))
plot(out$obj,type='l',ylab='ELBO',xlab='iteration')

out$ebnm_res$fitted_g
$pi
[1] 8.149807e-07 9.999992e-01

$mean
[1] 0 0

$scale
[1] 0.000000 2.984682

attr(,"class")
[1] "laplacemix"
attr(,"row.names")
[1] 1 2
plot(x,main='',col='grey80',pch=20)
lines(mu,col='grey50')
lines(out$mean_est,col=4)
legend('topleft',c('data','true mean','estimated mean'),pch=c(20,NA,NA),lty=c(NA,1,1),col=c('grey80','grey50',4))

When r is unknown, estimating r is much harder

We start with letting r_init = true r = 10,

out = nb_mean_polya_gamma(x=x,r=10,est_r=T,update_r_every = 1,
                          maxiter = 300,
                          ebnm_params=list(mode='estimate',prior_family='point_laplace'))
plot(out$obj,type='l',ylab='ELBO',xlab='iteration')

out$r
[1] 0.117094
out$r_trace
 [1] 10.0000000  5.3994464  4.2571362  3.7459465  3.4417621  3.2254966
 [7]  3.0513310  2.8979003  2.7537618  2.6118909  2.4670139  2.3137253
[13]  2.1435736  1.9312473  1.6695600  1.4635529  1.2882753  1.1361468
[19]  1.0028800  0.8852373  0.7803753  0.6855529  0.5978500  0.5137165
[25]  0.4278534  0.3297926  0.2073982  0.1621014  0.1482636  0.1398205
[31]  0.1341019  0.1300825  0.1271699  0.1250137  0.1231944  0.1219864
[37]  0.1209781  0.1202742  0.1197466  0.1191310  0.1188525  0.1186333
[43]  0.1184591  0.1183066  0.1134649  0.1170940
plot(x,main='',col='grey80',pch=20)
lines(mu,col='grey50')
lines(out$mean_est,col=4)
legend('topleft',c('data','true mean','estimated mean'),pch=c(20,NA,NA),lty=c(NA,1,1),col=c('grey80','grey50',4))

It gets stuck at a local optimum - mean converges to constant, r converges to 0.

Maybe this is due to the inaccurate posterior mean and variance at the beginning, we can try to update r after a couple iterations and see what will happen.

out = nb_mean_polya_gamma(x=x,r=10,est_r=T,update_r_every = 10,
                          tol=1e-8,
                          maxiter = 2000,
                          ebnm_params=list(mode='estimate',prior_family='point_laplace'))
plot(out$obj,type='l',ylab='ELBO',xlab='iteration')

out$r
[1] 0.2891371
out$r_trace
 [1] 10.0000000  9.2062539  8.8316224  8.5246025  8.2358090  7.9567243
 [7]  7.6856679  7.4221153  7.1657307  6.9162184  6.6732948  6.4366781
[13]  6.2060935  5.9812643  5.7619190  5.5477817  5.3385686  5.1339978
[19]  4.9337735  4.7375875  4.5451150  4.3560113  4.1698981  3.9863607
[25]  3.8049339  3.6250867  3.4461748  3.2674442  3.0879181  2.9063051
[31]  2.7207795  2.5285211  2.3245429  2.0972697  1.7649082  1.4894809
[37]  1.2621661  1.0746246  0.9193255  0.7895226  0.6791600  0.5826206
[43]  0.4940814  0.4054052  0.2891371
plot(x,main='',col='grey80',pch=20)
lines(mu,col='grey50')
lines(out$mean_est,col=4)
legend('topleft',c('data','true mean','estimated mean'),pch=c(20,NA,NA),lty=c(NA,1,1),col=c('grey80','grey50',4))

out = nb_mean_polya_gamma(x=x,r=10,est_r=T,update_r_every = 20,
                          maxiter = 2000,
                          tol=1e-8,
                          ebnm_params=list(mode='estimate',prior_family='point_laplace'))
plot(out$obj,type='l',ylab='ELBO',xlab='iteration')

out$r
[1] 2.075251
out$r_trace
 [1] 10.000000  9.568768  9.238541  8.922058  8.615424  8.318104  8.029711
 [8]  7.749870  7.478213  7.214412  6.958113  6.708991  6.466721  6.230989
[15]  6.001481  5.777892  5.559911  5.347226  5.139526  4.936488  4.737777
[22]  4.543045  4.351924  4.164013  3.978873  3.796019  3.614893  3.434830
[29]  3.255042  3.074518  2.891917  2.705337  2.511818  2.306060  2.075251
plot(x,main='',col='grey80',pch=20)
lines(mu,col='grey50')
lines(out$mean_est,col=4)
legend('topleft',c('data','true mean','estimated mean'),pch=c(20,NA,NA),lty=c(NA,1,1),col=c('grey80','grey50',4))

increase n

set.seed(12345)
n = 10000
w = 0.2
mu = c(rep(0.1,n*(1-w)),rep(10,n*w))
x = rnbinom(n, size = 10, mu=mu)
out = nb_mean_polya_gamma(x=x,r=10,est_r=T,update_r_every = 20,
                          maxiter = 2000,
                          tol=1e-8,
                          ebnm_params=list(mode='estimate',prior_family='point_laplace'))
plot(out$obj,type='l',ylab='ELBO',xlab='iteration')

out$r
[1] 1.744737
out$r_trace
 [1] 10.000000  9.595019  9.291391  9.000791  8.718828  8.445000  8.178980
 [8]  7.920465  7.669183  7.424824  7.187114  6.955787  6.730584  6.511253
[15]  6.297550  6.089234  5.886059  5.687790  5.494193  5.305028  5.120051
[22]  4.939017  4.761670  4.587746  4.416969  4.249042  4.083652  3.920458
[29]  3.759085  3.599111  3.440046  3.281320  3.122231  2.961903  2.799168
[36]  2.632390  2.459088  2.274988  2.070797  1.744737
plot(x,main='',col='grey80',pch=20)
lines(mu,col='grey50')
lines(out$mean_est,col=4)
legend('topleft',c('data','true mean','estimated mean'),pch=c(20,NA,NA),lty=c(NA,1,1),col=c('grey80','grey50',4))

m = out$m
v = out$v
delta = -sum(m)/2-sum(log(cosh(sqrt(m^2+v)/2)))-n*log(2)
r_vec = 1:50
obj_val = c()
for(r in r_vec){
  obj_val = c(obj_val,Fr(log(r),x,m,v,delta))
}
plot(r_vec,obj_val,type='l')

obj_val = c()
for(r in r_vec){
  obj_val = c(obj_val,Fr_d1(log(r),x,m,v,delta))
}
plot(r_vec,obj_val,type='l')


sessionInfo()
R version 4.2.1 (2022-06-23 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 22000)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.utf8 
[2] LC_CTYPE=English_United States.utf8   
[3] LC_MONETARY=English_United States.utf8
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.utf8    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] ebnm_1.0-11     workflowr_1.7.0

loaded via a namespace (and not attached):
 [1] tidyselect_1.1.2  xfun_0.32         bslib_0.4.0       ashr_2.2-54      
 [5] purrr_0.3.4       splines_4.2.1     lattice_0.20-45   colorspace_2.0-3 
 [9] vctrs_0.4.1       generics_0.1.3    htmltools_0.5.3   yaml_2.3.5       
[13] utf8_1.2.2        rlang_1.0.5       mixsqp_0.3-43     jquerylib_0.1.4  
[17] later_1.3.0       pillar_1.8.1      glue_1.6.2        trust_0.1-8      
[21] lifecycle_1.0.2   stringr_1.4.1     munsell_0.5.0     gtable_0.3.1     
[25] evaluate_0.16     knitr_1.40        callr_3.7.2       fastmap_1.1.0    
[29] httpuv_1.6.5      ps_1.7.1          invgamma_1.1      irlba_2.3.5      
[33] fansi_1.0.3       highr_0.9         Rcpp_1.0.9        scales_1.2.1     
[37] promises_1.2.0.1  cachem_1.0.6      horseshoe_0.2.0   jsonlite_1.8.0   
[41] truncnorm_1.0-8   fs_1.5.2          deconvolveR_1.2-1 ggplot2_3.3.6    
[45] digest_0.6.29     stringi_1.7.8     processx_3.7.0    dplyr_1.0.10     
[49] getPass_0.2-2     rprojroot_2.0.3   grid_4.2.1        cli_3.3.0        
[53] tools_4.2.1       magrittr_2.0.3    sass_0.4.2        tibble_3.1.8     
[57] whisker_0.4       pkgconfig_2.0.3   Matrix_1.4-1      SQUAREM_2021.1   
[61] rmarkdown_2.16    httr_1.4.4        rstudioapi_0.14   R6_2.5.1         
[65] git2r_0.30.1      compiler_4.2.1