def ica(data, k, c): n = data.count() # reduce dimensionality comps, latent, scores = svd4(data, k, 0) # whiten data whtMat = real(dot(inv(diag(sqrt(latent))), comps)) unwhtMat = real(dot(transpose(comps), diag(sqrt(latent)))) wht = data.map(lambda x: dot(whtMat, x)) # do multiple independent component extraction B = orth(random.randn(k, c)) Bold = zeros((k, c)) iterNum = 0 minAbsCos = 0 tol = 0.000001 iterMax = 1000 errVec = zeros(iterMax) while (iterNum < iterMax) & ((1 - minAbsCos) > tol): iterNum += 1 # update rule for pow3 nonlinearity (TODO: add other nonlins) B = wht.map(lambda x: outer(x, dot(x, B) ** 3)).sum() / n - 3 * B # orthognalize B = dot(B, real(sqrtm(inv(dot(transpose(B), B))))) # evaluate error minAbsCos = min(abs(diag(dot(transpose(B), Bold)))) # store results Bold = B errVec[iterNum - 1] = 1 - minAbsCos # get unmixing matrix W = dot(transpose(B), whtMat) # get components sigs = data.map(lambda x: dot(W, x)) return W, sigs, whtMat, unwhtMat
def pca(data, k): comps, latent, scores = svd4(data, k, 0) return comps, latent, scores