Position Sizing from Hidden Markov Model State Probabilities
Summary
The answer describes a trading approach for returns generated by a known two-state Gaussian hidden Markov model. Rather than infer regimes from moving averages or other heuristic indicators, it recommends updating the probability of each hidden state from observed returns with the HMM forward algorithm. Those probabilities produce a conditional expected return and variance for the next period.
Under unconstrained mean-variance sizing, the proposed position is proportional to expected return divided by variance, which allocates more exposure when the forecast return is favorable and less when forecast risk is high. The method is presented as a per-period decision that is recalculated as new returns arrive. The answer notes that transaction costs make the problem depend on future expected returns and variances, and that constraints can break the stated equivalence between Sharpe maximization and mean-variance optimization. Its result therefore rests on a simplified model and unconstrained positions; it does not give empirical performance evidence.
Key ideas
- Known HMM parameters let the trader estimate hidden-state probabilities directly from returns.
- The forward algorithm converts those probabilities into conditional forecasts of return and variance.
- In the unconstrained setup, position size is proportional to expected return divided by variance.
- Updating positions as new observations arrive adapts exposure to changing state probabilities.
- Transaction costs and trading constraints require a more involved optimization.
Tags
Full text
# Optimal trading strategy in toy world of simple Hidden Markov model with Gaussians
# Optimal trading strategy in toy world of simple Hidden Markov model with Gaussians
I want to solve the following optimization problem: What is the optimal general trading strategy (in the sense of the highest Sharpe ratio) on a time series which is the result of a Hidden Markov model with two states and two Gaussians with a known transition matrix and known mean and variance parameters?
The trading strategy should ideally be based only on easily observable technical indicators like intrinsic momentum or SMAs and volatility. My gut feeling is that the optimal lookback period for calculating these must depend on the transition matrix.
I am grateful (as usual) for ideas, literature, code (ideally R :-) and the like
## Answer by MichaelJ (score 3, accepted)
https://quant.stackexchange.com/a/12633
I don't understand how technical indicators are at all relevant to the question. State probabilities can be generated directly from the returns if the model is known. There is no need to guess at heuristic trading rules based on technical indicators.
Let $r_t$ be the return at time $t$. Your model is
- $E\{r_t | s_t=i\} \sim N(\mu_i,\sigma^2_i), i=0,1$
- $P\{s_t=i | s_{t-1}, s_{t-2}, ...\} = P\{s_t=i|s_{t-1}\}$
In other words, the state is Markov and returns are normal with known mean, variance in either state. Suppose we are standing at time $t$. We need to first determine $P\{s_t = 0 | r_{t-1}, ..., r_0\} = p_t$. Use the forward backward algorithm aka dynamic programming and implemented in most HMM packages.
Now $$E\{r_t\} = p_t\mu_0 + (1-p_t)\mu_1$$ and $$Var\{r_t\} = p_t\sigma_0^2 + (1-p_t)\sigma_1^2.$$
Now we need to choose our position $x$ to maximize the Sharpe
$$\frac{E\{x'r\}}{\sqrt{Var\{x'r\}}}$$
This is equivalent (up to a scaling factor) to the mean-variance problem
$$\min_x \{\lambda x' \Sigma x - x'\bar{r} \},$$ where $\Sigma$ is a diagonal matrix with $Var\{r_t\}, t= 0,1,...,T$ on the diagonal and $\bar{r} = E\{r\}$. The proof of this fact is by contradiction. Suppose there is an $x$ with higher Sharpe that isn't the solution to the mean-variance problem, $x^*$. We can scale $x$ by a positive constant $\alpha$ so that we have $\alpha \bar{r}'x = \bar{r}'x^*$ . At the same time, we know that $$\alpha\sqrt{x'\Sigma x} < \sqrt{(x^*) ' \Sigma x^*}$$ because $x^*$ isn't Sharpe-optimal. Squaring both sides gives
$$\alpha^2 x'\Sigma x < (x^*) ' \Sigma x^*.$$
The fact that $\alpha x$ has the same mean and strictly lower variance contradicts the assumption that $x^*$ is the solution. We thus conclude that the mean variance solution is always Sharpe optimal. Note that in more general problems (e.g. with constraints) this equivalence doesn't necessarily hold. Now the solution to the mean-variance problem (take derivative and set to zero) is just $\frac{1}{2 \lambda}\Sigma^{-1} \bar{r}$. However, at time $t$ we don't have to worry about anything other than $x_t$. It is possible to compute $x$ for future times that are optimal with respect to current expectations, but in practice it is better to re-run the forward-backward algorithm after we observe the next return and then re-compute $x_{t+1}$. The optimal solution therefore is to bet proportionally to $\frac{E\{r_t\}}{Var\{r_t\}}$. This has an intuitive interpretation as $$\frac{E\{r_t\}}{Var\{r_t\}} = \frac{E\{r_t\}}{\sqrt{Var\{r_t\}}} \frac{1}{\sqrt{Var\{r_t\}}}$$ so that $$x_t\sqrt{Var\{r_t\}} = \frac{E\{r_t\}}{\sqrt{Var\{r_t\}}},$$ i.e. take risk proportional to expected Sharpe at each time.
If you have transaction costs then you need to consider future means and variances, which makes the problem more difficult, but doable.Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.