MCPcopy Index your code
hub / github.com/pydata/xarray / test_bilinear_cov_corr

Function test_bilinear_cov_corr

xarray/tests/test_computation.py:1778–1838  ·  view source on GitHub ↗
(weighted: bool)

Source from the content-addressed store, hash-verified

1776
1777@pytest.mark.parametrize("weighted", [True, False])
1778def test_bilinear_cov_corr(weighted: bool) -> None:
1779 # Test the bilinear properties of covariance and correlation
1780 da = xr.DataArray(
1781 np.random.random((3, 21, 4)),
1782 coords={"time": pd.date_range("2000-01-01", freq="1D", periods=21)},
1783 dims=("a", "time", "x"),
1784 )
1785 db = xr.DataArray(
1786 np.random.random((3, 21, 4)),
1787 coords={"time": pd.date_range("2000-01-01", freq="1D", periods=21)},
1788 dims=("a", "time", "x"),
1789 )
1790 dc = xr.DataArray(
1791 np.random.random((3, 21, 4)),
1792 coords={"time": pd.date_range("2000-01-01", freq="1D", periods=21)},
1793 dims=("a", "time", "x"),
1794 )
1795 if weighted:
1796 weights = xr.DataArray(
1797 np.abs(np.random.random(4)),
1798 dims=("x"),
1799 )
1800 else:
1801 weights = None
1802 k = np.random.random(1)[0]
1803
1804 # Test covariance properties
1805 assert_allclose(
1806 xr.cov(da + k, db, weights=weights), xr.cov(da, db, weights=weights)
1807 )
1808 assert_allclose(
1809 xr.cov(da, db + k, weights=weights), xr.cov(da, db, weights=weights)
1810 )
1811 assert_allclose(
1812 xr.cov(da + dc, db, weights=weights),
1813 xr.cov(da, db, weights=weights) + xr.cov(dc, db, weights=weights),
1814 )
1815 assert_allclose(
1816 xr.cov(da, db + dc, weights=weights),
1817 xr.cov(da, db, weights=weights) + xr.cov(da, dc, weights=weights),
1818 )
1819 assert_allclose(
1820 xr.cov(k * da, db, weights=weights), k * xr.cov(da, db, weights=weights)
1821 )
1822 assert_allclose(
1823 xr.cov(da, k * db, weights=weights), k * xr.cov(da, db, weights=weights)
1824 )
1825
1826 # Test correlation properties
1827 assert_allclose(
1828 xr.corr(da + k, db, weights=weights), xr.corr(da, db, weights=weights)
1829 )
1830 assert_allclose(
1831 xr.corr(da, db + k, weights=weights), xr.corr(da, db, weights=weights)
1832 )
1833 assert_allclose(
1834 xr.corr(k * da, db, weights=weights), xr.corr(da, db, weights=weights)
1835 )

Callers

nothing calls this directly

Calls 3

assert_allcloseFunction · 0.90
covMethod · 0.80
corrMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…