Markov Transition MatrixMarkov Transition Matrix
This indicator classifies higher-timeframe returns into three states and then tracks how often price transitions from one state to the next.
The three states are:
Bullish when the N-bar return is above the selected positive threshold
Bearish when the N-bar return is below the selected negative threshold
Neutral when the return stays between those thresholds
The result is a 3x3 transition matrix that answers questions such as:
After a bullish state, how often does the next state stay bullish?
After a neutral state, how often does the next state turn bearish?
Which current state has the strongest tendency to persist or reverse?
How It Works
Choose an analysis timeframe.
Choose a lookback window in candles.
For each completed bar in that timeframe, the script computes the return from close to the current close.
That return is classified as Bullish, Neutral, or Bearish using the threshold input.
Each one-step transition from the previous state to the new state is added to the cumulative matrix.
What The Table Shows
The main value in each cell is the one-step transition probability for that row and column.
The detail row under each cell shows P^2, P^3, and the raw transition count.
The summary row shows the latest confirmed state plus the current row probabilities for Bullish, Neutral, and Bearish.
The bottom bar shows a simple directional bias: P(Bull) - P(Bear).
P^2 and P^3 in this script are powers of the one-step probability. They are not full 2-step or 3-step Markov-chain forecasts produced by matrix multiplication.
Inputs
Threshold (%) : Minimum absolute return needed to classify a bar as Bullish or Bearish.
Timeframe : Higher timeframe used for the transition analysis.
Lookback Window : Number of candles used to measure each return.
Table Position : Screen location of the matrix.
How To Read It
Diagonal cells show persistence. High values there mean a state often repeats.
Off-diagonal cells show transitions. High values there mean a state often rotates into another one.
A positive bottom-bar reading means Bullish transitions currently outweigh Bearish transitions from the latest confirmed state.
A negative bottom-bar reading means Bearish transitions currently outweigh Bullish transitions from the latest confirmed state.
Usage Notes
The chart timeframe must be less than or equal to the selected analysis timeframe.
The matrix is cumulative across all available history in the selected timeframe context.
Changing timeframe, threshold, or lookback recompiles the script and rebuilds the tally from scratch.
This is a state-transition study. It does not place trades or generate broker orders.
Practical Uses
Compare persistence vs reversal behavior across different symbols.
Test whether a market spends more time trending or mean-reverting at a chosen timeframe.
Check whether a recent state has historically led to bullish continuation, neutral drift, or bearish follow-through.
Indicator

Markov Forecaster PRO🟦 Markov Forecaster PRO is a regime-classification and probability-forecasting engine built on a discrete-time Markov chain over three states — Bull, Bear, Sideways. Every bar is labelled from its rolling N-bar log return; the labels feed a 3×3 transition matrix that is power-iterated for the stationary distribution and exponentiated for forward-probability cones (P¹, P³, P⁵, P^horizon). Unlike the dozens of textbook Markov indicators on PulseWire, this one layers four original refinements on top of the standard chain construction — each addressing a well-known weakness of the memoryless Markov assumption.
The indicator integrates seven analytical layers — adaptive regime classification, semi-Markov duration tracking, sample-size disclosure, pending-regime early warning, forward-probability forecasting, look-ahead-free backtesting with fees and slippage, and multi-timeframe confluence — each rendered on a single overlay chart through a regime ribbon, three-layer neon glow signals, and four theme-aware dashboard panels.
Built with statistical honesty in mind. The backtest charges configurable commission and slippage on every entry and exit, the transition matrix flags rows with insufficient data, the duration-conditional probabilities are shown alongside the unconditional ones, and the documentation is explicit about what the model can and cannot predict.
🟦 HOW THE CORE ENGINE WORKS
**Regime Classification**
Each bar, the engine measures the rolling N-bar log return:
logRet = log(close / close )
The bar is labelled by comparing this return against the configured boundary:
- `logRet > +threshold` → BULL
- `logRet < −threshold` → BEAR
- otherwise → SIDEWAYS
The classification runs every bar with no look-ahead. The choice of threshold determines how reactive the regime label is, and this is where the first refinement enters.
**Adaptive Threshold (k · σ · √N)**
Traditional Markov regime indicators use a fixed percentage cut — e.g. "±5 % over 20 bars". This collapses on real markets: the same 5 % is trivial in a 2017 mania and never reached in 2023 chop. The fix is to scale the boundary with realised volatility:
threshold_adaptive = k × σ × √N
where σ is the per-bar log-return standard deviation over a configurable window (default 100 bars). Under a random walk, k = 1.0 cuts at the 16th / 84th percentiles; k = 2.0 at the 2.5th / 97.5th percentiles. The default k = 1.5 reproduces classic ±1.5-sigma thresholds.
Fixed-percentage mode is still available for users who want to lock the threshold deliberately.
**Regime Confidence**
Once classified, the move's strength is normalised relative to the active boundary:
confidence = |logRet| / threshold
| Confidence | Tier | Visual |
|---|---|---|
| < 1.0× | weak | ▱▱▱ |
| 1.0× – 2.0× | moderate | ▰▱▱ |
| 2.0× – 3.0× | strong | ▰▰▱ |
| ≥ 3.0× | stretched | ▰▰▰ |
The confidence value drives the ribbon transparency (in Adaptive Intensity mode), feeds the High Confidence alert (≥ 2.5× trigger), and is reported in the Status dashboard.
🟦 SEMI-MARKOV DURATION BUCKETS
**The Memoryless Problem**
A standard Markov chain says: "Given I'm in Bull, the probability of staying Bull tomorrow is X — regardless of whether Bull started yesterday or 200 bars ago." This is the memoryless property, and on real markets it's wrong. A 200-day-old Bull regime carries different mean-reversion risk than a 5-day-old one.
**The Refinement**
Markov Forecaster PRO additionally builds two CONDITIONAL transition matrices:
- `P_young` — transitions counted when the source regime's age was below its empirical average duration
- `P_mature` — transitions counted when the source regime's age was at or above the average
Both matrices are constructed in parallel with the main P, using the same per-bar bucketing logic and updated continuously. The self-transition probabilities for the current regime are then surfaced in the Status dashboard:
P young / mature 91% / 64%
The user reads this as: "When this regime was young (under its avg duration), it continued 91 % of the time. When mature, only 64 %." On a long-running regime this is the canonical signal that mean-reversion risk is rising — without the rest of the chain math being polluted.
A minimum of 10 samples per bucket is required before a value is shown; below that the cell reports "—" rather than display an unreliable probability.
🟦 FORWARD PROBABILITY CONE
**Matrix Exponentiation**
The 3×3 transition matrix P encodes one-bar-ahead probabilities. To project further out, the matrix is multiplied by itself:
P¹ = P — next bar
P³ = P × P × P — 3 bars out
P⁵ = P × P × P × P × P — 5 bars out
P^h = repeated h times — user-configured horizon
The Forecast Cone panel renders all four horizons for each of the three destination regimes, conditioned on the current regime. A trader reading the row "BULL" sees the probability the market will be in Bull at each horizon, given the current regime.
**Stationary Distribution**
Power-iterating the matrix to convergence yields the stationary distribution — the long-run probability of being in each regime, independent of starting state. With 50 iterations (default), any well-behaved 3×3 stochastic matrix is essentially converged.
stat + stat + stat = 1.0
This is rendered as the "long-run" row in the Forecast panel and the "Long-run share" cell in the Status panel.
**Honest Limitation**
The cone uses the UNCONDITIONAL matrix (averaged over all regime ages). For duration-conditional probabilities, the Status panel's P cell is the relevant readout. This split is explicit in both the cone footer label and the Forecast input tooltip.
🟦 SAMPLE-SIZE DISCLOSURE
A probability is only as reliable as the data behind it. Markov Forecaster PRO surfaces sample size in three places:
**Per-row sample count in the Transition Matrix**
A fifth column "n" in the matrix panel reports the number of transitions from each source regime. The cell is colored by reliability tier:
| Sample N | Tier | Color |
|---|---|---|
| ≥ 100 | high | foreground |
| 30 – 99 | moderate | dim |
| < 30 | low | divergent (warning) |
A row with fewer than 30 transitions is flagged because three-decimal probabilities derived from sparse data are noise, not signal.
**Total Sample N in the Status panel**
The Status dashboard's "Sample N" cell sums all transition counts and reports a global reliability tier:
| Total N | Tier |
|---|---|
| ≥ 200 | high (full color) |
| 50 – 199 | moderate (foreground) |
| < 50 | low (divergent warning) |
**Matrix footer**
The matrix panel's footer also shows the total N in compact notation (e.g. "N = 1.8k") for at-a-glance check.
The goal of this layer is honesty: a freshly-loaded chart with 30 bars of history should NOT display the same matrix as a 10-year chart, and the reliability tier makes the difference obvious without the user having to inspect counts manually.
🟦 PENDING-REGIME EARLY WARNING
**The Lookback Lag**
Because the regime is classified from log(close / close ), the official regime label inherently lags — by the time the threshold is crossed, the move is already N bars old. This is a structural feature of the model, not a bug, but it can be partially mitigated.
**Pending Logic**
Inside Sideways, when the log return reaches 70 % of either boundary, the dashboard fires an early-warning cue:
distance_fraction = max(|logRet| / threshold, ...)
isPending = (regime == SIDE) AND (distance_fraction ≥ 0.70)
The Status panel's "Pending" cell displays the direction the return is leaning toward and the current fraction:
⚠ ▲ BULL 87%
Color matches the leaning regime. The Pending Regime alert (default OFF, opt-in) fires on the first bar a pending state is entered.
This is not a regime change signal — it's a "watch this" cue, triggered roughly 30 % before the official threshold is crossed. Used alongside the official regime change, it gives the user advance notice without compromising the threshold's strictness.
🟦 LOOK-AHEAD-FREE BACKTEST
**The Look-Ahead Trap**
`regime` is derived from `log(close / close )`, which contains today's close. Allocating today's return to today's regime is look-ahead bias — the strategy would "know" today's regime before today's close, which is impossible in real-time trading. Most published Markov backtests have this bug.
**The Fix**
Markov Forecaster PRO allocates positions on the PRIOR bar's confirmed regime:
regForAlloc = regime // yesterday's confirmed regime
If yesterday's regime was Bull, we are long today. The strategy is realisable in real time because the previous bar's regime is known when the current bar opens.
This means the strategy is delayed by one bar relative to the regime label — and that's the correct, honest treatment. If a Bull→Bear flip happens on bar t, the strategy takes bar t's loss (still long from regime =Bull) and exits at bar t+1.
**Fees and Slippage**
Every Bull entry and exit pays the configured per-fill cost:
costFrac = feesPct/100 + slippageBps/10000
costPerFill = log(1 − costFrac) // negative log-space cost
The cumulative cost is debited from the Bull log-return total:
Bull gross = exp(bullLogR) − 1
Bull net = exp(bullLogR + bullCostLogR) − 1
A round-trip pays the fee + slippage twice. With defaults (0.10 % fee, 5 bps slippage), each round-trip costs roughly 0.30 % of equity in log space.
**Display**
The Backtest panel renders:
| Field | Value |
|---|---|
| Per-regime rows | GROSS cumulative log return (no fees) |
| Strategy row | NET cumulative (fees applied) vs Buy-and-Hold |
| Methodology footer | trade count · fee % · slippage bps |
The headline strategy result is the NET number — the realistic outcome a trader would have experienced. The gross numbers are kept for diagnostic comparison.
**What This Is Not**
This is a diagnostic backtest, not a tradable strategy. There is no position sizing, no risk management, no overnight financing, no shorting. It tells you whether "long when prior bar was Bull, flat otherwise" would have beaten buy-and-hold after fees — nothing more.
🟦 MULTI-TIMEFRAME CONFLUENCE
The same regime logic runs on a user-configured higher timeframe via `request.security` with `lookahead = barmerge.lookahead_off` and `gaps = barmerge.gaps_off` (anti-repaint mandatory). The result is reported in the Status dashboard's HTF block:
| State | Display | Color |
|---|---|---|
| HTF regime matches LTF regime | ✓ ALIGNED | bull |
| HTF regime differs from LTF | ⚠ DIVERGENT | bear |
| Insufficient HTF data | — | foreground |
Divergent regimes are common at trend turns — the LTF flips before the HTF catches up. Aligned regimes carry higher conviction. A separate alert ("MTF Confluence") fires on regime entries only when the HTF agrees.
Recommended pairings:
| Chart | HTF |
|---|---|
| 1H | D |
| 4H | W |
| D | W |
Use at least 3× your chart timeframe — anything closer and the two regimes track each other with no information gain.
🟦 VISUAL LAYER
**Regime Ribbon**
The chart background is tinted to the current regime color with three style options:
| Style | Behaviour |
|---|---|
| Subtle | Fixed 92 % transparency (price stays hero) |
| Bold | Fixed 75 % transparency (easy to scan from far) |
| Adaptive Intensity | Transparency scales with confidence (60 % – 95 %) |
In Adaptive Intensity mode, a strong directional move (confidence ≥ 3×) renders the ribbon at full intensity; a weak move stays faint. The ribbon doubles as a visual confidence meter.
**Three-Layer Neon Glow Signals**
On every confirmed regime change (after the Min Hold filter), the indicator drops a three-layer halo on the chart:
| Layer | Size | Transparency | Purpose |
|---|---|---|---|
| Outer | size.large | 80 % | Soft halo |
| Middle | size.normal | 50 % | Mid-glow |
| Core | size.small | 0 % | Bright center |
Bull markers (▲) render below the bar; Bear (▼) and Sideways (◆) render above. The Min Hold input (default 4 bars) requires a new regime to persist before its flip is drawn — kills label spam in choppy zones without affecting the underlying transition counts.
**Confidence Tags (optional)**
An off-by-default toggle adds the confidence multiplier to each signal arrow ("BULL 2.3×"), useful for screen captures and analysis.
🟦 DASHBOARDS
Four theme-aware panels, each independently togglable and positionable:
**Status Panel** (default: Bottom Left)
Compact live readout — current regime, age, confidence, pending direction, average duration, young/mature bucket, P young vs mature, expected remaining bars, long-run share, sample size, and HTF alignment. 16 rows base, 19 with HTF block enabled.
**Transition Matrix Panel** (default: Top Right)
3×3 next-bar P matrix with diagonal-highlighted self-transition cells. The fifth column reports per-row sample size with reliability tier coloring. Matrix footer shows total N.
**Forecast Cone Panel** (default: Middle Right)
Forward probability for each destination regime at horizons +1, +3, +5, and +configured. Steady-state row shows the long-run distribution. Current regime is reported at the bottom for context.
**Backtest Panel** (default: Bottom Right)
Per-regime gross cumulative return, average per-bar, and the bar count. Strategy row shows NET return vs buy-and-hold. Methodology footer lists trade count, fee, and slippage.
All four panels share the same theme palette and adapt to Dark / Light display mode. Text size is independently configurable (Tiny / Small / Normal / Large).
🟦 COLOR THEMES
Ten cohesive palettes tuned to the Apex design system, each defining three regime axes (Bull, Bear, Sideways):
| Theme | Character | Bull | Bear | Sideways |
|---|---|---|---|---|
| Prism | Classic | Forest green | Crimson | Slate grey |
| Focus | Default | Cyan steel | Deep orange | Cool blue-grey |
| Solar | Warm | Amber | Indigo red | Lavender grey |
| Frost | Cool | Sky blue | Soft lavender | Pale steel |
| Laser | Neon | Lime green | Hot crimson | Charcoal grey |
| Aurora | Bright | Gold | Scarlet | Warm beige |
| Plasma | Electric | Aqua | Magenta | Slate teal |
| Bloom | Soft | Mint | Hot pink | Blue-grey |
| Eclipse | Deep | Navy | Dark crimson | Steel grey |
| Carbon | Minimal | Near-white | Mid-grey | Dark grey |
One theme selection drives every visual component: ribbon, glow signals, all four dashboard headers, regime-colored cells, diagonal matrix highlights, and HTF alignment color.
**Dark / Light Display Mode**
Dashboard chrome (background, foreground, borders, section dividers) flips between dark-on-bright and bright-on-dark. The regime axis colors remain consistent across modes — only the panel chrome changes.
🟦 ALERT SYSTEM
Six alert conditions, each independently togglable:
| Alert | Condition |
|---|---|
| Bull Regime Entry | Regime flipped to BULL (after Min Hold confirmation) |
| Bear Regime Entry | Regime flipped to BEAR |
| Sideways Regime Entry | Regime flipped to SIDEWAYS (default OFF) |
| High Confidence | confidence ≥ 2.5× threshold, first bar of crossing |
| MTF Confluence | Regime change + HTF agrees |
| Pending Regime | Inside Sideways, log return ≥ 70 % of either boundary (default OFF) |
All alerts fire on confirmed bar close and use the standard `alertcondition` mechanism. The Min Hold filter applies to entry alerts — a new regime must persist Min Hold bars before its entry alert fires, matching the on-chart glow markers.
The Sideways and Pending alerts are default-off because they can fire more frequently than the other types — opt-in by design.
🟦 SETTINGS REFERENCE
**Theme**
- Theme — One of 10 Apex palettes. Default: Focus
- Display Mode — Dark / Light. Default: Dark
**Regime Logic**
- Threshold Mode — Adaptive (k·σ·√N) / Fixed (%). Default: Adaptive
- Lookback Window — Bars for the rolling log return. Default: 20
- Adaptive k — Sigma multiplier. Default: 1.5
- Fixed Bull Threshold — Used only in Fixed mode. Default: 5.0 %
- Fixed Bear Threshold — Used only in Fixed mode. Default: 5.0 %
- Volatility Window — Bars for the per-bar stdev. Default: 100
- Min Hold — Bars a new regime must persist for label drawing. Default: 4
**Forecast**
- Forecast Horizon — Bars projected by the right-most cone column. Default: 10
- Stationary Power — Power-iteration count. Default: 50
**Regime Ribbon**
- Show Regime Ribbon — Toggle. Default: ON
- Ribbon Style — Subtle / Bold / Adaptive Intensity. Default: Adaptive Intensity
**Signal Labels**
- Show Regime Change Signals — Toggle. Default: ON
- Glow Effect — Three-layer halo toggle. Default: ON
- Show Confidence on Signal — Adds multiplier tag (e.g. "BULL 2.3×"). Default: OFF
**Multi-Timeframe**
- Enable HTF Confluence — Toggle. Default: ON
- HTF Resolution — Higher timeframe. Default: D
**Backtest**
- Trading Fee (% per fill) — Per-side commission. Default: 0.10 %
- Slippage (bps per fill) — Per-side slippage in basis points. Default: 5
**Dashboards**
- Show Status / Matrix / Forecast / Backtest — Independent toggles. Default: all ON
- Dashboard Size — Tiny / Small / Normal / Large. Default: Small
**Panel Positions**
- Status Panel — 9-position grid. Default: Bottom Left
- Matrix Panel — Default: Top Right
- Forecast Panel — Default: Middle Right
- Backtest Panel — Default: Bottom Right
**Alerts**
- Bull / Bear / Sideways Regime Entry — Independent toggles
- High Confidence — Default: ON
- MTF Confluence — Default: ON
- Pending Regime — Default: OFF
🟦 COMPATIBILITY
Works on all asset classes and all timeframes in PulseWire Pine Script v6.
- Crypto: Spot, futures, perpetual contracts
- Forex: All pairs
- Equities: Stocks, ETFs, indices
- Commodities: Metals, energy, agriculture
- Timeframes: 1m through Monthly
The adaptive threshold normalises by per-bar realised volatility, making the regime classification volatility-agnostic across assets without manual recalibration. The same default settings work on BTCUSDT daily, SPY weekly, and EURUSD 4H — only the HTF resolution input should be adjusted to match the chart timeframe.
🟦 TECHNICAL NOTES
- Pine Script v6
- `max_labels_count = 500`, `max_lines_count = 100`, `max_bars_back = 5000`
- No repainting — all regime classifications are computed on confirmed bar close. The HTF request uses `lookahead = barmerge.lookahead_off` and `gaps = barmerge.gaps_off`
- Regime change debouncing uses `ta.barssince` to avoid runtime-indexed history reads (which can trip "cannot determine max_bars_back" in Pine v6)
- Heavy computation (matrix exponentiation, stationary distribution, dashboard rendering) is gated on `barstate.islast` to run once per chart render
- Transition counting uses `barstate.isconfirmed` to avoid double-counting the live bar
- Backtest accumulators charge fees at trade boundaries — entries and exits detected by `regForAlloc != regForAlloc `
- Duration buckets use the SOURCE regime's age at the time of transition for classification; the threshold is the empirical average duration of that regime, computed continuously
- Matrix multiplication is implemented as an unrolled 3×3 flat-array routine for portability and speed
- Empty-row fallback to uniform 1/3 in the transition matrix prevents NaN propagation when a regime has not appeared in visible history
🟦 LIMITATIONS — READ THIS
This indicator is statistically honest about what it can and cannot do. Three known limitations:
1. **The Markov assumption is partially violated.** Markets are not memoryless. The duration buckets (Section: Semi-Markov Duration Buckets) mitigate this but do not eliminate it.
2. **Forward probabilities are not predictions.** They are conditional probabilities under the chain assumption. A "Bull 58 % at +10 bars" reading does not mean "58 % chance the next 10 bars are bullish" — it means "given a long-run sample of similar starting states, 58 % were in Bull at +10 bars". Use the cone as ONE input alongside other analysis.
3. **The regime label lags by N bars.** This is structural — the rolling log return necessarily looks back. The Pending early warning partially mitigates this but cannot eliminate the lag. Treat the official regime change as a confirmation, not a leading signal.
🟦 DISCLAIMER
This indicator is provided for educational and informational purposes only. It does not constitute financial advice. Past performance does not guarantee future results. The hypothetical backtest is a diagnostic tool — there is no position sizing, no risk management, and no consideration of overnight financing, dividends, or other real-world frictions beyond the configured fee and slippage. Always conduct your own analysis and apply proper risk management. Indicator

Hidden Markov Reversal Finder [UAlgo]Hidden Markov Reversal Finder is a regime aware reversal detection indicator that uses a compact 3 state Hidden Markov style filter with online adaptation to classify market conditions and highlight potential top and bottom rotations. The script models price behavior as transitions between three regimes:
- Bull Expansion
- Balance
- Bear Stress
Instead of running a heavy Baum Welch retraining loop, this version is designed as a lightweight real time filter. It updates regime probabilities using a transition matrix plus a two dimensional Gaussian emission model built from two normalized observations:
Return observation as a smoothed log return z score
Volatility observation as a realized volatility z score
The indicator runs in its own pane ( overlay=false ) but can optionally paint chart bars and place reversal labels on price using force overlay. It also includes a clean dashboard panel showing the current state, confidence, observation values, score, posterior probabilities, stretch, and the current setup classification.
The reversal engine is built around a top rotation and bottom rotation concept. It looks for a probability peak in a regime, then a fade from that peak, combined with momentum flip conditions and a stretch filter measured in ATR units relative to a baseline EMA. Signals are gated by a confidence threshold and a cooldown period to reduce repetitive prints.
This makes the indicator useful as a regime driven reversal framework that integrates:
State probabilities and confidence
Regime score and momentum flip
ATR based stretch extremes
Peak fade rotation logic
Clean visual markers and dashboard transparency
🔹 Features
🔸 1) Three Regime Model
The script uses three explicit regimes with distinct roles:
Bull Expansion, intended to represent positive drift conditions
Balance, intended to represent neutral or mixed drift
Bear Stress, intended to represent negative drift and higher stress conditions
Each regime has its own mean and variance assumptions for return and volatility, which are then adapted online.
🔸 2) Two Dimensional Observation System (Return and Volatility)
The model does not rely on only returns. It uses both:
A normalized return feature
A normalized volatility feature
This helps distinguish clean bullish trends from choppy balance periods, and balance periods from bearish stress regimes.
🔸 3) Transition Matrix with Persistence Controls
Users can control how sticky each regime is through persistence settings:
Bull persistence
Balance persistence
Bear persistence
The transition matrix is constructed so that most probability remains in the same regime, while the remainder flows into other regimes using asymmetric weights that reflect realistic behavior.
🔸 4) Real Time Bayesian Filter Update
Each bar, the model performs:
Prediction step using the transition matrix
Update step using Gaussian emissions
Posterior normalization
Active state selection by arg max
This produces a smooth probability based regime tracker suitable for live use.
🔸 5) Adaptation
After filtering, the model adapts its internal means and variances using a learning rate scaled by posterior responsibility. This allows the state distributions to slowly adjust to changing market conditions without full retraining.
This keeps the indicator responsive while still stable.
🔸 6) Regime Score Output
The main score line is:
Bull posterior minus Bear posterior
This produces a continuous signal that ranges between negative and positive values and functions as a regime tilt meter. A confidence ribbon is also plotted as an area band derived from the dominant posterior.
🔸 7) Confidence Gating and Visual Strength
Confidence is defined as the largest posterior probability among the three regimes. The script uses confidence to:
Gate reversal signals
Determine bar tint transparency when bar coloring is enabled
Decide whether state shift tags should be printed
This reduces noise during low clarity periods.
🔸 8) Rotation Style Reversal Engine
The reversal finder is built on rotation logic:
A top rotation occurs after a Bull probability peak fades while Bear probability begins to rise
A bottom rotation occurs after a Bear probability peak fades while Bull probability begins to rise
This is a probabilistic rotation concept rather than a simple oscillator crossover.
🔸 9) Momentum Flip Confirmation
Signals require momentum confirmation through:
Regime score change direction
Return observation crossing a flip threshold
This is designed to reduce premature top and bottom calls when the regime probabilities shift but price momentum has not actually flipped.
🔸 10) ATR Based Stretch Filter
The script computes stretch as distance from an EMA baseline measured in ATR units. Signals require:
Top signals only when stretch is above a positive threshold
Bottom signals only when stretch is below a negative threshold
This ensures reversal signals occur when price is extended, not when it is near equilibrium.
🔸 11) Cooldown Control
A cooldown setting prevents consecutive buy or sell reversal signals from printing too frequently. This is especially useful when the market chops around an extreme and repeatedly triggers partial rotation conditions.
🔸 12) Dashboard Panel
A table dashboard displays key information on the last bar:
Active state name
Confidence
Return z score and volatility z score
Regime score
Posterior probabilities
Stretch in ATR units
Current setup text such as BUY REVERSAL, SELL REVERSAL, TOP WATCH, BOTTOM WATCH, WAIT
This makes the indicator transparent and easy to interpret.
🔸 13) State Tags and Reversal Labels on Chart
When enabled, the script prints:
State tags such as BULL, BASE, BEAR with arrows
Reversal markers with a vertical guide line and bold letter B or S
Tooltips include confidence, peak probability, stretch, and current posterior probabilities.
🔸 14) Optional Probability Curves and Bar Coloring
Users can toggle:
State probability plots
Signal markers and dots
Dashboard visibility
State tag visibility
Bar coloring by regime with confidence adjusted transparency
This makes the indicator adaptable for minimalist or fully informational workflows.
🔹 Calculations
1) Return Observation Construction
The script uses log returns:
float logReturn = math.log(close / nz(close , close))
It smooths return with an EMA:
float smoothedReturn = ta.ema(logReturn, returnSmoothLength)
Then normalizes by the return standard deviation:
float returnStdev = math.max(nz(ta.stdev(logReturn, returnZLength), EPS), EPS)
float returnObs = clampFloat(smoothedReturn / returnStdev, -obsClamp, obsClamp)
Interpretation:
Return observation is a clamped z score like feature, where positive values represent bullish return pressure and negative values represent bearish return pressure.
2) Volatility Observation Construction
Realized volatility is measured as the standard deviation of log returns:
float realizedVol = nz(ta.stdev(logReturn, volLength), EPS)
Then it is normalized relative to a baseline EMA and baseline standard deviation:
float volMean = nz(ta.ema(realizedVol, volBaselineLength), realizedVol)
float volStdev = math.max(nz(ta.stdev(realizedVol, volBaselineLength), EPS), EPS)
float volObs = clampFloat((realizedVol - volMean) / volStdev, -obsClamp, obsClamp)
Interpretation:
Volatility observation is a clamped z score like feature, where higher values indicate volatility expansion relative to baseline.
3) Warmup Logic
The model waits for enough history to compute stable normalized observations:
int warmupBars = math.max(returnZLength, volBaselineLength) + volLength
bool ready = bar_index > warmupBars and not na(returnObs) and not na(volObs)
Before ready, the script avoids producing live signals and uses the initial posterior distribution.
4) Transition Matrix Configuration
The transition matrix uses persistence values and asymmetric drift splits:
From Bull, most drift flows to Balance and a smaller portion to Bear
From Bear, most drift flows to Balance and a smaller portion to Bull
From Balance, drift splits evenly between Bull and Bear
Core setup:
this.setTransition(STATE_BULL, STATE_BALANCE, bullDrift * 0.78)
this.setTransition(STATE_BULL, STATE_BEAR, bullDrift * 0.22)
...
this.setTransition(STATE_BEAR, STATE_BALANCE, bearDrift * 0.78)
this.setTransition(STATE_BEAR, STATE_BULL, bearDrift * 0.22)
This design makes Balance act like a bridge regime and reduces unrealistic direct flip frequency.
5) Emission Model: 2D Gaussian Density
Each state computes an emission probability from return and volatility observations using a 2D Gaussian likelihood:
float exponent = -0.5 * ((retDeviation * retDeviation) / retVariance + (volDeviation * volDeviation) / volVariance)
float normalizer = 1.0 / (2.0 * math.pi * math.sqrt(retVariance * volVariance))
math.max(normalizer * math.exp(math.max(exponent, -24.0)), EPS)
Variances are floored at 0.12 to prevent collapse.
6) Prediction Step
The model predicts next probabilities using the transition matrix:
predictedProbability += posterior * transition(fromState, toState)
Then normalizes the predicted vector so it sums to 1.
7) Filter Update Step
The posterior is updated by multiplying predicted probabilities by emission likelihoods:
nextPosterior = predicted * emission(state, retObs, volObs)
Then normalized. The active state is the arg max of the posterior.
8) Online Adaptation
The model updates state means and variances using posterior responsibility times learning rate:
float responsibility = posterior * learningRate
Means update by moving toward the current observation:
nextMuRet = oldMuRet + responsibility * retError
nextMuVol = oldMuVol + responsibility * volError
Variances update toward squared error:
nextVarRet = oldVarRet + responsibility * (retError * retError - oldVarRet)
nextVarVol = oldVarVol + responsibility * (volError * volError - oldVarVol)
All parameters are clamped to stability ranges so the model does not explode.
9) Regime Score and Confidence
Score is defined as:
posterior - posterior
Confidence is the maximum posterior:
posterior
These values drive visuals and signal gating.
10) Stretch Calculation in ATR Units
Stretch uses an EMA basis of price and measures distance in ATR units:
float basis = ta.ema(close, stretchLength)
float atrValue = math.max(ta.atr(14), syminfo.mintick)
float stretch = (close - basis) / atrValue
Top stretch requires:
stretch >= stretchThreshold
Bottom stretch requires:
stretch <= -stretchThreshold
This ensures reversals occur when price is statistically extended relative to recent volatility.
11) Probability Peak and Fade Logic
The script measures recent peaks for bull and bear probabilities:
float bullPeak = ta.highest(bullProb , peakLookback)
float bearPeak = ta.highest(bearProb , peakLookback)
Fade is peak minus current:
bullFade = bullPeak - bullProb
bearFade = bearPeak - bearProb
Top rotation condition requires:
Bull peak above threshold
Bull fade above minimum
Bear probability rising
Bottom rotation requires the mirrored conditions.
This captures the idea of regime dominance peaking, then fading as the opposite side begins to regain influence.
12) Momentum Flip Confirmation
Momentum down requires:
Regime score decreasing
Return observation strongly negative below a flip threshold
Momentum up requires:
Regime score increasing
Return observation strongly positive above the flip threshold
This prevents signals when probabilities fade but momentum remains neutral.
13) Signal Gating and Cooldown
Signals require confidence above the threshold and a cooldown to avoid repeated triggers:
confidenceValue >= confidenceThreshold
bar_index - lastSignalBar > cooldownBars
14) Buy and Sell Reversal Signals
Buy reversal:
Bottom rotation
Momentum up
Bottom stretch
Confidence filter
Cooldown filter
Sell reversal:
Top rotation
Momentum down
Top stretch
Confidence filter
Cooldown filter
A Balance signal is also triggered when the state changes to Balance with sufficient confidence.
15) Visual Outputs
The indicator plots:
Regime score line with area fill around zero
Confidence ribbon as an area band
Optional posterior curves for Bull, Balance, Bear
Normalized stretch line scaled by the stretch threshold
Optional dots on the chart for reversal events
Optional bar coloring on the main chart
It also prints:
Reversal labels B and S with stretch, confidence, and peak probability tooltips
State tags on regime shifts
A dashboard panel summarizing live state and setup context Indicator

