def _weighted_pearson(y, y_pred, w):
    """Calculate the weighted Pearson correlation coefficient."""
    with np.errstate(divide='ignore', invalid='ignore'):
        y_pred_demean = y_pred - np.average(y_pred, weights=w)
        y_demean = y - np.average(y, weights=w)
        corr = ((np.sum(w * y_pred_demean * y_demean) / np.sum(w)) /
                np.sqrt((np.sum(w * y_pred_demean ** 2) *
                         np.sum(w * y_demean ** 2)) /
                        (np.sum(w) ** 2)))
    if np.isfinite(corr):
        return np.abs(corr)
    return 0.
def _weighted_pearson ( y , y_pred , w ) : with np . errstate ( divide = 'ignore' , invalid = 'ignore' ) : y_pred_demean = y_pred - np . average ( y_pred , weights = w ) y_demean = y - np . average ( y , weights = w ) corr = ( ( np . sum ( w * y_pred_demean * y_demean ) / np . sum ( w ) ) / np . sqrt ( ( np . sum ( w * y_pred_demean ** 2 ) * np . sum ( w * y_demean ** 2 ) ) / ( np . sum ( w ) ** 2 ) ) ) if np . isfinite ( corr ) : return np . abs ( corr ) return 0.
Calculate the weighted Pearson correlation coefficient.