企業の感度分析
前の演習で扱った企業について、さまざまなmean_inflationとmean_volumeの値で利益がどう変化するかを確認します。将来のインフレ率や販売数量は確実には分からないため、複数の水準に備えて計画するのに役立ちます。
検討する平均インフレ率は0, 1, 2, 5, 10, 15, 20, 50、平均販売数量(mean volume)として使う値は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(pd)、numpy(np)、scipy.stats(st)、matplotlib.pyplot(plt)、seaborn(sns)です。
この演習はコースの一部です
Pythonで学ぶモンテカルロ・シミュレーション
演習の手順
profit_next_year_mc()を実行して、inflのリストとvolのリストをそれぞれループしながら、各組み合わせについて利益を100回計算し、モンテカルロシミュレーションを完成させます。- 得られたDataFrameの
Profit列に保存されたシミュレーション結果を、displotで可視化します。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
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()