Hidden Markov Model: Baum-Welch [UAlgo]Hidden Markov Model: Baum-Welch is a regime detection and reversal signaling indicator that applies a 3 state Hidden Markov Model to normalized log returns and continuously adapts its parameters using an online Baum Welch expectation maximization routine. The script is designed to classify the market into three latent regimes, then express that classification as real time probabilities for Bull, Range, and Bear conditions.
The indicator runs in its own pane ( overlay=false ) and outputs:
Probability curves for the three regimes
A dominant regime score scaled to 0 to 1
A regime strip visualization for quick bias reading
Adaptive background coloring based on the dominant regime and confidence
Optional regime shift markers
Optional buy and sell reversal markers driven by strict multi condition logic
The core idea is that price behavior can be modeled as transitions between hidden states that each have their own return distribution. The script fits a Gaussian emission model for each state, estimates state transition probabilities, and updates the posterior probability of each state on every bar. It retrains the full model at fixed intervals, while using a faster one step forward update between retrains for efficiency.
This implementation is not a simple threshold oscillator. It is a full mini HMM engine built in Pine with:
Scaled forward and backward algorithms
Expectation step producing gamma and xi posteriors
Maximization step updating initial distribution, transition matrix, state means, and state variances
Safeguards such as variance floors and transition floors to maintain numerical stability
The output is a regime aware probability system that can be used for bias, context, and reversal confirmation rather than simple entry signals.
Educational tool only. Not financial advice.
🔹 Features
🔸 1) Three State Hidden Markov Model Regime Engine
The model uses three hidden states and continuously estimates the probability of being in each state:
Bull regime
Range regime
Bear regime
This gives a probabilistic regime map rather than a single hard classification.
🔸 2) Baum Welch Training with Scheduled Retraining
The script retrains its parameters using an EM routine at a user defined interval in bars. Each retrain runs a configurable number of EM iterations. Between retrains, the indicator performs a one step forward Bayesian update of the posterior state probabilities.
This structure balances adaptability with performance.
🔸 3) Normalized Log Return Observations
The observation series is a z score normalized log return:
Log returns convert price changes into additive units
An EMA and rolling standard deviation normalize the series to stabilize the HMM fit
This helps the HMM learn regimes based on relative return behavior rather than raw price scale.
🔸 4) Automatic Bull, Range, and Bear Role Assignment
The model learns state means. The script then assigns roles by ranking those learned means:
The state with the lowest mean becomes the Bear state
The state with the highest mean becomes the Bull state
The remaining state is treated as Range
This keeps regime labeling consistent even as the internal state ordering shifts during training.
🔸 5) Probabilities and Dominant Regime Visualization
The script plots:
Bull probability curve
Range probability curve
Bear probability curve
It also plots an area for the dominant probability and a regime strip that makes it easy to see the dominant regime quickly without reading the full curves.
🔸 6) Regime Score Line (Bull minus Bear)
A continuous score is calculated as Bull probability minus Bear probability, then scaled to a 0 to 1 range. This score becomes the main regime momentum signal used for rebound and reversal logic.
🔸 7) Adaptive Background Coloring by Regime and Confidence
The pane background color changes based on the dominant regime. Transparency adapts according to confidence, so strong regime certainty produces a more visible background while low certainty remains subtle.
🔸 8) Strict Signal Filters for Bias and Reversal
The indicator provides bias filters:
Bull bias when Bull probability and confidence exceed thresholds and the dominant regime is Bull
Bear bias when Bear probability and confidence exceed thresholds and the dominant regime is Bear
It also provides reversal style buy and sell signals based on a multi condition framework described in the calculations section.
🔸 9) Reversal Logic Combining Extremes, Rebounds, and Transition Edge
Reversal signals are not generated by a single crossover. The script requires:
An extreme score pivot
An extreme regime probability at that pivot
A rebound trigger through predefined rebound levels
A minimum probability and confidence filter
A transition asymmetry and edge condition that favors switching toward the target regime
A momentum condition requiring Bull probability rising and Bear probability falling for buys, and the inverse for sells
A time window limit so reversals must occur within a limited number of bars after the extreme
This creates a high selectivity reversal engine.
🔸 10) Transition Matrix Insight and Switch Edge Metrics
The script computes predicted transition probabilities toward Bull and Bear using the current posterior and the transition matrix. It also measures transition asymmetry between Bull to Bear and Bear to Bull and uses these values as part of reversal confirmation.
This adds structural information that classic oscillators do not capture.
🔸 11) Anti Duplicate Reversal Signals
Once a pivot extreme has been used to generate a reversal signal, it is marked as consumed so the same pivot cannot repeatedly trigger additional buy or sell signals. This helps avoid signal repetition.
🔸 12) Full Informational Label Output
A live info label prints:
Current regime
Current signal text
Confidence
Bull, Range, Bear probabilities
Log likelihood
Key trigger thresholds
Reversal settings and edge settings
This provides transparency into what the model is currently seeing and why signals are or are not appearing.
🔹 Calculations
1) Observation Series: Normalized Log Returns
The script uses log returns:
logRet = math.log(close / nz(close , close))
Then normalizes them with an EMA mean and rolling standard deviation:
retMean = nz(ta.ema(logRet, normLength), 0.0)
retStd = math.max(nz(ta.stdev(logRet, normLength), 0.0), 1e-6)
obs = (logRet - retMean) / retStd
This creates an observation series with more stable scale properties across time.
2) Rolling Observation Window
The HMM is trained on a rolling window of length windowLen . Only the most recent processRecentBars are processed to control load:
startBar = last_bar_index - processRecentBars
activeRange = bar_index >= (startBar < 0 ? 0 : startBar)
If active, the observation is appended and the oldest one is removed:
if array.size(obsWindow) < windowLen
array.push(obsWindow, obs)
else
array.shift(obsWindow)
array.push(obsWindow, obs)
The model is ready only when the window is full.
3) Model Initialization
The script initializes a 3 state model with:
Uniform initial state probabilities
A transition matrix seeded with high persistence and equal small jump probabilities
State means initialized around zero with a configured separation
State variances initialized to a configured starting value
Key logic:
Stay probability equals initialPersistence
Jump probability equals the remaining probability split across other states
This gives the HMM a stable starting point before training.
4) Emission Model: Gaussian per State
Each state emits observations using a Gaussian density:
math.exp(-0.5 * d * d / varS) / math.sqrt(TWO_PI * varS)
Variance uses a floor:
float varS = math.max(array.get(this.vr, s), varMin)
This prevents variance collapse and numeric instability.
5) Forward Algorithm with Scaling
The script computes the forward probabilities alpha and applies scaling coefficients c to prevent underflow. It then recovers log likelihood from the scaling coefficients:
this.logLik := -sum(log(c ))
This is essential because HMM sequences quickly underflow without scaling.
6) Backward Algorithm with Scaling
The backward probabilities beta are computed using the scaling values from the forward pass, ensuring alpha and beta remain numerically stable across the entire window.
7) Expectation Step: Gamma and Xi
Gamma represents posterior probability of being in state i at time t . Xi represents posterior probability of transitioning from i to j between t and t+1 .
Xi is normalized per time step:
xij = xi_raw / denom
Gamma is computed as the sum of xi across outgoing transitions for each state:
gamma(t, i) = sum_j xi(t, i, j)
8) Maximization Step: Updating Parameters
Initial probabilities update from gamma at time 0:
pi = gamma(0, i)
Transition probabilities update from xi sums divided by gamma sums, with a transition floor and row normalization:
Each transition is clamped to transitionFloor
Each row is normalized to sum to 1
Means update as weighted averages of observations using gamma weights.
Variances update as weighted squared deviation sums with a variance floor.
9) Retraining Schedule and Online Updates
The model retrains when:
It is not initialized yet
Or the bar index matches the retrain interval
shouldRetrain = ready and (not modelInitialized or bar_index % retrainEveryBars == 0)
On retrain, Baum Welch is run for emIterations .
Between retrains, the script performs a one step forward update of the posterior:
hmm.forwardOne(posterior, obs, varianceFloor, posteriorTmp)
This provides continuous posterior updates without full retraining on every bar.
10) Role Mapping to Bull, Range, Bear
The script assigns which internal state corresponds to Bear and Bull by looking at the learned means:
Bear state is the state with the minimum mean
Bull state is the state with the maximum mean
Range is the remaining state index
This mapping updates dynamically as the model learns.
11) Regime Score and Confidence
The regime score is:
score = pBull - pBear
It is then scaled to 0 to 1:
score01 = 0.5 + 0.5 * score
Confidence is:
confidence = max(pBull, pRange, pBear)
This confidence drives background alpha and signal gating.
12) Probability Filters for Bias
Bull filter requires:
Bull probability above bullProbTrigger
Confidence above signalConfidenceMin
Bear filter requires similar conditions for Bear probability.
Bias validity adds the requirement that the dominant regime role matches the direction:
Bull bias requires dominantRole equals 1
Bear bias requires dominantRole equals minus 1
13) Extreme Pivot Logic for Reversal Candidates
The script looks for pivots in the score line:
ta.pivotlow(score01, pivotStrength, 1)
ta.pivothigh(score01, pivotStrength, 1)
It stores the most recent pivot low and pivot high along with the associated Bull or Bear probability at the pivot bar.
A low extreme is valid if:
Score at pivot is below dipScoreLevel
Bear probability at pivot exceeds extremeProbMin
A high extreme is valid if:
Score at pivot is above topScoreLevel
Bull probability at pivot exceeds extremeProbMin
14) Rebound Triggers
After an extreme, the script waits for rebound triggers:
Up rebound:
ta.crossover(score01, reboundUpLevel)
Down rebound:
ta.crossunder(score01, reboundDownLevel)
Rebound must occur within the reversal window bars from the extreme pivot.
15) Transition Edge and Asymmetry Logic
The script computes predicted probabilities of switching toward Bull or Bear using the transition matrix and current posterior. It also computes transition asymmetry between the Bull to Bear and Bear to Bull transitions.
A bullish switch condition requires:
Switch edge greater than hmmEdgeMin
Transition asymmetry favoring Bear to Bull at or above transitionAsymMin
Bull probability greater than Bear probability
A bearish switch condition uses the mirrored logic.
This adds a model based confirmation that a regime switch is plausible, not only that the score bounced.
16) Momentum Confirmation
Bull momentum requires:
Bull probability rising
Bear probability falling
Bear momentum requires the opposite.
These conditions prevent signals when probabilities are flat or conflicting.
17) Final Reversal Signal Construction
Buy reversal requires:
Valid low extreme
Not consumed
Inside reversal window
Rebound up
Bull probability and confidence filter
Bullish HMM switch condition
Bull momentum
Sell reversal requires the mirrored set of conditions.
The sell is suppressed if a buy is simultaneously true so conflicting signals do not print on the same bar.
18) Visualization Output
The script plots:
Probability curves for each regime
A dominant probability area
A thick score line colored by regime
A regime strip column plot
Fills between Bull and Bear curves and between rebound levels
Adaptive background
Optional markers for regime shifts
Reversal markers as glow plus label style plots
The info label consolidates the most important current state and threshold data for transparency. Indicator

