def test_error_band_cost_range_equiv(default_cost_err):
    covering = datamodel.ErrorBandCost(
        bands=[
            datamodel.CostBand(
                error_range=(-2, 2),
                cost_function='constant',
                cost_function_parameters=datamodel.ConstantCost(
                    cost=2.0, aggregation='sum', net=True
                )
            ),
            datamodel.CostBand(
                error_range=(-np.inf, np.inf),
                cost_function='constant',
                cost_function_parameters=datamodel.ConstantCost(
                    cost=4.0, aggregation='sum', net=False
                )
            )
        ]
    )
    split = datamodel.ErrorBandCost(
        bands=[
            datamodel.CostBand(
                error_range=(-2, 2),
                cost_function='constant',
                cost_function_parameters=datamodel.ConstantCost(
                    cost=2.0, aggregation='sum', net=True
                )
            ),
            datamodel.CostBand(
                error_range=(-np.inf, -2),
                cost_function='constant',
                cost_function_parameters=datamodel.ConstantCost(
                    cost=4.0, aggregation='sum', net=False
                )
            ),
            datamodel.CostBand(
                error_range=(2, np.inf),
                cost_function='constant',
                cost_function_parameters=datamodel.ConstantCost(
                    cost=4.0, aggregation='sum', net=False
                )
            )
        ]
    )
    fx = default_cost_err
    obs = pd.Series(0, index=fx.index)
    err_cov = deterministic.error_band_cost(
        obs, fx, covering, deterministic.error)
    err_split = deterministic.error_band_cost(
        obs, fx, split, deterministic.error)
    assert err_cov == err_split
def test_error_band_cost_no_bands(default_cost_err):
    params = datamodel.ErrorBandCost(
        bands=tuple()
    )
    fx = default_cost_err
    obs = pd.Series(0, index=fx.index)
    err = deterministic.error_band_cost(
        obs, fx, params, deterministic.error)
    assert err == 0
def test_error_band_cost(banded_cost_params):
    ind = pd.date_range(start='2020-05-01T00:00Z', freq='1h', periods=8)
    obs = pd.Series([1, 6, 1, 2, 2.00, 2.0, 0, 4], index=ind)
    fx = pd.Series([1., 2, 3, 0, -0.1, 1.9, 3, 5], index=ind)
    err = deterministic.error_band_cost(
        obs, fx, banded_cost_params.parameters, deterministic.error)
    exp = ((0 + 0 + 2 + -2 + 0.0 + -0.1 + 0 + 1) +
           (0 + 0 + 0 + 0. + 0.0 + 0.00 + 3 * 0.9 + 0) +
           (0 + -4 * -0.2 + 0 + 0. + -2.1 * -0.2 + 0.00 + 0 + 0))
    assert err == exp
def test_error_band_cost_out_of_range(default_cost_err):
    params = datamodel.ErrorBandCost(
        bands=[datamodel.CostBand(
            error_range=(10, 100),
            cost_function='constant',
            cost_function_parameters=datamodel.ConstantCost(
                cost=2.0, aggregation='sum', net=True
            )
        )]
    )
    fx = default_cost_err
    obs = pd.Series(0, index=fx.index)
    err = deterministic.error_band_cost(
        obs, fx, params, deterministic.error)
    assert err == 0
def test_error_band_cost_out_of_times(default_cost_err):
    params = datamodel.ErrorBandCost(
        bands=[datamodel.CostBand(
            error_range=(-10, 1),
            cost_function='datetime',
            cost_function_parameters=datamodel.DatetimeCost(
                datetimes=[pd.Timestamp('2020-05-01T02:00-07:00')],
                cost=[1.0],
                aggregation='sum',
                fill='forward',
                net=True
            )
        )]
    )
    fx = default_cost_err
    obs = pd.Series(0, index=fx.index)
    err = deterministic.error_band_cost(
        obs, fx, params, deterministic.error)
    assert err == 0