Get startedGet started for free

Company sensitivity analysis

You'll now examine what would happen to profits for the company from the previous exercise at a variety of mean_inflation and mean_volume values. This will help the company plan for several levels of inflation and sales volumes since no company can ever be certain what inflation or sales volumes they will have in the future.

The mean inflation percentages you'd like to explore are 0, 1, 2, 5, 10, 15, 20, 50, while the sales values for use as the mean volume value are 100, 200, 500, 800, 1000. As a reminder, here is the profit_next_year_mc() function definition, which has already been loaded for you.

def profit_next_year_mc(mean_inflation, mean_volume, n):
  profits = []
  for i in range(n):
    # Generate inputs by sampling from the multivariate normal distribution
    rate_sales_volume = st.multivariate_normal.rvs(mean=[mean_inflation,mean_volume], cov=cov_matrix,size=1000)
    # Deterministic calculation of company profit
    price = 100 * (100 + rate_sales_volume[:,0])/100
    volume = rate_sales_volume[:,1]
    loan_and_cost = 50 * volume + 45 * (100 + 3 * rate_sales_volume[:,0]) * (volume/100)
    profit = (np.mean(price * volume - loan_and_cost))
    profits.append(profit)
  return profits

The following packages have been imported: pandas as pd, numpy as np, scipy.stats as st, matplotlib.pyplot as plt, and seaborn as sns.

This exercise is part of the course

Monte Carlo Simulations in Python

View Course

Exercise instructions

  • Complete the Monte Carlo simulation by running profit_next_year_mc() to calculate profit 100 times, each time looping through the list of infl values and the list of vol values.
  • Use a displot to visualize the simulation results saved in the Profit column of the resulting DataFrame.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

x1 = []
x2 = []
y = []
for infl in [0, 1, 2, 5, 10, 15, 20, 50]:
    for vol in [100, 200, 500, 800, 1000]:
		# Run profit_next_year_mc so that it samples 100 times for each infl and vol combination
        avg_prof = np.mean(____)
        x1.append(infl)
        x2.append(vol)
        y.append(avg_prof)
df_sa = pd.concat([pd.Series(x1), pd.Series(x2), pd.Series(y)], axis=1)
df_sa.columns = ["Inflation", "Volume", "Profit"]
# Create a displot of the simulation results for "Profit"
____
plt.show()
Edit and Run Code