I am trying to compute the average log-likelihood where the likelihoods (probabilities) are very close to zero. I currently do so using:
mean_loglik = log(mean(exp(loglik)));
where loglik is a column vector of log-likelihoods. Doing so, all likelihoods (exp(loglik)) are rounded to zero, quashing the information because:
log(mean(zeros(size(loglik))))==-Inf
happens when taking the exponents. I have tried:
mean_loglik = log(mean(exp(loglik)));
dex = 1;
while (isfinite(mean_loglike)==0);
mean_loglik = log(mean(exp(loglik + dex * log(10^200)))) - dex * log(10^200);
dex=dex+1;
end
This essentially "multiplies" the likelihoods by some huge number, then "divides" them by such a number, doing so in the log stage rather than once exponentiated.
There are few problems with this. (1) It can get caught in infinite loop; (2) limiting the loop (e.g. adding && (dex<200)) might not solve the original issue; (3) this can be computationally very slow.
I have also tried to get the algebra to simplify using log rules, but haven't found an appropriate transformation. Is there some matlab function (or mathematical transformation) to solve or avoid this problem?
Example Code:
loglik = unifrnd(-1e100,-1e97,1000,1);
mean_loglik = log(mean(exp(loglik)));
dex=1;
while (isfinite(mean_loglik)==0) && (dex<2000);
mean_loglik = log(mean(exp(loglik + dex * log(10^200)))) - dex * log(10^200);
dex = dex+1;
end
isfinite(mean_loglik)