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

Add argument media_selected_times to ROI, CPIK, and marginal ROI analysis methods. #423

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
15 changes: 15 additions & 0 deletions meridian/analysis/analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2065,6 +2065,7 @@ def marginal_roi(
new_data: DataTensors | None = None,
selected_geos: Sequence[str] | None = None,
selected_times: Sequence[str] | None = None,
media_selected_times: Sequence[str] | Sequence[bool] | None = None,
aggregate_geos: bool = True,
aggregate_times: bool = True,
by_reach: bool = True,
Expand Down Expand Up @@ -2103,6 +2104,9 @@ def marginal_roi(
all geos are included.
selected_times: Optional. Contains a subset of times to include. By
default, all time periods are included.
media_selected_times: Optional list containing either a subset of dates to
include or booleans with length equal to the number of time periods in
`new_data`, if provided.
aggregate_geos: If `True`, the expected revenue is summed over all of the
regions.
aggregate_times: If `True`, the expected revenue is summed over all of
Expand Down Expand Up @@ -2136,6 +2140,7 @@ def marginal_roi(
"use_kpi": use_kpi,
"batch_size": batch_size,
"include_non_paid_channels": False,
"media_selected_times": media_selected_times,
}
# TODO: Switch from PerformanceTensors to DataTensors.
if new_data is None:
Expand Down Expand Up @@ -2211,6 +2216,7 @@ def roi(
new_data: DataTensors | None = None,
selected_geos: Sequence[str] | None = None,
selected_times: Sequence[str] | None = None,
media_selected_times: Sequence[str] | Sequence[bool] | None = None,
aggregate_geos: bool = True,
aggregate_times: bool = True,
use_kpi: bool = False,
Expand Down Expand Up @@ -2252,6 +2258,9 @@ def roi(
default, all geos are included.
selected_times: Optional list containing a subset of times to include. By
default, all time periods are included.
media_selected_times: Optional list containing either a subset of dates to
include or booleans with length equal to the number of time periods in
`new_data`, if provided.
aggregate_geos: Boolean. If `True`, the expected revenue is summed over
all of the regions.
aggregate_times: Boolean. If `True`, the expected revenue is summed over
Expand Down Expand Up @@ -2282,6 +2291,7 @@ def roi(
"use_kpi": use_kpi,
"batch_size": batch_size,
"include_non_paid_channels": False,
"media_selected_times": media_selected_times,
}
# TODO: Switch from PerformanceTensors to DataTensors.
if new_data is None:
Expand Down Expand Up @@ -2328,6 +2338,7 @@ def cpik(
new_data: DataTensors | None = None,
selected_geos: Sequence[str] | None = None,
selected_times: Sequence[str] | None = None,
media_selected_times: Sequence[str] | Sequence[bool] | None = None,
aggregate_geos: bool = True,
aggregate_times: bool = True,
batch_size: int = constants.DEFAULT_BATCH_SIZE,
Expand Down Expand Up @@ -2363,6 +2374,9 @@ def cpik(
default, all geos are included.
selected_times: Optional list containing a subset of times to include. By
default, all time periods are included.
media_selected_times: Optional list containing either a subset of dates to
include or booleans with length equal to the number of time periods in
`new_data`, if provided.
aggregate_geos: Boolean. If `True`, the expected KPI is summed over all of
the regions.
aggregate_times: Boolean. If `True`, the expected KPI is summed over all
Expand All @@ -2384,6 +2398,7 @@ def cpik(
new_data=new_data,
selected_geos=selected_geos,
selected_times=selected_times,
media_selected_times=media_selected_times,
aggregate_geos=aggregate_geos,
aggregate_times=aggregate_times,
batch_size=batch_size,
Expand Down
24 changes: 24 additions & 0 deletions meridian/analysis/analyzer_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,14 @@ def test_incremental_outcome_media_and_rf_new_params(self):
atol=1e-3,
)

def test_marginal_roi_media_selected_times_all_false_returns_zero(
self,
):
no_media_times = self.analyzer_media_and_rf.marginal_roi(
media_selected_times=[False] * _N_MEDIA_TIMES
)
self.assertAllEqual(no_media_times, tf.zeros_like(no_media_times))

@parameterized.product(
use_posterior=[False, True],
aggregate_geos=[False, True],
Expand Down Expand Up @@ -806,6 +814,14 @@ def test_roi_wrong_rf_spend_raises_exception(self):
),
)

def test_roi_media_selected_times_all_false_returns_zero(
self,
):
no_media_times = self.analyzer_media_and_rf.roi(
media_selected_times=[False] * _N_MEDIA_TIMES
)
self.assertAllEqual(no_media_times, tf.zeros_like(no_media_times))

@parameterized.product(
use_posterior=[False, True],
aggregate_geos=[False, True],
Expand Down Expand Up @@ -852,6 +868,14 @@ def test_roi_media_and_rf_default_returns_correct_value(self):
)
self.assertAllClose(expected_roi, roi)

def test_cpik_media_selected_times_all_false_returns_zero(
self,
):
no_media_times = self.analyzer_media_and_rf.cpik(
media_selected_times=[False] * _N_MEDIA_TIMES
)
self.assertAllEqual(no_media_times, tf.zeros_like(no_media_times))

@parameterized.product(
use_posterior=[False, True],
aggregate_geos=[False, True],
Expand Down
1 change: 1 addition & 0 deletions meridian/analysis/optimizer_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3326,6 +3326,7 @@ def test_incremental_outcome_called_correct_optimize(
# incremental_outcome() with the following arguments.
selected_geos=None,
selected_times=None,
media_selected_times=None,
aggregate_geos=True,
aggregate_times=True,
inverse_transform_outcome=True,
Expand Down
Loading