Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions scoringrules/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,12 +398,7 @@ def uv_weighted_score_chain(
obs, fct, a=float("-inf"), b=float("inf"), v_func=None, *, xp
):
if v_func is None:
a, b, obs, fct = (
xp.asarray(a),
xp.asarray(b),
xp.asarray(obs),
xp.asarray(fct),
)
a, b, obs, fct = asarray_matching(a, b, obs, fct, xp=xp)

def v_func(x):
return xp.minimum(xp.maximum(x, a), b)
Expand Down
28 changes: 28 additions & 0 deletions tests/test_wcrps.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,34 @@ def assert_close(result, expected, backend, *, rtol=1e-5, atol=1e-8):
), f"[{backend}] {arr} != {expected}"


@pytest.mark.parametrize("dtype", [np.float32, np.float64])
def test_twcrps_ensemble_preserves_numpy_dtype(dtype):
obs = np.array([0.25, 0.75], dtype=dtype)
fct = np.array(
[[0.0, 0.5, 1.0], [0.5, 0.8, 1.2]],
dtype=dtype,
)

result = sr.twcrps_ensemble(obs, fct, a=-0.1, b=0.9)

assert result.dtype == np.dtype(dtype)


def test_twcrps_ensemble_preserves_torch_bound_precision():
torch = pytest.importorskip("torch")

obs = torch.tensor([0.0], dtype=torch.float64)
fct = torch.tensor([[0.0, 2.0]], dtype=torch.float64)
upper_bound = 1.0 + 1e-8

result = sr.twcrps_ensemble(obs, fct, b=upper_bound)

expected = torch.tensor([upper_bound / 4], dtype=torch.float64)

assert result.dtype == torch.float64
torch.testing.assert_close(result, expected, rtol=0, atol=1e-12)


def test_owcrps_ensemble(backend, to_backend):
# test shapes
obs = to_backend(np.random.randn(N))
Expand Down
Loading