พล็อต Efficient Frontier พร้อมค่า Sharpe Ratio ที่ดีที่สุด
มาพล็อต Efficient Frontier อีกครั้ง คราวนี้จะเพิ่มจุดแสดงตำแหน่งของพอร์ตโฟลิโอที่มีค่า Sharpe Index ดีที่สุดด้วย การแสดงผลข้อมูลช่วยให้เข้าใจภาพรวมได้ชัดเจนขึ้นเสมอ
ทบทวนกันอีกครั้ง: Efficient Frontier จะพล็อตเป็น scatter plot โดยแกน x คือความผันผวนของพอร์ตโฟลิโอ และแกน y คือผลตอบแทน เราจะดึงวันที่ล่าสุดจาก covariances.keys() แม้จะใช้ดิกชันนารีอื่น เช่น portfolio_returns ก็ได้เช่นกัน จากนั้นดึงค่าความผันผวนและผลตอบแทนของวันที่ล่าสุดจาก portfolio_volatility และ portfolio_returns แล้วหาดัชนีของพอร์ตโฟลิโอที่มีค่า Sharpe Index ดีที่สุดจาก max_sharpe_idxs[date] และพล็อตทุกอย่างด้วย plt.scatter()
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
Machine Learning สำหรับการเงินด้วย Python
คำแนะนำการฝึกหัด
- กำหนด
cur_volatilityให้เป็นค่าความผันผวนของพอร์ตโฟลิโอสำหรับdateล่าสุด - สร้างกราฟ "Efficient Frontier" โดยพล็อตค่าความผันผวนบนแกน x และผลตอบแทนบนแกน y
- ดึงดัชนีพอร์ตโฟลิโอที่ดีที่สุดของ
dateล่าสุดจากmax_sharpe_idxs
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# Get most recent (current) returns and volatility
date = sorted(covariances.keys())[-1]
cur_returns = portfolio_returns[date]
cur_volatility = ____
# Plot efficient frontier with sharpe as point
plt.scatter(x=____, y=____, alpha=0.1, color='blue')
best_idx = max_sharpe_idxs[____]
# Place an orange "X" on the point with the best Sharpe ratio
plt.scatter(x=cur_volatility[best_idx], y=cur_returns[best_idx], marker='x', color='orange')
plt.xlabel('Volatility')
plt.ylabel('Returns')
plt.show()