• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nci / scores / 11847606997

15 Nov 2024 12:01AM UTC coverage: 100.0%. Remained the same
11847606997

Pull #744

github

web-flow
Merge da1a22bdd into 7a0d42be3
Pull Request #744: Update release notes

278 of 278 branches covered (100.0%)

Branch coverage included in aggregate %.

1928 of 1928 relevant lines covered (100.0%)

4.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/scores/continuous/correlation/correlation_impl.py
1
"""
2
This module contains functions to calculate the correlation.
3
"""
4

5
from typing import Optional
4✔
6

7
import xarray as xr
4✔
8

9
import scores.utils
4✔
10
from scores.typing import FlexibleDimensionTypes
4✔
11

12

13
def pearsonr(
4✔
14
    fcst: xr.DataArray,
15
    obs: xr.DataArray,
16
    *,  # Force keywords arguments to be keyword-only
17
    reduce_dims: Optional[FlexibleDimensionTypes] = None,
18
    preserve_dims: Optional[FlexibleDimensionTypes] = None,
19
) -> xr.DataArray:
20
    """
21
    Calculates the Pearson's correlation coefficient between two xarray DataArrays
22

23
    .. math::
24
        \\rho = \\frac{\\sum_{i=1}^{n}{(x_i - \\bar{x})(y_i - \\bar{y})}}
25
        {\\sqrt{\\sum_{i=1}^{n}{(x_i-\\bar{x})^2}\\sum_{i=1}^{n}{(y_i - \\bar{y})^2}}}
26

27
    where:
28
        - :math:`\\rho` = Pearson's correlation coefficient
29
        - :math:`x_i` = the values of x in a sample (i.e. forecast values)
30
        - :math:`\\bar{x}` = the mean value of the forecast sample
31
        - :math:`y_i` = the values of y in a sample (i.e. observed values)
32
        - :math:`\\bar{y}` = the mean value of the observed sample value
33

34
    Args:
35
        fcst: Forecast or predicted variables
36
        obs: Observed variables.
37
        reduce_dims: Optionally specify which dimensions to reduce when
38
            calculating the Pearson's correlation coefficient.
39
            All other dimensions will be preserved.
40
        preserve_dims: Optionally specify which dimensions to preserve when
41
            calculating the Pearson's correlation coefficient. All other dimensions will
42
            be reduced. As a special case, 'all' will allow all dimensions to be
43
            preserved. In this case, the result will be in the same shape/dimensionality
44
            as the forecast, and the errors will be the absolute error at each
45
            point (i.e. single-value comparison against observed), and the
46
            forecast and observed dimensions must match precisely.
47
    Returns:
48
        xr.DataArray: An xarray object with Pearson's correlation coefficient values
49

50
    Note:
51
        This function isn't set up to take weights.
52

53
    Reference:
54
        https://en.wikipedia.org/wiki/Pearson_correlation_coefficient
55
    """
56
    reduce_dims = scores.utils.gather_dimensions(
4✔
57
        fcst.dims, obs.dims, reduce_dims=reduce_dims, preserve_dims=preserve_dims
58
    )
59

60
    return xr.corr(fcst, obs, reduce_dims)
4✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc