เริ่มต้นใช้งานเริ่มต้นใช้งานได้ฟรี

การวิเคราะห์ความไวของบริษัท

คราวนี้จะมาดูว่าผลกำไรของบริษัทจากแบบฝึกหัดก่อนหน้าจะเปลี่ยนแปลงอย่างไร เมื่อใช้ค่า mean_inflation และ mean_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 เป็น pd, numpy เป็น np, scipy.stats เป็น st, matplotlib.pyplot เป็น plt และ seaborn เป็น sns

แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร

Monte Carlo Simulations ใน Python

ดูคอร์ส

คำแนะนำการฝึกหัด

  • เรียกใช้ profit_next_year_mc() เพื่อคำนวณผลกำไร 100 ครั้ง โดยวนซ้ำผ่านรายการค่า infl และรายการค่า vol ในแต่ละครั้ง เพื่อให้การจำลอง Monte Carlo สมบูรณ์
  • ใช้ displot เพื่อแสดงผลการจำลองที่บันทึกไว้ในคอลัมน์ Profit ของ DataFrame ที่ได้

แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ

ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์

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()
แก้ไขและรันโค้ด