HenrikBengtsson / matrixStats

R package: Methods that Apply to Rows and Columns of Matrices (and to Vectors)
https://cran.r-project.org/package=matrixStats
202 stars 33 forks source link

Feature request: `log_inv_ logit` and/or `log1p` #221

Open wds15 opened 1 year ago

wds15 commented 1 year ago

I happen to use on a few of my programs a specialised function called log_inv_logit which is arithmetically equal to log(inv_logit(x)).

For numerical stability reasons the function is defined as:

log_inv_logit <- function(mat) {
    idx <- mat < 0
    mat[idx] <- mat[idx] - log1p(exp(mat[idx]))
    mat[!idx] <- -1*log1p(exp(-mat[!idx]))
    mat
}

The log1p function would also be great to have on its own in the usual vector/row/col flavour: log1p(x) = log(1 + x)

Other than that: Great package! matrixStats made with it's logSumExp calls EMs coded in R 3x-6x faster in a package of mine.