Evaluating problematic function when cdf is close to one? Let $F(x;\theta)$ be a cumulative distribution function and $\beta>0$. I need to evaluate
$$\rho=\frac{F(x;\theta)^\beta}{F(x;\theta)-F(x;\theta)^{\beta+1}},$$
but for some values of $\theta$, R returns $F(x;\theta)=1$, and then I have some problems calculating $\rho$. The CDF is not exactly 1 at those parameter values. Is there a trick to obtain a stable calculation of $\rho$?
 A: Let $W_x(\theta) \equiv \log F(x|\theta)$ and note that $W_x(\theta) \leqslant 0$ since it is a log-probability.  (Since you have specified that $F(x|\theta)$ is close to one, this means that $W_x(\theta)$ is close to zero.)  Then you can have:
$$\begin{equation} \begin{aligned}
\log \rho 
&= \beta \log F(x;\theta) - \log ( F(x;\theta)-F(x;\theta)^{\beta+1}) \\[6pt]
&= (\beta-1) \log F(x;\theta) - \log ( 1 - F(x;\theta)^\beta) \\[6pt]
&= (\beta-1) \log F(x;\theta) - \log ( 1 - \exp(\beta \log F(x;\theta))) \\[6pt]
&= (\beta-1) W_x(\theta) - \log ( 1 - \exp(\beta W_x(\theta))) \\[6pt]
&= (\beta-1) W_x(\theta) - \text{log1mexp}(-\beta W_x(\theta)), \\[6pt]
\end{aligned} \end{equation}$$
where $\text{log1mexp}(a) \equiv \log (1-e^{-a})$.  Accurate calculation of expression hinges on accurate calculation of the $\text{log1mexp}$ function for argument values $a$ near zero.  Accurate calculation of this function is considered in Machler (2012), and is implemented in the VGAM package in R.  This is the required code:
library(VGAM);

#Function to compute log-rho
#Input W and beta
LOG_RHO <- function(W, beta) { (beta-1)*W - VGAM::log1mexp(beta*abs(W)); }

#Example with W close to zero (so F is close to one)
W    <- -10^(-8);
beta <- 100;

LOG_RHO(W, beta);
[1] 13.81551

exp(LOG_RHO(W, beta));
[1] 999999.5

You can see that this allows you to calculate your function even for values of $F(x|\theta)$ that are extremely close to one.  This should serve your purposes well.  If you would like to know more about computational issues relating to log-probabilties, see related questions here and here.
