Exemplo n.º 1
0
def mean_bias_removal(hindcast,
                      alignment,
                      cross_validate=True,
                      **metric_kwargs):
    """Calc and remove bias from py:class:`~climpred.classes.HindcastEnsemble`.

    Args:
        hindcast (HindcastEnsemble): hindcast.
        alignment (str): which inits or verification times should be aligned?
            - maximize/None: maximize the degrees of freedom by slicing ``hind`` and
            ``verif`` to a common time frame at each lead.
            - same_inits: slice to a common init frame prior to computing
            metric. This philosophy follows the thought that each lead should be
            based on the same set of initializations.
            - same_verif: slice to a common/consistent verification time frame prior
            to computing metric. This philosophy follows the thought that each lead
            should be based on the same set of verification dates.
        cross_validate (bool): Use properly defined mean bias removal function. This
            excludes the given initialization from the bias calculation. With False,
            include the given initialization in the calculation, which is much faster
            but yields similar skill with a large N of initializations.
            Defaults to True.

    Returns:
        HindcastEnsemble: bias removed hindcast.

    """
    def bias_func(a, b, **kwargs):
        return a - b

    bias_metric = Metric("bias", bias_func, True, False, 1)

    bias = hindcast.verify(
        metric=bias_metric,
        comparison="e2o",
        dim="init",
        alignment=alignment,
        **metric_kwargs,
    ).squeeze()

    if cross_validate:
        mean_bias_func = _mean_bias_removal_cross_validate
    else:
        mean_bias_func = _mean_bias_removal_quick

    bias_removed_hind = mean_bias_func(hindcast._datasets["initialized"], bias,
                                       "init")
    bias_removed_hind = bias_removed_hind.squeeze()
    if "dayofyear" in bias_removed_hind.coords:
        del bias_removed_hind["dayofyear"]
    hindcast_bias_removed = hindcast.copy()
    hindcast_bias_removed._datasets["initialized"] = bias_removed_hind
    return hindcast_bias_removed
Exemplo n.º 2
0
from climpred.bootstrap import bootstrap_perfect_model
from climpred.comparisons import PM_COMPARISONS
from climpred.metrics import __ALL_METRICS__ as all_metrics, Metric, __pearson_r
from climpred.prediction import compute_hindcast, compute_perfect_model


def my_mse_function(forecast, reference, dim=None, **metric_kwargs):
    return ((forecast - reference)**2).mean(dim)


my_mse = Metric(
    name='mse',
    function=my_mse_function,
    positive=True,
    probabilistic=False,
    unit_power=2,
    long_name='MSE',
    aliases=['mSe', '<<<SE'],
)

ITERATIONS = 2


@pytest.mark.parametrize('comparison', PM_COMPARISONS)
def test_custom_metric_passed_to_compute(PM_da_initialized_1d,
                                         PM_da_control_1d, comparison):
    """Test custom metric in compute_perfect_model."""
    actual = compute_perfect_model(
        PM_da_initialized_1d,
        PM_da_control_1d,
Exemplo n.º 3
0
from climpred.comparisons import PM_COMPARISONS
from climpred.metrics import __ALL_METRICS__ as all_metrics, Metric, __pearson_r
from climpred.stats import rm_poly


def my_mse_function(forecast, verif, dim=None, **metric_kwargs):
    # function
    return ((forecast - verif)**2).mean(dim)


my_mse = Metric(
    name="mse",
    function=my_mse_function,
    positive=True,
    probabilistic=False,
    unit_power=2,
    long_name="MSE",
    aliases=["mSe", "<<<SE"],
)

ITERATIONS = 2


@pytest.mark.parametrize("comparison", PM_COMPARISONS)
def test_custom_metric_passed_to_verify(
        perfectModelEnsemble_initialized_control, comparison):
    """Test custom metric in PerfectModelEnsemble.verify()."""
    kwargs = dict(comparison=comparison, dim="init")
    actual = perfectModelEnsemble_initialized_control.verify(metric=my_mse,
                                                             **kwargs)