def _estimate(self, X, var, n_factors): n, p = X.numRows(), X.numCols() old_ll = -numpy.inf llconst = p * numpy.log(2. * numpy.pi) + n_factors psi = numpy.ones(p, dtype=numpy.float32) nsqrt = numpy.sqrt(n) logliks = [] logger.info("Computing factor analysis") for _ in range(self.max_iter): sqrt_psi = numpy.sqrt(psi) + self.__eps s, V, unexp_var = svd(self._tilde(X, sqrt_psi, nsqrt), n_factors) s = s**2 W = self._update_factors(s, V, sqrt_psi) ll = self._loglik(llconst, unexp_var, s, psi, n) logliks.append(ll) psi = self._update_variance(var, W) if abs(ll - old_ll) < self.threshold: break old_ll = ll return W, logliks, psi
def _compute_pcs(X): sds, loadings, _ = svd(X) sds = sds / scipy.sqrt(max(1, X.numRows() - 1)) return loadings, sds
def _whiten(self, X): s, v, _ = svd(X, X.numCols()) K = (v.T / s)[:, :self.n_components] S = K * scipy.sqrt(X.numRows()) S = DenseMatrix(S.shape[0], S.shape[1], S.flatten(), True) return X.multiply(S), K