I’m currently using a Python library called “scoringrules” to calculate energy and variogram scores for multivariate forecasts. It serves similar purposes as the scoringRules library in R.
I ran into an array shape problem even though my arrays both have shapes (1, 24).
So I went back to the github page for a test code and I’m facing a similar issue with the test code provided by the original author. The code was run as provided on the github page.
import numpy as np
import pytest
from scoringrules._energy import energy_score
from scoringrules.backend import backends
# Define the backends you want to test
RUN_TESTS = ["numba", "jax"]
# Filter available backends based on RUN_TESTS
BACKENDS = [b for b in backends.available_backends if b in
RUN_TESTS]
# Register the filtered backends
for backend in RUN_TESTS:
backends.register_backend(backend)
ENSEMBLE_SIZE = 51
N = 100
N_VARS = 3
@pytest.mark.parametrize("backend", BACKENDS)
def test_energy_score(backend):
obs = np.random.randn(N, N_VARS)
fct = np.expand_dims(obs, axis=-2) + np.random.randn(N,
ENSEMBLE_SIZE, N_VARS)
res = energy_score(obs, fct, backend=backend)
if backend in ["numpy", "numba"]:
assert isinstance(res, np.ndarray)
elif backend == "jax":
assert isinstance(res, jax.Array)
x = test_energy_score(backend)
print(x)
print(x.shape)
The test code from the github (https://github.com/frazane/scoringrules/blob/main/tests/test_energy.py) returned this :
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[12], line 32
29 elif backend == "jax":
30 assert isinstance(res, jax.Array)
---> 32 x = test_energy_score(backend)
33 print(x)
34 print(x.shape)
Cell In[12], line 25, in test_energy_score(backend)
22 obs = np.random.randn(N, N_VARS)
23 fct = np.expand_dims(obs, axis=-2) + np.random.randn(N, ENSEMBLE_SIZE, N_VARS)
---> 25 res = energy_score(obs, fct, backend=backend)
27 if backend in ["numpy", "numba"]:
28 assert isinstance(res, np.ndarray)
File ~anaconda3Libsite-packagesscoringrules_energy.py:52, in energy_score(forecasts, observations, m_axis, v_axis, backend)
20 r"""Compute the Energy Score for a finite multivariate ensemble.
21
22 The Energy Score is a multivariate scoring rule expressed as
(...)
49 The computed Energy Score.
50 """
51 backend = backend if backend is not None else backends._active
---> 52 forecasts, observations = multivariate_array_check(
53 forecasts, observations, m_axis, v_axis, backend=backend
54 )
56 if backend == "numba":
57 return energy._energy_score_gufunc(forecasts, observations)
File ~anaconda3Libsite-packagesscoringrulescoreutils.py:31, in multivariate_array_check(fcts, obs, m_axis, v_axis, backend)
29 m_axis = m_axis if m_axis >= 0 else fcts.ndim + m_axis
30 v_axis = v_axis if v_axis >= 0 else fcts.ndim + v_axis
---> 31 _multivariate_shape_compatibility(fcts, obs, m_axis)
32 return _multivariate_shape_permute(fcts, obs, m_axis, v_axis, backend=backend)
File ~anaconda3Libsite-packagesscoringrulescoreutils.py:12, in _multivariate_shape_compatibility(fcts, obs, m_axis)
10 o_shape_broadcast = o_shape[:m_axis] + (f_shape[m_axis],) + o_shape[m_axis:]
11 if o_shape_broadcast != f_shape:
---> 12 raise ValueError(
13 f"Forecasts shape {f_shape} and observations shape {o_shape} are not compatible for broadcasting!"
14 )
ValueError: Forecasts shape (100, 3) and observations shape (100, 51, 3) are not compatible for broadcasting!
Tried to : Test a code for calculating energy score of multivariate forecasts. Forecast array was of shape (100,3) and observation array of shape (100, 51, 3). Several other tries were made where forecast and observation arrays were of the same shape but in vain.
Expected result : Energy score array of shape (100, 3)
Alessandro Fabiani Bigaunah is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.