LessonThis is a lesson restated in new words. Any figure in it is an example from the source, not this desk's record.
A three-state Markov chain can label each new five-second bar as low, mid, or high volatility. You assign past bars with percentiles, estimate how volatile each bucket is and how often it switches, then pick the state that best explains the bar you just saw. The lecture wires that calculation into a live Apple feed. It is a classifier you can recalibrate, not a claim that the label makes a trade profitable.
Why it matters
One distribution for every bar ignores the lecture's premise that volatility clusters and that the data-generating distribution moves. Separate emission distributions, one per state, are a compact way to say the next range is more plausible in one regime than another. The same shell can later be crossed with momentum or sentiment. Three volatility states are the worked example because the pattern is easy to see on a chart: green, orange, and red.
The chain starts with a uniform belief over the three states and a sticky transition matrix. From low, the initial probabilities are 0.90 to stay, 0.08 to mid, and 0.02 to jump straight to high. From mid they are 0.10, 0.80, and 0.10. High mirrors that stickiness: a direct jump to low is unlikely, a step to mid is more plausible, and staying high is the common case. That is the lecture's picture of clustering. Starter emission means are much smaller in the low state than in the high state, with starter spreads ordered the same way. They are only a prior. The fit replaces them.
The idea in plain words
Calibration bails out if fewer than 20 historical bars are present, or if fewer than 20 strictly positive volatilities remain after flat bars are dropped. Volatility is again the bar's high minus low, divided by the close when the close is positive. The 33rd and 67th percentiles cut the sample into codes 0, 1, and 2. A level below the 33rd percentile is low. Between the two cuts is mid. At or above the 67th is high. The lecture's reason for percentiles is that "high" is relative: a VIX at 30 can be the calm print after a stretch near 50.
Each occupied state needs at least three bars before its mean and standard deviation are estimated. The standard deviation is floored at a tiny positive number so a flat patch cannot produce a zero width. Means are then sorted so the low code really is the smaller average. Transition counts are the number of times the percentile labels stepped from one code to another. Each row is turned into probabilities with a little smoothing: add 0.1 and divide by the row sum plus 0.3, so an unseen jump is not assigned probability zero. After the fit, the state belief is reset to uniform.
How to use it
The dashboard already had a recalibrate button. The handler does nothing unless the stream is running. It clears stored history, requests 300 seconds of five-second trade bars under request id 3, and calls calibrate on that list. Three hundred seconds is sixty bars of five seconds. The chart paints each candle with the state's muted background color and can show the latest label in a status strip. Two bugs had to be fixed before that worked: the historical-data-end callback name was misspelled, and the high-minus-low difference needed parentheses so the percentiles had a real volatility series. The button itself had to be set to a normal, enabled state when streaming started, not given new text.
What usually goes wrong
A zero probability for a jump you have not seen recently is the mistake the smoothing is there to prevent. Using the raw percentile counts as the initial belief, without the same kind of smoothing, can give a state probability zero just because the last minute missed it. The lecture leaves that variant as an extension and keeps the initial belief uniform.
Questions people ask
How are the three states defined on history?
By the 33rd and 67th percentiles of bar volatility in the calibration window. Below the first cut is low, between the cuts is mid, and at or above the second cut is high.
What do you do with a new five-second bar?
Form a prior from the transition matrix, score the bar's volatility with each state's Gaussian, multiply, and choose the largest posterior. That posterior is the next prior.
How often is the model refit?
On demand, using 300 seconds of five-second trade bars, which is the latest 60 bars. The stream has to be running or the button returns immediately.