開始使用免費開始

銀行會倒閉嗎?

把上一個練習得到的違約次數(命名為 n_defaults)畫成一個 CDF。你在第 1 章寫的 ecdf() 函式可以使用。

如果利率條件是:只要有 10 筆或以上的貸款違約,銀行就會虧損,那銀行虧損的機率是多少?

本練習屬於課程

Statistical Thinking in Python (Part 1)

檢視課程

練習說明

  • 計算 n_defaults 的 ECDF 之 xy 值。
  • 繪製 ECDF,並確實加上座標軸標籤。呼叫 plt.plot() 時,除了傳入 xy,也要加入 marker = '.'linestyle = 'none'
  • 顯示圖形。
  • 計算你在 n_defaults 陣列中大於或等於 10 的總筆數。作法是先建立一個布林陣列,判斷 n_defaults 的各元素是否 >= 10,接著用 np.sum() 將此布林陣列加總。例如,np.sum(n_defaults <= 5) 會得到違約次數小於等於 5 的筆數。
  • 銀行虧損的機率等於 n_defaults 中大於或等於 10 的比例。按下送出來列印結果!

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Compute ECDF: x, y


# Plot the ECDF with labeled axes




# Show the plot


# Compute the number of 100-loan simulations with 10 or more defaults: n_lose_money


# Compute and print probability of losing money
print('Probability of losing money =', n_lose_money / len(n_defaults))
編輯並執行程式碼