Simulation of a profit problem
You work for a company that manufactures industrial equipment. The sales price of each piece of equipment is $100,000. You also know that there is a strong negative correlation between the inflation_rate
and sales volume
. This relationship is captured by the covariance matrix cov_matrix
, which is available in the console for you.
The function profit_next_year_mc()
performs a Monte Carlo simulation returning expected profit (in thousands of dollars), given the mean inflation rate and mean sales volume as arguments. You'll also need to pass n
, the number of time the simulation should be run. The function has been loaded for you, and the definition is below.
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 for you: 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
Exercise instructions
- Perform a Monte Carlo simulation by running
profit_next_year_mc()
500 times using amean_inflation
of2
and amean_volume
of500
. - Visualize the simulation results using a
displot
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Run a Monte Carlo simulation 500 times using a mean_inflation of 2 and a mean_volume of 500
profits = profit_next_year_mc(____)
# Create a displot of the results
____
plt.show()