logsumexp: Compute log-sum-exp and softmax functions.

View source: R/util.R

logsumexpR Documentation

Compute log-sum-exp and softmax functions.

Description

Efficient implementation (via Rcpp) of log-sum-exp and softmax functions.

Usage

logsumexp(x, v = NULL)
softmax(x, v = NULL)

Arguments

x

a matrix of dimension (n x k).

v

an optional vector of length k. If not provided a vector of zeros is used.

Details

Given the matrix x logsumexp() calculates for each row x[i,] the log-sum-exp function computed as

m[i] + log(sum(exp(x[i,] + v) - m[i]))

where m[i] = max(x[i,] + v) .

softmax() calculates for each row x[i,] the softmax (aka multinomial logistic) function

exp( x_[i,c] + v_[c]) / (sum_{j=1}^k exp( x_[i,j] + v_[j]) )

Value

logsumexp() returns a vector of values of length equal to the number of rows of x. softmax() returns a matrix of values of the same dimension as x.

Author(s)

Luca Scrucca

Examples

x = matrix(rnorm(15), 5, 3)
v = log(c(0.5, 0.3, 0.2))
logsumexp(x, v)
(z = softmax(x, v))
rowSums(z)

mclustAddons documentation built on Jan. 6, 2023, 5:21 p.m.