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

feat: allow the use of different channels weights to the removal effect results before normalization #87

Closed
wants to merge 1 commit into from
Closed
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
13 changes: 13 additions & 0 deletions marketing_attribution_models/MAM.py
Original file line number Diff line number Diff line change
Expand Up @@ -1238,6 +1238,7 @@ def attribution_markov(
transition_to_same_state=False,
group_by_channels_models=True,
conversion_value_as_frequency=True,
channel_weights: dict = None
):
"""Attribution using Markov."""
model_name = "attribution_markov"
Expand Down Expand Up @@ -1287,6 +1288,7 @@ def path_to_matrix(paths):
matrix[-2, -2] = 1
return matrix

#Adds the string (inicio) and (conversion)/(null) into all journeys
temp = self.channels.apply(
lambda x: ["(inicio)"] + x
) + self.journey_with_conv.apply(lambda x: ["(conversion)" if x else "(null)"])
Expand All @@ -1295,6 +1297,7 @@ def path_to_matrix(paths):
dest = []
journey_length = []

#Create arrays with origin and destination of all touchpoints for every journey.
def save_orig_dest(arr):
orig.extend(arr[:-1])
dest.extend(arr[1:])
Expand Down Expand Up @@ -1338,7 +1341,17 @@ def save_orig_dest(arr):
temp["orig"] = temp.orig.apply(channels_names.index)
temp["dest"] = temp.dest.apply(channels_names.index)
matrix = path_to_matrix(temp[["orig", "dest", "count"]].values)

#Calculate non-normalized removal effect percentage
removal_effect_result = removal_effect(matrix)[1:-2]

#Apply channel weight into non-normalized removal effect percentage
if channel_weights:
for channel, weight in channel_weights.items():
#Filter [1:-2] to exclude (inicio), (null) and (conversion), as it was done to create the removal_effect_result
channel_index = channels_names[1:-2].index(channel)
removal_effect_result[channel_index] *= weight

results = removal_effect_result / removal_effect_result.sum(axis=0)

# Channels weights
Expand Down
Loading