Compute $\frac{\sum_{i=1}^nx_iy_i}{\sum_{i=1}^nx_i}$ in logarithmic space

44 Views Asked by At

Let $x,y\in(0,\infty)^n$. Is there a clever way to compute $$\frac{\sum_{i=1}^nx_iy_i}{\sum_{i=1}^nx_i}\tag1$$ by calculating $\ln\frac{\sum_{i=1}^nx_iy_i}{\sum_{i=1}^nx_i}$ instead? My problem is that I need to build $(1)$ in a computer program and suffer from floint-point imprecision. Maybe we can use that $$\ln(x+y)=x+\ln\left(1+e^{\ln y-\ln x})\right)\;\;\;\text{for all }x,y>0.\tag2$$

1

There are 1 best solutions below

2
On

I don't think working with a logarithmic scale will do any good.

Let us concentrate on the (weighted) arithmetical mean you want to compute.

In presence of several order of magnitudes among the $x_i$, I advise you to adopt an "incremental" strategy (successive adjustments) :

1) Make "clusters" of $x_i$ by decreasing order of magnitude (for each cluster, give as compound weight the sum of the weights of its constituants)

2) For each cluster, compute its mean (with compound weights),

3) Make a compound mean of all these means with the compound weights.