[SciPy-User] scipy.interpolate.rbf: how is "smooth" defined?

Gael Varoquaux gael.varoquaux at normalesup.org
Mon Aug 30 12:22:29 EDT 2010


On Mon, Aug 30, 2010 at 12:18:58PM -0400, josef.pktd at gmail.com wrote:
> Please do, I can also use it for other things. (I never read the small
> print in Ledoit-Wolf.)

There you go (BSD licensed). This will land in scikit learn at some point
because we are going to need it in different places, but I haven't found
the time to polish it.

Gael

def ledoit_wolf(x, return_factor=False):
    """ Estimates the shrunk Ledoit-Wolf covariance matrix.

        Parameters
        ----------
        x: 2D ndarray, shape (n, p)
            The data matrix, with p features and n samples.
        return_factor: boolean, optional
            If return_factor is True, the regularisation_factor is
            returned.

        Returns
        -------
        regularised_cov: 2D ndarray
            Regularized covariance
        regularisation_factor: float
            Regularisation factor

        Notes
        -----
        The regularised covariance is::

            (1 - regularisation_factor)*cov 
                    + regularisation_factor*np.identity(n_features)
    """
    n_samples, n_features = x.shape
    if n_features == 1:
        if return_factor:
            return np.atleast_2d(x.std()), 0
        return np.atleast_2d(x.std())
    cov = np.dot(x.T, x)/n_samples
    i = np.identity(n_features)
    mu = np.trace(cov)/n_features
    delta = ((cov - mu*i)**2).sum()/n_features
    x2 = x**2
    beta_ = 1./(n_features*n_samples) * np.sum(
                            np.dot(x2.T, x2)/n_samples - cov**2
                )

    beta = min(beta_, delta)
    alpha = delta - beta
    if not return_factor:
        return beta/delta * mu * i + alpha/delta * cov
    else:
        return beta/delta * mu * i + alpha/delta * cov, beta/delta






More information about the SciPy-User mailing list