def pcaTB(f, time, a=0.5, p=.99, no=5, parallel=True): """ This function computes tolerance bounds for functional data containing phase and amplitude variation using fPCA :param f: numpy ndarray of shape (M,N) of N functions with M samples :param time: vector of size M describing the sample points :param a: confidence level of tolerance bound (default = 0.05) :param p: coverage level of tolerance bound (default = 0.99) :param no: number of principal components (default = 5) :param parallel: enable parallel processing (default = T) :type f: np.ndarray :type time: np.ndarray :rtype: tuple of boxplot objects :return warp: alignment data from time_warping :return pca: functional pca from jointFPCA :return tol: tolerance factor """ # Align Data out_warp = fs.fdawarp(f, time) out_warp.srsf_align( method="median", parallel=parallel) # Calculate pca out_pca = fpca.fdajpca(out_warp) out_pca.calc_fpca(no) # Calculate TB tol = mvtol_region(out_pca.coef, a, p, 100000) return warp, pca, tol
def joint_gauss_model(self, n=1, no=3): """ This function models the functional data using a joint Gaussian model extracted from the principal components of the srsfs :param n: number of random samples :param no: number of principal components (default = 3) :type n: integer :type no: integer """ # Parameters fn = self.fn time = self.time qn = self.qn gam = self.gam M = time.size # Perform PCA jfpca = fpca.fdajpca(self) jfpca.calc_fpca(no=no) s = jfpca.latent U = jfpca.U C = jfpca.C mu_psi = jfpca.mu_psi # compute mean and covariance mq_new = qn.mean(axis=1) mididx = jfpca.id m_new = np.sign(fn[mididx, :]) * np.sqrt(np.abs(fn[mididx, :])) mqn = np.append(mq_new, m_new.mean()) # generate random samples vals = np.random.multivariate_normal(np.zeros(s.shape), np.diag(s), n) tmp = np.matmul(U, np.transpose(vals)) qhat = np.tile(mqn.T, (n, 1)).T + tmp[0:M + 1, :] tmp = np.matmul(U, np.transpose(vals) / C) vechat = tmp[(M + 1):, :] psihat = np.zeros((M, n)) gamhat = np.zeros((M, n)) for ii in range(n): psihat[:, ii] = geo.exp_map(mu_psi, vechat[:, ii]) gam_tmp = cumtrapz(psihat[:, ii]**2, np.linspace(0, 1, M), initial=0.0) gamhat[:, ii] = (gam_tmp - gam_tmp.min()) / (gam_tmp.max() - gam_tmp.min()) ft = np.zeros((M, n)) fhat = np.zeros((M, n)) for ii in range(n): fhat[:, ii] = uf.cumtrapzmid( time, qhat[0:M, ii] * np.fabs(qhat[0:M, ii]), np.sign(qhat[M, ii]) * (qhat[M, ii] * qhat[M, ii]), mididx) ft[:, ii] = uf.warp_f_gamma(np.linspace(0, 1, M), fhat[:, ii], gamhat[:, ii]) self.rsamps = True self.fs = fhat self.gams = gamhat self.ft = ft self.qs = qhat[0:M, :] return
def calc_model(self, pca_method="combined", no=5, smooth_data=False, sparam=25, parallel=False, C=None): """ This function identifies a regression model with phase-variability using elastic pca :param pca_method: string specifing pca method (options = "combined", "vert", or "horiz", default = "combined") :param no: scalar specify number of principal components (default=5) :param smooth_data: smooth data using box filter (default = F) :param sparam: number of times to apply box filter (default = 25) :param parallel: run in parallel (default = F) :param C: scale balance parameter for combined method (default = None) """ if smooth_data: self.f = fs.smooth_data(self.f, sparam) N1 = self.f.shape[1] # Align Data self.warp_data = fs.fdawarp(self.f, self.time) self.warp_data.srsf_align(parallel=parallel) # Calculate PCA if pca_method == 'combined': out_pca = fpca.fdajpca(self.warp_data) elif pca_method == 'vert': out_pca = fpca.fdavpca(self.warp_data) elif pca_method == 'horiz': out_pca = fpca.fdahpca(self.warp_data) else: raise Exception('Invalid fPCA Method') out_pca.calc_fpca(no) # OLS using PCA basis lam = 0 R = 0 Phi = np.ones((N1, no + 1)) Phi[:, 1:(no + 1)] = out_pca.coef xx = dot(Phi.T, Phi) inv_xx = inv(xx + lam * R) xy = dot(Phi.T, self.y) b = dot(inv_xx, xy) alpha = b[0] b = b[1:no + 1] # compute the SSE int_X = np.zeros(N1) for ii in range(0, N1): int_X[ii] = np.sum(out_pca.coef * b) SSE = np.sum((self.y - alpha - int_X)**2) self.alpha = alpha self.b = b self.pca = out_pca self.SSE = SSE self.pca_method = pca_method return
def calc_model(self, pca_method="combined", no=5, smooth_data=False, sparam=25, parallel=False): """ This function identifies a logistic regression model with phase-variability using elastic pca :param f: numpy ndarray of shape (M,N) of N functions with M samples :param y: numpy array of N responses :param time: vector of size M describing the sample points :param pca_method: string specifing pca method (options = "combined", "vert", or "horiz", default = "combined") :param no: scalar specify number of principal components (default=5) :param smooth_data: smooth data using box filter (default = F) :param sparam: number of times to apply box filter (default = 25) :param parallel: run model in parallel (default = F) :type f: np.ndarray :type time: np.ndarray """ if smooth_data: self.f = fs.smooth_data(self.f, sparam) N1 = self.f.shape[1] # Align Data self.warp_data = fs.fdawarp(self.f, self.time) self.warp_data.srsf_align(parallel=parallel) # Calculate PCA if pca_method == 'combined': out_pca = fpca.fdajpca(self.warp_data) elif pca_method == 'vert': out_pca = fpca.fdavpca(self.warp_data) elif pca_method == 'horiz': out_pca = fpca.fdahpca(self.warp_data) else: raise Exception('Invalid fPCA Method') out_pca.calc_fpca(no) # OLS using PCA basis lam = 0 R = 0 Phi = np.ones((N1, no + 1)) Phi[:, 1:(no + 1)] = out_pca.coef # Find alpha and beta using l_bfgs b0 = np.zeros(self.n_classes * (no + 1)) out = fmin_l_bfgs_b(rg.mlogit_loss, b0, fprime=rg.mlogit_gradient, args=(Phi, self.Y), pgtol=1e-10, maxiter=200, maxfun=250, factr=1e-30) b = out[0] B0 = b.reshape(no + 1, self.n_classes) alpha = B0[0, :] # compute the Loss LL = rg.mlogit_loss(b, Phi, self.y) b = B0[1:no + 1, :] self.alpha = alpha self.b = b self.pca = out_pca self.LL = LL self.pca_method = pca_method return