loggy_reweight: reweight logs without underflow

View source: R/extract_logls.R

loggy_reweightR Documentation

reweight logs without underflow

Description

This is not exposed to the user

Usage

loggy_reweight(mat, wts)

Arguments

mat

a matrix with r rows and c columns of logged values (like probabilities)

wts

a vector of length r that is parallel to the rows in mat.

Details

This is made for the situation where each column of mat holds log probabilities of a given realization under r different hypotheses. We want to get the log probability of the given realization under a prior wts on the different hypotheses. This is super easy when mat consists of probabilities, but is a little trickier when mat holds logged prob values which need to be exponentiated, summed with weights, and then re-logged. One has to worry about underflow in these situations. This function deals with that by pulling a constant out of each and putting it back in as a log for each column.

Examples

# this is just a little thing to show that it gets the correct result:
tmp <- matrix(runif(120,0,1), nrow = 3)
wtt <- c(.23, .67, 1.8)
wts <- wtt/sum(wtt)
correct <- log(colSums(tmp * wts))

# and then compute it with loggy_reweight
lw <- loggy_reweight(log(tmp), wts)

# see they are give the same thing
all.equal(lw, correct)

# see that they are only different at machine precision
lw - correct

eriqande/CKMRsim documentation built on Aug. 2, 2024, 7:23 a.m.