Value at Risk (VaR) In Python: Parametric Method
Vložit
- čas přidán 4. 06. 2024
- Dive into our comprehensive guide on "Value at Risk (VaR) In Python: Parametric Method". From installing essential libraries to interpreting the final VaR results, this video covers every step. Learn how to choose stock tickers, calculate daily log returns, set up confidence intervals, and more using Python. Master the art of financial risk management and enhance your portfolio analysis skills today.
💻 Free Code Here: ryanoconnellfinance.com/estim...
🎓 Tutor With Me: 1-On-1 Video Call Sessions Available
► Join me for personalized finance tutoring tailored to your goals: ryanoconnellfinance.com/finance-tutoring/
👨💼 My Freelance Financial Modeling Services:
► Custom financial modeling solutions tailored for your needs: ryanoconnellfinance.com/freelance-finance-services/
📚 CFA Exam Prep Discount - AnalystPrep:
► Get 20% off CFA Level 1, 2, and 3 complete courses with promo code "RYAN20". Explore here: analystprep.com/shop/all-3-levels-of-the-cfa-exam-complete-course-by-analystprep/?ref=mgmymmr
📘 FRM Exam Prep Discount - AnalystPrep:
► Get 20% off FRM Part 1 and Part 2 complete courses with promo code "RYAN20". Explore here: analystprep.com/shop/frm-part-1-and-part-2-complete-course-by-analystprep/?ref=mgmymmr
Chapters:
0:00 - Intro to "Value at Risk (VaR) In Python: Parametric Method"
0:19 - Installing Necessary Libraries
0:48 - Set Time Range of Historical Returns
1:59 - Choose Your Stock Tickers
2:39 - Download Adjusted Close Prices from yFinance
4:19 - Calculate Individual Stock Daily Log Returns
6:11 - Create an Equally Weighted Portfolio
7:15 - Calculate Total Portfolio Daily Returns
8:10 - Find Portfolio Returns for a Range of Days
9:22 - Create the Covariance Matrix
10:05 - Calculate Portfolio Standard Deviation
11:10 - Set Confidence Intervals for VaR
11:39 - Calculate Value at Risk (VaR) In Python
13:00 - Print and Interpret the VaR Results
Disclosure: This is not financial advice and should not be taken as such. The information contained in this video is an opinion. Some of the information could be wrong. This channel is owned and operated by Portfolio Constructs LLC. Some of the links above are affiliate links, meaning, at no additional cost to you, I will earn a commission if you click through and make a purchase.
CORRECTION: @11:46 when I calculated VaR, I made a mistake.
The code I wrote was:
VaR = portfolio_value * portfolio_std_dev * norm.ppf(cl) * np.sqrt(days/252)
The corrected code is:
VaR = portfolio_value * (norm.ppf(1 - cl) * portfolio_std_dev * np.sqrt(days / 252) - historical_returns.mean() * days)
The original code does not account for the fact that the portfolio has an expected return for the time period in question and the downside risk must be subtracted from that. The new code does account for this.
💻 Free Code Here: ryanoconnellfinance.com/estimating-value-at-risk-with-python/
🎓 Tutor With Me: 1-On-1 Video Call Sessions Available
► Join me for personalized finance tutoring tailored to your goals: ryanoconnellfinance.com/finance-tutoring/
👨💼 My Freelance Financial Modeling Services:
► Custom financial modeling solutions tailored for your needs: ryanoconnellfinance.com/freelance-finance-services/
Excellent video, loved it. You can also replace the for loop with a list comprehension statement: VaRs = [portfolio_value * (norm.ppf(1 - x) * portfolio_std_dev * np.sqrt(days / 252) - historical_returns.mean() * days) for x in confidence_levels]
@@GilberTellez97 Thank you for that tip!
Thank you for the v clear explanation!However, just thinking the coding should be having opposite sign? as the formula should be - [mu + Zscore x std dev] based on my understanding?
that is:
VaR = portfolio_value * (-(norm.ppf(1 - cl) * portfolio_std_dev * np.sqrt(days / 252) + historical_returns.mean() * days))
Excellent work Ryan. I was able to replicate the numbers above. However, I think that in the above code, instead of norm.ppf(1-cl), it should be norm.ppf(cl) as it should be a positive number based on how the formula is structured.
Thank you for your feedback! The use of norm.ppf(1 - cl) is indeed correct for calculating VaR, as it aligns with the convention of measuring the left tail risk, which corresponds to the lower percentile of the distribution
Very easy to understand from your teaching. Thank you!
I am learning a lot from your video keep posting its of great value for us. I am thankful for the video keep up the good work.
It is my pleasure! I appreciate the kind words and I will keep posting more
I very much appreciate your effort putting into this video.
Thank you, your feedback means a lot!
Hello Ryan! Many thanks for sharing all this knowledge! I really appreciate these rich contents and help me a lot for inducing Python on that usage.
It is my pleasure and I'm glad to see that you are getting value out of it! Thank you for the feedback
Great content!!
Why do you use 5 days , whats change If you use 1 for example?
Loved your video. Could you please make a video on Full revaluation VaR calculation.
Thank you! I can look into this in the future
Great video! One question though, do you use any distribution that best describes the data to calculate predicted VaR since you're using parametric method? Thanks!
In this video specifically, I just assumed a standard normal distribution. You could take the analysis a step further by first determining which distribution fits the data best however
Thanks!
Appreciate it! This is the first time I've gotten one of these Super Thanks donations 🙂
@@RyanOConnellCFA hopefully first of many. Thanks for your time and effort into making these videos!
@@sahilk27 It is my pleasure! And I hope so 🤞
thanks for this kind of videos ( VaR's method), they were so helpfu😀l. i hope you help us with the codes of backtesting in VaR.
It is my pleasure! I can look into making a video on that in the future 🙂
Thank you 🙏
@@zakichet7493 👍
Hi Ryan! Great videos, I am learning a lot.
Quick question: in minute 12:00, you computed VaR as:
portfolio_value * portfolio_std_dev * norm.ppf(cl) * np.sqrt(days/252)
Shouldn't it be:
portfolio_value * portfolio_std_dev * expected_returns * days/252 + portfolio_value * portfolio_std_dev * norm.ppf(1 - cl) * np.sqrt(days/252)
Because if you computed as you did, aren't you assuming that the expected returns are 0? Note also that I wrote "1 - cl" instead of just cl.
Thanks!!
Great catch, you are right about that! I'm going to update my pinned comment and the code on my website with your correction. Thank you!
@@RyanOConnellCFA great! Very happy to help
@@manuelchabierescolaperez9444 Thanks! I think this code best reflects the needed addition of the expected return for the day:
VaR = portfolio_value * (norm.ppf(1 - cl) * portfolio_std_dev * np.sqrt(days / 252) - historical_returns.mean() * days)
Can you please explain how to interpret it?
First of all, thanks for this video. It's very enlighning and helpful. For learning purposes, I'm calculating the VaR of my personal portfolio and something odd is happening. The VaRs signals are negative and positive in the chart. It's like the VaR is positive. However, as far as I understand, the VaR is supposed to be a kind of loss figure, which means that the signal in he chart should be negative. Can you help me, please?
You are correct that you should not have both negative and positive VaRs. Its hard for me to tell whats going wrong without seeing your code. The best thing to do is to go back through and make sure you havent incorrectly typed a single thing. A negative sign in the wrong place could throw everything off
VaR is reported as a positive number. In the line where VaR is plotted, the signs are reversed, that's why the difference might be appearing.