<commit_before>"""
Covariance estimators using shrinkage.

Shrinkage corresponds to regularising `cov` using a convex combination:
shrunk_cov = (1-shrinkage)*cov + shrinkage*structured_estimate.

"""

# Author: <NAME> <<EMAIL>>
#         <NAME> <<EMAIL>>
#
# License: BSD Style.

import numpy as np

from .empirical_covariance_ import empirical_covariance, EmpiricalCovariance

###############################################################################
# ShrunkCovariance estimator

def shrunk_covariance(emp_cov, shrinkage=0.1):
    """Calculates a covariance matrix shrunk on the diagonal
    
    Params
    ------
    emp_cov: 2D ndarray, shape (n_features, n_features)
      Covariance matrix to be shrunk
    shrinkage: float, 0 <= shrinkage <= 1
      coefficient in the convex combination used for the computation
      of the shrunk estimate.
    
    Returns
    -------
    shrunk_cov: 2D ndarray
      Shrunk covariance
    
    Notes
    -----
    The regularized (shrunk) covariance is given by
    
    (1 - shrinkage)*cov
      + shrinkage*mu*np.identity(n_features)
    
    where mu = trace(cov) / n_features
    
    """
    emp_cov = np.atleast_2d(emp_cov)
    n_features = emp_cov.shape[0]
    
    mu = np.trace(emp_cov) / n_features
    shrunk_cov = (1.-shrinkage)*emp_cov
    shrunk_cov.flat[::n_features + 1] += shrinkage*mu

    return shrunk_cov

class ShrunkCovariance(EmpiricalCovariance):
    """Covariance estimator with shrinkage
    
    Parameters
    ----------
    shrinkage: float, 0 <= shrinkage <= 1
      coefficient in the convex combination used for the computation
      of the shrunk estimate.
    
    Attributes
    ----------
    `shrinkage_`: float, 0 <= shrinkage <= 1
      coefficient in the convex combination used for the computation
      of the shrunk estimate.
    
    Notes
    -----
    The regularized covariance is given by
    
    (1 - shrinkage)*cov
      + shrinkage*mu*np.identity(n_features)
    
    where mu = trace(cov) / n_features
    
    """
    def __init__(self, store_precision=True, shrinkage=0.1):
        super(ShrunkCovariance, self).__init__(store_precision)
        self.shrinkage_ = shrinkage


    def fit(self, X, **params):
        self._set_params(**params)
        covariance = shrunk_covariance(empirical_covariance(X), self.shrinkage_)
        self._set_estimates(covariance)
        
        return self

################################################################################
# Ledoit-Wolf estimator

def ledoit_wolf(X):
    """Estimates the shrunk Ledoit-Wolf covariance matrix.

    Parameters
    ----------
    X: 2D ndarray, shape (n_samples, n_features)
      Data from which to compute the covariance estimate
    
    Returns
    -------
    shrunk_cov: 2D ndarray, shape (n_features, n_features)
      Shrunk covariance
    shrinkage: float
      coefficient in the convex combination used for the computation
      of the shrunk estimate.
    
    Notes
    -----
    The regularised (shrunk) covariance is:
    
    (1 - shrinkage)*cov
      + shrinkage * mu * np.identity(n_features)
    
    where mu = trace(cov) / n_features
    
    """
    X = np.asanyarray(X, dtype=np.float64)
    # for only one feature, the result is the same whatever the shrinkage
    if X.ndim == 1:
        return np.atleast_2d((X**2).mean()), 0.
    n_samples, n_features = X.shape
    
    emp_cov = empirical_covariance(X)
    print emp_cov
    mu = np.trace(emp_cov) / n_features
    delta_ = emp_cov.copy()
    delta_.flat[::n_features + 1] -= mu
    delta = (delta_**2).sum() / n_features
    X2 = X**2
    beta_ = 1./(n_features*n_samples) \
        * np.sum(np.dot(X2.T, X2)/n_samples - emp_cov**2)
    
    print emp_cov
    beta = min(beta_, delta)
    shrinkage = beta/delta
    shrunk_cov = (1.-shrinkage)*emp_cov
    shrunk_cov.flat[::n_features + 1] += shrinkage*mu
    
    return shrunk_cov, shrinkage


class LedoitWolf(ShrunkCovariance):<commit_msg>Cosmit<commit_after>"""
Covariance estimators using shrinkage.

Shrinkage corresponds to regularising `cov` using a convex combination:
shrunk_cov = (1-shrinkage)*cov + shrinkage*structured_estimate.

"""

# Author: <NAME> <<EMAIL>>
#         <NAME> <<EMAIL>>
#
# License: BSD Style.

import numpy as np

from .empirical_covariance_ import empirical_covariance, EmpiricalCovariance

###############################################################################
# ShrunkCovariance estimator

def shrunk_covariance(emp_cov, shrinkage=0.1):
    """Calculates a covariance matrix shrunk on the diagonal
    
    Params
    ------
    emp_cov: 2D ndarray, shape (n_features, n_features)
      Covariance matrix to be shrunk
    shrinkage: float, 0 <= shrinkage <= 1
      coefficient in the convex combination used for the computation
      of the shrunk estimate.
    
    Returns
    -------
    shrunk_cov: 2D ndarray
      Shrunk covariance
    
    Notes
    -----
    The regularized (shrunk) covariance is given by
    
    (1 - shrinkage)*cov
      + shrinkage*mu*np.identity(n_features)
    
    where mu = trace(cov) / n_features
    
    """
    emp_cov = np.atleast_2d(emp_cov)
    n_features = emp_cov.shape[0]
    
    mu = np.trace(emp_cov) / n_features
    shrunk_cov = (1.-shrinkage)*emp_cov
    shrunk_cov.flat[::n_features + 1] += shrinkage*mu

    return shrunk_cov

class ShrunkCovariance(EmpiricalCovariance):
    """Covariance estimator with shrinkage
    
    Parameters
    ----------
    shrinkage: float, 0 <= shrinkage <= 1
      coefficient in the convex combination used for the computation
      of the shrunk estimate.
    
    Attributes
    ----------
    `shrinkage_`: float, 0 <= shrinkage <= 1
      coefficient in the convex combination used for the computation
      of the shrunk estimate.
    
    Notes
    -----
    The regularized covariance is given by
    
    (1 - shrinkage)*cov
      + shrinkage*mu*np.identity(n_features)
    
    where mu = trace(cov) / n_features
    
    """
    def __init__(self, store_precision=True, shrinkage=0.1):
        super(ShrunkCovariance, self).__init__(store_precision)
        self.shrinkage_ = shrinkage


    def fit(self, X, **params):
        self._set_params(**params)
        covariance = shrunk_covariance(empirical_covariance(X), self.shrinkage_)
        self._set_estimates(covariance)
        
        return self

################################################################################
# Ledoit-Wolf estimator

def ledoit_wolf(X):
    """Estimates the shrunk Ledoit-Wolf covariance matrix.

    Parameters
    ----------
    X: 2D ndarray, shape (n_samples, n_features)
      Data from which to compute the covariance estimate
    
    Returns
    -------
    shrunk_cov: 2D ndarray, shape (n_features, n_features)
      Shrunk covariance
    shrinkage: float
      coefficient in the convex combination used for the computation
      of the shrunk estimate.
    
    Notes
    -----
    The regularised (shrunk) covariance is:
    
    (1 - shrinkage)*cov
      + shrinkage * mu * np.identity(n_features)
    
    where mu = trace(cov) / n_features
    
    """
    X = np.asanyarray(X, dtype=np.float64)
    # for only one feature, the result is the same whatever the shrinkage
    if X.ndim == 1:
        return np.atleast_2d((X**2).mean()), 0.
    n_samples, n_features = X.shape
    
    emp_cov = empirical_covariance(X)
    mu = np.trace(emp_cov) / n_features
    delta_ = emp_cov.copy()
    delta_.flat[::n_features + 1] -= mu
    delta = (delta_**2).sum() / n_features
    X2 = X**2
    beta_ = 1./(n_features*n_samples) \
        * np.sum(np.dot(X2.T, X2)/n_samples - emp_cov**2)
    
    beta = min(beta_, delta)
    shrinkage = beta/delta
    shrunk_cov = (1.-shrinkage)*emp_cov
    shrunk_cov.flat[::n_features + 1] += shrinkage*mu
    
    return shrunk_cov, shrinkage


class LedoitWolf(ShrunkCovariance):
{'fraction_non_alphanumeric': 0.14200583238240141, 'fraction_numerical': 0.009636110054520097, 'mean_word_length': 2.6877045348293596, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '969869', 'n_tokens_mistral': 2659, 'n_tokens_neox': 2409, 'n_words': 627}