def ledoit_wolf(X, assume_centered=False, block_size=1000):
 X = np.asarray(X)
 if ((len(X.shape) == 2) and (X.shape[1] == 1)):
  if (not assume_centered):
   X = (X - X.mean())
  return (np.atleast_2d((X ** 2).mean()), 0.0)
 if (X.ndim == 1):
  X = np.reshape(X, (1, (-1)))
  warnings.warn('Only one sample available. You may want to reshape your data array')
  n_samples = 1
  n_features = X.size
 else:
  (n_samples, n_features) = X.shape
 shrinkage = ledoit_wolf_shrinkage(X, assume_centered=assume_centered, block_size=block_size)
 emp_cov = empirical_covariance(X, assume_centered=assume_centered)
 mu = (np.sum(np.trace(emp_cov)) / n_features)
 shrunk_cov = ((1.0 - shrinkage) * emp_cov)
 shrunk_cov.flat[::(n_features + 1)] += (shrinkage * mu)
 return (shrunk_cov, shrinkage)

def ledoit_wolf ( X , assume_centered = False , block_size = 1000 ) : X = np . asarray ( X ) if ( ( len ( X . shape ) == 2 ) and ( X . shape [ 1 ] == 1 ) ) : if ( not assume_centered ) : X = ( X - X . mean ( ) ) return ( np . atleast_2d ( ( X ** 2 ) . mean ( ) ) , 0.0 ) if ( X . ndim == 1 ) : X = np . reshape ( X , ( 1 , ( - 1 ) ) ) warnings . warn ( 'Only one sample available. You may want to reshape your data array' ) n_samples = 1 n_features = X . size else : ( n_samples , n_features ) = X . shape shrinkage = ledoit_wolf_shrinkage ( X , assume_centered = assume_centered , block_size = block_size ) emp_cov = empirical_covariance ( X , assume_centered = assume_centered ) mu = ( np . sum ( np . trace ( emp_cov ) ) / n_features ) shrunk_cov = ( ( 1.0 - shrinkage ) * emp_cov ) shrunk_cov . flat [ : : ( n_features + 1 ) ] += ( shrinkage * mu ) return ( shrunk_cov , shrinkage )
estimates the shrunk ledoit-wolf covariance matrix .