Combining factors with the same base but different exponents
When we have numbers with the same base but different exponents being multiplied, we can just sum the exponents. For example:
\[a^{x_1}\times a^{x_2} \times a^{x_3}=a^{x_1+x_2+x_3}\] We can write the equation above much more succintly as:
\[\prod_{i=1}^3 a^{x_i} =a^{\sum_{i=1}^3 x_i}\] Knowing this can be handy to avoid some numerical issues in R. For example, we know that:
\[p_1=e^{1000}/e^{1000}=e^{1000-1000}=e^0=1\] However, we can’t really calculate this in R even though \(e^0\) is ok:
exp(1000)/exp(1000)
## [1] NaN
exp(0)
## [1] 1
Log of several quantities being multiplied
We can express the log of several quantities that are being multiplied together as the sum of the logs of the individual quantities. For example:
\[log(x_1\times x_2\times x_3)=log(x_1)+log(x_2)+log(x_3)\] Again, this can be written much more succintly as: \[log(\prod_{i=1}^3 x_i)=\sum_{i=1}^3 log(x_i)\] This is particularly important to avoid numerical issues associated with doing calculations on a computer with finite precision. As described in (McElreath 2020), say that we want to calculate
\[p_1=log(0.01 \times 0.01 \times ... \times 0.01)=log(0.01^{200})\]
We know that this is equal to:
\[p_2=200 \times log(0.01)\] When we calculate this in R, here is what we get:
=log(0.01^200); p1 p1
## [1] -Inf
=200*log(0.01); p2 p2
## [1] -921.034
The correct answer is given by “p2” in the R script above.
Comments?
Send me an email at