This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
az.plot_posterior( | |
trace, | |
var_names=['intercept', 'coefficient_'], | |
filter_vars='like' | |
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trace_vars = [i.replace('coefficient_','') for i in dir(trace['posterior']) if i.startswith("coefficient_") or i=='intercept'] | |
trace_coeff_vars = [i for i in dir(trace['posterior']) if i.startswith("coefficient_") or i=='intercept'] | |
trace_alpha_vars = [i for i in dir(trace['posterior']) if i.startswith("alpha_")] | |
trace_gamma_vars = [i for i in dir(trace['posterior']) if i.startswith("gamma_")] | |
model_results = pd.DataFrame({ | |
"variable": trace_vars, | |
"coefficient": [float(trace['posterior'][i].mean()) for i in trace_coeff_vars] | |
}).merge( | |
pd.DataFrame({ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def scale(x, y): | |
return x * y.sum() / x.sum() | |
with pm.Model() as mmm: | |
target = media_transformed['REVENUE'] / media_transformed['REVENUE'].mean() | |
media_contributions = [] | |
for channel in channel_priors.keys(): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# define coefficient | |
channel_prior = channel_priors[channel] | |
channel_coefficient = pm.TruncatedNormal(f"coefficient_{channel}", mu=channel_prior, sigma=0.0001, lower=0, upper=0.15) | |
# define saturation | |
alpha = pm.Uniform(f"alpha_{channel}", lower=0.5, upper=2) | |
gamma = pm.Uniform(f"gamma_{channel}", lower=0.5, upper=1.5) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
channel_priors = { | |
"CTV": 0.02, | |
"DISPLAY": 0.05, | |
"DIRECT_MAIL": 0.02, | |
"EMAIL": 0.03, | |
"PAID_SOCIAL": 0.08, | |
"PAID_SEARCH": 0.14, | |
"TV": 0.01 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
media = pd.read_csv('media.csv', parse_dates=['DATE']) | |
media = media.resample('W-Mon', label='left', closed='left', on='DATE').sum() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def hill_transform(x, alpha, gamma): | |
return 1 / (1 + (x/gamma)**-alpha) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
lag = { | |
"CTV": 2, | |
"DISPLAY": 2, | |
"DIRECT_MAIL": 2, | |
"EMAIL": 2, | |
"PAID_SOCIAL": 2, | |
"PAID_SEARCH": 1, | |
"TV": 3 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def fp_growth(transaction_db, min_sup, fp_list, prefix = []): | |
####### STEP A: CONCATENATE ####### | |
# Get one itemsets which meet minimum support | |
one_itemset_dict = get_one_itemsets(transaction_db, min_sup) | |
# Concanenate frequent patterns with prefix | |
freq_patterns = [] | |
for key,val in one_itemset_dict.items(): | |
pattern = prefix + [key] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def insert_tree(prefix, suffix, current_root, fp_tree, node_link_dict, transaction_count): | |
# Check if route to the prefix node exists in tree | |
# If exists, get the node | |
new_root = tree_contains_node(prefix, current_root, fp_tree) | |
if new_root == False: | |
# If route doesn't exist, create new child node to current root | |
new_root = {'name':prefix, 'count':transaction_count, 'parent': current_root} | |