This vignette1 introduces the Viterbi algorithm for state decoding. The decoded state sequence in combination with the estimated model parameters can be used for forecasting.
For financial markets, it is of special interest to infer the underlying (hidden) states in order to gain insight about the actual market situation. Decoding a full time series \(S_1, \ldots, S_T\) is called global decoding. Hereby, we aim to find the most likely trajectory of hidden states under the estimated model. Global decoding can be accomplished by using the so-called Viterbi algorithm which is a recursive scheme enabling to find the global maximum without being confronted with huge computational costs. To this end, we follow Zucchini, MacDonald, and Langrock (2016) and define \[\zeta_{1i} = Pr(S_1 = i, X_1 = x_1) = \delta_i p_i(x_1)\] for \(i = 1, \ldots, N\) and for the following \(t = 2, \ldots, T\) \[\zeta_{ti} = \operatorname*{max}_{s_1, \ldots, s_{t-1}} Pr(S_{t-1} = s_{t-1}, S_t = i, X_t = x_t).\] Then, the trajectory of most likely states \(i_1, \ldots, i_T\) can be calculated recursively from \[i_T = \operatorname*{argmax}_{i = 1, \ldots, N} \zeta_{Ti}\] and for the following \(t = T-1, \ldots, 1\) from \[i_t = \operatorname*{argmax}_{i = 1, \ldots, N} (\zeta_{ti} \gamma_{i, i_{t+1}}).\] Transferring the state decoding to HHMMs is straightforward: at first the coarse-scale state process must be decoded. Afterwards, by using this information the fine-scale state process can be decoded, see Adam et al. (2019).
decode_states()
functionWe revisit the DAX model of the vignette on model estimation:
data(dax_model_3t)
The underlying states can be decoded via the decode_states()
function:
<- decode_states(dax_model_3t)
dax_model_3t #> Decoded states
We now can visualize the decoded time series:
plot(dax_model_3t)
Mind that the model is invariant to permutations of the state labels. Therefore, {fHMM} provides the option to switch labels after decoding via the reorder_states()
function, for example:
<- reorder_states(dax_model_3t, 3:1) dax_model_3t
Having decoded the underlying states, it is possible to compute the state probabilities of next observations. Based on these probabilities and in combination with the estimated state-dependent distributions, next observations can be predicted, compare Zucchini, MacDonald, and Langrock (2016):
predict(dax_model_3t, ahead = 10)
#> state_1 state_2 state_3 lb estimate ub
#> 1 0.00515 0.97315 0.02170 -0.02190 -0.00023 0.02145
#> 2 0.01006 0.94769 0.04225 -0.02179 -0.00020 0.02138
#> 3 0.01477 0.92354 0.06170 -0.02168 -0.00018 0.02132
#> 4 0.01926 0.90063 0.08011 -0.02158 -0.00016 0.02126
#> 5 0.02356 0.87890 0.09754 -0.02148 -0.00014 0.02121
#> 6 0.02767 0.85829 0.11403 -0.02140 -0.00012 0.02116
#> 7 0.03161 0.83874 0.12965 -0.02131 -0.00010 0.02111
#> 8 0.03537 0.82020 0.14442 -0.02124 -0.00008 0.02107
#> 9 0.03898 0.80261 0.15841 -0.02116 -0.00007 0.02103
#> 10 0.04243 0.78593 0.17164 -0.02110 -0.00005 0.02100
This vignette was build using R 4.1.0 with the {fHMM} 1.0.3 package.↩︎