Spaces:
Sleeping
Sleeping
File size: 6,825 Bytes
85d2c7e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
from sklearn.preprocessing import MinMaxScaler
import warnings
warnings.filterwarnings("ignore")
import plotly.graph_objects as go
## reading input data
df= pd.read_csv('response_curves_input_file.csv')
df.dropna(inplace=True)
df['Date'] = pd.to_datetime(df['Date'])
df.reset_index(inplace=True)
channel_cols = [
'Broadcast TV',
'Cable TV',
'Connected & OTT TV',
'Display Prospecting',
'Display Retargeting',
'Video',
'Social Prospecting',
'Social Retargeting',
'Search Brand',
'Search Non-brand',
'Digital Partners',
'Audio',
'Email']
spend_cols = [
'tv_broadcast_spend',
'tv_cable_spend',
'stream_video_spend',
'disp_prospect_spend',
'disp_retarget_spend',
'olv_spend',
'social_prospect_spend',
'social_retarget_spend',
'search_brand_spend',
'search_nonbrand_spend',
'cm_spend',
'audio_spend',
'email_spend']
prospect_cols = [
'Broadcast TV_Prospects',
'Cable TV_Prospects',
'Connected & OTT TV_Prospects',
'Display Prospecting_Prospects',
'Display Retargeting_Prospects',
'Video_Prospects',
'Social Prospecting_Prospects',
'Social Retargeting_Prospects',
'Search Brand_Prospects',
'Search Non-brand_Prospects',
'Digital Partners_Prospects',
'Audio_Prospects',
'Email_Prospects']
def hill_equation(x, Kd, n):
return x**n / (Kd**n + x**n)
def hill_func(x_data,y_data,x_minmax,y_minmax):
# Fit the Hill equation to the data
initial_guess = [1, 1] # Initial guess for Kd and n
params, covariance = curve_fit(hill_equation, x_data, y_data, p0=initial_guess,maxfev = 1000)
# Extract the fitted parameters
Kd_fit, n_fit = params
# Generate y values using the fitted parameters
y_fit = hill_equation(x_data, Kd_fit, n_fit)
x_data_inv = x_minmax.inverse_transform(np.array(x_data).reshape(-1,1))
y_data_inv = y_minmax.inverse_transform(np.array(y_data).reshape(-1,1))
y_fit_inv = y_minmax.inverse_transform(np.array(y_fit).reshape(-1,1))
# # Plot the original data and the fitted curve
# plt.scatter(x_data_inv, y_data_inv, label='Actual Data')
# plt.scatter(x_data_inv, y_fit_inv, label='Fit Data',color='red')
# # plt.line(x_data_inv, y_fit_inv, label=f'Fitted Hill Equation (Kd={Kd_fit:.2f}, n={n_fit:.2f})', color='red')
# plt.xlabel('Ligand Concentration')
# plt.ylabel('Fraction of Binding')
# plt.title('Fitting Hill Equation to Data')
# plt.legend()
# plt.show()
return y_fit,y_fit_inv,Kd_fit, n_fit
def data_output(channel,X,y,y_fit_inv,x_ext_data,y_fit_inv_ext):
fit_col = 'Fit_Data_'+channel
plot_df = pd.DataFrame()
plot_df[f'{channel}_Spends'] = X
plot_df['Date'] = df['Date']
plot_df['MAT'] = df['MAT']
y_fit_inv_v2 = []
for i in range(len(y_fit_inv)):
y_fit_inv_v2.append(y_fit_inv[i][0])
plot_df[fit_col] = y_fit_inv_v2
# adding extra data
y_fit_inv_v2_ext = []
for i in range(len(y_fit_inv_ext)):
y_fit_inv_v2_ext.append(y_fit_inv_ext[i][0])
# print(x_ext_data)
ext_df = pd.DataFrame()
ext_df[f'{channel}_Spends'] = x_ext_data
ext_df[fit_col] = y_fit_inv_v2_ext
ext_df['Date'] = [
np.datetime64('1950-01-01'),
np.datetime64('1950-06-15'),
np.datetime64('1950-12-31')
]
ext_df['MAT'] = ["ext","ext","ext"]
print(ext_df)
plot_df= plot_df.append(ext_df)
return plot_df
def input_data(df,spend_col,prospect_col):
X = np.array(df[spend_col].tolist())
y = np.array(df[prospect_col].tolist())
x_minmax = MinMaxScaler()
x_scaled = x_minmax.fit_transform(df[[spend_col]])
x_data = []
for i in range(len(x_scaled)):
x_data.append(x_scaled[i][0])
y_minmax = MinMaxScaler()
y_scaled = y_minmax.fit_transform(df[[prospect_col]])
y_data = []
for i in range(len(y_scaled)):
y_data.append(y_scaled[i][0])
return X,y,x_data,y_data,x_minmax,y_minmax
def extend_s_curve(x_max,x_minmax,y_minmax, Kd_fit, n_fit):
print(x_max)
x_ext_data = [x_max*1.2,x_max*1.3,x_max*1.5]
# x_ext_data = [1500000,2000000,2500000]
# x_ext_data = [x_max+100,x_max+200,x_max+5000]
x_scaled = x_minmax.transform(pd.DataFrame(x_ext_data))
x_data = []
for i in range(len(x_scaled)):
x_data.append(x_scaled[i][0])
print(x_data)
y_fit = hill_equation(x_data, Kd_fit, n_fit)
y_fit_inv = y_minmax.inverse_transform(np.array(y_fit).reshape(-1,1))
return x_ext_data,y_fit_inv
def fit_data(spend_col,prospect_col,channel):
### getting k and n parameters
temp_df = df[df[spend_col]>0]
temp_df.reset_index(inplace=True)
X,y,x_data,y_data,x_minmax,y_minmax = input_data(temp_df,spend_col,prospect_col)
y_fit, y_fit_inv, Kd_fit, n_fit = hill_func(x_data,y_data,x_minmax,y_minmax)
print('k: ',Kd_fit)
print('n: ', n_fit)
##### extend_s_curve
x_ext_data,y_fit_inv_ext= extend_s_curve(temp_df[spend_col].max(),x_minmax,y_minmax, Kd_fit, n_fit)
plot_df = data_output(channel,X,y,y_fit_inv,x_ext_data,y_fit_inv_ext)
return plot_df
plotly_data = fit_data(spend_cols[0],prospect_cols[0],channel_cols[0])
plotly_data.tail()
for i in range(1,13):
print(i)
pdf = fit_data(spend_cols[i],prospect_cols[i],channel_cols[i])
plotly_data = plotly_data.merge(pdf,on = ["Date","MAT"],how = "left")
def response_curves(channel,chart_typ):
if chart_typ == 'View Scattered Plot':
mode_f1 = "markers"
elif chart_typ == 'View Line Plot':
mode_f1 = "lines"
else:
mode_f1 = "lines+markers"
# Initialize the Plotly figure
fig = go.Figure()
x_col = channel+"_Spends"
y_col = "Fit_Data_"+channel
fig.add_trace(go.Scatter(
x=plotly_data.sort_values(by=x_col, ascending=True)[x_col],
y=plotly_data.sort_values(by=x_col, ascending=True)[y_col],
mode=mode_f1,
name=x_col.replace('_Spends', '')
))
fig.add_trace(go.Scatter(
x=plotly_data[plotly_data['Date'] == plotly_data['Date'].max()][x_col],
y=plotly_data[plotly_data['Date'] == plotly_data['Date'].max()][y_col],
mode='markers',
marker=dict(
size=13 # Adjust the size value to make the markers larger or smaller
, color = 'green'
),
name="Current Spends"
))
# Update layout with titles
fig.update_layout(
title=channel+' Response Curve',
xaxis_title='Weekly Spends',
yaxis_title='Prospects'
)
# Show the figure
return fig
|