Loading…

Gaussian Mixture Fitter

A single mean and standard deviation fitted to bimodal data describes the "average" of two regimes that never actually occurs - the fitted mean can sit in the valley between two real peaks where almost no observations actually fall. This is what happens whenever a strategy's return distribution is really a mixture of two underlying regimes (say, calm and stressed volatility periods) and only one normal curve is fitted to the whole blend. Expectation-Maximization fixes this by alternating two steps. The E-step asks, given the current two candidate normal curves, what is the probability each observed point belongs to component 1 versus component 2. The M-step then re-estimates each component's mean, variance and weight as a probability-weighted computation using those responsibilities. Repeating this provably increases the data's likelihood under the model every iteration until it settles at a local optimum - this is genuine EM, not a clustering heuristic dressed up as one. The components are seeded from the sorted sample's lower and upper halves rather than a random start, which avoids the classic failure mode of both components landing on top of each other and never separating. Component variances are floored at a small epsilon so a component that ends up responsible for very few points cannot collapse to zero variance and divide by zero on the next iteration. Notes and limitations - One dimension, two components only. A general k-component or multivariate mixture needs a full covariance-matrix EM, which is a different and considerably heavier piece of machinery. - EM converges to a LOCAL optimum, not necessarily the global one. The split-based initialisation used here is a good practical starting point for genuinely bimodal data, but a sample that is not actually a mixture of two normals will still get two components fitted to it - always look at whether the fitted weights and means are sensible, not just whether the function returned true. - Component labelling is arbitrary: component "1" and "2" can land on either regime depending on where the initial split happens to fall. The demo shows the standard way to resolve this - compare each fitted mean against whatever external reference (a known regime, a prior fit) is actually meaningful to the caller. - The variance floor (1e-8) means a component responsible for only one or two nearly-identical points will not report an unrealistic near-zero variance, but this also means the fit is not meaningful on extremely small samples (well under the n<4 hard minimum this header enforces).

Script · MT5 · v1.0 · Free

About Gaussian Mixture Fitter

Gaussian Mixture Fitter is a script for MetaTrader 5. It has 4 settings, such as Sample size calm, Sample size stressed and Max iterations. MQL source code included.

Files in the download: GaussianMixtureFitter.mq5.mqh, GaussianMixtureFitter.txt, GaussianMixtureFitterDemo.ex5, GaussianMixtureFitterDemo.mq5.

Settings

SettingDefaultWhat it does
Sample size calm60points drawn from the calm regime
Sample size stressed40points drawn from the stressed regime
Max iterations200EM iteration cap
Tolerance1.0e-6log-likelihood change that stops EM

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
  3. Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
  4. Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
  5. Drag the script onto a chart to run it once.

Download the PDF guide

Tags: MT5 Script · MQL5 Source Code

MQL4 & MQL5 scripts · PolyMaria