Markov Chain Trend ProbabilityA Markov Chain is a mathematical model that predicts future states based on the current state, assuming that the future depends only on the present (not the past). Originally developed by Russian mathematician Andrey Markov, this concept is widely used in:
Finance: Risk modeling, portfolio optimization, credit scoring, algorithmic trading
Weather Forecasting: Predicting sunny/rainy days, temperature patterns, storm tracking
Here's an example of a Markov chain: If the weather is sunny, the probability that will be sunny 30 min later is say 90%. However, if the state changes, i.e. it starts raining, how the probability that will be raining 30 min later is say 70% and only 30% sunny.
Similar concept can be applied to markets price action and trends.
Mathematical Foundation
The core principle follows the Markov Property: P(X_{t+1}|X_t, X_{t-1}, ..., X_0) = P(X_{t+1}|X_t)
Transition Matrix :
-------------Next State
Current----
--------P11 P12
-----P21 P22
Probability Calculations:
P(Up→Up) = Count(Up→Up) / Count(Up states)
P(Down→Down) = Count(Down→Down) / Count(Down states)
Steady-state probability: π = πP (where π is the stationary distribution)
State Definition:
State = UPTREND if (Price_t - Price_{t-n})/ATR > threshold
State = DOWNTREND if (Price_t - Price_{t-n})/ATR < -threshold
How It Works in Trading
This indicator applies Markov Chain theory to market trends by:
Defining States: Classifies market conditions as UPTREND or DOWNTREND based on price movement relative to ATR (Average True Range)
Learning Transitions: Analyzes historical data to calculate probabilities of moving from one state to another
Predicting Probabilities: Estimates the likelihood of future trend continuation or reversal
How to Use
Parameters:
Lookback Period: Number of bars to analyze for trend detection (default: 14)
ATR Threshold: Sensitivity multiplier for state changes (default: 0.5)
Historical Periods: Sample size for probability calculations (default: 33)
Trading Applications:
Trend confirmation for entry/exit decisions
Risk assessment through probability analysis
Market regime identification
Early warning system for potential trend reversals
The indicator works on any timeframe and asset class. Enjoy! Indicator

