On Sat, Jan 08, 2011 at 12:20:59AM +0800, zhaoxing731 wrote:
> Hello
>
> I want to calculate natural logarithm of sum of combinations as follow:
> (R code)
>
> {
>
> com_sum=choose(2000000,482)*choose(1000000,118)+choose(2000000,483)*choose(1000000,117)+...+choose(2000000,i)*choose(1000000,600-i)+...+choose(2000000,600)*choose(1000000,0)
> #calculate the sum
> result=log(com_sum) #calculate
> the log of the sum
> }
>
> But every element of the com_sum is Inf, so I can't get the result
> Thank you in advance
The natural logarithm of choose() is lchoose(), so the
natural logarithms of the products above are
i <- 482:600
logx <- lchoose(2000000,i) + lchoose(1000000,600-i)
maxlog <- max(logx)
# [1] 5675.315
The sum of numbers, which have very different magnitudes, may
be approximated by their maximum, so max(logx) is an
approximation of the required logarithm. A more accurate
calculation can be done, for example, as follows
maxlog + log(sum(exp(logx - maxlog)))
# [1] 5675.977
Petr Savicky.
______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.