import numpy as np from scipy.optimize import curve_fit import matplotlib.pyplot as plt import pandas as pd def hello(): print ("world!") # def polynomial_func(x, a,b,c,d): # y = a * x - b * x**2 + c * x**3 + d def polynomial_func(x, a,b,c): y = a * x - b * x**2 + c * x**3 return y def get_country_metrics(country,min_date='2023-07-01',max_date='2023-09-01'): df_country = pd.read_csv('ecom_cvr_aov_by_month.csv',parse_dates=['DATE']) df_country['CVR']=df_country['CVR'].apply(lambda x: x.replace("%","")).astype(float)/100 df_country['PC']=df_country['PC'].apply(lambda x: x.replace("%","")).astype(float)/100 if country in df_country.Country.unique(): return df_country[(df_country.Country==country) & (df_country.DATE>=min_date) & (df_country.DATE=min_date) & (df_country.DATE0) & (df.country==country) & (df.platform==platform) & (df['date']>min_date) & (df['date']0) & (df.country==country) & (df.platform==platform) & (df.sub_channel==sub_channel) & (df['date']>min_date) & (df['date']=0) & (df_marginal.marginal_income.shift(-1)<0),'cumulative_cost'].min() highest_amount_spent = df_marginal.cum_cost.max() except: if pprint==True: output_msg.append("Something happened during marginal CPC and equation calculations. We couldn't calculate max_spend_threshold and highest_amount_spent...") max_spend_threshold=np.nan highest_amount_spent=np.nan rSquared=np.nan total_negative_roas_spend=np.nan if pd.notnull(max_spend_threshold): if (max_spend_threshold>highest_amount_spent): marginal_cpc,cum_clicks,cum_cpc,cum_revenue = np.nan, np.nan, np.nan, np.nan if pprint==True: output_msg.append(f"Highest amount spent during this period is: {highest_amount_spent}") else: marginal_cpc,cum_clicks,cum_cpc,cum_revenue = df_marginal.loc[df_marginal.cumulative_cost==max_spend_threshold, ['marginal_cpc','cum_clicks','cum_cpc','cum_revenue']].values[0] # any spend beyond the max_spend_threshold needs to be saved total_negative_roas_spend = df_temp.loc[df_temp.cum_cost>max_spend_threshold,['cost']].sum().values[0] marginal_cpc_HAS,cum_clicks_HAS,cum_cpc_HAS,cum_revenue_HAS = df_marginal.loc[df_marginal.cumulative_cost==highest_amount_spent, ['marginal_cpc','cum_clicks','cum_cpc','cum_revenue']].values[0] # # so to get the max cost for this tactic or strategy, if pprint==True: output_msg.append(f"Max spend threshold is: {max_spend_threshold}") output_msg.append(f"For this spend, marginal_cpc={marginal_cpc}, cumulative_clicks={cum_clicks}, Average_cpc={cum_cpc}, cumulative_revenue={cum_revenue}") output_msg.append(f"Amount spent during this period is: {highest_amount_spent}") output_msg.append(f"For this spend, marginal_cpc={marginal_cpc_HAS}, cumulative_clicks={cum_clicks_HAS}, Average_cpc={cum_cpc_HAS}, cumulative_revenue={cum_revenue_HAS}") output_msg.append(f"Total amount spent in negative ROAS={total_negative_roas_spend}") # print (output_msg) else: marginal_cpc,cum_clicks,cum_cpc,cum_revenue = np.nan, np.nan, np.nan, np.nan total_negative_roas_spend=np.nan if pprint==True: output_msg.append(f"Not enough data for mCPC analysis. Max threshold resulted in {max_spend_threshold}. Highest amount spent during this period is: {highest_amount_spent}") print (f"Not enough data for mCPC analysis. Max threshold resulted in {max_spend_threshold}. Highest amount spent during this period is: {highest_amount_spent}") try: ax1 = fig.add_subplot(121); plt.scatter(df_temp['cum_cost'],df_temp['cpc'],color='black'); plt.axhline(1*cvr*aov,color='red',linestyle='--');plt.axhline(1*cvr*aov*pc,color='blue',linestyle='--'); plt.annotate(f'Any clicks above {np.round(cvr*aov,2)} SEK cpc has negative ROAS \n Above {np.round(cvr*aov*pc,2)} SEK is negative PC',size=12,xy=[0,40], color="black") plt.xlabel('Cumulative ad spend that month (in SEK)'); plt.ylabel('Cost per individual click (in SEK)') # ax2.axes.get_xaxis().set_visible(False) ax1.title.set_text('CPC for at different spend levels'); except: pass try: ax2 = fig.add_subplot(122) plt.plot(df_temp['cum_cost'],df_temp['marginal_income'], '.', label="data", color='r') # plt.plot(df_temp['cum_cost'], polynomial_func(df_temp['cum_cost'], a,b,c,d), '--', color='blue', label="fitted") plt.plot(df_temp['cum_cost'], polynomial_func(df_temp['cum_cost'], a,b,c), '--', color='blue', label="fitted") plt.xlabel('cumulative cost'); plt.ylabel('marginal income') ax2.title.set_text('Net income at different spend levels') except: pass values = (max_spend_threshold,highest_amount_spent,total_negative_roas_spend,marginal_cpc,cum_clicks,cum_cpc,cum_revenue,rSquared) return fig,output_msg,values # return fig,output_msg,values,df_marginal def loop_mCPC_countries(df,min_date,max_date, country_list=['UK', 'DE', 'US', 'NL', 'SE', 'CH', 'BE', 'EU', 'FR', 'AU'], platform='meta'): country_flag = '' df_result = pd.DataFrame() for i,d in df[(df.index>min_date) & (df.index