Normal 分配的 VaR
為了熟悉 在險價值(Value at Risk,VaR),先把它套用到一個已知的分配會很有幫助。Normal(或稱 Gaussian)分配特別實用,因為:1)它有解析上簡潔的形式,2)能刻劃多種實證現象。這個練習中你會假設投資組合的損失服從常態分配,也就是說,從分配中抽到的值越大,損失越大。
你將學到如何使用 scipy.stats.norm 的 ppf()(percent point function)以及 numpy 的 quantile() 來分別找出標準常態分配在 95% 與 99% 信賴水準下的 VaR。你也會將 VaR 視覺化,作為常態分配圖上的一個門檻。
本練習屬於課程
Python 量化風險管理
練習說明
- 使用
norm的.ppf()(percent point function)來求 95% 信賴水準下的 VaR。 - 接著,對 100,000 個來自 Normal 分配的隨機
draws使用 Numpy 的quantile(),求出 99% 的 VaR。 - 用一個
print陳述式比較 95% 與 99% 的 VaR。 - 繪製常態分配,並加入一條線標示 95% 的 VaR。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Create the VaR measure at the 95% confidence level using norm.ppf()
VaR_95 = norm.ppf(____)
# Create the VaR measure at the 99% confidence level using numpy.quantile()
draws = norm.rvs(size = 100000)
VaR_99 = np.quantile(____, 0.99)
# Compare the 95% and 99% VaR
print("95% VaR: ", ____, "; 99% VaR: ", ____)
# Plot the normal distribution histogram and 95% VaR measure
plt.hist(draws, bins = 100)
plt.axvline(x = ____, c='r', label = "VaR at 95% Confidence Level")
plt.legend(); plt.show()