💻 Code here: ryanoconnellfinance.com/monte-carlo-value-at-risk-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/
Enjoyed it. Used to calculate VaR for a futures energy trading portfolio. But the VaR calculations took a whole night in 2000. So I’m happy to see it can be brought down to seconds.
Pretty cool. Learning Python after being an insular R user is a real pain. The language looks cleaner but obscures a lot. Can't complain about the libraries though, it's been a real joy having so many good ones.
Hi Ryan, just started with your tutorial videos on learning python. Have to say you make things really interesting and easy with the way you teach. Also, you explain the concepts really well!
Why does the covariance matrix at 10:24 not output a 1 one when the tickers are plotted against each other? In other words, shouldn't the covariance between SPY and SPY be 1, and likewise for BND x BND and all the other tickers?
Awesome video! I’m trying to do a MC for structured notes. I want to include total yield and capital gains yields (because the price of structured notes is derived from the capital gains yields on the reference assets). But, having the two different returns seems to really complicate things. Any suggestions?
Incorporating total yield and capital gains yields into your Monte Carlo simulation for structured notes can indeed add complexity, but you can manage this by separately modeling each type of return and then combining them to simulate the overall return distribution. A practical approach might be to use the Monte Carlo framework to simulate the underlying asset price paths, then apply the total and capital gains yields accordingly to each simulated path before aggregating the results. This method allows you to capture the distinct characteristics of both yields, providing a more comprehensive analysis of potential outcomes.
Ryan, I always have problems interpreting VaR in simple terms - in your last example does 99% CI mean that I have a 1% chance to lose 82572 USD within 20 days on all the etfs?
That would be a decent interpretation of of a 99% confidence interval, 20 day VaR. I would recommend you to watch this video to understand the simplest aspects of VaR: czcams.com/video/2SMkbMDypXI/video.html
Thanks for an amazing video! @13:16 first part of addition is expected return on # of days and second part is expected variance on # of days, and z_score is for monte carlo simulation, correct? But I'm not understanding np.sqrt(days) part, could you elaborate?
My pleasure! The np.sqrt(days) component is basically just a way to scale standard deviation down from an annual value to the actual time period we want in days. We want to find the standard deviation for the specific time period we are interested in
Hi Ryan, I had watched your Parametric Method before this Monte Carlo vid. Just have a question about the step of creating a covariance matix for all the securities for both methods. In Parametric Method, the code goes like "cov_matrix = log_returns.cov()*252". But in Monte Carlo, it goes like "cov_matrix = log_returns.cov()". May I know why we have the discrepancy ?
You've made a keen observation regarding the use of the covariance matrix in the Monte Carlo simulation. In this specific case, the simulation is focused on short-term risk assessment over a 20-day period, rather than annualizing the risk, which is why the covariance matrix isn't multiplied by 252. I believe the current approach aligns with the intended purpose of estimating short-term VaR
I’m really confused about the days part in the section that calculates scenario gain/loss. Why just 5 days? Is the simulation based on the 5 days or the 15 year timeline? Thanks in advance to anyone willing to explain 🙏
The "5 days" in the Monte Carlo VaR calculation represents a short-term horizon (e.g., a week) for estimating potential gains or losses, while the simulation itself utilizes the 15-year timeline to generate the underlying statistical distributions. This approach allows for assessing risk over a specific, near-term period using long-term data. Does that make sense?
To calculate Conditional Value at Risk (CVaR) using your Monte Carlo simulation in Python, first run the simulations as you did for VaR. Then, filter the scenario returns to include only those that fall below the VaR threshold. Finally, calculate the average of these returns to get the CVaR, which represents the expected loss exceeding the VaR at your specified confidence level.
I have doubts. I found another video on MonteCarlo Simulation for a Stock Portfolio, but they're using GBM (Geometric Brownian Motion) and Cholesky Decomposition. What's the difference? What's the best model?
I'll have to look into this in the future. I have not performed any VaR analysis with GBM (Geometric Brownian Motion) and Cholesky Decomposition before
💻 Code here: ryanoconnellfinance.com/monte-carlo-value-at-risk-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/
Hello Ryan. I found your codes on historical and Parametrical VaR. Excellent!
Awesome, I will be posting videos with this code very soon as well!
Enjoyed it. Used to calculate VaR for a futures energy trading portfolio.
But the VaR calculations took a whole night in 2000.
So I’m happy to see it can be brought down to seconds.
Wow, that is just amazing that it took that long. I guess computers back then were just a fraction of a percentage of the power they are today
Pretty cool. Learning Python after being an insular R user is a real pain. The language looks cleaner but obscures a lot. Can't complain about the libraries though, it's been a real joy having so many good ones.
I also learned R before Python and for me R was such a confusing nightmare! I find Python to be so much simpler and more intuitive personally.
Hi Ryan, just started with your tutorial videos on learning python. Have to say you make things really interesting and easy with the way you teach. Also, you explain the concepts really well!
Glad you like them! Thank you for the feedback and I hope you can keep finding value in the videos
Why does the covariance matrix at 10:24 not output a 1 one when the tickers are plotted against each other? In other words, shouldn't the covariance between SPY and SPY be 1, and likewise for BND x BND and all the other tickers?
Loved your excel stuff so happy to see python stuff!!!
Much appreciated, I will be adding a lot more Python videos in the future!
Great video! Much appreciated
Thank you, it is my pleasure!
Thanks Ryan, great stuff
My pleasure Paul!
What's the logic behind multiplying weights (The non-transposed one) for the second time with cov_matrix while calculating variance?
Thank you for making this video, really helpful for my thesis! May i ask how to count CVaR/ ES in python using the Monte Carlo method?
It is my pleasure! I plan to make a video on this topic in the future
Cant wait to see it! Do you have any source for the algorithm especially the formulas to calculate VaR for stock portfolios?@@RyanOConnellCFA
Awesome video! I’m trying to do a MC for structured notes. I want to include total yield and capital gains yields (because the price of structured notes is derived from the capital gains yields on the reference assets). But, having the two different returns seems to really complicate things. Any suggestions?
Incorporating total yield and capital gains yields into your Monte Carlo simulation for structured notes can indeed add complexity, but you can manage this by separately modeling each type of return and then combining them to simulate the overall return distribution. A practical approach might be to use the Monte Carlo framework to simulate the underlying asset price paths, then apply the total and capital gains yields accordingly to each simulated path before aggregating the results. This method allows you to capture the distinct characteristics of both yields, providing a more comprehensive analysis of potential outcomes.
Ryan, I always have problems interpreting VaR in simple terms - in your last example does 99% CI mean that I have a 1% chance to lose 82572 USD within 20 days on all the etfs?
That would be a decent interpretation of of a 99% confidence interval, 20 day VaR. I would recommend you to watch this video to understand the simplest aspects of VaR: czcams.com/video/2SMkbMDypXI/video.html
Thanks for an amazing video! @13:16 first part of addition is expected return on # of days and second part is expected variance on # of days, and z_score is for monte carlo simulation, correct? But I'm not understanding np.sqrt(days) part, could you elaborate?
My pleasure! The np.sqrt(days) component is basically just a way to scale standard deviation down from an annual value to the actual time period we want in days. We want to find the standard deviation for the specific time period we are interested in
Can you estimate a VaR for a Portfolio in python? Historical and Parametrical.
My Historical VaR can be found here: czcams.com/video/jZJsPi4j7wQ/video.html
Hi Ryan, I had watched your Parametric Method before this Monte Carlo vid. Just have a question about the step of creating a covariance matix for all the securities for both methods. In Parametric Method, the code goes like "cov_matrix = log_returns.cov()*252". But in Monte Carlo, it goes like "cov_matrix = log_returns.cov()". May I know why we have the discrepancy ?
You've made a keen observation regarding the use of the covariance matrix in the Monte Carlo simulation. In this specific case, the simulation is focused on short-term risk assessment over a 20-day period, rather than annualizing the risk, which is why the covariance matrix isn't multiplied by 252. I believe the current approach aligns with the intended purpose of estimating short-term VaR
Excellent. What's the VaR in dollars?
Thank you! At @16:55 I print out the VaR in dollar terms
@@RyanOConnellCFA You're absolutely right. Oops. Sorry.
I’m really confused about the days part in the section that calculates scenario gain/loss. Why just 5 days? Is the simulation based on the 5 days or the 15 year timeline? Thanks in advance to anyone willing to explain 🙏
The "5 days" in the Monte Carlo VaR calculation represents a short-term horizon (e.g., a week) for estimating potential gains or losses, while the simulation itself utilizes the 15-year timeline to generate the underlying statistical distributions. This approach allows for assessing risk over a specific, near-term period using long-term data. Does that make sense?
@@RyanOConnellCFA yes, thanks so much!!
@@meggyeggy2cute my pleasure!
@@RyanOConnellCFA would you happen to have any videos about backtesting?
@@meggyeggy2cute Not right now but I plan to add some in the future!
Please did you use VS Code for your programming?
You are correct! I used a Jupyter Notebook file in VS Code. You can see how I set this up in this video here: czcams.com/video/uOqPElBsBmM/video.html
Hi,
How could I modify this code to get the Conditional Value at Risk as well?
To calculate Conditional Value at Risk (CVaR) using your Monte Carlo simulation in Python, first run the simulations as you did for VaR. Then, filter the scenario returns to include only those that fall below the VaR threshold. Finally, calculate the average of these returns to get the CVaR, which represents the expected loss exceeding the VaR at your specified confidence level.
By the way, can you calculate CVaR?
I can look into this in the future!
when you calculated the weights of each security, you did -> (1/len)*len. doesn't this just make the weights as 1?
Hey, thanks for your question Monica. For a portfolio of 5 stocks that are assumed to be equally weighted, it will weight each one at 20%
I have doubts. I found another video on MonteCarlo Simulation for a Stock Portfolio, but they're using GBM (Geometric Brownian Motion) and Cholesky Decomposition. What's the difference? What's the best model?
I'll have to look into this in the future. I have not performed any VaR analysis with GBM (Geometric Brownian Motion) and Cholesky Decomposition before
I’m wondering the same thing!! I need to incorporate the cholesky decomp in my model and I’m lost!
Hello Meghan. It was resolved already. @@meggyeggy2cute