def mix_targets(preds, targets, target_name1, target_name2, weight1=0.5,
                weight2=0.5, skip_missing=False, new_name=None):
    """
    Linearly combines two targets into one, optionally with custom weights,
    storing it under the name of the first one, unless `new_name` is given.
    `weight1` and `weight2` can also be keys into `targets`.
    If `skip_missing` is true-ish, then if the second target is not present,
    just uses the first one without weighting.
    """
    target1 = targets[target_name1]
    try:
        target2 = targets[target_name2]
    except KeyError:
        if skip_missing:
            output = target1
        else:
            raise
    else:
        if isinstance(weight1, str):
            weight1 = targets[weight1]
        if isinstance(weight2, str):
            weight2 = targets[weight2]
        output = weight1 * target1 + weight2 * target2
    if new_name is None:
        new_name = target_name1
    targets[new_name] = output
    return 0