2. Normal–Cauchy Example#
Professor Vidakovic already has a Python implementation of this available: norcau.py. I’ve cleaned it up a bit below to remove some deprecation warnings and unnecessary repetition of some calculations.
from scipy.integrate import quad
from numpy import exp, inf
x = 2
num = lambda th: th * exp(-0.5 * (x - th) ** 2) / (1 + th**2)
denom = lambda th: exp(-0.5 * (x - th) ** 2) / (1 + th**2)
# quadrature function returns integral and absolute error
numerator, num_err = quad(num, -inf, inf)
denominator, denom_err = quad(denom, -inf, inf)
delta2 = numerator / denominator
print(f"δ(2) = {delta2}")
# computation of relative error: ratio of the absolute error to the value of the integral itself.
relative_error = delta2 * ((num_err / numerator) + (denom_err / denominator))
print(f"Relative error: {relative_error}")
δ(2) = 1.2821951026935339
Relative error: 2.3036713373086032e-08
Assuming the errors for each individual integral calculation are uncorrelated, the error for \(f\) should be given by:
In the code above, \(f\) is delta2
, \(a\) is numerator
, and \(b\) is denominator
. \(\Delta_a\) and \(\Delta_b\) are num_err
and denom_err
, respectively.
However, students have pointed out that instead of using the square root of the sum of squares, the professor used the simpler form:
It was probably just a simplification under the assumption that the errors in the numerator and the denominator are small and the terms \((\Delta_a / a)^2\) and \((\Delta_b / b)^2\) are negligible.