from ChannelAttributionPro import *
import pandas as pd
token="yourtoken"
Data = pd.read_csv("https://app.channelattribution.io/data/Data.csv",sep=";")
total_budget_new=100000
perc_reall=0.1
res_attr=heuristic_models(Data=Data, var_path="path", var_conv="total_conversions", var_value="total_conversion_value")
res_attr=res_attr[['path_id','channel','channel_position','last_touch_conversions','last_touch_value']]
res_attr.columns=['path_id','channel','channel_position','total_conversions','total_conversion_value']
tab_costs=pd.DataFrame({'channel':['alpha','iota','eta','beta','theta','lambda','epsilon','zeta','kappa','gamma','mi','delta'],
'value':[41111.43,10387.21,23816.66,6743.46,1650.4,523.52,709.94,288.77,269.46,153.57,0.61,0.43]})
res=budget_allocation(tab_attribution=res_attr,tab_costs=tab_costs,tab_index=None,total_budget_new=total_budget_new,perc_reall=perc_reall)
print("Budget Allocation when Costs are available")
print(res)
res=budget_allocation(tab_attribution=res_attr,tab_costs=None,tab_index=None,total_budget_new=total_budget_new,perc_reall=perc_reall)
print("Budget Allocation when Costs are not available")
print(res)
del res_attr['total_conversion_value']
res=budget_allocation(tab_attribution=res_attr,tab_costs=None,tab_index=None,total_budget_new=total_budget_new,perc_reall=perc_reall)
print("Budget Allocation when Conversion Value is not available")
print(res)
res_attr=markov_model(Data=Data, var_path="path", var_conv="total_conversions", var_value="total_conversion_value", var_null="total_null")
tab_attribution=res_attr['attribution']
tab_index=res_attr['parameters']
tab_index.columns=['channel','value']
tab_costs=pd.DataFrame({'channel':['alpha','iota','eta','beta','theta','lambda','epsilon','zeta','kappa','gamma','mi','delta'],
'value':[41111.43,10387.21,23816.66,6743.46,1650.4,523.52,709.94,288.77,269.46,153.57,0.61,0.43]})
res=budget_allocation(tab_attribution=tab_attribution,tab_costs=tab_costs,tab_index=tab_index,total_budget_new=total_budget_new,perc_reall=perc_reall)
print("Budget Allocation when Costs are available")
print(res)
res=budget_allocation(tab_attribution=tab_attribution,tab_costs=None,tab_index=None,total_budget_new=total_budget_new,perc_reall=perc_reall)
print("Budget Allocation when Costs are not available")
print(res)
del tab_attribution['total_conversion_value']
res=budget_allocation(tab_attribution=tab_attribution,tab_costs=None,tab_index=None,total_budget_new=total_budget_new,perc_reall=perc_reall)
print("Allocate budget when Conversion Value is not available")
print(res)
df_aggr = pd.read_csv("https://app.channelattribution.io/data/data_aggregated_w_value.csv", sep=";")
target = "conversions"
channels = [
x for x in df_aggr.columns
if x not in ["timestamp_from", "timestamp_to", target, "value"]
]
measure_map = {
"A": "clicks",
"B": "impressions",
"C": "impressions",
"D": "clicks",
"E": "clicks",
"F": "clicks",
}
D_variables = pd.DataFrame({
"variable": channels,
"channel": channels,
"measure": [measure_map.get(ch, "clicks") for ch in channels],
})
uam_res = uam(
df_aggr=df_aggr[["timestamp_from", "timestamp_to", target] + channels],
D_variables=D_variables,
target=target,
df_paths=None,
baseline_model="reward_model",
max_p=12,
nsim=1000,
seed=1234567,
verbose=1,
order=1,
sep=">",
ncore=1,
password=token,
return_diagnostics=False,
)
res_attr = uam_res["attribution"] if isinstance(uam_res, dict) else uam_res
res_attr = pd.melt(
res_attr,
id_vars=["timestamp_from", "timestamp_to", target],
var_name="channel",
value_name="attribution",
)
res_attr = pd.merge(
res_attr,
df_aggr[["timestamp_from", "timestamp_to", "value"]],
how="inner",
on=["timestamp_from", "timestamp_to"],
)
res_attr["total_conversion_value"] = (
res_attr["value"] * res_attr["attribution"] / res_attr[target]
)
res_attr = res_attr.rename(columns={"attribution": "total_conversions"})
res_attr = res_attr[
[
"timestamp_from",
"timestamp_to",
target,
"value",
"channel",
"total_conversions",
"total_conversion_value",
]
]
tab_costs = pd.DataFrame({
"channel": ["A", "B", "C", "D", "E", "F"],
"value": [1345, 1456, 987, 1121, 879, 124],
})
res = budget_allocation(
tab_attribution=res_attr,
tab_costs=tab_costs,
tab_index=None,
total_budget_new=total_budget_new,
perc_reall=perc_reall,
min_perc_budget=0.01,
)
print("Budget Allocation when Costs are available")
print(res)
res = budget_allocation(
tab_attribution=res_attr,
tab_costs=None,
tab_index=None,
total_budget_new=total_budget_new,
perc_reall=perc_reall,
min_perc_budget=0.01,
)
print("Budget Allocation when Costs are not available")
print(res)