def reward_func(win_before, loss_before, tie_win_before, tie_loss_before,
                win_after, loss_after, tie_win_after, tie_loss_after):
    """
    Returns the reward according to: 
    Reward = 2 [ P(win | state after goal) - P(win| state before goal)] 
            + 1 [P(tie | state after goal) - P(tie | state before goal)]]

    Author: Jon Vik
    Updates by: Rasmus Säfvenberg
    
    Parameters
    ----------
    win_before : integer
        Amount of occurrences of winning in regulation before the goal.
    loss_before : integer
        Amount of occurrences of losing in regulation before the goal.
    tie_win_before : integer
        Amount of occurrences of winning in overtime before the goal.
    tie_loss_before : integer
        Amount of occurrences of losing in overtime before the goal.
    win_after : integer
        Amount of occurrences of winning in regulation after the goal.
    loss_after : integer
        Amount of occurrences of losing in regulation after the goal.
    tie_win_after : integer
        Amount of occurrences of winning in overtime after the goal.
    tie_loss_after : integer
        Amount of occurrences of losing in overtime after the goal.

    Returns
    -------
    reward : float
        The reward calculated by the aforementioned formula.

    """
    # Calculate the total number of occurences prior to the goal
    all_before = win_before + loss_before + tie_win_before + tie_loss_before
    
    # Calculate the total number of occurences after the goal
    all_after = win_after + loss_after + tie_win_after + tie_loss_after

    if all_before == 0:
        p_win_before = 0
        p_tie_loss_before = 0
    else:
        # Probability of winning before the goal is scored
        p_win_before = (win_before + tie_win_before)/all_before
        # Probability of losing in overtime before the goal is scored
        p_tie_loss_before = tie_loss_before/all_before
    
    if all_after == 0:
        p_win_after = 0
        p_tie_loss_after = 0
    else:
        # Probability of winning after the goal is scored
        p_win_after = (win_after + tie_win_after)/all_after
        # Probability of losing in overtime after the goal is scored
        p_tie_loss_after = tie_loss_after/all_after    
    
    # Calculate the reward
    reward = 2 * (p_win_after - p_win_before) + 1 * (p_tie_loss_after - p_tie_loss_before)
    return reward