def subtract_shuffled_null_model_from_panel(panel, shift_null_model=False):
    """Standardize a panel dataset by subtracting the expected value in a null
    model.

    Given a panel dataset of export values, with items as countries,
    time as major_axis, and products as minor_axis, create a new panel in which
    an expected value in a null model is subtracted from the data. The null
    model assumes that a country's exports are allocated to different products
    in the same proportions as those products' total exports are compared to
    the total exports of all products.

    Parameters
    ----------
    panel : pandas Panel
        A panel dataset with `items` being the names of different trajectories
        (people, countries, etc.), time as `major_axis`, and features as
        `minor_axis`.

    shift_null_model : bool, optional, default: False
        Whether to shift the null model by one time step so that data is not
        normalized by data that depends on itself.

    Returns
    -------
    panel_normalized_null_model : pandas Panel
        A normalized panel in which an expected value is subtracted from each
        entry in `panel`. The new normalized panel is essentially
            `panel - (panel.sum(axis='minor_axis') * panel.sum('items') /
                      panel.sum('items').sum(axis=1)).
    """
    panel_normalized_null_model = panel.copy()

    sum_across_items = panel.sum('items')
    sum_across_items_and_features = panel.sum('items').sum(axis=1)
    share_of_each_feature = (
        sum_across_items.div(
            sum_across_items_and_features, axis='index')
        .shift(int(shift_null_model)))

    for item in panel.items:
        sum_across_features = panel.loc[item].sum(axis=1)

        expected = (
            share_of_each_feature).mul(
                sum_across_features, axis='index')

        panel_normalized_null_model.loc[item] -= expected
    return panel_normalized_null_model