Last updated: 2022-10-06

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 9842e30. 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:    .DS_Store
    Ignored:    .Rhistory
    Ignored:    .Rproj.user/

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_penalty.Rmd) and HTML (docs/normal_mean_penalty.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 9842e30 Dongyue Xie 2022-10-06 wflow_publish("analysis/normal_mean_penalty.Rmd")
html 6e18a0a Dongyue Xie 2022-09-30 Build site.
Rmd aa4d90d Dongyue Xie 2022-09-30 add hess
html 86c651b Dongyue Xie 2022-06-06 Build site.
Rmd 06b638b Dongyue Xie 2022-06-06 Publish the initial files for myproject

Introduction

What do the penalties \(\rho_{g,s^2}(\theta)\) and \(c_{g,s^2}(\theta) = \rho_{g,s^2}(S_{g,s^2}(\theta))\) look like?

The first one is the original penalty and we’ll need inversion \(S^{-1}(\cdot)\) to evaluate it; the second one is the compound penalty.

\[\rho_{g,s^2}(\theta) = \rho_{g,s^2}(S_{g,s^2}(S^{-1}_{g,s^2}(\theta))) = c_{g,s^2}(S^{-1}_{g,s^2}(\theta))\]

\[c_{g,s^2}(\theta) = \rho_{g,s^2}(S_{g,s^2}(\theta)) = -l_{NM}(\theta;g,s^2)-\frac{(\theta-S_{g,s}(\theta))^2}{2s^2}\]

(To evaluate \(\rho(\theta)\), we find \(z\) such that \(S(z) = \theta\) then \(\rho(\theta) = c(z)\).)

We also evaluate the second derivative of the penalties:

For the compound penalty \(c(\theta) = -l(\theta) - \frac{s^2(l'(\theta))^2}{2}\)

\[c'(\theta) = -l'(\theta) - s^2l'(\theta)l''(\theta)\]

\[c''(\theta) = -l''(\theta) - s^2(l''(\theta)^2+l'''(\theta)l'(\theta))\]

For the penalty \(\rho(\theta)\):

\[\rho_{g,s^2}'(\theta) = \frac{S^{-1}_{g,s^2}(\theta)-\theta}{s^2}\]

\[\rho_{g,s^2}''(\theta) = \frac{1}{s^2}\left(\frac{1}{1+s^2l''_{nm}(S^{-1}_{g,s^2}(\theta))}-1\right) = \frac{-l''_{nm}(S^{-1}_{g,s^2}(\theta))}{1+s^2l''_{nm}(S^{-1}_{g,s^2}(\theta))}\]

\[\rho''(S(\theta)) = -l''(\theta)/S'(\theta).\]

\[S(\theta) = \theta + s^2l_{NM}'(\theta)\]

\[S'(\theta) = 1 + s^2l_{NM}''(\theta)\]

\[\rho''(S(\theta)) = -l''(\theta)/S'(\theta) = -l''(\theta)/(1+s^2l''(\theta)).\]

source('code/normal_mean_model_utils.R')

# nm = function(z,g,s){
#   fit = ash(z,s,g = g,fixg = T)
#   return(list(l_nm = fit$loglik, pm=fit$result$PosteriorMean))
# }
# 
# penalty = function(theta,g,s){
#   temp = nm(theta,g,s)
#   return(-temp$l_nm - (theta-temp$pm)^2/2/s^2)
# }
# 
# penalty_vec = function(x,g,s){
#   n = length(x)
#   out = rep(0,n)
#   for(i in 1:n){
#     out[i] = penalty(x[i],g,s)
#   }
#   return(out)
# }
# 
# 
# 
# 
# #'posterior mean operator
# S = function(z,g,s){
#   pw = log(g$pi)+dnorm(z,mean=0,sd=sqrt(s^2+g$sd^2),log=TRUE)
#   pw = pw - max(pw)
#   pw = exp(pw)/sum(exp(pw))
#   pm = z/(1+s^2/g$sd^2)
#   return(sum(pw*pm))
# }
 
# #'Solve S(z) = theta for a given theta
# #'@param theta 
# #'@param g a normalmix object with weights pi and sd
# #'@param s known standard error
# inv_S = function(theta,g,s=1){
#   eqn = function(z,theta,g,s){
#     S(z,g,s) - theta
#   }
#   if(theta==0){
#     sol = 0
#   }else if(theta>0){
#     sol = uniroot(eqn,interval=c(0,theta/min(1/(1+s^2/g$sd^2))),theta=theta,g=g,s=s)$root
#   }else{
#     sol = uniroot(eqn,interval=c(theta/min(1/(1+s^2/g$sd^2)),0),theta=theta,g=g,s=s)$root
#   }
#   return(sol)
# }
# 
# inv_S_vec = function(x,g,s=1){
#   n = length(x)
#   out = c()
#   for(i in 1:n){
#     out[i] = inv_S(x[i],g,s)
#   }
#   out
# }


plot_penalty = function(x,s=1,w,grid){

  # plot compound penalty
  compound_penalty = nm_penalty_compound(x,s,w,grid)
  original_penalty = nm_penalty(x,s,w,grid)
  
  plot(x,compound_penalty,type='l',xlab='theta',ylab='penalty',ylim=range(c(compound_penalty,original_penalty)),
       main=paste('w=(',paste(w,collapse = ", "),'); grid=(',paste(grid,collapse = ", "),')',sep=''))
  lines(x,original_penalty,col='blue',lty=2)
  legend('bottomright',c(expression(rho(S(theta))),expression(rho(theta))),lty=c(1,2),col=c(1,'blue'))
}

plot_penalty_hess = function(x,s=1,w,grid){

  # plot compound penalty
  compound_penalty = nm_penalty_compound_hess(x,s,w,grid)
  original_penalty = nm_penalty_hess(x,s,w,grid)
  
  plot(x,compound_penalty,type='l',xlab='theta',ylab='2nd derivative of penalty',ylim=range(c(compound_penalty,original_penalty)),
       main=paste('w=(',paste(w,collapse = ", "),'); grid=(',paste(grid,collapse = ", "),')',sep=''))
  lines(x,original_penalty,col='blue',lty=2)
  legend('topright',c(expression(rho^"''"*(S(theta))),expression(rho^"''"*(theta))),lty=c(1,2),col=c(1,'blue'))
}
n = 101
x = seq(-10,10,length.out = n)
s = rep(1,n)
w = c(0.9,0.1)
par(mfrow=c(2,2))
grid_list=c(1,2,3,5)
for(gg in grid_list){
  plot_penalty(x,s,w,c(0,gg))
}

Version Author Date
6e18a0a Dongyue Xie 2022-09-30
for(gg in grid_list){
  plot_penalty_hess(x,s,w,c(0,gg))
}

Version Author Date
6e18a0a Dongyue Xie 2022-09-30
n = 101
x = seq(-10,10,length.out = n)
s = rep(1,n)
w = c(0.5,0.5)
par(mfrow=c(2,2))
grid_list=c(1,2,3,5)
for(gg in grid_list){
  plot_penalty(x,s,w,c(0,gg))
}

Version Author Date
6e18a0a Dongyue Xie 2022-09-30
86c651b Dongyue Xie 2022-06-06
for(gg in grid_list){
  plot_penalty_hess(x,s,w,c(0,gg))
}

Version Author Date
6e18a0a Dongyue Xie 2022-09-30
n = 101
x = seq(-10,10,length.out = n)
s = rep(1,n)
w = c(0.1,0.9)
par(mfrow=c(2,2))
grid_list=c(1,2,3,5)
for(gg in grid_list){
  plot_penalty(x,s,w,c(0,gg))
}

Version Author Date
6e18a0a Dongyue Xie 2022-09-30
86c651b Dongyue Xie 2022-06-06
for(gg in grid_list){
  plot_penalty_hess(x,s,w,c(0,gg))
}

Version Author Date
6e18a0a Dongyue Xie 2022-09-30
#'softmax
softmax = function(a){
  exp(a-max(a))/sum(exp(a-max(a)))
}
s2 = exp(seq(-8,5,by=log(2)))
w = softmax(log(1/s2))
round(w,2)
round(s2,2)
K = length(w)
g = normalmix(pi = w,mean=rep(0,K),sd = sqrt(s2))
x = seq(-10,10,length.out = 101)
plot_penalty(x,w,s,sqrt(s2))

sessionInfo()
R version 4.2.0 (2022-04-22)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Big Sur/Monterey 10.16

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.2/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     

other attached packages:
[1] ashr_2.2-54     workflowr_1.7.0

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.8.3     highr_0.9        bslib_0.3.1      compiler_4.2.0  
 [5] pillar_1.7.0     later_1.3.0      git2r_0.30.1     jquerylib_0.1.4 
 [9] tools_4.2.0      getPass_0.2-2    digest_0.6.29    lattice_0.20-45 
[13] jsonlite_1.8.0   evaluate_0.15    tibble_3.1.6     lifecycle_1.0.1 
[17] pkgconfig_2.0.3  rlang_1.0.2      Matrix_1.4-1     cli_3.3.0       
[21] rstudioapi_0.13  yaml_2.3.5       xfun_0.30        fastmap_1.1.0   
[25] invgamma_1.1     httr_1.4.2       stringr_1.4.0    knitr_1.38      
[29] sass_0.4.1       fs_1.5.2         vctrs_0.4.1      grid_4.2.0      
[33] rprojroot_2.0.3  glue_1.6.2       R6_2.5.1         processx_3.5.3  
[37] fansi_1.0.3      rmarkdown_2.13   mixsqp_0.3-47    irlba_2.3.5     
[41] callr_3.7.0      magrittr_2.0.3   whisker_0.4      ps_1.7.0        
[45] promises_1.2.0.1 htmltools_0.5.2  ellipsis_0.3.2   httpuv_1.6.5    
[49] utf8_1.2.2       stringi_1.7.6    truncnorm_1.0-8  SQUAREM_2021.1  
[53] crayon_1.5.1