#include #include // // Written by: // -- // John L. Weatherwax 2010-02-25 // // email: wax@alum.mit.edu // // Please send comments and especially bug reports to the // above email address. // //----- void covMarket( matrix & x, double shrink, matrix & sigma ){ // // Input: // x (t*n): t iid observations on n random variables // shrink : shrinkage amount // Output: // sigma (n*n): invertible covariance matrix estimator // // This estimator is a weighted average of the sample // covariance matrix and a "prior" or "shrinkage target". // Here, the prior is given by a one-factor model. // The factor is equal to the cross-sectional average // of all the random variables. // // The notation follows Ledoit and Wolf (2003) // assert( (0.0 <= shrink) && (shrink <= 1.0) ); bool demeanReturns = true; size_t t=x.size1(); size_t n=x.size2(); // Do we demean the stock returns: if( demeanReturns ){ // compute the mean of the returns: matrix meanx(1,n); meanx.clear(); for( size_t jj=0; jj xmkt(t,1); xmkt.clear(); for( size_t ii=0; ii sample(n,n); sample = prod( trans(x), x ) / t; // compute the covariance of xmkt with each stock "covmkt": matrix covmkt(n,1); covmkt.clear(); for( size_t ii=0; ii prior(n,n); prior.clear(); prior = prod( covmkt, trans(covmkt) ) / varmkt; // override the diagonal elements: for( size_t ii=0; ii