Last updated: 2021-10-17

Checks: 7 0

Knit directory: misc/

This reproducible R Markdown analysis was created with workflowr (version 1.6.2). 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(20191122) 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 fd399fc. 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/
    Ignored:    data/.DS_Store

Untracked files:
    Untracked:  analysis/EM_var.md
    Untracked:  data/ROTS_results.RData
    Untracked:  data/ROTS_results2.RData
    Untracked:  data/cate_cate_high.RData
    Untracked:  data/cate_cate_low.RData
    Untracked:  data/cate_cate_mid.RData
    Untracked:  data/cate_cate_null.RData
    Untracked:  data/mout_low.RData
    Untracked:  data/mout_mid.RData
    Untracked:  data/mout_null.RData
    Untracked:  data/nonUMI_simu1.RData
    Untracked:  data/pbmc.rds
    Untracked:  data/pbmc3k_filtered_gene_bc_matrices/
    Untracked:  data/pbmc_counts.rds
    Untracked:  data/prices.csv
    Untracked:  data/scCD14.RData
    Untracked:  data/scCD4.RData
    Untracked:  data/scCD8.RData
    Untracked:  data/scMB.RData
    Untracked:  data/sva_sva_low.RData
    Untracked:  data/sva_sva_mid.RData
    Untracked:  data/sva_sva_null.RData

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/logistic_bound.Rmd) and HTML (docs/logistic_bound.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 fd399fc Dongyue Xie 2021-10-17 wflow_publish(“analysis/logistic_bound.Rmd”)
html f8983cd Dongyue Xie 2021-10-16 Build site.
Rmd c38464e Dongyue Xie 2021-10-16 wflow_publish(“analysis/logistic_bound.Rmd”)

Introduction

Let \(y\) be a binary variable with \(p(y=1) = p\), where \(p = g(\beta)\) and \(g(x) = \frac{1}{1+\exp(-x)}\). The function \(g\) is usually called logistic function or sigmoid function.

The log likelihood is

\[\begin{equation} \begin{split} l(\beta) &= \log p(y|\beta) \\&= y\log g(\beta)+(1-y)\log (1-g(\beta)) \\&= (-\beta-\log(1+\exp(-\beta))) +y\beta \\&= (y-1)\beta - \log(1+\exp(-\beta)) \end{split} \end{equation}\]

It’s sometimes more convenient to write

\[l(\beta) = g((2y-1)\beta).\]

The summation inside \(\log\) function is a pain and it is hard to deal with. For example in variational inference of logistic regression. One method to bypass the log sum is to lower bound the function \(-\log(1+\exp(-\beta))\).

In the paper Jaakkola and Jordan, 2000, the following lower bound is suggested.

Notice that \[-\log(1+\exp(-\beta)) = \frac{\beta}{2}-\log(\exp(\beta/2)+\exp(-\beta/2)),\]

where \(f(\beta) = -\log(\exp(\beta/2)+\exp(-\beta/2))\) is a convex function in \(\beta^2\). So we can bound \(f(\beta)\) globally with a first order Taylor expansion in \(\beta^2\), leading to

\[f(\beta)\geq -\frac{\eta}{2}-\log(1+\exp(-\eta))-\frac{1}{4\eta}tanh(\frac{\eta}{2})(\beta^2-\eta^2).\]

(I find the derivative \(\frac{\partial f(\beta)}{\partial\beta^2}\) should be \(-\frac{1}{4\eta}tanh(\frac{\eta}{2})\). Let’s have a check. It suggests that it should be \(-\frac{1}{4\eta}tanh(\frac{\eta}{2})\))

g = function(x){
  1/(1+exp(-x))
}
f0 = function(x){
  -log(exp(x/2)+exp(-x/2))
}
f0_lb = function(x,eta){
  -eta/2 + log(g(eta))-1/4/eta*tanh(eta/2)*(x^2-eta^2)
}
f0(0)
[1] -0.6931472
f0_lb(0,1)
[1] -0.6977324
f0_lb(0,-1)
[1] -0.6977324

This lower bound is exact if \(\beta^2 = \eta^2\). The lower bound is also a quadratic function in \(\beta^2\) which is a very useful property for doing Gaussian approximation.

The unknown parameter \(\eta\) is suggested to be optimized within the algorithm. It turns out that \(\eta^2 = E(\beta^2)\), the second moment of \(\beta\).

Another two useful reference:

  1. Spike and slab variational Bayes for high dimensional logistic regression

  2. VARIATIONAL BAYESIAN INFERENCE FOR LINEAR AND LOGISTIC REGRESSION

Now a question is how about \(f(x,y) = -\log(e^\frac{x+y}{2}+e^{-\frac{x+y}{2}})\).

library(plotly)
Loading required package: ggplot2

Attaching package: 'plotly'
The following object is masked from 'package:ggplot2':

    last_plot
The following object is masked from 'package:stats':

    filter
The following object is masked from 'package:graphics':

    layout
f = function(x,y){
  -log(exp((x+y)/2)+exp(-(x+y)/2))
}
x = y = seq(0,10,length.out = 30)
z = outer(x,y,f)
fig = plot_ly(x=x^2,y=y^2,z=z)
fig <- fig %>% add_surface()
fig

The plot suggests it is a convex function in \((x^2,y^2)\). A first order Taylor series expansion of in the variable \((x^2,y^2)\) yields \[f(x,y)\geq f(a,b)-\frac{1}{4a}tanh(\frac{a+b}{2})(x^2-a^2) - \frac{1}{4b}tanh(\frac{a+b}{2})(y^2-b^2)\]


sessionInfo()
R version 4.0.3 (2020-10-10)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS High Sierra 10.13.6

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRblas.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.0/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] plotly_4.9.2.1  ggplot2_3.3.2   workflowr_1.6.2

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.5        pillar_1.4.6      compiler_4.0.3    later_1.1.0.1    
 [5] git2r_0.27.1      tools_4.0.3       digest_0.6.27     viridisLite_0.3.0
 [9] jsonlite_1.7.1    evaluate_0.14     lifecycle_1.0.0   tibble_3.0.4     
[13] gtable_0.3.0      pkgconfig_2.0.3   rlang_0.4.10      DBI_1.1.0        
[17] rstudioapi_0.11   crosstalk_1.1.0.1 yaml_2.2.1        xfun_0.26        
[21] httr_1.4.2        withr_2.3.0       stringr_1.4.0     dplyr_1.0.5      
[25] knitr_1.36        htmlwidgets_1.5.2 generics_0.1.0    fs_1.5.0         
[29] vctrs_0.3.7       tidyselect_1.1.0  rprojroot_1.3-2   grid_4.0.3       
[33] data.table_1.13.2 glue_1.4.2        R6_2.4.1          rmarkdown_2.5    
[37] farver_2.0.3      tidyr_1.1.2       purrr_0.3.4       magrittr_2.0.1   
[41] whisker_0.4       backports_1.1.10  scales_1.1.1      promises_1.1.1   
[45] ellipsis_0.3.1    htmltools_0.5.1.1 assertthat_0.2.1  colorspace_1.4-1 
[49] httpuv_1.5.4      stringi_1.5.3     lazyeval_0.2.2    munsell_0.5.0    
[53] crayon_1.3.4