Exemplo n.º 1
0
def _predictor(truths: List[Truth], preds: List[Prediction], loss_fn, fill_fn,
               merging_fn, update_fn):
    output = []
    epiweeks = preds[0].coords["epiweek"]

    # Extract init_weights from update_fn
    init_weights = signature(update_fn).parameters["init_weights"].default
    weights_history = []

    for idx, ew in enumerate(epiweeks):
        if idx == 0:
            if init_weights is None:
                weights = uniform_weights(
                    [pred.attrs["model"] for pred in preds], ones=False)
            else:
                weights = init_weights
        else:
            truth = merging_fn(fill_fn(mask_truths(truths, ew)))
            losses = [loss_fn(pred.loc[:(ew - 1)], truth) for pred in preds]
            weights = update_fn(losses)
            weights = weights / weights.sum()

        weights_history.append(weights)

        output.append(
            weighted_prediction([pred.loc[ew] for pred in preds], weights))

    return xr.concat(output, dim="epiweek"), weights_history
Exemplo n.º 2
0
def noop(losses: List[Loss], init_weights=None) -> Weight:
    """
    Do nothing.
    """

    models = [loss.attrs["model"] for loss in losses]
    if init_weights is None:
        return uniform_weights(models, ones=False)
    else:
        return init_weights
Exemplo n.º 3
0
def hedge(losses: List[Loss], eta: float, init_weights=None) -> Weight:
    r"""
    Exponential weight update. :math:`w_i(t + 1) = w_i(t) e^{- \eta m_i(t)}`
    """

    models = [loss.attrs["model"] for loss in losses]

    if init_weights is None:
        init_weights = uniform_weights(models)

    updates = [np.exp(-eta * np.sum(loss)) for loss in losses]

    return init_weights * updates
Exemplo n.º 4
0
def mw(losses: List[Loss], eta: float, init_weights=None) -> Weight:
    r"""
    Multiplicative weight update. :math:`w_i(t + 1) = w_i(t) (1 - \eta m_i(t))`
    """

    models = [loss.attrs["model"] for loss in losses]

    if init_weights is None:
        init_weights = uniform_weights(models)

    updates = [np.prod(1 - eta * loss) for loss in losses]

    return init_weights * updates
Exemplo n.º 5
0
def fixed_share(losses: List[Loss], eta: float, alpha: float, init_weights=None) -> Weight:
    r"""
    Fixed share update.
    """

    models = [loss.attrs["model"] for loss in losses]
    M = len(models)
    T = len(losses[0])

    if init_weights is None:
        weights = uniform_weights(models, ones=False)
    else:
        weights = init_weights

    # Vectorize this
    for t in range(T):
        vs = weights * np.exp([-eta * loss[t] for loss in losses])
        weights = (alpha * np.sum(vs) / M) + (1 - alpha) * vs

    return weights