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

Improved data to show differences in model performance #315

Merged
merged 1 commit into from
Jan 10, 2025
Merged
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
18 changes: 10 additions & 8 deletions examples/ih/Example_IH_Analysis.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {
"nbsphinx": "hidden"
},
Expand All @@ -31,7 +31,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -64,14 +64,14 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# ih = IH.from_ds_export(\n",
"# \"../../data/Data-pxStrategyResult_pxInteractionHistory_20210101T010000_GMT.zip\"\n",
"# )\n",
"ih = IH.from_mock_data()"
"ih = IH.from_mock_data(n=1000000)"
]
},
{
Expand Down Expand Up @@ -131,10 +131,11 @@
"outputs": [],
"source": [
"fig = ih.plot.action_distribution(\n",
" query=pl.col.Outcome.is_in([\"Clicked\", \"Accepted\"]), facet=\"Channel\",\n",
" title=\"Distribution of Clicked or Accepted actions\"\n",
" query=pl.col.Outcome.is_in([\"Clicked\", \"Accepted\"]), \n",
" title=\"Distribution of Actions\",\n",
" color=\"Outcome\",\n",
")\n",
"fig.update_layout(yaxis=dict(tickmode=\"linear\"))\n",
"# fig.update_layout(yaxis=dict(tickmode=\"linear\")) # to show all names\n",
"fig"
]
},
Expand Down Expand Up @@ -370,7 +371,8 @@
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3"
"pygments_lexer": "ipython3",
"version": "3.12.3"
}
},
"nbformat": 4,
Expand Down
49 changes: 32 additions & 17 deletions python/pdstools/ih/IH.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,17 @@ def from_mock_data(cls, days=90, n=100000):
The properly initialized IH object
"""
n_actions = 10
click_rate = 0.2
accept_rate = 0.15
click_avg_duration_minutes = 2
accept_avg_duration_minutes = 30
convert_over_accept_click_rate_test = 0.5
convert_over_accept_click_rate_control = 0.3
convert_avg_duration_days = 2
inbound_base_propensity = 0.02
outbound_base_propensity = 0.01
inbound_modelnoise_NaiveBayes = 0.2 # relative amount of extra noise added to models
inbound_modelnoise_GradientBoost = 0.0
outbound_modelnoise_NaiveBayes = 0.3
outbound_modelnoise_GradientBoost = 0.1

now = datetime.datetime.now()

Expand All @@ -119,7 +121,9 @@ def thompson_sampler(propensity, responses=10000):
ih_fake_impressions = pl.DataFrame(
{
"pxInteractionID": [str(int(1e9 + i)) for i in range(n)],
"pyChannel": random.choices(["Web", "Email"], k=n),
"pyChannel": random.choices(
["Web", "Email"], k=n
), # Direction will be derived from this later
"pyIssue": random.choices(
["Acquisition", "Retention", "Risk", "Service"], k=n
),
Expand Down Expand Up @@ -170,6 +174,7 @@ def thompson_sampler(propensity, responses=10000):
"Temp.ConvertDurationDays": [
random.uniform(0, 2 * convert_avg_duration_days) for i in range(n)
],
"Temp.RandomUniform": [random.uniform(0, 1) for i in range(n)],
}
).with_columns(
pyDirection=pl.when(pl.col("pyChannel") == "Web")
Expand Down Expand Up @@ -206,20 +211,16 @@ def thompson_sampler(propensity, responses=10000):
pl.col("Temp.Zipf")
.mean()
.over(["pyChannel", "pyDirection"])
.alias("Temp.ZipfMean")
.alias("Temp.ZipfMean"),
pl.when(pl.col("pyDirection") == "Inbound")
.then(pl.lit(inbound_base_propensity))
.otherwise(pl.lit(outbound_base_propensity))
.alias("Temp.ChannelBasePropensity"),
)
.with_columns(
BasePropensity=pl.when(pl.col("pyDirection") == "Inbound")
.then(
pl.col("Temp.Zipf")
* inbound_base_propensity
/ pl.col("Temp.ZipfMean")
)
.otherwise(
pl.col("Temp.Zipf")
* outbound_base_propensity
/ pl.col("Temp.ZipfMean")
)
BasePropensity=pl.col("Temp.Zipf")
* pl.col("Temp.ChannelBasePropensity")
/ pl.col("Temp.ZipfMean")
)
.with_columns(
pyPropensity=pl.col("BasePropensity").map_elements(
Expand All @@ -228,9 +229,23 @@ def thompson_sampler(propensity, responses=10000):
)
)

# Add artificial noise to the models to manipulate some scenarios
ih_fake_impressions = ih_fake_impressions.with_columns(
pl.when((pl.col.pyModelTechnique == "NaiveBayes") & (pl.col.pyDirection == "Inbound"))
.then(pl.col("Temp.ChannelBasePropensity") * inbound_modelnoise_NaiveBayes)
.when((pl.col.pyModelTechnique == "GradientBoost") & (pl.col.pyDirection == "Inbound"))
.then(pl.col("Temp.ChannelBasePropensity") * inbound_modelnoise_GradientBoost)
.when((pl.col.pyModelTechnique == "NaiveBayes") & (pl.col.pyDirection == "Outbound"))
.then(pl.col("Temp.ChannelBasePropensity") * outbound_modelnoise_NaiveBayes)
.when((pl.col.pyModelTechnique == "GradientBoost") & (pl.col.pyDirection == "Outbound"))
.then(pl.col("Temp.ChannelBasePropensity") * outbound_modelnoise_GradientBoost)
.otherwise(pl.lit(0.0))
.alias("Temp.ExtraModelNoise")
)

ih_fake_clicks = (
ih_fake_impressions.filter(pl.col.pyDirection == "Inbound")
.sample(fraction=click_rate)
.filter(pl.col("Temp.RandomUniform") < (pl.col("pyPropensity") + pl.col("Temp.ExtraModelNoise")))
.with_columns(
pxOutcomeTime=pl.col.pxOutcomeTime
+ pl.duration(minutes=pl.col("Temp.ClickDurationMinutes")),
Expand All @@ -239,7 +254,7 @@ def thompson_sampler(propensity, responses=10000):
)
ih_fake_accepts = (
ih_fake_impressions.filter(pl.col.pyDirection == "Outbound")
.sample(fraction=accept_rate)
.filter(pl.col("Temp.RandomUniform") < (pl.col("pyPropensity") + pl.col("Temp.ExtraModelNoise")))
.with_columns(
pxOutcomeTime=pl.col.pxOutcomeTime
+ pl.duration(minutes=pl.col("Temp.AcceptDurationMinutes")),
Expand Down
12 changes: 8 additions & 4 deletions python/pdstools/ih/Plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,16 +208,16 @@ def success_rate_tree_map(
def action_distribution(
self,
*,
# TODO change - one is the by, when multiple join together
# other is the facet dimension/condition
by: Optional[str] = "Name",
title: Optional[str] = "Action Distribution",
query: Optional[QUERY] = None,
color: Optional[str] = None,
facet: Optional[str] = None,
return_df: Optional[bool] = False,
):
group_by_clause = cdh_utils.safe_flatten_list([by, facet])
plot_data = self.ih.aggregates.summary_outcomes(by=group_by_clause, query=query)
plot_data = self.ih.aggregates.summary_outcomes(
by=[by, color, facet], query=query
)

if return_df:
return plot_data
Expand All @@ -226,11 +226,15 @@ def action_distribution(
plot_data.collect(),
x="Count",
y=by,
color=color,
facet_col=facet,
template="pega",
title=title,
)

fig.update_layout(barmode="stack")
fig.update_yaxes(categoryorder="total ascending")
fig.update_layout(yaxis=dict(title=""))
fig.for_each_annotation(lambda a: a.update(text=a.text.split("=")[-1]))

return fig
Expand Down
8 changes: 7 additions & 1 deletion python/pdstools/utils/cdh_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1168,4 +1168,10 @@ def safe_flatten_list(alist: List) -> List:
for item in sublist
]
alist = list(filter(partial(is_not, None), alist))
return alist if len(alist) > 0 else None
seen = set()
unique_alist = []
for item in alist:
if item not in seen:
unique_alist.append(item)
seen.add(item)
return unique_alist if len(unique_alist) > 0 else None
Loading