Variance Equality Tests
Two return, spread, or P/L samples almost never have EXACTLY the same variance by pure chance - the real question is whether the difference observed is bigger than random sampling alone would produce. Bartlett's test and Levene's test both answer that question, from two different angles. Bartlett's test is the classical choice, more statistically powerful when both samples are plausibly close to normally distributed - but its own power comes at a cost: it is itself sensitive to non-normality, and can report a "significant" difference in variance that is really just a difference in shape. Levene's test (the Brown-Forsythe variant used here, which centres each sample on its MEDIAN rather than its mean before comparing) is markedly more robust to non-normal, skewed samples - which trading return and spread samples routinely are - at some cost in statistical power when normality genuinely does hold. Running both side by side, as the demo does, is the honest way to use either. MQL4 has no MathErf/MathErfc, so p-values here use a chi-square approximation rather than an exact F-distribution or chi-square computation: Bartlett's statistic is asymptotically chi-square(1) under the null hypothesis of equal variances; Levene's statistic is an F(1, n1+n2-2) statistic, and F(1, df2) converges to chi-square(1) as df2 grows (the standard chi-square limit of the F distribution with a numerator degree of freedom of 1) - so the same chi-square(1) p-value helper serves both tests. The chi-square(1) tail itself is computed exactly via P(chi2_1 > x) = 2*P(Z > sqrt(x)), using the Abramowitz & Stegun normal-tail polynomial approximation already used elsewhere in this catalogue for the same MathErf gap. Notes and limitations - Both p-values are LARGE-SAMPLE approximations. Bartlett's chi-square(1) approximation and Levene's F-to-chi-square(1) approximation both become progressively less accurate as sample sizes shrink; treat results from samples smaller than roughly 20-30 points per group as indicative rather than precise, and prefer the test statistic itself for relative comparisons across several pairs of samples. - Two-sample only. Neither function generalises to comparing three or more groups' variances at once (the general k-group Bartlett/Levene statistics need a different degrees-of-freedom formula this header does not implement). - Bartlett's test itself assumes approximate normality; if a sample is visibly skewed or heavy-tailed, prefer Levene's result, or require both tests to agree before acting on a "variance changed" signal. - VetSampleVariance uses the standard n-1 (unbiased) denominator.
Script · MT4 · v1.0 · Free
About Variance Equality Tests
Variance Equality Tests is a script for MetaTrader 4. It has 1 setting, such as Sample size. MQL source code included.
Files in the download: VarianceEqualityTests.mqh, VarianceEqualityTests.txt, VarianceEqualityTestsDemo.mq4.
Settings
| Setting | Default | What it does |
|---|---|---|
| Sample size | 30 | Number of points in each synthetic sample |
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