Last updated: 2022-09-29
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 ca6fb43. 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/
Ignored: analysis/figure/
Unstaged changes:
Modified: analysis/index.Rmd
Modified: code/normal_mean_model_utils.R
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/normal_mean_penalized_optimization_inversion.Rmd
)
and HTML
(docs/normal_mean_penalized_optimization_inversion.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 | ca6fb43 | Dongyue Xie | 2022-09-29 | add inversion method |
\[y|\mu\sim N(\mu,s^2),\mu\sim g(\cdot)\]
Optimization: \(\theta = E_q\mu\),
\[\min_{\theta,g}h(\theta,g) = \frac{1}{2s^2}(y-\theta)^2+\rho_{g,s}(\theta).\]
We use inversion method for solving the optimization problem.
For another method, see compound method
source("code/normal_mean_model_utils.R")
generate data, and get grid by running ash
set.seed(12345)
n = 200
w0 = 0.9
mu = c(rep(0,round(n*w0)),rep(10,n-round(n*w0)))
w_true = c(w0,1-w0)
grid_true = c(0.01,7)
s = rep(1,n)
y = rnorm(n,mu,s)
library(ashr)
fit.ash = ashr::ash(y,s,mixcompdist = 'normal')
#grid = exp(seq(log(s/100),log(sqrt(max(abs(y^2-s^2)))),by=log(sqrt(2))))
#fit.ash = S(y,s,w_true,grid_true)
#plot(fit.ash$fitted_g$sd,fit.ash$fitted_g$pi)
grid = fit.ash$fitted_g$sd
grid = grid[-1]
K = length(grid)
#plot(y,main='ash fit',col='grey80')
#lines(mu,col='grey60')
#lines(fit.ash$result$PosteriorMean)
#legend('topleft',c('data','true mean','ash posteriorMean'),pch=c(1,NA,NA),lty=c(NA,1,1),col=c('grey80','grey60',1))
f_obj_known_g = function(theta,y,s,w,grid,z_range){
z = S_inv(theta,s,w,grid,z_range)
return((y-theta)^2/2/s^2 - l_nm(z,s,w,grid)-(z-theta)^2/2/s^2)
}
f_obj_known_g_grad = function(theta,y,s,w,grid,z_range){
z = S_inv(theta,s,w,grid,z_range)
return((z-y)/s^2)
}
ebnm_penalized_inversion_known_g = function(x,s,w,grid,theta_init = NULL,z_range=NULL,opt_method = 'L-BFGS-B'){
n = length(x)
if(length(s)==1){
s = rep(s,n)
}
if(is.null(theta_init)){
theta_init = rep(0,n)
}
if(is.null(z_range)){
z_range = range(x) + c(-1,1)
}
theta = double(n)
for(i in 1:n){
theta[i] = optim(theta_init[i],
fn=f_obj_known_g,
gr = f_obj_known_g_grad,
y=x[i],
s=s[i],
w=w,
grid=grid,
z_range=z_range,
method = opt_method)$par
}
return(theta)
}
ploter = function(fit,y,mu,main='known prior'){
plot(y,main=main,col='grey80')
lines(mu,col='grey60')
lines(fit)
legend('topleft',c('data','true mean','estimated mean'),pch=c(1,NA,NA),lty=c(NA,1,1),col=c('grey80','grey60',1))
}
fit = ebnm_penalized_inversion_known_g(y,s,w_true,grid_true,theta_init = y,opt_method = 'L-BFGS-B')
ploter(fit,y,mu,main='known prior, init at y')
fit = ebnm_penalized_inversion_known_g(y,s,w_true,grid_true,theta_init = rep(0,n),opt_method = 'L-BFGS-B')
ploter(fit,y,mu,main='known prior, init at 0')
#'objective function
#'@param theta (theta,w)
#'@param grid prior sds
f_obj = function(params,y,s,grid,z_range,opt_method='L-BFGS-B'){
n = length(y)
w = softmax(params[-(1:n)])
theta = params[1:n]
z = S_inv(theta,s,w,grid,z_range)
return(sum((y-theta)^2/2/s^2 - l_nm(z,s,w,grid)-(z-theta)^2/2/s^2))
}
#'objective function
#'@param theta (theta,w)
#'@param grid prior sds
f_obj_grad = function(params,y,s,grid,z_range,opt_method='L-BFGS-B'){
n = length(y)
a = params[-(1:n)]
w = softmax(a)
theta = params[1:n]
z = S_inv(theta,s,w,grid,z_range)
grad_theta = (z-y)/s^2
grad_a = -colSums(l_nm_d1_a(z,s,a,grid))
return(c(grad_theta,c(grad_a)))
}
ebnm_penalized_inversion = function(x,s,grid,theta_init = NULL,w_init=NULL,z_range=NULL,opt_method = 'L-BFGS-B'){
n = length(x)
K = length(grid)
if(is.null(w_init)){
w_init = rep(1/K,K)
}
if(length(s)==1){
s = rep(s,n)
}
if(is.null(theta_init)){
theta_init = rep(0,n)
}
if(is.null(z_range)){
z_range = range(x) + c(-1,1)
}
params = c(theta_init,w_init)
out = optim(params,
fn=f_obj,
gr = f_obj_grad,
y=x,
s=s,
grid=grid,
z_range=z_range,
method = opt_method)
return(list(posteriorMean = out$par[1:n],w = softmax(out$par[-(1:n)]),opt_res = out))
}
ploter = function(fit,y,mu,main='estimate prior'){
plot(y,main=main,col='grey80')
lines(mu,col='grey60')
lines(fit$posteriorMean)
legend('topleft',c('data','true mean','estimated mean'),pch=c(1,NA,NA),lty=c(NA,1,1),col=c('grey80','grey60',1))
}
fit = ebnm_penalized_inversion(y,s,grid,theta_init = y,opt_method = 'L-BFGS-B')
plot(grid,fit$w,ylab='w hat')
fit$opt_res$value
[1] 408.2922
ploter(fit,y,mu,main='estimate prior, init at y')
fit = ebnm_penalized_inversion(y,s,grid,theta_init = rep(0,n),opt_method = 'L-BFGS-B')
plot(grid,fit$w,ylab='w hat')
fit$opt_res$value
[1] 408.2915
ploter(fit,y,mu,main='estimate prior, init at 0')
fit.ash = ash(y,s,mixcompdist = 'normal',pointmass=FALSE,prior='uniform',mixsd=grid)
plot(y,main='ash fit',col='grey80')
lines(mu,col='grey60')
lines(fit.ash$result$PosteriorMean)
legend('topleft',c('data','true mean','ash posteriorMean'),pch=c(1,NA,NA),lty=c(NA,1,1),col=c('grey80','grey60',1))
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] ashr_2.2-54 workflowr_1.7.0
loaded via a namespace (and not attached):
[1] Rcpp_1.0.9 highr_0.9 compiler_4.2.1 pillar_1.8.1
[5] bslib_0.4.0 later_1.3.0 git2r_0.30.1 jquerylib_0.1.4
[9] tools_4.2.1 getPass_0.2-2 digest_0.6.29 lattice_0.20-45
[13] jsonlite_1.8.0 evaluate_0.16 tibble_3.1.8 lifecycle_1.0.2
[17] pkgconfig_2.0.3 rlang_1.0.5 Matrix_1.4-1 cli_3.3.0
[21] rstudioapi_0.14 yaml_2.3.5 xfun_0.32 fastmap_1.1.0
[25] invgamma_1.1 httr_1.4.4 stringr_1.4.1 knitr_1.40
[29] fs_1.5.2 vctrs_0.4.1 sass_0.4.2 grid_4.2.1
[33] rprojroot_2.0.3 glue_1.6.2 R6_2.5.1 processx_3.7.0
[37] fansi_1.0.3 rmarkdown_2.16 mixsqp_0.3-43 irlba_2.3.5
[41] callr_3.7.2 magrittr_2.0.3 whisker_0.4 ps_1.7.1
[45] promises_1.2.0.1 htmltools_0.5.3 httpuv_1.6.5 utf8_1.2.2
[49] stringi_1.7.8 truncnorm_1.0-8 SQUAREM_2021.1 cachem_1.0.6