Skip to content
Merged
Changes from 7 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
81 changes: 76 additions & 5 deletions xarray/core/computation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1281,7 +1281,11 @@ def apply_ufunc(


def cov(
da_a: T_DataArray, da_b: T_DataArray, dim: Dims = None, ddof: int = 1
da_a: T_DataArray,
da_b: T_DataArray,
dim: Dims = None,
ddof: int = 1,
weights: T_DataArray = None,
) -> T_DataArray:
"""
Compute covariance between two DataArray objects along a shared dimension.
Expand All @@ -1297,6 +1301,8 @@ def cov(
ddof : int, default: 1
If ddof=1, covariance is normalized by N-1, giving an unbiased estimate,
else normalization is by N.
weights : DataArray, default: None
Array of weights.

Returns
-------
Expand Down Expand Up @@ -1358,11 +1364,22 @@ def cov(
"Only xr.DataArray is supported."
f"Given {[type(arr) for arr in [da_a, da_b]]}."
)

return _cov_corr(da_a, da_b, dim=dim, ddof=ddof, method="cov")
if weights is not None:
if not isinstance(weights, DataArray):
raise TypeError("Only xr.DataArray is supported." f"Given {type(weights)}.")
return _weighted_cov_corr(
da_a, da_b, weights=weights, dim=dim, ddof=ddof, method="cov"
)
else:
return _cov_corr(da_a, da_b, dim=dim, ddof=ddof, method="cov")


def corr(da_a: T_DataArray, da_b: T_DataArray, dim: Dims = None) -> T_DataArray:
def corr(
da_a: T_DataArray,
da_b: T_DataArray,
dim: Dims = None,
weights: T_DataArray = None,
) -> T_DataArray:
"""
Compute the Pearson correlation coefficient between
two DataArray objects along a shared dimension.
Expand All @@ -1375,6 +1392,8 @@ def corr(da_a: T_DataArray, da_b: T_DataArray, dim: Dims = None) -> T_DataArray:
Array to compute.
dim : str, iterable of hashable, "..." or None, optional
The dimension along which the correlation will be computed
weights : DataArray, default: None
Array of weights.

Returns
-------
Expand Down Expand Up @@ -1437,7 +1456,12 @@ def corr(da_a: T_DataArray, da_b: T_DataArray, dim: Dims = None) -> T_DataArray:
f"Given {[type(arr) for arr in [da_a, da_b]]}."
)

return _cov_corr(da_a, da_b, dim=dim, method="corr")
if weights is not None:
if not isinstance(weights, DataArray):
raise TypeError("Only xr.DataArray is supported." f"Given {type(weights)}.")
return _weighted_cov_corr(da_a, da_b, weights=weights, dim=dim, method="corr")
else:
return _cov_corr(da_a, da_b, dim=dim, method="corr")


def _cov_corr(
Expand Down Expand Up @@ -1482,6 +1506,53 @@ def _cov_corr(
return corr


def _weighted_cov_corr(
da_a: T_DataArray,
da_b: T_DataArray,
weights: T_DataArray,
dim: Dims = None,
ddof: int = 0,
method: Literal["cov", "corr", None] = None,
) -> T_DataArray:
"""
Internal method for weighted xr.cov() and xr.corr(), extending
_cov_corr() functionality.
"""
# 1. Broadcast the two arrays
da_a, da_b = align(da_a, da_b, join="inner", copy=False)

# 2. Ignore the nans
valid_values = da_a.notnull() & da_b.notnull()
da_a = da_a.where(valid_values)
da_b = da_b.where(valid_values)

# 3. Detrend along the given dim
demeaned_da_a = da_a - da_a.weighted(weights).mean(dim=dim)
demeaned_da_b = da_b - da_b.weighted(weights).mean(dim=dim)

# 4. Compute covariance along the given dim
# N.B. `skipna=True` is required or auto-covariance is computed incorrectly. E.g.
# Try xr.cov(da,da) for da = xr.DataArray([[1, 2], [1, np.nan]], dims=["x", "time"])
cov = (
(demeaned_da_a.conj() * demeaned_da_b)
.weighted(weights)
.mean(dim=dim, skipna=True)
)

if method == "cov":
# Adjust covariance for degrees of freedom
valid_count = valid_values.sum(dim)
adjust = valid_count / (valid_count - ddof)
return cov * adjust

else:
# Compute std and corr
da_a_std = da_a.weighted(weights).std(dim=dim)
da_b_std = da_b.weighted(weights).std(dim=dim)
corr = cov / (da_a_std * da_b_std)
return corr


def cross(
a: DataArray | Variable, b: DataArray | Variable, *, dim: Hashable
) -> DataArray | Variable:
Expand Down