利润问题的模拟
您所在的公司生产工业设备。每台设备的销售价格为 $100,000。您还知道,inflation_rate 与销售 volume 之间存在显著的负相关。这一关系由协方差矩阵 cov_matrix 表示,已在控制台中为您提供。
函数 profit_next_year_mc() 会执行一次 Monte Carlo 模拟,并在给定平均通胀率与平均销量作为参数时,返回预期利润(单位:千美元)。您还需要传入 n,即应运行模拟的次数。该函数已为您加载,定义如下所示。
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
以下包已为您导入:将 pandas 导入为 pd,numpy 为 np,scipy.stats 为 st,matplotlib.pyplot 为 plt,以及 seaborn 为 sns。
本练习是课程的一部分
Python 中的蒙特卡洛模拟
练习说明
- 使用
mean_inflation为2、mean_volume为500,运行profit_next_year_mc()共 500 次,完成一次 Monte Carlo 模拟。 - 使用
displot可视化模拟结果。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# 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()