General Topics
Technical Topics
Brokerage Firms
Community Lounge
Site Support

# Hidden Markov Model

Discussion in 'Strategy Development' started by bearmountain, Oct 20, 2011.

1. ### bearmountain

Is anyone here familiar with the hidden markov model? I need help in understanding it, how would it apply to trading? Could someone please give me an example. Thanks.

Consider two friends, Alice and Bob, who live far apart from each other and who talk together daily over the telephone about what they did that day. Bob is only interested in three activities: walking in the park, shopping, and cleaning his apartment. The choice of what to do is determined exclusively by the weather on a given day. Alice has no definite information about the weather where Bob lives, but she knows general trends. Based on what Bob tells her he did each day, Alice tries to guess what the weather must have been like.

Alice believes that the weather operates as a discrete Markov chain. There are two states, "Rainy" and "Sunny", but she cannot observe them directly, that is, they are hidden from her. On each day, there is a certain chance that Bob will perform one of the following activities, depending on the weather: "walk", "shop", or "clean". Since Bob tells Alice about his activities, those are the observations. The entire system is that of a hidden Markov model (HMM).

Alice knows the general weather trends in the area, and what Bob likes to do on average. In other words, the parameters of the HMM are known. They can be represented as follows in the Python programming language:

states = ('Rainy', 'Sunny')

observations = ('walk', 'shop', 'clean')

start_probability = {'Rainy': 0.6, 'Sunny': 0.4}

transition_probability = {
'Rainy' : {'Rainy': 0.7, 'Sunny': 0.3},
'Sunny' : {'Rainy': 0.4, 'Sunny': 0.6},
}

emission_probability = {
'Rainy' : {'walk': 0.1, 'shop': 0.4, 'clean': 0.5},
'Sunny' : {'walk': 0.6, 'shop': 0.3, 'clean': 0.1},
}

In this piece of code, start_probability represents Alice's belief about which state the HMM is in when Bob first calls her (all she knows is that it tends to be rainy on average). The particular probability distribution used here is not the equilibrium one, which is (given the transition probabilities) approximately {'Rainy': 0.57, 'Sunny': 0.43}. The transition_probability represents the change of the weather in the underlying Markov chain. In this example, there is only a 30% chance that tomorrow will be sunny if today is rainy. The emission_probability represents how likely Bob is to perform a certain activity on each day. If it is rainy, there is a 50% chance that he is cleaning his apartment; if it is sunny, there is a 60% chance that he is outside for a walk.

2. ### Soon2Bgreat

Isn't half the trouble finding it in the first place?

Indeed. I know of a world class expert or two in this area that have asked me that question.
Finding the states to model is a big part of the challenge.

4. ### heech

What's the question, exactly...?

It's been a while since I've looked at Markov chains, but I'm extremely interested in the topic... in fact, I've been on the look out for a good/current intro grad student level book on the topic. If there are any references out there, I'd appreciate it.

But I do know enough to be dangerous... imagine Bob has told Alice over the past 5 days:

walk, shop, walk, shop, clean ...

Alice can work through the various conditional probabilities by chugging through the math... and end up with a probability distribution, for today, whether it was sunny or rainy where Bob lived.

As far as how you apply this to the trading world... in this specific case, you can imagine creating a similar model for any stock.

We can describe 3 states for stock A, for example:
{ under-valued, over-valued, fairly valued }

The observations could be:
{ up a lot, up a little, fell a lot, fell a little, flat }

We can describe the transition probability this way:

- if was under-valued before, 50% still under-valued, 30% fairly valued, 20% over-valued. i.e., if the stock was under-valued 1 bar ago, then there's a 50% chance it's still under-valued the next bar.

- similar for over-valued,
- similar for flat.

We can describe the "emission" probability this way:

under-valued: up a lot = 10%, up a little = 30%, fell a lot = 5%, fell a little =15%, flat = 40%

... idea is, *if* a stock was under-valued, how might it behave for any given bar. And we repeat for all of the other *states* that a stock might be in.

And now we can calculate the conditional probability... look back 20 bars, and try to look at how the stock moved. And based on that, we can try to guess whether indeed, the stock is today under-valued or over-valued.

The market is the game of misdirecting the other side to either sell you cheap or buy from you expensive.

Nothing related to this naive example of Alice and Bob, two apparently honest average IQ persons.

7. ### heech

That blog is a naive application of probability and Queuing theory to trading. The author appears as an expert but he makes assumptions he shouldn't be making. Especially in his last post where he tries to estimate toxicity. You never know how many brokers or dark pools a certain informed trader is using and whether he is bluffing or not. I remember Soros (I think) said once that before entering a very large buy order he would enter a few sell orders to see if the market was holding well.

You cannot use noise to predict anything useful. These people are noise traders and if they happen to make money it is not because of their superduper models but because of the stupidity of some other traders.

10. ### GoldStandard

One way people use HMMs in trading is for "regime shifting" or estimating what trading strategy would work best in the current conditions. For example to distinguish between market "regimes" where a trend following strategy would be successful vs times when a mean reverting strategy would be successful.

I don't have any experience with them myself but here are some links I have bookmarked:

http://www.mathfinance.cn/forecast-volatility-regime-switching-GARCH-model/
http://www.mathworks.com/matlabcentral/fileexchange/15789
http://ideas.repec.org/a/eee/jbfina/v32y2008i9p1970-1983.html

Also there's some material about Markov models in trading in Murray Gunn's book "Trading Regime Analysis - the probability of volatility".

#10     Oct 21, 2011
ET IS FREE BECAUSE OF THE FINANCIAL SUPPORT FROM THESE COMPANIES: