开始使用免费开始使用

公司敏感性分析

接下来,您将考察在不同 mean_inflationmean_volume 取值下,上一练习中那家公司的利润会发生什么变化。这将帮助公司为不同程度的通货膨胀和销售量做规划,因为没有任何公司能确定未来的通胀或销量。

您要探索的平均通胀百分比为 0, 1, 2, 5, 10, 15, 20, 50,而用于作为平均销量的销售值为 100, 200, 500, 800, 1000。作为提醒,下面是已经为您加载的 profit_next_year_mc() 函数定义。

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 导入为 pdnumpy 导入为 npscipy.stats 导入为 stmatplotlib.pyplot 导入为 plt,以及 seaborn 导入为 sns

本练习是课程的一部分

Python 中的蒙特卡洛模拟

查看课程

练习说明

  • 运行 profit_next_year_mc() 完成蒙特卡罗模拟:计算利润 100 次,每次遍历 infl 值列表与 vol 值列表。
  • 使用 displot 可视化结果数据框中 Profit 列保存的模拟结果。

交互式实操练习

通过完成这段示例代码来试试这个练习。

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()
编辑并运行代码