Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

330 consecutive imputation links #15

Merged
merged 5 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions src/cumulative_imputation_links.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import numpy as np


def get_cumulative_links(
dataframe,
forward_or_backward,
time_difference,
Jday7879 marked this conversation as resolved.
Show resolved Hide resolved
strata,
reference,
target,
period,
imputation_link,
):
"""
Create cumulative imputation links for multiple consecutive periods
without a return.

Parameters
----------
dataframe : pandas.DataFrame
forward_or_backward: str
either f or b for forward or backward method
time_difference : int
time difference between predictive and target period in months
strata : str
column name containing strata information (sic)
reference : str
column name containing business reference id
target : str
column name containing target variable
period : str
column name containing time period
imputation_link : string
column name containing imputation links

Returns
-------
pandas.DataFrame
dataframe with imputation_group and
cumulative_forward/backward_imputation_link column
"""

dataframe.sort_values([strata, reference, period], inplace=True)
dataframe["missing_value"] = np.where(dataframe[target].isnull(), True, False)

dataframe["imputation_group"] = (
(
(dataframe["missing_value"].diff(time_difference) != 0)
| (dataframe[strata].diff(time_difference) != 0)
| (dataframe[reference].diff(time_difference) != 0)
)
.astype("int")
.cumsum()
)

if forward_or_backward == "f":
dataframe["cumulative_" + imputation_link] = dataframe.groupby(
"imputation_group"
)[imputation_link].cumprod()
elif forward_or_backward == "b":
dataframe["cumulative_" + imputation_link] = (
dataframe[::-1].groupby("imputation_group")[imputation_link].cumprod()[::-1]
)

return dataframe[["imputation_group", "cumulative_" + imputation_link]]
7 changes: 7 additions & 0 deletions tests/cumulative_links.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
strata,reference,target,period,forward_imputation_link,backward_imputation_link,imputation_group,cumulative_forward_imputation_link,cumulative_backward_imputation_link
100,100000,200,202402,1,2,1,1,2
100,100000,,202403,2,0.6,2,2,0.6
100,100000,,202404,3,1,2,6,1
200,100001,,202402,1,4,3,1,2
200,100001,,202403,3,0.5,3,3,0.5
200,100001,300,202404,0.5,1,4,0.5,1
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might be misunderstanding the code / method, but why isn't cumulative_forward_imputation_link for the last row 1.5. Is that because it is in a different imputation group?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes that is correct. We only want to multiply the links where there are consecutive missing values. In fact, maybe we should turn all cumulative links in the same row as a return to nan because it has no meaning.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to confirm, the last row is just the same as the imputation_link column because it is a return not a missing value. I have made a change to turn the cumulative link columns to NaN if there is a return

64 changes: 64 additions & 0 deletions tests/test_cumulative_imputation_links.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
from pathlib import Path

import pytest
from helper_functions import load_and_format
from pandas.testing import assert_frame_equal

from src.cumulative_imputation_links import get_cumulative_links


@pytest.fixture(scope="class")
def cumulative_links_test_data():
return load_and_format(Path("tests") / "cumulative_links.csv")


class TestComulativeLinks:
def test_get_cumulative_links_forward(self, cumulative_links_test_data):
input_data = cumulative_links_test_data.drop(
columns=["cumulative_forward_imputation_link", "imputation_group"]
)

expected_output = cumulative_links_test_data[
[
"imputation_group",
"cumulative_forward_imputation_link",
]
]

actual_output = get_cumulative_links(
input_data,
"f",
1,
"strata",
"reference",
"target",
"period",
"forward_imputation_link",
)

assert_frame_equal(actual_output, expected_output)

def test_get_cumulative_links_backward(self, cumulative_links_test_data):
input_data = cumulative_links_test_data.drop(
columns=["cumulative_backward_imputation_link", "imputation_group"]
)

expected_output = cumulative_links_test_data[
[
"imputation_group",
"cumulative_backward_imputation_link",
]
]

actual_output = get_cumulative_links(
input_data,
"b",
1,
"strata",
"reference",
"target",
"period",
"backward_imputation_link",
)

assert_frame_equal(actual_output, expected_output)
Loading