Loading…

Shrinkage Covariance Estimator

A raw sample covariance matrix from p assets is unreliable with anything less than roughly ten times p observations, and is not even mathematically invertible with fewer observations than assets - which is exactly the situation a short recent-history window over several correlated instruments produces. Portfolio optimisation, risk parity weighting and any other calculation that needs the covariance matrix's INVERSE fails outright at precisely the moment data is scarcest, with no warning beyond a matrix inversion error deep inside the caller's code. Ledoit-Wolf shrinkage (Ledoit & Wolf, 2004) fixes this by blending the noisy sample covariance toward a simple, well-conditioned target - a scaled identity matrix here, i.e. a target with no correlation between assets and equal variance across all of them - using a shrinkage intensity computed directly from the data rather than a fixed or guessed ratio. The intensity trades off two errors: too little shrinkage keeps all the sample noise, too much shrinkage forces every asset toward being uncorrelated with equal variance regardless of what the data actually shows. Ledoit and Wolf derived a closed-form intensity that minimises expected squared estimation error under fairly general conditions, requiring only one pass over the data - no iterative fitting or cross-validation needed. The resulting matrix is symmetric positive-definite, and therefore invertible, for any shrinkage intensity above zero, which in practice means it is invertible whenever the sample data has any noise in it at all. Notes and limitations - The shrinkage target here is a scaled identity matrix (equal variance, zero correlation). This is the standard Ledoit-Wolf 2004 target and works well generally, but a "constant correlation" target (Ledoit-Wolf 2003) can be more appropriate when assets are known to share a common positive correlation structure - that alternative target is not implemented here. - ShrinkLedoitWolf assumes the input has already been demeaned (call ShrinkDemean first) - feeding raw, non-centred returns will silently produce a covariance matrix contaminated by the mean level, not an error. - The sample covariance inside this header uses the 1/n (MLE) normalisation, not 1/(n-1) - this matches the asymptotic variance estimator the Ledoit- Wolf formula is built on and is standard for this specific estimator, but differs from the more familiar 1/(n-1) sample covariance used elsewhere. - ShrinkInvertMatrix is a general-purpose Gauss-Jordan inverter included to demonstrate and exercise invertibility; for genuinely large asset counts (dozens or more) a dedicated numerical library outside MQL4 would be more appropriate than this straightforward O(p^3) implementation. - Shrinkage intensity is reported so a caller can judge how much the result actually differs from the raw sample covariance - an intensity near 0 means the data had enough observations that shrinkage barely mattered; an intensity near 1 means the sample covariance was very noisy relative to the target.

Script · MT4 · v1.0 · Free

About Shrinkage Covariance Estimator

Shrinkage Covariance Estimator is a script for MetaTrader 4. It has 3 settings, such as Assets, Observations and Seed. MQL source code included.

Files in the download: ShrinkageCovariance.mqh, ShrinkageCovariance.txt, ShrinkageCovarianceDemo.mq4.

Settings

SettingDefaultWhat it does
Assets6Number of synthetic assets (columns)
Observations4Number of return observations (rows) - deliberately < InpAssets
Seed9Random seed

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
  3. Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
  4. Open the .mq4 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: MT4 Script · MQL4 Source Code

MQL4 & MQL5 scripts · PolyMaria