(weighted: bool)
| 1776 | |
| 1777 | @pytest.mark.parametrize("weighted", [True, False]) |
| 1778 | def 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 | ) |
nothing calls this directly
no test coverage detected
searching dependent graphs…