So I am trying to write a python function to return a metric called the Mielke-Berry R value. The metric is calculated like so:
The current code I have written works, b
Here's one vectorized way to leverage broadcasting to get total -
np.abs(forecasted_array[:,None] - observed_array).sum()
To accept both lists and arrays alike, we can use NumPy builtin for the outer subtraction, like so -
np.abs(np.subtract.outer(forecasted_array, observed_array)).sum()
We can also make use of numexpr module for faster absolute computations and perform summation-reductions in one single numexpr evaluate call and as such would be much more memory efficient, like so -
import numexpr as ne
forecasted_array2D = forecasted_array[:,None]
total = ne.evaluate('sum(abs(forecasted_array2D - observed_array))')