Of Unforeseens With Moving Means

Show code
import pandas as pd
import numpy as np
import os
import sys
sys.path.append("../python")
import general
import visualizations
import moving_average as MA
Show code
from IPython.display import HTML

HTML('''<script>
code_show=true; 
function code_toggle() {
 if (code_show){
 $('div.input').hide();
 } else {
 $('div.input').show();
 }
 code_show = !code_show
} 
$( document ).ready(code_toggle);
</script>
<form action="javascript:code_toggle()"><input type="submit" value="Click here to toggle on/off the raw code."></form>''')
Show code
params = {'OUTPUT' : {'path' : os.path.join('output_html', 'moving_average'),
                      'name' : 'moving_average_20200924'},
         'TIME' : { 'weeks_to_simulate' : 13, # Roughly one corporate quarter
                    'MA_days' : 7,
                    'days_of_week' : [('Sunday', 'weekend'), ('Monday', 'week'), ('Tuesday', 'week'), ('Wednesday', 'week'), 
                    ('Thursday','week'),('Friday', 'week'), ('Saturday', 'weekend')]},
         'CONVERSION_RATES' : { 'week' : 0.25,
                                'weekend' : 0.75},
         'VOLUME' : {'week' : 5000,
                     'weekend' : 100}}
Show code
conversion_df = MA.generateConversionData(params)
Show code
conversion_df = MA.calculateConversionRatesAndMAs(params, conversion_df)

A common debacle emerges
When rate of conversion diverges
In the midst of the week
While the odds may be bleak
On the weekend, conversion rate surges

But leads, on the weekend, are thin
When fewer inquire within
Though most are converted
More still are diverted
To weekdays when pain points begin

Show code
visualizations.MA(conversion_df, params, [('conversion_rate', 'red')])

To flatten the ebbs and the flows
And cyclical jumps decompose:
The well-followed lodestar,
The seven-day boxcar
Will quickly, a trendline, expose…

Show code
visualizations.MA(conversion_df, params, [('conversion_rate', 'red'), ('7Day_MA','green')])

But is this the trend we desire?
Or is this misleadingly higher?
Conversion rate rises
With small sample sizes
A fictional mean amplifier!

Be wary when averaging fractions
Of disparate rates of transactions
With methods so crude
The mean will be skewed
Demanding regretful retractions!

Show code
visualizations.MA(conversion_df, params, [('conversion_rate', 'red'), ('7Day_MA','green'), ('7Day_MA_RightWay','cyan')])

But in this, superior version
We sum all the leads and conversion
For each week implied
Before we divide
Thus a trend, the desired emersion

Show code
if not os.path.exists(params['OUTPUT']['path']): os.makedirs(params['OUTPUT']['path'])
general.publish('movingAverage', params['OUTPUT']['path'], params['OUTPUT']['name'])