pythonnumpydivide-by-zerouncertainty

How to fix ZeroDivisionError while using uncertainties package?


I have two unumpy arrays:

A= [390.9999999999952+/-19.77371993328507
 129.99999999999932+/-11.40175425099135
 34.99999999999997+/-5.9160797830996135
 4.999999999999999+/-2.2360679774997894 0.0+/-0 0.0+/-0
 4.999999999999999+/-2.2360679774997894]
B= [33.999999999999964+/-5.830951894845297
 17.33333333333334+/-4.163331998932266
 9.666666666666666+/-3.1091263510296048
 4.999999999999999+/-2.2360679774997894 0.0+/-nan 0.0+/-nan
 4.999999999999999+/-2.2360679774997894]

I would like to propagate their errors while calculating the error in the mean ratio through:

fraction = np.where(unumpy.nominal_values(A) > 0, unumpy.std_devs(B/A), np.nan)

But I do not know how to fix the following error I am receiving:

Traceback (most recent call last):
  File "my_code4.py", line 2076, in <module>
    fraction =            np.where(unumpy.nominal_values(A) > 0, unumpy.std_devs(B/A), np.nan)
  File "/home/username/anaconda3/lib/python3.6/site-packages/uncertainties/core.py", line 661, in f_with_affine_output
    f_nominal_value = f(*args_values, **kwargs)
ZeroDivisionError: float division by zero

Solution

  • I resolved the issue by the following changes in the line above: What I did was basically propagating the error through the division of two values where errors in the two parameters are added in quadrature from first principle.

    fraction = np.where(unumpy.nominal_values(B) * unumpy.nominal_values(A) > 0, (unumpy.nominal_values(B)/unumpy.nominal_values(A)) * np.sqrt(np.sum(np.square([unumpy.std_devs(B)/unumpy.nominal_values(B), unumpy.std_devs(A)/unumpy.nominal_values(A)]), axis=0)), 0.)