I Day Traded $1000 with the Hidden Markov Model

แชร์
ฝัง
  • เผยแพร่เมื่อ 17 ม.ค. 2025

ความคิดเห็น • 88

  • @hu5116
    @hu5116 10 หลายเดือนก่อน +25

    Great Video! However, there appear to be a number of missing steps. Maybe that was in a prior video but you never said so. For example, where did the transition matrix come from? You mention very briefly about from past data, but never connected what that means or how it’s done. Similar for rest of the process. Maybe a follow up “show me” video would be in order.

    • @sinisinsini
      @sinisinsini หลายเดือนก่อน

      up !

    • @renasdopai
      @renasdopai หลายเดือนก่อน

      The matrix came from data from march 2023 to march 2024, 8:30

    • @renasdopai
      @renasdopai หลายเดือนก่อน

      I don’t think this video is correct like at all since a hidden markov works with 2 matrix’s, working back and forth to calculate the probability of a observable sequence given the hidden sequence
      And he only has 1 matrix

  • @ekaterinaburakova8629
    @ekaterinaburakova8629 10 หลายเดือนก่อน +5

    Awesome series, keep up the good work!

    • @ritvikmath
      @ritvikmath  10 หลายเดือนก่อน

      Thanks, will do!

  • @gsm7490
    @gsm7490 9 หลายเดือนก่อน +2

    Really great content! If you don’t mind, may be there is another way to use ML and NN in financial market: you can not only predict future returns and prices, you can also predict how different factors affect price fluctuations right at the moment?

  • @ResilientFighter
    @ResilientFighter 10 หลายเดือนก่อน +1

    Thanks Ritvik! Great content as usual

    • @ritvikmath
      @ritvikmath  10 หลายเดือนก่อน

      My pleasure!

  • @garyc9402
    @garyc9402 9 หลายเดือนก่อน +1

    @ritvikmath I have a trading system that I'm currently adding hidden markov models to. Any chance I can run some ideas by you? It has to do with programmatically detecting which price distribution regime changes using a hidden markov model.

  • @humanparaquat69
    @humanparaquat69 10 หลายเดือนก่อน +11

    We love your videos dude!

    • @ritvikmath
      @ritvikmath  10 หลายเดือนก่อน +4

      Love you all 🙏

  • @nathanlewis42
    @nathanlewis42 10 หลายเดือนก่อน +11

    Is the code for this in your TH-cam repo on github?

  • @poisonza
    @poisonza 10 หลายเดือนก่อน +10

    can you do a notebook demo on how to fit hmm and do inference on out-of-sample data?
    maybe include these
    - fit returns on other distribution other than gaussian (single, multi-variate case)
    - set initial state transition matrix ; like higher value to diagonal entries (since regimes rarely changes)
    - show us how the input(length of observation) might change the inference value

    • @ritvikmath
      @ritvikmath  10 หลายเดือนก่อน +6

      Good suggestion!

    • @simonebenzi4189
      @simonebenzi4189 4 หลายเดือนก่อน +1

      I agree with him, a practical tutorial would be very useful!

  • @posthocprior
    @posthocprior 10 หลายเดือนก่อน +2

    Maybe I've missed something: what exactly is the latent variable you're trying to approximate? If, as you've described, the sentiment values of positive, negative, and neutral correspond to distinct normal distributions with respective means, then this is simply a linear model. It would be hidden if the three sentiment values corresponded to the three, respective distributions and the latent variable was the stock price.

  • @saikrishna0716
    @saikrishna0716 9 หลายเดือนก่อน +1

    Intriguing Content as usual! Btw, how did you come you with the sentiment?

    • @ritvikmath
      @ritvikmath  9 หลายเดือนก่อน

      basically the model outputs hidden states as integers {0,1,2}, each with a corresponding mean for its normal distribution. We map the integers to sentiments via ranking the means.

  • @DistortedV12
    @DistortedV12 10 หลายเดือนก่อน +2

    How did you train this? How does it map to sentiment if you are just putting Hidden units to 3? As an aside, I think you should start adding jupyter notebooks to your videos like Karpathy

    • @ritvikmath
      @ritvikmath  10 หลายเดือนก่อน +2

      The model outputs numerical hidden states of 0,1,2 and mu,sigma for each. We map the numerical hidden states to sentiments by ascending mu

    • @DistortedV12
      @DistortedV12 10 หลายเดือนก่อน

      "ascending mu?" do you have any sentiment data to validate this on? @@ritvikmath

  • @superman39756
    @superman39756 10 หลายเดือนก่อน +2

    Can you explain how you traded based off this model specifically? Did you predict sentiment for the next day and sell/buy/hold based on that for a day with a rolling window of 1 ?

    • @ritvikmath
      @ritvikmath  10 หลายเดือนก่อน +1

      Yes exactly

  • @emmang2010
    @emmang2010 10 หลายเดือนก่อน +2

    great video!

    • @ritvikmath
      @ritvikmath  10 หลายเดือนก่อน

      Thanks!

  • @cv462-l4x
    @cv462-l4x 10 หลายเดือนก่อน +19

    Maybe you saw some article of guy who says machine learning cannot predict finances. All what we can do is optimize models like Fama and French model to find more important factors affecting stocks prices. Or for example use machine learning to optimize weights of given assets in the portfolio. What do you think? Article with name "Why machine learning can’t predict the prices of stocks or cryptocurrencies: the limits to applying machine learning in empirical asset pricing". Its easy to find I dont want to advertise it. Just wanted to know your thoughts on the subject

    • @poisonza
      @poisonza 10 หลายเดือนก่อน +1

      The Fama and French model is essentially a straightforward application of Ordinary Least Squares (OLS) on factors to analyze a portfolio of asset returns. Since OLS is a machine learning algorithm, I fail to see your point. Perhaps you're skeptical about complex models with too many parameters, suggesting that in essence, the parameters cannot be accurately estimated?

    • @cv462-l4x
      @cv462-l4x 10 หลายเดือนก่อน +1

      @poisonza you can google the article I mentioned. Its big enough. That's not my opinion. That's the subject of discussion

    • @poisonza
      @poisonza 10 หลายเดือนก่อน +4

      @@cv462-l4x "All what we can do is optimize models like ..." isnt this your claim?
      article you suggested says the opposite
      "Traditional empirical asset pricing models like CAPM and Fama-French have limitations in accurately capturing systematic risks and explaining stock returns. These models may be over-parameterized, mis-specified, or unable to capture non-linear relationships among factors."

    • @cumibakar10
      @cumibakar10 10 หลายเดือนก่อน +8

      It’s true, you cannot “predict stock prices”. Stock prices in an efficient market are semi-martingales: it’s mostly random walk. Why is it random? Because everything that can be predicted has already been predicted and priced into the asset. The price of all assets at any given time already incorporates all available information.
      That’s why no firm on wall street tries to “predict future prices”. They either sell financial services to companies, provide a market, or other services (or in case of HFT try to arbitrage millisecond differences in prices between markets).
      This is the danger of data science/machine learning without domain expertise. Or even a basic understanding of the domain.

    • @gabrielplzdks3891
      @gabrielplzdks3891 10 หลายเดือนก่อน +3

      ​@@cumibakar10Right, if I'm not mistaken what quants do at most is manage risk in portfolios by trying to accurately model the components of some commodity rather than predicting any specific future state. If the market is efficient then you can't beat anyone and if it isn't, you aren't the one to find out either.

  • @EvilMinion111
    @EvilMinion111 3 หลายเดือนก่อน

    Awesome! How did you derive the values in the transition matrix? Did you just measure the probabilities over a given timeframe?

  • @rexlaurus5894
    @rexlaurus5894 10 หลายเดือนก่อน +5

    How did you arrive at the transition probabilities for sentiment? Like you said, isnt it hidden?
    How about the distributions?
    Would be really helpfull to get the code for these videos. This is cool, but we dont even know how the RNN was implemented.

    • @ritvikmath
      @ritvikmath  10 หลายเดือนก่อน +3

      So once the HMM is fitted we have 3 hidden states encoded as 0, 1, 2 in the code. We associate the one with the smallest mean as negative sentiment, etc.

    • @rexlaurus5894
      @rexlaurus5894 10 หลายเดือนก่อน +4

      @@ritvikmath Thank you. That is for the distributions i believe.
      But how about coming up with the sentiment transition probabilities?

    • @drdca8263
      @drdca8263 10 หลายเดือนก่อน

      @@rexlaurus5894I would imagine you could just do (constrained) gradient descent on the training data?

  • @cv462-l4x
    @cv462-l4x 10 หลายเดือนก่อน +3

    Do you really stock trade? I think these 1k experiments are just paper experiments, correct? Do you have any specific view on the trading? If you do, what kind of prediction technique would you use considering all possible variants?

    • @ritvikmath
      @ritvikmath  10 หลายเดือนก่อน +6

      These are actual experiments and I do trade. Of course you can argue why not just run them as paper trading and that’s a totally valid point. It just personally makes me more invested (no pun intended) if I put down real money

    • @Dani-gs8zf
      @Dani-gs8zf 10 หลายเดือนก่อน +3

      @@ritvikmath And are the fees calculated in these returns?

    • @ritvikmath
      @ritvikmath  10 หลายเดือนก่อน +4

      Yes any fees are included in the final return calculation

  • @l.d.t.6327
    @l.d.t.6327 12 วันที่ผ่านมา

    So you have a model fed with 1 year of data, and you test it for 1 day on the stock market? How do you know your results aren't just random (luck in this case)?
    How many trades did you make during the day?

  • @jamesprivet
    @jamesprivet 8 หลายเดือนก่อน

    Very nice! Do you calculate the HMM states and probabilities on the price data or the price return data?

  • @Pythoncode-daily
    @Pythoncode-daily 9 หลายเดือนก่อน

    Pretty interesting. If you build a transition matrix between the states rising trend - neutral - falling trend, then the probabilities of transitions will be very different from the proposed matrix of transitions between sentiments. The probability of observing a growing trend tomorrow, if we saw a growing trend today, will be about 78%, if it was neutral, then it will remain neutral about 60%, if there was a falling trend, then the falling trend will disappear about 81%. The only problem is with the cost of transition (for example, from a growing market to a neutral market). It can be so high that the mathimatical expectation becomes close to zero, despite such remarkable probabilities.

  • @andrashorvath2411
    @andrashorvath2411 10 หลายเดือนก่อน +2

    Are you considering commission fees? In my experience that makes the return hard to go to positive at all because the fees make it a negative sum game and so with a negative bias which gives loss on long term with high probability.

    • @ritvikmath
      @ritvikmath  10 หลายเดือนก่อน +2

      Yes they are included

    • @andrashorvath2411
      @andrashorvath2411 10 หลายเดือนก่อน

      @@ritvikmath Thank you. I recommend you to do a statistical T test on your profit return values (negative and positive values over time) to see if its mean is significantly different from zero to the positive direction. If so, then you know your model has some real power. This helps to see out of the fog of random noise. Also the non normality of the distribution of profits must be corrected to get a better picture. Since the T test converges to be Mean / SE as the number of values grows, you can use this as a simple metric, but I recommend you to use the absolute cubic differences instead of the squared one when calculating SE, this can account for the non normality in a simpler way. I can derive this for you if interested. Keep up the good work. Cheers.

    • @ekaterinaburakova8629
      @ekaterinaburakova8629 10 หลายเดือนก่อน

      @@ritvikmath Amazing, I thought this is a gross return purely from the price diference!

  • @decluttercharts
    @decluttercharts 8 หลายเดือนก่อน

    Great Video! working on exact model and this helps to validate mine

  • @matiasmardijs8675
    @matiasmardijs8675 20 วันที่ผ่านมา

    Something I don't understand about these unsupervised latent variable models, is how you interpret the results, since what you get isn't labeled. As in Latent Dirichlet Allocation where the topics aren't labeled, here you don't have labeled hidden states. You just assumed that the emission distribution with positive skewness corresponded to the positive hidden state, for example. How is this map from hidden state to interpretation usually done?

  • @Ryuuuuuk
    @Ryuuuuuk 10 หลายเดือนก่อน +1

    What about changing the time scale of your model. Currently your "delta t" is 1 day, but what about e.g. the sentiment over the course of a week/ month/ year

    • @ritvikmath
      @ritvikmath  10 หลายเดือนก่อน +2

      Cool suggestion!

  • @noggginn
    @noggginn 3 หลายเดือนก่อน

    How many trades are you making during the test?

  • @Gingeey23
    @Gingeey23 10 หลายเดือนก่อน +4

    Great video - thanks for sharing. I think it would be great to see what the average pct. change of the SP500 was (as a whole, not as an index) to check for general market movement bias. Great to see the comparison of the RNN and random approach. Also I never knew a coffee in the States was 8$!! Outrageous

    • @ritvikmath
      @ritvikmath  10 หลายเดือนก่อน +2

      Thanks! And appreciate the suggestions.

  • @shubh4319
    @shubh4319 6 หลายเดือนก่อน

    Great video . Can you do some Multivariate analysis data series with different business case scenarios

  • @Sam-tg4ii
    @Sam-tg4ii 5 หลายเดือนก่อน

    Where did the 0.4% for RNN come from suddenly?

  • @petegranneman1623
    @petegranneman1623 8 หลายเดือนก่อน

    Interested concept. How did you estimate transition (and emission) probabilities? Obviously the most likely sequence of hidden states could be inferred with the viterbi algorithm (covered in one of your other videos). I'm assuming you used the baum welch algorithm to estimate state transition probabilities (but this was not explained).

  • @xxxs8309
    @xxxs8309 8 หลายเดือนก่อน

    So, how is it different than technical analysis

  • @notejp-d8w
    @notejp-d8w 5 หลายเดือนก่อน

    my sentiment on this video: thumbs up!

  • @vikraal6974
    @vikraal6974 10 หลายเดือนก่อน

    Does Deep Learning acheive this by default?

  • @miraclemaxicl
    @miraclemaxicl 10 หลายเดือนก่อน

    what did S&P500 return on that day?

  • @anindadatta164
    @anindadatta164 7 หลายเดือนก่อน

    Is it possible to predict volatility of an asset using Markov model? As volatility is mean reverting, so should the return distribution curves be narrower?

  • @miraclemaxicl
    @miraclemaxicl 10 หลายเดือนก่อน

    can you run a different test on this strategy: buy 5 stock and short 5 stocks ($500 long + $500 short), allocated by historical volatility?

    • @ritvikmath
      @ritvikmath  10 หลายเดือนก่อน

      Interesting idea!

  • @marcfruchtman9473
    @marcfruchtman9473 10 หลายเดือนก่อน +3

    Great explanation. It would be interesting to follow the predictions of the models on a regular basis just to see what happens when the market shifts. Ie, does the model predict that... or more than likely, can not make such predictions.
    Thanks for the video.

    • @ritvikmath
      @ritvikmath  10 หลายเดือนก่อน +1

      Great suggestion!

    • @kab791
      @kab791 10 หลายเดือนก่อน +1

      @@ritvikmathmaybe do a weekly session on the results of previously implemented model. Good idea for useful and interesting content

  • @Ryuuuuuk
    @Ryuuuuuk 10 หลายเดือนก่อน

    I'm living in Italy where a coffee costs less than two dollars so I choked a bit ahaha

    • @ritvikmath
      @ritvikmath  10 หลายเดือนก่อน

      😂

  • @Septumsempra8818
    @Septumsempra8818 10 หลายเดือนก่อน

    Triple Barrier Labeling or some of the other work by Prof. de Prado

  • @JonathanFraser-i7h
    @JonathanFraser-i7h 7 หลายเดือนก่อน

    0.02% return in a day is 7.5% annual return. Pretty much market index
    0.2% daily is a 107% annual return on your investment (you money doubles every year), that's something.
    0.4% is something like 330% return per year.
    There's no way these returns are consistent.

    • @ritvikmath
      @ritvikmath  7 หลายเดือนก่อน

      Definitely not

    • @lcrocianilc
      @lcrocianilc 6 หลายเดือนก่อน

      @@ritvikmath To make things more authentic, I think you should make some backtesting over years. Just to be more clear with your audience 😃

  • @jeevacation
    @jeevacation 10 หลายเดือนก่อน

    Interesting

    • @ritvikmath
      @ritvikmath  10 หลายเดือนก่อน +1

      Indeed

  • @atxdeadgod
    @atxdeadgod 12 วันที่ผ่านมา

    MOST BS video ever. How to interpret the sentiment is it just the returns or something else? Filled of holes.