2

I have a numerical problem while doing likelihood ratio tests in python. I'll not go into too much detail about what the statistics mean, my problems comes down to calculating this:

LR = LR_H0 / LR_h1 

where LR is the number of interest and LR_H0 and LR_H1 are numbers that can be VERY close to zero. This leads to a few numerical issues; if LR_H1 is too small then python will recognise this as a division by zero.

ZeroDivisionError: float division by zero

Also, although this is not the main issue, if LR_H1 is small enough to allow the division then the fraction LR_H0 / LR_h1 might become too big (I'm assuming that python also has an upper limit value of what a float can be).

Any tips on what the best way is to circumvent this problem? I'm considering doing something like:

def small_enough( num ): 
    if num == 0.0: 
        return *other small number* 
    else: 
    return num 

But this is not ideal because it would approximate the LR value and I would like to guarantee some precision.

4

1 回答 1

7

使用对数。取所有可能性的对数,然后加或减对数,而不是乘除。您将能够使用更大范围的值而不会丢失精度。

于 2013-08-20T09:59:08.347 回答