Kernel Density Estimator
A histogram's shape depends heavily on an arbitrary bin width the user has to pick by hand: too narrow and every bin is noise, too wide and real structure (a second bump, a skew) gets smoothed away entirely. Kernel density estimation replaces "which bin is this point in" with "how much does this point raise the density everywhere nearby", summing a small Gaussian bump centred on every observation - producing a smooth curve with no bin edges at all. The one parameter kernel density estimation still needs is the bandwidth, i.e. how wide each bump is. This header picks it automatically with Silverman's rule of thumb, h = 0.9 * min(stdev, IQR/1.34) * n^(-1/5), using the smaller of the sample's standard deviation and its scaled interquartile range - the standard refinement over the plain stdev-only formula, which keeps a few extreme outliers from inflating the bandwidth and over-smoothing everything else. KdeEvaluateGrid produces a plottable density curve over any range; KdeArgMax then finds the estimated MODE (the most typical value) from that curve - which, on a skewed sample, differs meaningfully from both the mean and the median. The demo shows exactly this: a right-skewed synthetic sample where the mean sits well above where observations actually cluster most densely. Notes and limitations - One-dimensional data only. A multivariate kernel density estimate needs a bandwidth matrix rather than a single scalar, which is out of scope for this header. - KdeSilvermanBandwidth returns 0.0 for a degenerate sample (fewer than 2 points, or every value identical) - callers must check for this before passing the bandwidth into KdeDensityAt/KdeEvaluateGrid, which both treat a non-positive bandwidth as an error and return 0.0/false. - Silverman's rule assumes an approximately unimodal, roughly normal-ish underlying distribution when choosing the bandwidth - it still works reasonably on the moderately skewed demo sample, but a strongly multimodal distribution may need a narrower, manually-tuned bandwidth to resolve separate peaks that Silverman's rule would otherwise smooth together. - Evaluating the density is O(n) per grid point, so an n by gridSteps evaluation is O(n * gridSteps) in total - fine for the sample sizes a single EA/indicator would realistically build (hundreds of points), not intended for tens of thousands of points evaluated on every tick.
Script · MT4 · v1.0 · Free
About Kernel Density Estimator
Kernel Density Estimator is a script for MetaTrader 4. It has 2 settings, such as Sample size and Grid steps. MQL source code included.
Files in the download: KernelDensityEstimator.mqh, KernelDensityEstimator.txt, KernelDensityEstimatorDemo.mq4.
Settings
| Setting | Default | What it does |
|---|---|---|
| Sample size | 150 | number of synthetic observations |
| Grid steps | 200 | resolution of the evaluated density curve |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
- Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
- Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: MT4 Script · MQL4 Source Code