def test_pca_svd(): xreduced, factors, evals, evecs = pca(xf) factors_wconst = np.c_[factors, np.ones((factors.shape[0], 1))] beta = np.dot(np.linalg.pinv(factors_wconst), xf) #np.dot(np.linalg.pinv(factors_wconst),x2/1000.).T[:,:4] - evecs assert_array_almost_equal(beta.T[:, :4], evecs, 14) xred_svd, factors_svd, evals_svd, evecs_svd = pcasvd(xf, keepdim=0) assert_array_almost_equal(evals_svd, evals, 14) msign = (evecs / evecs_svd)[0] assert_array_almost_equal(msign * evecs_svd, evecs, 14) assert_array_almost_equal(msign * factors_svd, factors, 13) assert_array_almost_equal(xred_svd, xreduced, 14) pcares = pca(xf, keepdim=2) pcasvdres = pcasvd(xf, keepdim=2) check_pca_svd(pcares, pcasvdres)
def test_pca_svd(): xreduced, factors, evals, evecs = pca(xf) factors_wconst = np.c_[factors, np.ones((factors.shape[0],1))] beta = np.dot(np.linalg.pinv(factors_wconst), xf) #np.dot(np.linalg.pinv(factors_wconst),x2/1000.).T[:,:4] - evecs assert_array_almost_equal(beta.T[:,:4], evecs, 14) xred_svd, factors_svd, evals_svd, evecs_svd = pcasvd(xf, keepdim=0) assert_array_almost_equal(evals_svd, evals, 14) msign = (evecs/evecs_svd)[0] assert_array_almost_equal(msign*evecs_svd, evecs, 14) assert_array_almost_equal(msign*factors_svd, factors, 13) assert_array_almost_equal(xred_svd, xreduced, 14) pcares = pca(xf, keepdim=2) pcasvdres = pcasvd(xf, keepdim=2) check_pca_svd(pcares, pcasvdres)
if xnames: ax.set_xticks(np.arange(nvars)+0.5) ax.set_xticklabels(xnames, minor=True, fontsize='small',rotation=45, horizontalalignment='right') #some keywords don't work in previous line ? plt.setp( ax.get_xticklabels(), fontsize='small', rotation=45, horizontalalignment='right') fig.colorbar(axim) ax.set_title(title) plot_corr(rrcorr, xnames=ticksym) nvars = rrcorr.shape[0] plt.figure() plt.hist(rrcorr[np.triu_indices(nvars,1)]) plt.title('Correlation Coefficients') xreda, facta, evaa, evea = sbtools.pcasvd(rr) evallcs = (evaa).cumsum() print evallcs/evallcs[-1] xred, fact, eva, eve = sbtools.pcasvd(rr, keepdim=4) pcacorr = np.corrcoef(xred, rowvar=0) resid = rr-xred residcorr = np.corrcoef(resid, rowvar=0) plot_corr(residcorr, xnames=ticksym, title='Correlation Residuals') plt.matshow(residcorr) plt.imshow(residcorr, cmap=plt.cm.jet, interpolation='nearest', extent=(0,30,0,30), vmin=-1.0, vmax=1.0) plt.colorbar() #plt.show() #plt.close('all')