시작하기무료로 시작하기

회사 민감도 분석

이제 이전 연습 문제의 회사를 대상으로, 다양한 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

다음 패키지가 임포트되어 있습니다: pandaspd, numpynp, scipy.statsst, matplotlib.pyplotplt, 그리고 seabornsns로 불러왔습니다.

이 연습은 강의의 일부입니다

Python으로 배우는 Monte Carlo 시뮬레이션

강의 보기

연습 안내

  • 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()
코드 편집 및 실행