Skip to content
Back to formatted view

Raw Message

Message-ID: <03c53f05-3e2c-5b83-87a9-fbd91a36ba66@gmail.com>
Date: 2020-08-20T11:07:24Z
From: Duncan Murdoch
Subject: how to prove sum of probabilities to be one in R?
In-Reply-To: <CAGR+MS5nfrmsRqSsH1QUe2sZHw2X5ZtytqTeD5ZCMkS9yM4FFg@mail.gmail.com>

On 20/08/2020 3:42 a.m., Shaami wrote:
 > Hi Dear
 >
 > I am facing a floating-point problem related to the sum of probabilities.
 > It is really difficult to prove that sum of probabilities is 1 because of
 > some minor differences. The MWE is as follows.
 >
 >> p1=0.99999999
 >> p2=0.00000000003> p1+p2==1[1] FALSE
 >
 > The sum of probabilities is approximately 1. The difference from 1 is
 > 1-p1-p2 =  9.97e-09, that is very small. I need to apply the sum of
 > probabilities conditions in my many functions. But execution is halted
 > because of floating point.
 >
 > Could anyone please guide about that?
Use the approximate test

isTRUE(all.equal(p1+p2, 1))

If the default tolerance of sqrt(.Machine$double.eps) (about 1.5e-8) is 
wrong, change it:

isTRUE(all.equal(p1+p2, 1, tolerance = 0.1))

This says anything between 0.9 and 1.1 is equal to 1.

Duncan Murdoch