開始使用免費開始

由擬合分配計算 VaR

要最小化 CVaR,必須先在某個信賴水準(例如 95%)下計算 VaR。先前你從常態(Gaussian)分配以分位數的方式推得 VaR,但更一般地,要最小化 CVaR 需要從最能「擬合」資料的分配來計算該分位數。

在本練習中,已提供一個 fitted「損失分配」,它是對 2005–2010 年等權重投資銀行投資組合的損失所做的擬合。你會先使用它的 .evaluate() 方法將此分配繪圖(關於擬合分配會在第 4 章更詳細說明)。

接著,你會使用 fitted 物件的 .resample() 方法,從此擬合分配隨機抽取 100,000 筆觀測作為 sample

最後,對這個隨機 sample 使用 np.quantile(),即可計算 95% 的 VaR。

本練習屬於課程

Python 量化風險管理

檢視課程

練習說明

  • 繪製 fitted 損失分配。留意 fitted 分配與常態分配的差異。
  • 使用 fitted.resample() 方法,從該擬合分配隨機抽樣產生含 100,000 點的 sample
  • 使用 np.quantile() 從這個隨機 sample 找出 95% 的 VaR,並顯示結果。

動手互動練習

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

# Visualize the fitted distribution with a plot
x = np.linspace(-0.25,0.25,1000)
plt.____(x,fitted.evaluate(x))
plt.show()

# Create a random sample of 100,000 observations from the fitted distribution
sample = fitted.____(____)

# Compute and display the 95% VaR from the random sample
VaR_95 = np.____(sample, ____)
print(VaR_95)
編輯並執行程式碼