Indicator

Library

MarkovChainLibrary "MarkovChain"
Generic Markov Chain type functions.
---
A Markov chain or Markov process is a stochastic model describing a sequence of possible events in which the
probability of each event depends only on the state attained in the previous event.
---
reference:
Understanding Markov Chains, Examples and Applications. Second Edition. Book by Nicolas Privault.
en.wikipedia.org
www.geeksforgeeks.org
towardsdatascience.com
github.com
stats.stackexchange.com
timeseriesreasoning.com
www.ris-ai.com
github.com
gist.github.com
github.com
gist.github.com
writings.stephenwolfram.com
kevingal.com
towardsdatascience.com
spedygiorgio.github.io
github.com
www.projectrhea.org
method to_string(this)
Translate a Markov Chain object to a string format.
Namespace types: MC
Parameters:
this (MC) : `MC` . Markov Chain object.
Returns: string
method to_table(this, position, text_color, text_size)
Namespace types: MC
Parameters:
this (MC)
position (string)
text_color (color)
text_size (string)
method create_transition_matrix(this)
Namespace types: MC
Parameters:
this (MC)
method generate_transition_matrix(this)
Namespace types: MC
Parameters:
this (MC)
new_chain(states, name)
Parameters:
states (state )
name (string)
from_data(data, name)
Parameters:
data (string )
name (string)
method probability_at_step(this, target_step)
Namespace types: MC
Parameters:
this (MC)
target_step (int)
method state_at_step(this, start_state, target_state, target_step)
Namespace types: MC
Parameters:
this (MC)
start_state (int)
target_state (int)
target_step (int)
method forward(this, obs)
Namespace types: HMC
Parameters:
this (HMC)
obs (int )
method backward(this, obs)
Namespace types: HMC
Parameters:
this (HMC)
obs (int )
method viterbi(this, observations)
Namespace types: HMC
Parameters:
this (HMC)
observations (int )
method baumwelch(this, observations)
Namespace types: HMC
Parameters:
this (HMC)
observations (int )
Node
Target node.
Fields:
index (series int) : . Key index of the node.
probability (series float) : . Probability rate of activation.
state
State reference.
Fields:
name (series string) : . Name of the state.
index (series int) : . Key index of the state.
target_nodes (Node ) : . List of index references and probabilities to target states.
MC
Markov Chain reference object.
Fields:
name (series string) : . Name of the chain.
states (state ) : . List of state nodes and its name, index, targets and transition probabilities.
size (series int) : . Number of unique states
transitions (matrix) : . Transition matrix
HMC
Hidden Markov Chain reference object.
Fields:
name (series string) : . Name of thehidden chain.
states_hidden (state ) : . List of state nodes and its name, index, targets and transition probabilities.
states_obs (state ) : . List of state nodes and its name, index, targets and transition probabilities.
transitions (matrix) : . Transition matrix
emissions (matrix) : . Emission matrix
initial_distribution (float ) Library

