| 1756 | @pytest.mark.parametrize("n", range(5)) |
| 1757 | @pytest.mark.parametrize("dim", [None, "time", "x", ["time", "x"]]) |
| 1758 | def test_autocov(n: int, dim: str | None, arrays) -> None: |
| 1759 | da = arrays[n] |
| 1760 | |
| 1761 | # Testing that the autocovariance*(N-1) is ~=~ to the variance matrix |
| 1762 | # 1. Ignore the nans |
| 1763 | valid_values = da.notnull() |
| 1764 | # Because we're using ddof=1, this requires > 1 value in each sample |
| 1765 | da = da.where(valid_values.sum(dim=dim) > 1) |
| 1766 | expected = ((da - da.mean(dim=dim)) ** 2).sum(dim=dim, skipna=True, min_count=1) |
| 1767 | actual = xr.cov(da, da, dim=dim) * (valid_values.sum(dim) - 1) |
| 1768 | assert_allclose(actual, expected) |
| 1769 | |
| 1770 | |
| 1771 | def test_complex_cov() -> None: |