loggy_reweight.Rd
This is not exposed to the user
loggy_reweight(mat, wts)
a matrix with r rows and c columns of logged values (like probabilities)
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)
#> [1] TRUE
# see that they are only different at machine precision
lw - correct
#> [1] 0.000000e+00 -2.220446e-16 0.000000e+00 -1.110223e-16 2.220446e-16
#> [6] 0.000000e+00 1.110223e-16 -4.163336e-17 0.000000e+00 -2.220446e-16
#> [11] -5.551115e-17 2.775558e-17 -5.551115e-17 5.551115e-17 0.000000e+00
#> [16] 0.000000e+00 0.000000e+00 0.000000e+00 1.110223e-16 0.000000e+00
#> [21] 2.220446e-16 0.000000e+00 -1.110223e-16 0.000000e+00 8.326673e-17
#> [26] 0.000000e+00 2.220446e-16 1.110223e-16 1.110223e-16 0.000000e+00
#> [31] 1.665335e-16 -2.220446e-16 1.110223e-16 2.220446e-16 1.110223e-16
#> [36] 2.220446e-16 -2.220446e-16 5.551115e-17 0.000000e+00 5.551115e-17