Last updated: 2019-10-02

Checks: 7 0

Knit directory: ebpmf_demo/

This reproducible R Markdown analysis was created with workflowr (version 1.4.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(20190923) 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 version displayed above was the version of the Git repository at the time these results were generated.

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:  analysis/.ipynb_checkpoints/
    Untracked:  analysis/ebpmf_demo.Rmd
    Untracked:  analysis/softmax_experiments.ipynb
    Untracked:  docs/figure/test.Rmd/

Unstaged changes:
    Modified:   analysis/softmax_experiments.Rmd

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 R Markdown and HTML files. If you’ve configured a remote Git repository (see ?wflow_git_remote), click on the hyperlinks in the table below to view them.

File Version Author Date Message
Rmd 2851c1b zihao12 2019-10-02 start developing ebpmf rank k

library(ebpm)
library(matrixStats)
## Note: in rank1 case, what we need is just row and  column sum of X

## TODO: 
## 1. think about how to store qg. They include:
# qls_mean_log = matrix(replicate(n*K, 0), ncol =  K)
# qfs_mean_log = matrix(replicate(p*K, 0), ncol =  K)
# qls_mean = matrix(replicate(n*K, 0), ncol =  K)
# qfs_mean = matrix(replicate(p*K, 0), ncol =  K)
# # ... gls, gfs

## 2. think about the importance of initialization, and how to

ebpmf_rankk_exponential <- function(X, K, m, init, maxiter.out = 10, maxiter.int = 1, seed){
  X = as(X, "dgTMatrix") ## triplet representation: i,j, x
  set.seed(123)
  qg = init
  for(i in 1:maxiter.out){
    for(k in 1:K){
      ## get row & column sum of <Z_ijk>
      Ez = get_Ez(X, qg, k) 
      ## update q, g
      tmp = ebpmf_rank1_exponential_helper(Ez$rsum,Ez$csum,qg$qls_mean[,k],m, maxiter.int) ## need to deal with (manipulate) sparse matrix
      qg = update_qg(tmp, qg, k)
    }
  }
  return(qg)
}

## compute the row & col sum of <Z_ijk> for a given k
## since <Z_ijk> != 0 only if X_ij != 0, we only need to loop over nonzero elements of X
get_Ez <- function(X, qg, k){
  rsum = replicate(nrow(x), 0)
  csum = replicate(ncol(x), 0)
  for(l in 1:length(X@i)){
    i = X@i[l]
    j = X@j[l]
    current = X[i,j] * softmax1d(qg$qls_mean_log[i,] + qg$qfs_mean_log[j,])[k] ## <Z_ijk> = X_ij * psi_ijk
    rsum[i] = rsum[i] + current
    csum[j] = csum[j] + current
  }
  return(list(rsum = rsum, csum = csum))
}

softmax1d <- function(x){
  return(exp(x - logSumExp(x)))
}

update_qg <- function(tmp, qg, k){
  qg$qls_mean[,k] = tmp$ql$mean
  qg$qls_mean_log[,k] = tmp$ql$mean_log
  qg$qfs_mean[,k] = tmp$qf$mean
  qg$qfs_mean_log[,k] = tmp$qf$mean_log
  qg$gls[[k]] = tmp$gl
  qg$gfs[[k]] = tmp$gf
  return(qg)
}

# X: a matrix/array of shape n by p 
# different in that only row and column sum of X is provided
ebpmf_rank1_exponential_helper <- function(X_rowsum,X_colsum, init, m = 2, maxiter = 1){
  #El = init$ql$mean
  ql = init
  #E_f = get_exp_F(init)
  for(i in 1:maxiter){
    ## update q(f), g(f)
    sum_El = sum(ql$mean)
    tmp = ebpm_exponential_mixture(x = X_colsum, s = replicate(p,sum_El), m = m)
    qf = tmp$posterior
    gf = tmp$fitted_g
    ll_f = tmp$log_likelihood
    ## update q(l), g(l)
    sum_Ef = sum(qf$mean)
    tmp = ebpm_exponential_mixture(x = X_rowsum, s = replicate(n,sum_Ef), m = m)
    ql = tmp$posterior
    gl = tmp$fitted_g
    ll_l = tmp$log_likelihood
    qg = list(ql = ql, gl = gl, qf = qf, gf = gf, ll_f = ll_f, ll_l = ll_l)
    # elbo = compute_elbo(X, qg)
    # print(sprintf("ELBO: %f", elbo))
  }
  return(qg)
}

sessionInfo()
R version 3.5.1 (2018-07-02)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS  10.14

Matrix products: default
BLAS: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.5/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] matrixStats_0.54.0 ebpm_0.0.0.9000   

loaded via a namespace (and not attached):
 [1] workflowr_1.4.0 Rcpp_1.0.2      digest_0.6.21   rprojroot_1.3-2
 [5] backports_1.1.4 git2r_0.25.2    magrittr_1.5    evaluate_0.14  
 [9] stringi_1.4.3   fs_1.3.1        whisker_0.3-2   rmarkdown_1.13 
[13] tools_3.5.1     stringr_1.4.0   glue_1.3.1      mixsqp_0.1-120 
[17] xfun_0.8        yaml_2.2.0      compiler_3.5.1  htmltools_0.3.6
[21] knitr_1.25