# Written By : Sarbadal Pal # Narrative : A simple advertising adstock transformation. # Credit: http://stackoverflow.com/questions/14372880/simple-examples-of-filter-function-recursive-option-specifically import numpy as np import pandas as pd import matplotlib.pyplot as plt # Define Adstock Rate adstock_rate = 0.50 # Create Data advertising = [117.913, 120.112, 125.828, 115.354, 177.090, 141.647, 137.892, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 158.511, 109.385, 91.084, 79.253, 102.706, 78.494, 135.114, 114.549, 87.337, 107.829, 125.020, 82.956, 60.813, 83.149, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 129.515, 105.486, 111.494, 107.099, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000] def adstocked_advertising(adstock_rate=0.5, advertising=advertising): adstocked_advertising = [] for i in range(len(advertising)): if i == 0: adstocked_advertising.append(advertising[i]) else: adstocked_advertising.append(advertising[i] + adstock_rate * adstocked_advertising[i-1]) return adstocked_advertising adstocked_advertising = adstocked_advertising(adstock_rate=0.5, advertising=advertising) fig, ax = plt.subplots(figsize=(10,5), dpi=100) plt.subplots_adjust(left=0.2, bottom=None, right=None, top=None, wspace=None, hspace=None) ax.spines['left'].set_visible(False) ax.spines['right'].set_visible(False) ax.spines['top'].set_visible(False) ax.yaxis.grid(b=False, which='major', color='gray', linestyle='--') ax.set_ylim([0, 1.3*max(adstocked_advertising)]) bar = ax.bar(np.arange(len(advertising)), advertising, width=0.8, color='gray', alpha=0.7, label='Actual Ad') line = ax.plot(np.arange(len(advertising)), adstocked_advertising, marker='o', markersize=3, linestyle='-', linewidth=2, color='blue', alpha=0.7, label='Ad Stock') ax.set_xlabel('Time') ax.set_ylabel('Adstock & Advertising') #ax.set_title('advertising adstock transformation') ax.annotate("Advertising Adstock Transformation", (np.mean(np.arange(len(advertising))), 315), verticalalignment='bottom', horizontalalignment='center', fontsize=15, color='#681963') ax.set_xticks(np.arange(len(advertising))) ax.set_xticklabels(labels=np.arange(len(advertising)), minor=False, fontsize=7, rotation=90) ax.legend(loc='center right') plt.show()
Tuesday, August 1, 2017
A simple advertising adstock transformation in Python
Subscribe to:
Post Comments (Atom)
This was very helpful -- thanks!
ReplyDelete