What to do when your likelihood function has a double product with small values near zero - log transform doesn't work? I currently have a likelihood function defined as the following:
$$ 
L=\prod_{i=1}^{N}\left[\prod_{s=1}^{S_i}L_{is}(y\space|\space \rho_A)\times\phi + \prod_{s=1}^{S_i}L_{is}(y\space|\space \rho_B)\times(1-\phi)\right]
$$
Here, $y$ is an observation, $\rho_A$ and $\rho_B$ are defined to be a vector set of parameters, and $\phi$ is just a variable. 
What makes this likelihood different than the usual likelihoods I have seen is that not only are there are $N$ observations, but each observation has sub-values up to $S_i$, for $i=1,...,N$. 
My problem right now is that the $L_{is}(y\space|\space \rho_A)$ are small to begin with, with some values around $0.000000001$ or even $0$. Therefore, if I multiply many of these small values, I get an even smaller result that R or most other languages will just treat as zero. However, taking the logarithm of $L$ only converts the first product into a sum, which doesn't help at all.
I've also tried to take the exponential, and then the log of the product, but I still have values that go straight to zero. Would anyone know of a prudent transformation to use? I have tried to use the log-transform method where I find the maximum, then re-scale, as in here: Converting (normalizing) very small likelihood values to probability. However, it appears that this would work for likelihood's with denominator's of zero. Thanks!
 A: I recently had to deal with the same issue when computing conditional probabilities involving numbers on the order of $10^{-10000}$ (because normalizing the probability distribution would have required a great deal of unnecessary calculations).  The heart of this difficulty, which comes up repeatedly in statistical computation, concerns taking logarithms of sums which themselves can overflow or underflow the computing platform's form of numeric representation (typically IEEE doubles).  In the present case (to abstract away all irrelevant detail) we may write
$$L = \prod_i \left(\sum_j x_{ij}\right)$$
where the products $x_{i1} = \prod_{s=1}^{S_i}L_{is}(y\space|\space \rho_A)\phi$ and $x_{i2} = \prod_{s=1}^{S_i}L_{is}(y\space|\space \rho_B)(1-\phi)$ are themselves best computed using logarithms.  Naturally we would like to compute $L$ as
$$L = \exp(\log(L));\quad \log(L) = \sum_i \log \left(\sum_j x_{ij}\right)$$
and in most applications, having $\log(L)$ is all that is really needed.  There is the rub: how to compute logarithms of sums?  Dropping the (now superfluous) subscript $i$, and assuming the $x_j$ will be computed using logarithms, we are asking how to calculate expressions like
$$\log\sum_j \exp(\log(x_j))$$
without over- or underflow.
(Note, too, that in many statistical applications it suffices to obtain a natural logarithm to an accuracy of only one or two decimal places, so high precision is not usually needed.)
The solution is to keep track of the magnitudes of the arguments.  When $x_j \gg x_k$, then (to within a relative error of $x_k/x_j$) it is the case that $x_j + x_k = x_j$.  
Often this magnitude-tracking can be done by estimating the order of magnitude of the sum (in some preliminary approximate calculation, for instance) and simply ignoring any term which is much smaller in magnitude.  For more generality and flexibility, we can examine the relative orders of magnitude as we go along.  This works best when summands have the most similar orders of magnitude, so that as little precision as possible is lost in each partial sum.  Provided, then, that we arrange to sum the terms from smallest to largest, we may repeatedly apply this useful approximation and, in the end, lose no meaningful precision.
In many statistical applications (such as computing log likelihoods, which frequently involve Gamma and Beta functions) it is handy to have built-in functions to return the logarithms of factorials $n!$ and binomial coefficients $\binom{n}{k}$.  Most statistical and numerical computing platforms have such functions--even Excel does (GAMMALN).  Use these wherever possible.

Here is R code to illustrate how this could be implemented.  So that it will serve as (executable) pseudocode, only simple Fortran-like expressions are used: they will port easily to almost any computing environment.
log.sum <- function(y, precision=log(10^17)) {
  # 
  # Returns the logarithm of sum(exp(y)) without overflow or underflow.
  # Achieves a relative error approximately e^(-precision).
  #
  # Some precision may be lost in computing `log(1+exp(*))`, depending on
  # the computing platform.
  #
  if (length(y) == 0) return(1)
  log.plus <- function(a, b) ifelse(abs(b-a) > precision, 
                                    max(b,a), max(b,a) + log(1 + exp(-abs(b-a))))
  y <- sort(y)
  x <- y[1]
  for (z in y[-1]) x <- log.plus(x, z)
  return (x)  
}

For example, let's compute $\log\sum_{i=0}^{1000}\exp(i) = 1000.45867514538708\ldots$ in R with and without this method:

x <- seq(0, 1000, by=1); print(log(sum(exp(x))), digits=18)

[1] Inf

Summation simply overflows.  However,

print(log.sum(x), digits=18)

[1] 1000.45867514538713

is accurate to 17 full decimal places, which is all one can expect of IEEE doubles.
A: This is just a small addition to @whuber's answer (+1).
To compute $$\log \sum \exp (x_i)$$ without overflowing or underflowing, a "log-sum-trick" is often used, see e.g. here for description. The trick is to compute $$\max(x_i) + \log \sum \exp (x_i - \max(x_i))$$ instead. This will prevent overflowing, and if some terms underflow then they are irrelevant anyway.
Using @whuber's example of $x_i=1\ldots 1000$, here is a demonstration in Matlab. Direct computation
x = 1:1000;
log(sum(exp(x)))

yields Inf, but 
a = max(x) + log(sum(exp(x - max(x))));
display(num2str(a, 20))

yields 1000.4586751453871329, correct to 17 places, as in @whuber's answer.
