開始使用免費開始

比較 CVaR 與 VaR

「條件在險值」(CVaR),也稱為「期望短缺」(ES),是在某一信心水準下,條件為損失超過某個門檻時,平均損失會是多少。它以 VaR 為出發點,但因為考量了損失分配的「尾部」,所以包含更多資訊。

你會先針對投資組合損失的常態分配,計算 95% VaR,其平均數與標準差與 2005-2010 年投資銀行的 portfolio_losses 相同。接著使用該 VaR 計算 95% CVaR,並將兩者一併與常態分配比較繪圖。

工作空間中已提供 portfolio_losses,以及來自 scipy.stats 的常態分配 norm

本練習屬於課程

Python 量化風險管理

檢視課程

練習說明

  • 計算 portfolio_losses 的平均數與標準差,分別指定給 pmps
  • 使用 norm.ppf() 方法找出 95% VaR——此方法的引數中,loc 代表平均數、scale 代表標準差。
  • 使用 95% VaR 與 norm.expect() 方法求得 tail_loss,並用它來計算相同信心水準下的 CVaR。
  • 在常態分配的直方圖上加上垂直線,標示 VaR(紅色)與 CVaR(綠色)。

動手互動練習

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

# Compute the mean and standard deviation of the portfolio returns
pm = portfolio_losses.____
ps = portfolio_losses.____

# Compute the 95% VaR using the .ppf()
VaR_95 = norm.ppf(0.95, loc = ____, scale = ____)
# Compute the expected tail loss and the CVaR in the worst 5% of cases
tail_loss = norm.____(lambda x: x, loc = ____, scale = ____, lb = VaR_95)
CVaR_95 = (1 / (1 - 0.95)) * ____

# Plot the normal distribution histogram and add lines for the VaR and CVaR
plt.hist(norm.rvs(size = 100000, loc = pm, scale = ____), bins = 100)
plt.axvline(x = VaR_95, c='r', label = "VaR, 95% confidence level")
plt.axvline(x = ____, c='g', label = "CVaR, worst 5% of outcomes")
plt.legend(); plt.show()
編輯並執行程式碼