"""This module implements some common validation schemes from pca and pls. """ from scipy import ones,mean,sqrt,dot,newaxis,zeros,sum,empty,\ apply_along_axis,eye,kron,array,sort from scipy.stats import median from scipy.linalg import triu,inv,svd,norm from select_generators import w_pls_gen,w_pls_gen_jk,pls_gen,pca_gen,diag_pert from engines import w_simpls,pls,bridge,pca from cx_utils import m_shape def w_pls_cv_val(X, Y, amax, n_blocks=None, algo='simpls'): """Returns rmsep and aopt for pls tailored for wide X. comments: -- X, Y inputs need to be centered (fixme: check) """ k, l = m_shape(Y) PRESS = zeros((l, amax+1), dtype='f') if n_blocks==None: n_blocks = Y.shape[0] XXt = dot(X, X.T) V = w_pls_gen(XXt, Y, n_blocks=n_blocks, center=True) for Din, Doi, Yin, Yout in V: ym = -sum(Yout, 0)[newaxis]/(1.0*Yin.shape[0]) Yin = Yin - ym PRESS[:,0] = PRESS[:,0] + ((Yout - ym)**2).sum(0) if algo=='simpls': dat = w_simpls(Din, Yin, amax) Q, U, H = dat['Q'], dat['U'], dat['H'] That = dot(Doi, dot(U, inv(triu(dot(H.T,U))) )) else: raise NotImplementedError Yhat = [] for j in range(l): TQ = dot(That, triu(dot(Q[j,:][:,newaxis], ones((1,amax)))) ) E = Yout[:,j][:,newaxis] - TQ E = E + sum(E, 0)/Din.shape[0] PRESS[j,1:] = PRESS[j,1:] + sum(E**2, 0) #Yhat = Y - dot(That,Q.T) rmsep = sqrt(PRESS/Y.shape[0]) aopt = find_aopt_from_sep(rmsep) return rmsep, aopt def pls_val(X, Y, amax=2, n_blocks=10, algo='pls', metric=None): """ Validation results of pls model. comments: -- X, Y inputs need to be centered (fixme: check) """ k, l = m_shape(Y) PRESS = zeros((l, amax+1), dtype='