Value at Risk (VaR) In Python: Monte Carlo Method

Sdílet
Vložit
  • čas přidán 5. 09. 2024

Komentáře • 51

  • @RyanOConnellCFA
    @RyanOConnellCFA  Před rokem +2

    💻 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/

  • @aarondelarosa3146
    @aarondelarosa3146 Před rokem +6

    Hello Ryan. I found your codes on historical and Parametrical VaR. Excellent!

    • @RyanOConnellCFA
      @RyanOConnellCFA  Před rokem +2

      Awesome, I will be posting videos with this code very soon as well!

  • @runycalmera-business
    @runycalmera-business Před rokem +5

    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.

    • @RyanOConnellCFA
      @RyanOConnellCFA  Před rokem

      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

  • @uignireddngfiurdsgfiurdse

    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.

    • @RyanOConnellCFA
      @RyanOConnellCFA  Před rokem +3

      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.

  • @yuvrajpradyumna2193
    @yuvrajpradyumna2193 Před 8 měsíci +1

    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!

    • @RyanOConnellCFA
      @RyanOConnellCFA  Před 8 měsíci +1

      Glad you like them! Thank you for the feedback and I hope you can keep finding value in the videos

  • @michael.
    @michael. Před měsícem +1

    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?

  • @jimherebarbershop8188
    @jimherebarbershop8188 Před rokem +1

    Loved your excel stuff so happy to see python stuff!!!

    • @RyanOConnellCFA
      @RyanOConnellCFA  Před rokem

      Much appreciated, I will be adding a lot more Python videos in the future!

  • @faranak777
    @faranak777 Před 11 měsíci +1

    Great video! Much appreciated

  • @paulparker4078
    @paulparker4078 Před 9 měsíci +1

    Thanks Ryan, great stuff

  • @sayanbanerjee7214
    @sayanbanerjee7214 Před 4 měsíci

    What's the logic behind multiplying weights (The non-transposed one) for the second time with cov_matrix while calculating variance?

  • @eunikegracee
    @eunikegracee Před rokem +1

    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?

    • @RyanOConnellCFA
      @RyanOConnellCFA  Před rokem

      It is my pleasure! I plan to make a video on this topic in the future

    • @eunikegracee
      @eunikegracee Před rokem

      Cant wait to see it! Do you have any source for the algorithm especially the formulas to calculate VaR for stock portfolios?@@RyanOConnellCFA

  • @meggyeggy2cute
    @meggyeggy2cute Před 7 měsíci +1

    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?

    • @RyanOConnellCFA
      @RyanOConnellCFA  Před 6 měsíci +1

      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.

  • @victoricus1
    @victoricus1 Před rokem +3

    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?

    • @RyanOConnellCFA
      @RyanOConnellCFA  Před rokem +3

      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

  • @haneulkim4902
    @haneulkim4902 Před rokem +2

    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?

    • @RyanOConnellCFA
      @RyanOConnellCFA  Před rokem +2

      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

  • @aarondelarosa3146
    @aarondelarosa3146 Před rokem +2

    Can you estimate a VaR for a Portfolio in python? Historical and Parametrical.

    • @RyanOConnellCFA
      @RyanOConnellCFA  Před rokem +2

      My Historical VaR can be found here: czcams.com/video/jZJsPi4j7wQ/video.html

  • @hengshengrao10
    @hengshengrao10 Před 7 měsíci +1

    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 ?

    • @RyanOConnellCFA
      @RyanOConnellCFA  Před 7 měsíci

      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

  • @aarondelarosa3146
    @aarondelarosa3146 Před rokem +2

    Excellent. What's the VaR in dollars?

    • @RyanOConnellCFA
      @RyanOConnellCFA  Před rokem +2

      Thank you! At @16:55 I print out the VaR in dollar terms

    • @aarondelarosa3146
      @aarondelarosa3146 Před rokem +2

      @@RyanOConnellCFA You're absolutely right. Oops. Sorry.

  • @meggyeggy2cute
    @meggyeggy2cute Před 7 měsíci +1

    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 🙏

    • @RyanOConnellCFA
      @RyanOConnellCFA  Před 6 měsíci +1

      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?

    • @meggyeggy2cute
      @meggyeggy2cute Před 6 měsíci +1

      @@RyanOConnellCFA yes, thanks so much!!

    • @RyanOConnellCFA
      @RyanOConnellCFA  Před 6 měsíci

      @@meggyeggy2cute my pleasure!

    • @meggyeggy2cute
      @meggyeggy2cute Před 6 měsíci +1

      @@RyanOConnellCFA would you happen to have any videos about backtesting?

    • @RyanOConnellCFA
      @RyanOConnellCFA  Před 6 měsíci +1

      @@meggyeggy2cute Not right now but I plan to add some in the future!

  • @DerrickKKumi
    @DerrickKKumi Před rokem +2

    Please did you use VS Code for your programming?

    • @RyanOConnellCFA
      @RyanOConnellCFA  Před rokem +1

      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

  • @filipborzecki7426
    @filipborzecki7426 Před 7 měsíci +1

    Hi,
    How could I modify this code to get the Conditional Value at Risk as well?

    • @RyanOConnellCFA
      @RyanOConnellCFA  Před 7 měsíci

      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.

  • @aarondelarosa3146
    @aarondelarosa3146 Před rokem +2

    By the way, can you calculate CVaR?

  • @monicaankala6210
    @monicaankala6210 Před 7 měsíci

    when you calculated the weights of each security, you did -> (1/len)*len. doesn't this just make the weights as 1?

    • @RyanOConnellCFA
      @RyanOConnellCFA  Před 7 měsíci

      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%

  • @aarondelarosa3146
    @aarondelarosa3146 Před rokem

    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?

    • @RyanOConnellCFA
      @RyanOConnellCFA  Před rokem +1

      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

    • @meggyeggy2cute
      @meggyeggy2cute Před 7 měsíci +1

      I’m wondering the same thing!! I need to incorporate the cholesky decomp in my model and I’m lost!

    • @aarondelarosa3146
      @aarondelarosa3146 Před 7 měsíci

      Hello Meghan. It was resolved already. @@meggyeggy2cute