I am recently getting more interested in Hidden Markov Models (HMM) and its application on financial assets to understand their behavior. But what captured my attention the most is the use of asset regimes as information to portfolio optimization problem.
I am refering to this article
I searched in many sites for the code to apply an asset allocation problem based on HMM estimations but I can't find ..
I am extremely interesed ..I would be very grateful if you could provide me any code example that uses HMM to asset allocation problem.

  • $\begingroup$ I'm not sure what your asking, do you want references to papers applying HMM to asset allocation? $\endgroup$
    – Bob Jansen
    Jul 28, 2016 at 11:37
  • $\begingroup$ No, I am searching for program code to apply HMM to asset allocation problem $\endgroup$ Jul 28, 2016 at 11:42
  • $\begingroup$ So you would like the code that implements the method described in the article? $\endgroup$
    – Bob Jansen
    Jul 28, 2016 at 11:56
  • $\begingroup$ If it possible, if not, I would be grateful if you could give me any similar code example $\endgroup$ Jul 28, 2016 at 12:02
  • $\begingroup$ I'm not really a fan of this type of question as if the code exists the answer will just be a link to some source code. I'm not sure this fits the format. I've created a discussion and I'm hoping for community input. $\endgroup$
    – Bob Jansen
    Jul 28, 2016 at 12:09

2 Answers 2


The basic approach is as follows:

When you estimate the HMM you estimate three things:

  1. When you are in which state
  2. The drifts of your assets
  3. The covariance matrices of your assets

You would then take 2. and 3. for each state (1.) and feed it into your favourite allocation optimizer to estimate your optimal portfolio for each state.


  • $\begingroup$ @NourhaineNefzi: Thank you. If it was helpful it is appreciated if you could accept it. $\endgroup$
    – vonjd
    Aug 4, 2016 at 5:35

I don't think you'll find anything. Why don't you contact the authors? They must have some code to generate the HMM simulations in the paper, maybe they can share the code with you?

Have you checked the Supplementary Materials? Some papers have it.

If you're really determined, you can implement a HMM model yourself. You'll need to supply the Markov transition model (included in the paper), then use the EM algorithm to fit a model. Python scikit-learn has a module for that.

  • $\begingroup$ Thank you for your answer .. In fact, I am not familiar with codes, fortunately, there are packages developed in R and Matlab to train HMM .. but still don't know the technique to apply HMM to asset allocation problem .. I contacted the authors but I didn't get an answer.. thank you again for your care $\endgroup$ Jul 28, 2016 at 12:33
  • $\begingroup$ @NourhaineNefzi Unless you have very good reason, I don't think it's a good idea to try to reproduce the paper. You don't have the data set they used, you don't know how exactly they derived the diagrams, etc. Do you have a supervisor (like professor) who can guide you about your problem? $\endgroup$
    – SmallChess
    Jul 28, 2016 at 12:35
  • $\begingroup$ I am PhD student in Finance, I want to get a code example so I can apply it to the context of the research..unfortunately my supervisor can't help $\endgroup$ Jul 28, 2016 at 12:47
  • 1
    $\begingroup$ @John: exactly, pls. see my answer. quant.stackexchange.com/a/28349/12 $\endgroup$
    – vonjd
    Jul 29, 2016 at 7:37
  • 1
    $\begingroup$ I don't want to optimize a portfolio in each regime .. I need to optimize one portfolio based on HMM regimes ( as information).. in the paper for example, they generated scenarios based on HMM probabilities .. $\endgroup$ Jul 29, 2016 at 9:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.