View source: R/extract_logls.R
loggy_reweight | R Documentation |
This is not exposed to the user
loggy_reweight(mat, wts)
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. |
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.
# 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
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.