Library

FunctionBaumWelchLibrary "FunctionBaumWelch"
Baum-Welch Algorithm, also known as Forward-Backward Algorithm, uses the well known EM algorithm
to find the maximum likelihood estimate of the parameters of a hidden Markov model given a set of observed
feature vectors.
---
### Function List:
> `forward (array pi, matrix a, matrix b, array obs)`
> `forward (array pi, matrix a, matrix b, array obs, bool scaling)`
> `backward (matrix a, matrix b, array obs)`
> `backward (matrix a, matrix b, array obs, array c)`
> `baumwelch (array observations, int nstates)`
> `baumwelch (array observations, array pi, matrix a, matrix b)`
---
### Reference:
> en.wikipedia.org
> github.com
> en.wikipedia.org
> www.rdocumentation.org
> www.rdocumentation.org
forward(pi, a, b, obs)
Computes forward probabilities for state `X` up to observation at time `k`, is defined as the
probability of observing sequence of observations `e_1 ... e_k` and that the state at time `k` is `X`.
Parameters:
pi (float ) : Initial probabilities.
a (matrix) : Transmissions, hidden transition matrix a or alpha = transition probability matrix of changing
states given a state matrix is size (M x M) where M is number of states.
b (matrix) : Emissions, matrix of observation probabilities b or beta = observation probabilities. Given
state matrix is size (M x O) where M is number of states and O is number of different
possible observations.
obs (int ) : List with actual state observation data.
Returns: - `matrix _alpha`: Forward probabilities. The probabilities are given on a logarithmic scale (natural logarithm). The first
dimension refers to the state and the second dimension to time.
forward(pi, a, b, obs, scaling)
Computes forward probabilities for state `X` up to observation at time `k`, is defined as the
probability of observing sequence of observations `e_1 ... e_k` and that the state at time `k` is `X`.
Parameters:
pi (float ) : Initial probabilities.
a (matrix) : Transmissions, hidden transition matrix a or alpha = transition probability matrix of changing
states given a state matrix is size (M x M) where M is number of states.
b (matrix) : Emissions, matrix of observation probabilities b or beta = observation probabilities. Given
state matrix is size (M x O) where M is number of states and O is number of different
possible observations.
obs (int ) : List with actual state observation data.
scaling (bool) : Normalize `alpha` scale.
Returns: - #### Tuple with:
> - `matrix _alpha`: Forward probabilities. The probabilities are given on a logarithmic scale (natural logarithm). The first
dimension refers to the state and the second dimension to time.
> - `array _c`: Array with normalization scale.
backward(a, b, obs)
Computes backward probabilities for state `X` and observation at time `k`, is defined as the probability of observing the sequence of observations `e_k+1, ... , e_n` under the condition that the state at time `k` is `X`.
Parameters:
a (matrix) : Transmissions, hidden transition matrix a or alpha = transition probability matrix of changing states
given a state matrix is size (M x M) where M is number of states
b (matrix) : Emissions, matrix of observation probabilities b or beta = observation probabilities. given state
matrix is size (M x O) where M is number of states and O is number of different possible observations
obs (int ) : Array with actual state observation data.
Returns: - `matrix _beta`: Backward probabilities. The probabilities are given on a logarithmic scale (natural logarithm). The first dimension refers to the state and the second dimension to time.
backward(a, b, obs, c)
Computes backward probabilities for state `X` and observation at time `k`, is defined as the probability of observing the sequence of observations `e_k+1, ... , e_n` under the condition that the state at time `k` is `X`.
Parameters:
a (matrix) : Transmissions, hidden transition matrix a or alpha = transition probability matrix of changing states
given a state matrix is size (M x M) where M is number of states
b (matrix) : Emissions, matrix of observation probabilities b or beta = observation probabilities. given state
matrix is size (M x O) where M is number of states and O is number of different possible observations
obs (int ) : Array with actual state observation data.
c (float ) : Array with Normalization scaling coefficients.
Returns: - `matrix _beta`: Backward probabilities. The probabilities are given on a logarithmic scale (natural logarithm). The first dimension refers to the state and the second dimension to time.
baumwelch(observations, nstates)
**(Random Initialization)** Baum–Welch algorithm is a special case of the expectation–maximization algorithm used to find the
unknown parameters of a hidden Markov model (HMM). It makes use of the forward-backward algorithm
to compute the statistics for the expectation step.
Parameters:
observations (int ) : List of observed states.
nstates (int)
Returns: - #### Tuple with:
> - `array _pi`: Initial probability distribution.
> - `matrix _a`: Transition probability matrix.
> - `matrix _b`: Emission probability matrix.
---
requires: `import RicardoSantos/WIPTensor/2 as Tensor`
baumwelch(observations, pi, a, b)
Baum–Welch algorithm is a special case of the expectation–maximization algorithm used to find the
unknown parameters of a hidden Markov model (HMM). It makes use of the forward-backward algorithm
to compute the statistics for the expectation step.
Parameters:
observations (int ) : List of observed states.
pi (float ) : Initial probaility distribution.
a (matrix) : Transmissions, hidden transition matrix a or alpha = transition probability matrix of changing states
given a state matrix is size (M x M) where M is number of states
b (matrix) : Emissions, matrix of observation probabilities b or beta = observation probabilities. given state
matrix is size (M x O) where M is number of states and O is number of different possible observations
Returns: - #### Tuple with:
> - `array _pi`: Initial probability distribution.
> - `matrix _a`: Transition probability matrix.
> - `matrix _b`: Emission probability matrix.
---
requires: `import RicardoSantos/WIPTensor/2 as Tensor` Library

FunctionSMCMCLibrary "FunctionSMCMC"
Methods to implement Markov Chain Monte Carlo Simulation (MCMC)
markov_chain(weights, actions, target_path, position, last_value) a basic implementation of the markov chain algorithm
Parameters:
weights : float array, weights of the Markov Chain.
actions : float array, actions of the Markov Chain.
target_path : float array, target path array.
position : int, index of the path.
last_value : float, base value to increment.
Returns: void, updates target array
mcmc(weights, actions, start_value, n_iterations) uses a monte carlo algorithm to simulate a markov chain at each step.
Parameters:
weights : float array, weights of the Markov Chain.
actions : float array, actions of the Markov Chain.
start_value : float, base value to start simulation.
n_iterations : integer, number of iterations to run.
Returns: float array with path. Library

Library

Indicator
