zatonovo / tawny

Covariance estimation and portfolio optimization using random matrix theory and shrinkage techniques
12 stars 3 forks source link

Incorrect calculations of Ledoit-Wolf shrinkage (?) #1

Open tonytonov opened 6 years ago

tonytonov commented 6 years ago

Hi, I've been working on the Ledoit-Wolf shrinkage algorithm, coding it directly from their 2004 paper and using tawny as a reference to check for numerical correctness of my code. I started observing discrepancies, so I went ahead to debug step by step to find out what's the source of these. To back up my claims, I'm using the original MATLAB code to minimize the possibility of my own bugs. Here's what I have found.

  1. Function shrinkage.p, https://github.com/zatonovo/tawny/blob/76594297a15fecfb16573d545420674d794da5a0/R/shrinkage.R#L127 Instead of phi.mat <- (term.1 - term.2 + term.3)/T there should be phi.mat <- (term.1 - term.2)/T + term.3, see p.13, the expansion term s_{ij} is under the summation, so the 1/T cancels out.

  2. Function shrinkage.r, https://github.com/zatonovo/tawny/blob/76594297a15fecfb16573d545420674d794da5a0/R/shrinkage.R#L159 Same here, should be script.is <- (term.1 - term.2 - term.3) / T + term.4.

  3. Next line, https://github.com/zatonovo/tawny/blob/76594297a15fecfb16573d545420674d794da5a0/R/shrinkage.R#L162 When compared to MATLAB, the result is missing a transpose, so either ratios <- t(diag(sample) %o% diag(sample)^-1)^0.5 or (to align with the MATLAB version) ratios <- sqrt((1 / diag(sample)) %*% t(diag(sample))). With that fix, there is no need to average in the next line, i.e. rhos <- r.bar * ratios * script.is (the same simplification is in the MATLAB code).

All these are my suspicions and I'm not 100% sure, but I've been checking the MATLAB code against my own R implementation (which is very straightforward and mostly using outer to code formulae from the original paper's appendix), and these do match. The fixes I listed above return the same result, which is double-checked, so to speak. I'm not attaching my own version of the code, but if you'd like to see it, feel free to email me and I'll send it over.

profbrowe commented 6 years ago

Thanks, I'll take a look