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.
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.
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.Function
shrinkage.p
, https://github.com/zatonovo/tawny/blob/76594297a15fecfb16573d545420674d794da5a0/R/shrinkage.R#L127 Instead ofphi.mat <- (term.1 - term.2 + term.3)/T
there should bephi.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.Function
shrinkage.r
, https://github.com/zatonovo/tawny/blob/76594297a15fecfb16573d545420674d794da5a0/R/shrinkage.R#L159 Same here, should bescript.is <- (term.1 - term.2 - term.3) / T + term.4
.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.