Distribution Distance Metrics
Comparing two periods' return or spread distributions by their mean and variance alone misses shape changes entirely - a distribution can keep the same mean and variance while becoming bimodal, more skewed, or fat-tailed in a way that matters a great deal for risk, and a two-number summary cannot see it. This header implements three distance measures built for comparing whole distributions, each answering a slightly different question. Kullback-Leibler divergence is the information-theoretic standard: how many extra nats are needed to encode data from P using a code built for Q. It is asymmetric (KL(P||Q) is not KL(Q||P)) and grows to infinity wherever P has probability mass that Q assigns none - a real limitation when comparing two empirical histograms, since an empty bin in one and a populated bin in the other is common with limited data. Jensen-Shannon divergence fixes both problems by measuring each distribution's KL divergence against their average instead of against each other directly, which keeps the result symmetric and bounded in [0, ln 2] regardless of how disjoint the supports are; its square root, the Jensen-Shannon distance, is a true metric. Wasserstein-1 (earth mover's) distance answers a different, more physical question: the minimum total amount of probability mass that has to move, multiplied by how far it has to move, to turn one distribution into the other. In one dimension this has a simple closed form - the L1 distance between the two cumulative distribution functions - so no optimal-transport solver is needed. Two implementations are provided: one for pre-binned histograms sharing a grid, and one for raw samples of possibly different sizes, which resamples both onto a shared quantile grid first. Notes and limitations - KL and Jensen-Shannon both require the two inputs to already be normalised probability vectors over the SAME bins in the SAME order - run raw counts through DistNormalizeHistogram first. - The additive smoothing in DistNormalizeHistogram makes KL well-defined everywhere but means very sparse histograms (many empty bins) get a divergence that partly reflects the smoothing choice, not just the data - use enough bins/data that most bins have real counts. - DistWassersteinHistogram assumes both histograms share an identical bin grid (same count, same width, same starting point). Comparing histograms built on different grids will silently produce a meaningless number. - DistWassersteinSamples uses quantile interpolation to compare samples of different sizes; with very small samples (well under 30) the interpolated quantile function is a rough approximation and the distance is noisy. - None of these distances say WHICH direction the distribution moved (higher mean, fatter tail, etc) - only how far apart the two distributions are as a whole. Pair with a mean/variance comparison for direction.
Script · MT5 · v1.0 · Free
About Distribution Distance Metrics
Distribution Distance Metrics is a script for MetaTrader 5 based on Divergence. It has 6 settings, such as Bin count, Bin width and Shift current. MQL source code included.
Files in the download: DistributionDistances.mq5.mqh, DistributionDistances.txt, DistributionDistancesDemo.ex5, DistributionDistancesDemo.mq5.
Settings
| Setting | Default | What it does |
|---|---|---|
| Bin count | 20 | Number of histogram bins |
| Bin width | 1.0 | Width of each bin (same unit as the data) |
| Shift current | 2.0 | Mean shift applied to the "current" distribution |
| Widen current | 1.4 | Spread multiplier applied to the "current" distribution |
| Sample size | 300 | Raw sample size used for the sample-based Wasserstein check |
| Seed | 3 | Random seed |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
- Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
- Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: Divergence · MT5 Script · MQL5 Source Code