Prevent numerical precision error when calculating $\frac1{|Y|}\sum_{y\in Y}\left|f(y)-q(y)\right|$ in a clever way

35 Views Asked by At

Let $Y\subseteq[0,1)$ be a nonempty set and $f,q:[0,1)\to[0,1)$. I want to compute $$A:=\frac1{|Y|}\sum_{y\in Y}\left|f(y)-q(y)\right|$$ in a computer program.

Now, the problem is that $|Y|$ is large and hence the computation of $(1)$ is slow.

However, given $\varepsilon>0$, there is a small $Y_\varepsilon\subseteq Y$ such that $$f(y)<\varepsilon\;\;\;\text{for all }y\in Y_\varepsilon^c\tag1.$$ Moreover, you can assume that it is easy to compute $Y_\varepsilon$ and I already know $$I:=\frac1{|Y|}\sum_{y\in Y}q(y)$$ and hence I don't need to compute it.

If $$A_\varepsilon:=\frac1{|Y|}\sum_{y\in Y_\varepsilon}\left(\left|f(y)-q(y)\right|-q(y)\right)+I,$$ we see that $$\left|A_\varepsilon-A\right|<\varepsilon\frac{|Y_\varepsilon^c|}{|Y|}\tag2.$$

However, if I implement the computation of $A_\varepsilon$ (in C++) literally, then I've observed that I encounter $$\left|A_\varepsilon-A\right|\ge\varepsilon\frac{|Y_\varepsilon^c|}{|Y|}\tag3.$$ This is most probably to a numerical precision error in the summation. Can I do this in a clever way to reduce the error as much as possible?

EDIT: I've asked for the implementation part of this question on StackOverflow as well (but it's slightly different; I didn't want to make things too complicated there): https://stackoverflow.com/q/76136651/547231.