Notebook to accompany article on Practical Business Python
Update to use numpy for faster loops based on comments here
In [1]:
import pandas as pd
import numpy as np
import seaborn as sns
In [2]:
sns.set_style('whitegrid')
In [3]:
# Define the variables for the Percent to target based on historical results
avg = 1
std_dev = .1
num_reps = 500
num_simulations = 100000
In [4]:
# Show an example of calculating the percent to target
pct_to_target = np.random.normal(
avg,
std_dev,
size=(num_reps, num_simulations)
)
In [5]:
pct_to_target[0:10]
Out[5]:
In [6]:
# Another example for the sales target distribution
sales_target_values = [75_000, 100_000, 200_000, 300_000, 400_000, 500_000]
sales_target_prob = [.3, .3, .2, .1, .05, .05]
sales_target = np.random.choice(sales_target_values, p=sales_target_prob,
size=(num_reps, num_simulations))
In [7]:
sales_target[0:10]
Out[7]:
In [8]:
commission_percentages = np.take(
np.array([0.02, 0.03, 0.04]),
np.digitize(pct_to_target, bins=[.9, .99, 10])
)
In [9]:
commission_percentages[0:10]
Out[9]:
In [10]:
total_commissions = (commission_percentages * sales_target).sum(axis=0)
In [11]:
total_commissions.std()
Out[11]:
In [12]:
# Show how to create the dataframe
df = pd.DataFrame(data={'Total_Commissions': total_commissions})
df.head()
Out[12]:
In [13]:
df.plot(kind='hist', title='Commissions Distribution')
Out[13]:
In [14]:
df.describe()
Out[14]:
In [ ]: