Equivalent to copying, but additionally the covariance matrix will be cleaned of negative and very
small eigenvalues to make it positive definite.
438264
public MarketEquilibrium clean() {

        final PrimitiveMatrix tmpAssetVolatilities = FinanceUtils.toVolatilities(myCovariances, true);
        final PrimitiveMatrix tmpCleanedCorrelations = FinanceUtils.toCorrelations(myCovariances, true);

        final PrimitiveMatrix tmpCovariances = FinanceUtils.toCovariances(tmpAssetVolatilities, tmpCleanedCorrelations);

        return new MarketEquilibrium(myAssetKeys, tmpCovariances, myRiskAversion);
    }
851778
def feature_reduction(data, method, n_features):
    
    if method == "PCA":
        feature_red_method = sklearn.decomposition.PCA(n_components=n_features)
        data_processed = feature_red_method.fit_transform(data)

    elif method == "agglom":
        feature_red_method = sklearn.cluster.FeatureAgglomeration(n_clusters=n_features)
        data_processed = feature_red_method.fit_transform(data)

    elif method == "ica":
        feature_red_method = sklearn.decomposition.FastICA(n_components=n_features)
        data_processed = feature_red_method.fit_transform(data)

    elif method == "kernelPCA":
        feature_red_method = sklearn.decomposition.KernelPCA(n_components=n_features, kernel='linear')
        data_processed = feature_red_method.fit_transform(data)

    elif method == "kernelPCA":
        feature_red_method = sklearn.decomposition.KernelPCA(n_components=n_features, kernel='linear')
        data_processed = feature_red_method.fit_transform(data)

    elif method == "sparsePCA":
        feature_red_method = sklearn.decomposition.SparsePCA(n_components=n_features)
        data_processed = feature_red_method.fit_transform(data)

    elif method == "incrementalPCA":
        feature_red_method = sklearn.decomposition.IncrementalPCA(n_components=n_features)
        data_processed = feature_red_method.fit_transform(data)

    elif method == "nmf":
        if np.min(data) < 0:
            data -= np.min(data)
        feature_red_method = sklearn.decomposition.NMF(n_components=n_features)
        data_processed = feature_red_method.fit_transform(data)

    else:
        feature_red_method = None
        data_processed = data.copy()

    return(data_processed)
1362057
public void decompose( DMatrixRMaj U , DMatrixRMaj S , DMatrixRMaj V ) {
		// this ensures the resulting rotation matrix will have a determinant of +1 and thus be a real rotation matrix
		if( CommonOps_DDRM.det(U) < 0 ) {
			CommonOps_DDRM.scale(-1,U);
			CommonOps_DDRM.scale(-1,S);
		}

		if( CommonOps_DDRM.det(V) < 0 ) {
			CommonOps_DDRM.scale(-1,V);
			CommonOps_DDRM.scale(-1,S);
		}

		// for possible solutions due to ambiguity in the sign of T and rotation
		extractTransform(U, V, S, solutions.get(0), true, true);
		extractTransform(U, V, S, solutions.get(1), true, false);
		extractTransform(U, V, S, solutions.get(2) , false,false);
		extractTransform(U, V, S, solutions.get(3), false, true);
	}
586720
def _reformat_matrix(self):
        
        nxax = len(self.current_table.data['independent_variables'])
        nyax = len(self.current_table.data['dependent_variables'])
        npts = len(self.current_table.data['dependent_variables'][0]['values'])

        # check if 1 x-axis, and npts (>=2) equals number of y-axes
        if nxax != 1 or nyax != npts or npts < 2:
            return False

        # add second independent variable with each value duplicated npts times
        if len(self.current_table.xheaders) == 2:
            xheader = self.current_table.xheaders[1]
        else:
            xheader = copy.deepcopy(self.current_table.data['independent_variables'][0]['header'])
        self.current_table.data['independent_variables'].append({'header': xheader, 'values': []})
        for value in self.current_table.data['independent_variables'][0]['values']:
            self.current_table.data['independent_variables'][1]['values'].extend([copy.deepcopy(value) for npt in range(npts)])

        # duplicate values of first independent variable npts times
        self.current_table.data['independent_variables'][0]['values'] \
            = [copy.deepcopy(value) for npt in range(npts) for value in self.current_table.data['independent_variables'][0]['values']]

        # suppress header if different for second y-axis
        if self.current_table.data['dependent_variables'][0]['header'] != \
                self.current_table.data['dependent_variables'][1]['header']:
            self.current_table.data['dependent_variables'][0]['header'] = {'name': ''}

        # remove qualifier if different for second y-axis
        iqdel = [] # list of qualifier indices to be deleted
        for iq, qualifier in enumerate(self.current_table.data['dependent_variables'][0]['qualifiers']):
            if qualifier != self.current_table.data['dependent_variables'][1]['qualifiers'][iq]:
                iqdel.append(iq)
        for iq in iqdel[::-1]: # need to delete in reverse order
            del self.current_table.data['dependent_variables'][0]['qualifiers'][iq]

        # append values of second and subsequent y-axes to first dependent variable
        for iy in range(1, nyax):
            for value in self.current_table.data['dependent_variables'][iy]['values']:
                self.current_table.data['dependent_variables'][0]['values'].append(value)

        # finally, delete the second and subsequent y-axes in reverse order
        for iy in range(nyax-1, 0, -1):
            del self.current_table.data['dependent_variables'][iy]

        return True
327156
def _refit(self):
        
        # temporary variables for manipulation
        mu_diff = np.empty_like(self.f.components[0].mu)
        sigma   = np.empty_like(self.f.components[0].sigma)
        mean    = np.empty_like(mu_diff)
        cov     = np.empty_like(sigma)

        for j, c in enumerate(self.g.components):
            # stop if inv_map is empty for j-th comp.
            if not self.inv_map[j]:
                self.g.weights[j] = 0.
                continue

            # (re-)initialize new mean/cov to zero
            mean[:] = 0.0
            cov[:] = 0.0

            # compute total weight and mean
            self.g.weights[j] = self.f.weights[self.inv_map[j]].sum()
            for i in self.inv_map[j]:
                mean += self.f.weights[i] * self.f.components[i].mu

            # rescale by total weight
            mean /= self.g.weights[j]

            # update covariance
            for i in self.inv_map[j]:
                # mu_diff = mu'_j - mu_i
                mu_diff[:] = mean
                mu_diff -= self.f.components[i].mu

                # sigma = (mu'_j - mu_i) (mu'_j - mu_i)^T
                sigma[:] = np.outer(mu_diff, mu_diff)

                # sigma += sigma_i
                sigma += self.f.components[i].sigma

                # multiply with alpha_i
                sigma *= self.f.weights[i]

                # sigma_j += alpha_i * (sigma_i + (mu'_j - mu_i) (mu'_j - mu_i)^T
                cov += sigma

            # 1 / beta_j
            cov /= self.g.weights[j]

            # update the Mixture
            c.update(mean, cov)
836687
public T copy() {
        T ret = createLike();
        ret.getMatrix().set(this.getMatrix());
        return ret;
    }
939687
def G(self):
        
        if self.c is None:
            raise RuntimeError('Inverse cross spectral density requires '
                               'invertible noise covariance matrix c.')
        A = self.A()
        # TODO: can we do that more efficiently?
        G = np.einsum('ji..., jk... ->ik...', A.conj(), self.Cinv())
        G = np.einsum('ij..., jk... ->ik...', G, A)
        return G
1319090
def noise_covariance(fit, dof=2, **kw):
    
    ev = fit.eigenvalues

    measurement_noise = ev[-1]/(fit.n-dof)
    return 4*ev*measurement_noise
205912
def _to_cov(x, n):
    

    if np.isscalar(x):
        if x < 0:
            raise ValueError('covariance must be > 0')
        return np.eye(n) * x

    x = np.atleast_2d(x)
    try:
        # quickly find out if we are positive definite
        np.linalg.cholesky(x)
    except:
        raise ValueError('covariance must be positive definit')

    return x
611029
def decompose_covariance(c):
    

    # make it a kickass copy of the original
    c = _n.array(c)

    # first get the error vector
    e = []
    for n in range(0, len(c[0])): e.append(_n.sqrt(c[n][n]))

    # now cycle through the matrix, dividing by e[1]*e[2]
    for n in range(0, len(c[0])):
        for m in range(0, len(c[0])):
            c[n][m] = c[n][m] / (e[n]*e[m])

    return [_n.array(e), _n.array(c)]
259889
def to_gmfs(shakemap, spatialcorr, crosscorr, site_effects, trunclevel,
            num_gmfs, seed, imts=None):
    
    N = len(shakemap)  # number of sites
    std = shakemap['std']
    if imts is None or len(imts) == 0:
        imts = std.dtype.names
    else:
        imts = [imt for imt in imts if imt in std.dtype.names]
    val = {imt: numpy.log(shakemap['val'][imt]) - std[imt] ** 2 / 2.
           for imt in imts}
    imts_ = [imt.from_string(name) for name in imts]
    M = len(imts_)
    cross_corr = cross_correlation_matrix(imts_, crosscorr)
    mu = numpy.array([numpy.ones(num_gmfs) * val[str(imt)][j]
                      for imt in imts_ for j in range(N)])
    dmatrix = geo.geodetic.distance_matrix(
        shakemap['lon'], shakemap['lat'])
    spatial_corr = spatial_correlation_array(dmatrix, imts_, spatialcorr)
    stddev = [std[str(imt)] for imt in imts_]
    for im, std in zip(imts_, stddev):
        if std.sum() == 0:
            raise ValueError('Cannot decompose the spatial covariance '
                             'because stddev==0 for IMT=%s' % im)
    spatial_cov = spatial_covariance_array(stddev, spatial_corr)
    L = cholesky(spatial_cov, cross_corr)  # shape (M * N, M * N)
    if trunclevel:
        Z = truncnorm.rvs(-trunclevel, trunclevel, loc=0, scale=1,
                          size=(M * N, num_gmfs), random_state=seed)
    else:
        Z = norm.rvs(loc=0, scale=1, size=(M * N, num_gmfs), random_state=seed)
    # Z has shape (M * N, E)
    gmfs = numpy.exp(numpy.dot(L, Z) + mu) / PCTG
    if site_effects:
        gmfs = amplify_gmfs(imts_, shakemap['vs30'], gmfs)
    if gmfs.max() > MAX_GMV:
        logging.warning('There suspiciously large GMVs of %.2fg', gmfs.max())
    return imts, gmfs.reshape((M, N, num_gmfs)).transpose(1, 2, 0)
269460
def shrink(self):
        
        x = np.nan_to_num(self.X.values)

        # de-mean returns
        t, n = np.shape(x)
        meanx = x.mean(axis=0)
        x = x - np.tile(meanx, (t, 1))
        xmkt = x.mean(axis=1).reshape(t, 1)

        # compute sample covariance matrix
        sample = np.cov(np.append(x, xmkt, axis=1), rowvar=False) * (t - 1) / t
        covmkt = sample[0:n, n].reshape(n, 1)
        varmkt = sample[n, n]
        sample = sample[:n, :n]
        prior = np.dot(covmkt, covmkt.T) / varmkt
        prior[np.eye(n) == 1] = np.diag(sample)

        # compute shrinkage parameters
        if self.delta is None:
            c = np.linalg.norm(sample - prior, "fro") ** 2
            y = x ** 2
            p = 1 / t * np.sum(np.dot(y.T, y)) - np.sum(sample ** 2)
            # r is divided into diagonal
            # and off-diagonal terms, and the off-diagonal term
            # is itself divided into smaller terms
            rdiag = 1 / t * np.sum(y ** 2) - sum(np.diag(sample) ** 2)
            z = x * np.tile(xmkt, (n,))
            v1 = 1 / t * np.dot(y.T, z) - np.tile(covmkt, (n,)) * sample
            roff1 = (
                np.sum(v1 * np.tile(covmkt, (n,)).T) / varmkt
                - np.sum(np.diag(v1) * covmkt.T) / varmkt
            )
            v3 = 1 / t * np.dot(z.T, z) - varmkt * sample
            roff3 = (
                np.sum(v3 * np.dot(covmkt, covmkt.T)) / varmkt ** 2
                - np.sum(np.diag(v3).reshape(-1, 1) * covmkt ** 2) / varmkt ** 2
            )
            roff = 2 * roff1 - roff3
            r = rdiag + roff

            # compute shrinkage constant
            k = (p - r) / c
            shrinkage = max(0, min(1, k / t))
            self.delta = shrinkage
        else:
            # use specified constant
            shrinkage = self.delta

        # compute the estimator
        sigma = shrinkage * prior + (1 - shrinkage) * sample
        return self.format_and_annualise(sigma)
1223155
function(scale,originX,originY) {
            originX = originX || 0;
            originY = originY || 0;
            return new Matrix(this.mtx && this.mtx.translate(originX,originY).scale(scale).translate(-originX,-originY));
        }
1102802
def cov2corr(cov):
    
    assert cov.ndim == 2, 'covariance matrix should be 2D array'
    inv_sd = 1 / np.sqrt(np.diag(cov))
    corr = cov * inv_sd[None, :] * inv_sd[:, None]
    return corr
1340920
private static Matrix permuteMatrix(Matrix mat, int cpRows, int cpCols) {
		//fix up cp to be positive
		if(cpRows == 0 && cpCols == 0) {
			return mat.copy();
		}
		if(cpRows < 0) {
			cpRows = mat.getRowDimension()+cpRows;
		}
		if(cpRows < 0 || cpRows >= mat.getRowDimension()) {
			throw new ArrayIndexOutOfBoundsException( String.format(
					"Can't permute rows by %d: only %d rows.",
					cpRows, mat.getRowDimension() )
			);
		}

		if(cpCols < 0) {
			cpCols = mat.getColumnDimension()+cpCols;
		}
		if(cpCols < 0 || cpCols >= mat.getColumnDimension()) {
			throw new ArrayIndexOutOfBoundsException( String.format(
					"Can't permute cols by %d: only %d rows.",
					cpCols, mat.getColumnDimension() )
			);
		}

		int[] rows = new int[mat.getRowDimension()];
		for(int i=0;i<rows.length;i++) {
			rows[i] = (i+cpRows)%rows.length;
		}
		int[] cols = new int[mat.getColumnDimension()];
		for(int i=0;i<cols.length;i++) {
			cols[i] = (i+cpCols)%cols.length;
		}

		Matrix newMat = mat.getMatrix(rows, cols);
		assert(newMat.getRowDimension() == mat.getRowDimension());
		assert(newMat.getColumnDimension() == mat.getColumnDimension());
		assert(newMat.get(0, 0) ==
			mat.get(cpRows%mat.getRowDimension(), cpCols%mat.getColumnDimension()));


		return newMat;
	}
1022599
def copy(self):
        

        copied_model = GPModel( kernel = self.model.kern.copy(),
                                noise_var= self.noise_var ,
                                exact_feval= self.exact_feval,
                                n_samples = self.n_samples,
                                n_burnin = self.n_burnin,
                                subsample_interval = self.subsample_interval,
                                step_size = self.step_size,
                                leapfrog_steps= self.leapfrog_steps,
                                verbose= self.verbose)

        copied_model._create_model(self.model.X,self.model.Y)
        copied_model.updateModel(self.model.X,self.model.Y, None, None)
        return copied_model
131032
def _u_distance_covariance_sqr_naive(x, y, exponent=1):
    
    a = _u_distance_matrix(x, exponent=exponent)
    b = _u_distance_matrix(y, exponent=exponent)

    return u_product(a, b)