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, 13) assert_array_almost_equal(msign*factors_svd, factors, 12) assert_array_almost_equal(xred_svd, xreduced, 13) 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)
print("Error with unpickling, a new pickle file can be created with findow_1") raise ticksym = rrdm.columns.tolist() rr = rrdm.values[1:400] rrcorr = np.corrcoef(rr, rowvar=0) 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) plot_corr(pcacorr, xnames=ticksym, title='Correlation PCA') 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()
except Exception: #blanket for any unpickling error print "Error with unpickling, a new pickle file can be created with findow_1" raise ticksym = rrdm.columns.tolist() rr = rrdm.values[1:400] rrcorr = np.corrcoef(rr, rowvar=0) 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) plot_corr(pcacorr, xnames=ticksym, title='Correlation PCA') 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',