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]
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
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
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!!
@@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)
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
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))
another great vid.... so happy to see yout channel taking off!
Thank you so much Ron, I remember you actively commenting from the very beginning!
Very easy to understand from your teaching. Thank you!
I very much appreciate your effort putting into this video.
Thank you, your feedback means a lot!
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
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
Loved your video. Could you please make a video on Full revaluation VaR calculation.
Thank you! I can look into this in the future
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
Can you please explain how to interpret it?
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 🤞
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 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)
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.