Last updated: 2020-10-09

Checks: 7 0

Knit directory: vgapois/analysis/

This reproducible R Markdown analysis was created with workflowr (version 1.6.2.9000). 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(1) 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 d999aa5. 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:


working directory clean

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/vgapois_demo_1d.Rmd) and HTML (docs/vgapois_demo_1d.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 d999aa5 Peter Carbonetto 2020-10-09 workflowr::wflow_publish(“vgapois_demo_1d.Rmd”)
Rmd c5d5206 Peter Carbonetto 2020-10-09 Added workflowr files.

Add text here.

source("../code/vgapois.R")
set.seed(1)

Simulate data

n  <- 10
b0 <- -1
b  <- 1.5
x  <- rnorm(n)
r  <- exp(b0 + x*b)
y  <- rpois(n,r)

Compute importance sampling estimate of marginal likelihood

s0 <- 3
ns <- 1e5
b  <- rnorm(ns,sd = sqrt(s0))
w  <- rep(0,ns)
for (i in 1:ns)
  w[i] <- compute_loglik_pois(x,y,b0,b[i])
a <- max(w)
lnZ <- log(mean(exp(w - a))) + a

Fit variational approximation

fit <- vgapois1(x,y,b0,s0)
mu  <- fit$par["mu"]
s   <- fit$par["s"]
print(compute_elbo_vgapois1(x,y,b0,s0,mu,s),digits = 6)
# N = 2, M = 5 machine precision = 2.22045e-16
# L = -inf 1e-15 
# X0 = 0 1 
# U = inf inf 
# At X0, 0 variables are exactly at the bounds
# At iterate     0  f=       12.879  |proj g|=       2.6103
# Iteration     0
# 
# ---------------- CAUCHY entered-------------------
# 
# There are 1  breakpoints
# 
# Piece      1 f1, f2 at start point -1.1137e+01  1.1137e+01
# Distance to the next break point =   4.8095e-01
# Distance to the stationary point =   1.0000e+00
# Variable  2  is fixed.
# 
# GCP found in this segment
# Piece      2 f1, f2 at start point -3.5367e+00  6.8139e+00
# Distance to the stationary point =   5.1905e-01
# Cauchy X =  2.61034 1e-15 
# 
# ---------------- exit CAUCHY----------------------
# 
# 1  variables are free at GCP on iteration 1
# LINE SEARCH 0 times; norm of step = 1
# X = 0.933822 0.642261 
# G = 2.77367 4.7579 
# Iteration     1
# 
# ---------------- CAUCHY entered-------------------
# 
# There are 1  breakpoints
# 
# Piece      1 f1, f2 at start point -3.0331e+01  4.5078e+02
# Distance to the next break point =   1.3499e-01
# Distance to the stationary point =   6.7285e-02
# 
# GCP found in this segment
# Piece      1 f1, f2 at start point -3.0331e+01  4.5078e+02
# Distance to the stationary point =   6.7285e-02
# Cauchy X =  0.747195 0.322126 
# 
# ---------------- exit CAUCHY----------------------
# 
# Variable 2 enters the set of free variables
# 0 variables leave; 1 variables enter
# 2  variables are free at GCP on iteration 2
# LINE SEARCH 1 times; norm of step = 0.253669
# X = 0.987541 0.394346 
# G = 1.61641 3.27392 
# Iteration     2
# 
# ---------------- CAUCHY entered-------------------
# 
# There are 1  breakpoints
# 
# Piece      1 f1, f2 at start point -1.3331e+01  1.7626e+02
# Distance to the next break point =   1.2045e-01
# Distance to the stationary point =   7.5634e-02
# 
# GCP found in this segment
# Piece      1 f1, f2 at start point -1.3331e+01  1.7626e+02
# Distance to the stationary point =   7.5634e-02
# Cauchy X =  0.865286 0.146727 
# 
# ---------------- exit CAUCHY----------------------
# 
# 2  variables are free at GCP on iteration 3
# LINE SEARCH 1 times; norm of step = 0.139074
# X = 0.99301 0.255379 
# G = 0.879783 1.98679 
# Iteration     3
# 
# ---------------- CAUCHY entered-------------------
# 
# There are 1  breakpoints
# 
# Piece      1 f1, f2 at start point -4.7214e+00  6.2208e+01
# Distance to the next break point =   1.2854e-01
# Distance to the stationary point =   7.5896e-02
# 
# GCP found in this segment
# Piece      1 f1, f2 at start point -4.7214e+00  6.2208e+01
# Distance to the stationary point =   7.5896e-02
# Cauchy X =  0.926238 0.104589 
# 
# ---------------- exit CAUCHY----------------------
# 
# 2  variables are free at GCP on iteration 4
# LINE SEARCH 1 times; norm of step = 0.0908631
# X = 1.01968 0.168519 
# G = 0.648301 0.761998 
# Iteration     4
# 
# ---------------- CAUCHY entered-------------------
# 
# There are 1  breakpoints
# 
# Piece      1 f1, f2 at start point -1.0009e+00  1.5697e+01
# Distance to the next break point =   2.2115e-01
# Distance to the stationary point =   6.3768e-02
# 
# GCP found in this segment
# Piece      1 f1, f2 at start point -1.0009e+00  1.5697e+01
# Distance to the stationary point =   6.3768e-02
# Cauchy X =  0.97834 0.119928 
# 
# ---------------- exit CAUCHY----------------------
# 
# 2  variables are free at GCP on iteration 5
# LINE SEARCH 0 times; norm of step = 0.100318
# X = 0.921671 0.14712 
# G = -0.126003 -0.180958 
# Iteration     5
# 
# ---------------- CAUCHY entered-------------------
# 
# There are 0  breakpoints
# 
# GCP found in this segment
# Piece      1 f1, f2 at start point -4.8622e-02  9.4136e-01
# Distance to the stationary point =   5.1651e-02
# Cauchy X =  0.92818 0.156466 
# 
# ---------------- exit CAUCHY----------------------
# 
# 2  variables are free at GCP on iteration 6
# LINE SEARCH 0 times; norm of step = 0.0151149
# X = 0.935665 0.152832 
# G = -0.0114464 0.0223763 
# Iteration     6
# 
# ---------------- CAUCHY entered-------------------
# 
# There are 1  breakpoints
# 
# Piece      1 f1, f2 at start point -6.3172e-04  9.0129e-03
# Distance to the next break point =   6.8301e+00
# Distance to the stationary point =   7.0091e-02
# 
# GCP found in this segment
# Piece      1 f1, f2 at start point -6.3172e-04  9.0129e-03
# Distance to the stationary point =   7.0091e-02
# Cauchy X =  0.936468 0.151263 
# 
# ---------------- exit CAUCHY----------------------
# 
# 2  variables are free at GCP on iteration 7
# LINE SEARCH 0 times; norm of step = 0.0042122
# X = 0.939401 0.150886 
# G = 0.0050822 -0.01076 
# Iteration     7
# 
# ---------------- CAUCHY entered-------------------
# 
# There are 0  breakpoints
# 
# GCP found in this segment
# Piece      1 f1, f2 at start point -1.4161e-04  2.6096e-03
# Distance to the stationary point =   5.4263e-02
# Cauchy X =  0.939126 0.15147 
# 
# ---------------- exit CAUCHY----------------------
# 
# 2  variables are free at GCP on iteration 8
# LINE SEARCH 0 times; norm of step = 0.00132129
# X = 0.938236 0.15151 
# G = -3.54695e-06 0.000115329 
# Iteration     8
# 
# ---------------- CAUCHY entered-------------------
# 
# There are 1  breakpoints
# 
# Piece      1 f1, f2 at start point -1.3313e-08  3.3087e-07
# Distance to the next break point =   1.3137e+03
# Distance to the stationary point =   4.0237e-02
# 
# GCP found in this segment
# Piece      1 f1, f2 at start point -1.3313e-08  3.3087e-07
# Distance to the stationary point =   4.0237e-02
# Cauchy X =  0.938237 0.151505 
# 
# ---------------- exit CAUCHY----------------------
# 
# 2  variables are free at GCP on iteration 9
# LINE SEARCH 0 times; norm of step = 6.45625e-06
# X = 0.93824 0.151505 
# G = -1.89279e-07 1.09575e-07 
# Iteration     9
# 
# ---------------- CAUCHY entered-------------------
# 
# There are 1  breakpoints
# 
# Piece      1 f1, f2 at start point -4.7833e-14  3.6496e-13
# Distance to the next break point =   1.3827e+06
# Distance to the stationary point =   1.3107e-01
# 
# GCP found in this segment
# Piece      1 f1, f2 at start point -4.7833e-14  3.6496e-13
# Distance to the stationary point =   1.3107e-01
# Cauchy X =  0.93824 0.151505 
# 
# ---------------- exit CAUCHY----------------------
# 
# 2  variables are free at GCP on iteration 10
# LINE SEARCH 0 times; norm of step = 3.66473e-08
# X = 0.93824 0.151505 
# G = 5.47063e-10 3.91771e-10 
# 
# iterations 10
# function evaluations 14
# segments explored during Cauchy searches 11
# BFGS updates skipped 0
# active bounds at final generalized Cauchy point 0
# norm of the final projected gradient 5.47063e-10
# final function value 10.1593
# 
# X = 0.93824 0.151505 
# F = 10.1593
# final  value 10.159268 
# converged
#       mu 
# -10.1593

Compare exact and approximate posterior distributions

b    <- seq(-1,3,length.out = 1000)
n    <- length(b)
logp <- rep(0,n)
qb   <- dnorm(b,mu,sqrt(s))
for (i in 1:n)
  logp[i] <- compute_logp_pois(x,y,b0,b[i],s0)
maxlp <- max(logp)
plot(b,exp(logp - maxlp),type = "l",lwd = 2,col = "dodgerblue",
     xlab = "b",ylab = "relative posterior")
lines(b,qb/max(qb),col = "magenta",lwd = 2)


sessionInfo()
# R version 3.6.2 (2019-12-12)
# Platform: x86_64-apple-darwin15.6.0 (64-bit)
# Running under: macOS Catalina 10.15.6
# 
# Matrix products: default
# BLAS:   /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRblas.0.dylib
# LAPACK: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRlapack.dylib
# 
# locale:
# [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
# 
# attached base packages:
# [1] stats     graphics  grDevices utils     datasets  methods   base     
# 
# loaded via a namespace (and not attached):
#  [1] workflowr_1.6.2.9000 Rcpp_1.0.5           rprojroot_1.3-2     
#  [4] digest_0.6.23        later_1.0.0          R6_2.4.1            
#  [7] backports_1.1.5      git2r_0.26.1         magrittr_1.5        
# [10] evaluate_0.14        stringi_1.4.3        rlang_0.4.5         
# [13] fs_1.3.1             promises_1.1.0       whisker_0.4         
# [16] rmarkdown_2.3        tools_3.6.2          stringr_1.4.0       
# [19] glue_1.3.1           httpuv_1.5.2         xfun_0.11           
# [22] yaml_2.2.0           compiler_3.6.2       htmltools_0.4.0     
# [25] knitr_1.26