def sif(coord_mat, resy_design, h_opt): """ Smoothing individual function without preselected bandwidth. Args: coord_mat (matrix): common coordinate matrix (l*d) resy_design (matrix): residual response matrix (n*l*m, m=d in MFSDA) h_opt (matrix): optimal bandwidth (m*d) """ # Set up n, l, m = resy_design.shape d = coord_mat.shape[1] efit_eta = np.zeros((n, l, m)) w = np.zeros((1, d + 1)) w[0] = 1 t_mat0 = np.zeros((l, l, d + 1)) # L x L x d + 1 matrix t_mat0[:, :, 0] = np.ones((l, l)) for dii in range(d): t_mat0[:, :, dii + 1] = np.dot(np.atleast_2d(coord_mat[:, dii]).T, np.ones((1, l))) \ - np.dot(np.ones((l, 1)), np.atleast_2d(coord_mat[:, dii])) for mii in range(m): k_mat = np.ones((l, l)) for dii in range(d): h = h_opt[mii, dii] k_mat = k_mat * ep_kernel( t_mat0[:, :, dii + 1] / h, h) # Epanechnikov kernel smoothing function t_mat = np.transpose(t_mat0, [0, 2, 1]) # L x d+1 x L matrix for lii in range(l): kx = np.dot(np.atleast_2d(k_mat[:, lii]).T, np.ones( (1, d + 1))) * t_mat[:, :, lii] # L0 x d+1 matrix sm_weight = np.dot( np.dot( w, inv( np.dot(kx.T, t_mat[:, :, lii]) + np.eye(d + 1) * 0.0001)), kx.T) efit_eta[:, lii, mii] = np.squeeze( np.dot(resy_design[:, :, mii], sm_weight.T)) res_eta = resy_design - efit_eta # n x L x m matrix of difference between resy_design and fitted eta esig_eta = np.zeros((m, m, l)) for lii in range(l): esig_eta[:, :, lii] = np.dot( np.squeeze(efit_eta[:, lii, :]).T, np.squeeze( efit_eta[:, lii, :])) / n return efit_eta, res_eta, esig_eta
def bw(resy_design, coord_mat, vh, h_ii): """ optimal bandwidth selection in MVCM. Args: resy_design (matrix): residuals of imaging response data (n*l*m) coord_mat (matrix): common coordinate matrix (l*d) vh (matrix): candidate bandwidth matrix (nh*d) h_ii (scalar): candidate bandwidth index Outputs: gcv (vector): generalized cross validation index (m) """ # Set up l, d = coord_mat.shape m = resy_design.shape[2] efit_resy = resy_design * 0 gcv = np.zeros(m) w = np.zeros((1, d + 1)) w[0] = 1 t_mat0 = np.zeros((l, l, d + 1)) # L x L x d + 1 matrix t_mat0[:, :, 0] = np.ones((l, l)) for dii in range(d): t_mat0[:, :, dii + 1] = np.dot(np.atleast_2d(coord_mat[:, dii]).T, np.ones((1, l))) \ - np.dot(np.ones((l, 1)), np.atleast_2d(coord_mat[:, dii])) t_mat = np.transpose(t_mat0, [0, 2, 1]) # L x d+1 x L matrix k_mat = np.ones((l, l)) for dii in range(d): h = vh[h_ii, dii] k_mat = k_mat * ep_kernel(t_mat0[:, :, dii + 1] / h, h) # Epanechnikov kernel smoothing function for mii in range(m): for lii in range(l): kx = np.dot( np.atleast_2d(k_mat[:, lii]).T, np.ones( (1, d + 1))) * t_mat[:, :, lii] # L0 x d+1 matrix sm_weight = np.dot( np.dot( w, inv( np.dot(kx.T, t_mat[:, :, lii]) + np.eye(d + 1) * 0.0001)), kx.T) efit_resy[:, lii, mii] = np.squeeze( np.dot(resy_design[:, :, mii], sm_weight.T)) gcv[mii] = np.sum( (efit_resy[:, :, mii] - resy_design[:, :, mii])**2) return gcv
def lpks_wb1(coord_mat, x_design, y_design, flag): """ Local linear kernel smoothing for optimal bandwidth selection. Args: coord_mat (matrix): common coordinate matrix (l*d) x_design (matrix): design matrix (n*p) y_design (matrix): shape data (response matrix, n*l*m, m=d in MFSDA) flag (vector): indices for optimal bandwidth (m*1) """ # Set up n, p = x_design.shape l, d = coord_mat.shape m = y_design.shape[2] efit_beta = np.zeros((p, l, m)) efity_design = y_design * 0 nh = 50 # the number of candidate bandwidth w = np.zeros((1, d + 1)) w[0] = 1 t_mat0 = np.zeros((l, l, d + 1)) # L x L x d + 1 matrix t_mat0[:, :, 1] = np.ones((l, l)) for dii in range(d): t_mat0[:, :, dii + 1] = np.dot(np.atleast_2d(coord_mat[:, dii]).T, np.ones((1, l))) \ - np.dot(np.ones((l, 1)), np.atleast_2d(coord_mat[:, dii])) hat_mat = np.dot(inv(np.dot(x_design.T, x_design) + np.eye(p) * 0.0001), x_design.T) for mii in range(m): k_mat = np.ones((l, l)) for dii in range(d): coord_range = np.ptp(coord_mat[:, dii]) h_min = 0.01 # minimum bandwidth h_max = 0.5 * coord_range # maximum bandwidth vh = np.logspace(np.log10(h_min), np.log10(h_max), nh) # candidate bandwidth h = vh[int(flag[mii])] k_mat = k_mat * ep_kernel( t_mat0[:, :, dii + 1] / h, h) # Epanechnikov kernel smoothing function t_mat = np.transpose(t_mat0, [0, 2, 1]) # L x d+1 x L matrix for lii in range(l): kx = np.dot(np.atleast_2d(k_mat[:, lii]).T, np.ones( (1, d + 1))) * t_mat[:, :, lii] # L0 x d+1 matrix hat_y_design = np.dot(hat_mat, y_design[:, :, mii]) sm_weight = np.dot( np.dot( w, inv( np.dot(kx.T, t_mat[:, :, lii]) + np.eye(d + 1) * 0.0001)), kx.T) efit_beta[:, lii, mii] = np.squeeze(np.dot(hat_y_design, sm_weight.T)) efity_design[:, :, mii] = np.dot(x_design, efit_beta[:, :, mii]) return efit_beta, efity_design
def lpks(coord_mat, x_design, y_design): """ Local linear kernel smoothing for optimal bandwidth selection. Args: coord_mat (matrix): common coordinate matrix (l*d) x_design (matrix): design matrix (n*p) y_design (matrix): shape data (response matrix, n*l*m, m=d in MFSDA) """ # Set up n, p = x_design.shape l, d = coord_mat.shape m = y_design.shape[2] efit_beta = np.zeros((p, l, m)) efity_design = np.zeros((n, l, m)) nh = 50 # the number of candidate bandwidth gcv = np.zeros((nh, m)) # GCV performance function w = np.zeros((1, d + 1)) w[0] = 1 t_mat0 = np.zeros((l, l, d + 1)) # L x L x d + 1 matrix t_mat0[:, :, 0] = np.ones((l, l)) vh = np.zeros((nh, d)) h_opt = np.zeros((m, d)) for dii in range(d): t_mat0[:, :, dii + 1] = np.dot(np.atleast_2d(coord_mat[:, dii]).T, np.ones((1, l))) \ - np.dot(np.ones((l, 1)), np.atleast_2d(coord_mat[:, dii])) coord_range = np.ptp(coord_mat[:, dii]) h_min = 0.01 # minimum bandwidth h_max = 0.5 * coord_range # maximum bandwidth vh[:, dii] = np.logspace(np.log10(h_min), np.log10(h_max), nh) # candidate bandwidth t_mat = np.transpose(t_mat0, [0, 2, 1]) # L x d+1 x L matrix hat_mat = np.dot(inv(np.dot(x_design.T, x_design) + np.eye(p) * 0.00001), x_design.T) for nhii in range(nh): k_mat = np.ones((l, l)) for dii in range(d): h = vh[nhii, dii] k_mat = k_mat * ep_kernel(t_mat0[:, :, dii + 1] / h, h) # Epanechnikov kernel smoothing function for mii in range(m): hat_y_design = np.dot(hat_mat, y_design[:, :, mii]) for lii in range(l): kx = np.dot(np.atleast_2d(k_mat[:, lii]).T, np.ones((1, d + 1)))*t_mat[:, :, lii] # L0 x d+1 matrix sm_weight0 = np.dot(np.dot(w, inv(np.dot(kx.T, t_mat[:, :, lii]) + np.eye(d + 1) * 0.00001)), kx.T) efity_design[:, lii, mii] = np.squeeze(np.dot(x_design, np.dot(hat_y_design, sm_weight0.T))) gcv[nhii, mii] = np.mean((y_design[:, :, mii]-efity_design[:, :, mii])**2) flag = np.argmin(gcv, axis=0) # optimal bandwidth for mii in range(m): k_mat = np.ones((l, l)) hat_y_design = np.dot(hat_mat, y_design[:, :, mii]) for dii in range(d): h = vh[int(flag[mii]), dii] k_mat = k_mat * ep_kernel(t_mat0[:, :, dii + 1] / h, h) # Epanechnikov kernel smoothing function h_opt[dii, mii] = h for lii in range(l): kx = np.dot(np.atleast_2d(k_mat[:, lii]).T, np.ones((1, d + 1)))*t_mat[:, :, lii] # L0 x d+1 matrix sm_weight = np.dot(np.dot(w, inv(np.dot(kx.T, t_mat[:, :, lii])+np.eye(d+1)*0.00001)), kx.T) efit_beta[:, lii, mii] = np.squeeze(np.dot(hat_y_design, sm_weight.T)) efity_design[:, :, mii] = np.dot(x_design, efit_beta[:, :, mii]) return efit_beta, efity_design, h_opt
def lpks_pre_bw(coord_mat, x_design, y_design, h_opt): """ Local linear kernel smoothing for optimal bandwidth selection. Args: coord_mat (matrix): common coordinate matrix (l*d) x_design (matrix): design matrix (n*p) y_design (matrix): shape data (response matrix, n*l*m, m=d in MFSDA) h_opt (matrix): prefixed optimal bandwidth (m*d) """ # Set up n, p = x_design.shape l, d = coord_mat.shape m = y_design.shape[2] efit_beta = np.zeros((p, l, m)) efity_design = np.zeros((n, l, m)) nh = 50 # the number of candidate bandwidth w = np.zeros((1, d + 1)) w[0] = 1 t_mat0 = np.zeros((l, l, d + 1)) # L x L x d + 1 matrix t_mat0[:, :, 0] = np.ones((l, l)) vh = np.zeros((nh, d)) for dii in range(d): t_mat0[:, :, dii + 1] = np.dot(np.atleast_2d(coord_mat[:, dii]).T, np.ones((1, l))) \ - np.dot(np.ones((l, 1)), np.atleast_2d(coord_mat[:, dii])) coord_range = np.ptp(coord_mat[:, dii]) h_min = 0.01 # minimum bandwidth h_max = 0.5 * coord_range # maximum bandwidth vh[:, dii] = np.logspace(np.log10(h_min), np.log10(h_max), nh) # candidate bandwidth # h_opt[dii] = (4 / (n*(d + 2))) ** (1 / (d + 4)) * np.std(coord_mat[:, dii]) # Silverman's rule of thumb t_mat = np.transpose(t_mat0, [0, 2, 1]) # L x d+1 x L matrix hat_mat = np.dot(inv(np.dot(x_design.T, x_design) + np.eye(p) * 0.00001), x_design.T) for mii in range(m): k_mat = np.ones((l, l)) hat_y_design = np.dot(hat_mat, y_design[:, :, mii]) for dii in range(d): h = h_opt[mii, dii] k_mat = k_mat * ep_kernel( t_mat0[:, :, dii + 1] / h, h) # Epanechnikov kernel smoothing function h_opt[dii, mii] = h for lii in range(l): kx = np.dot(np.atleast_2d(k_mat[:, lii]).T, np.ones( (1, d + 1))) * t_mat[:, :, lii] # L0 x d+1 matrix sm_weight = np.dot( np.dot( w, inv( np.dot(kx.T, t_mat[:, :, lii]) + np.eye(d + 1) * 0.00001)), kx.T) efit_beta[:, lii, mii] = np.squeeze(np.dot(hat_y_design, sm_weight.T)) efity_design[:, :, mii] = np.dot(x_design, efit_beta[:, :, mii]) return efit_beta, efity_design
def lpks_wob(coord_mat, x_design, y_design): """ Local linear kernel smoothing for optimal bandwidth selection. Args: coord_mat (matrix): common coordinate matrix (l*d) x_design (matrix): design matrix (n*p) y_design (matrix): shape data (response matrix, n*l*m, m=d in MFSDA) """ # Set up n, p = x_design.shape l, d = coord_mat.shape m = y_design.shape[2] efity_design = y_design * 0 nh = 50 # the number of candidate bandwidth k = 5 # number of folders k_ind = np.floor(np.linspace(1, n, k + 1)) gcv = np.zeros((nh, m)) # GCV performance function w = np.zeros((1, d + 1)) w[0] = 1 t_mat0 = np.zeros((l, l, d + 1)) # L x L x d + 1 matrix t_mat0[:, :, 1] = np.ones((l, l)) for dii in range(d): t_mat0[:, :, dii + 1] = np.dot(np.atleast_2d(coord_mat[:, dii]).T, np.ones((1, l))) \ - np.dot(np.ones((l, 1)), np.atleast_2d(coord_mat[:, dii])) for nhii in range(nh): k_mat = np.ones((l, l)) for dii in range(d): coord_range = np.ptp(coord_mat[:, dii]) h_min = 0.01 # minimum bandwidth h_max = 0.5 * coord_range # maximum bandwidth vh = np.logspace(np.log10(h_min), np.log10(h_max), nh) # candidate bandwidth h = vh[nhii] k_mat = k_mat * ep_kernel( t_mat0[:, :, dii + 1] / h, h) # Epanechnikov kernel smoothing function t_mat = np.transpose(t_mat0, [0, 2, 1]) # L x d+1 x L matrix for mii in range(m): for lii in range(l): kx = np.dot( np.atleast_2d(k_mat[:, lii]).T, np.ones( (1, d + 1))) * t_mat[:, :, lii] # L0 x d+1 matrix indx = np.ones((1, n)) for kii in range(k): ind_beg = int(k_ind[kii] - 1) ind_end = int(k_ind[kii + 1]) indx[0, ind_beg:ind_end] = 0 x_design0 = x_design[np.nonzero(indx == 1)[0], :] hat_mat = np.dot( inv( np.dot(x_design0.T, x_design0) + np.eye(p) * 0.0001), x_design0.T) hat_y_design0 = np.dot( hat_mat, y_design[np.nonzero(indx == 1)[0], :, mii]) sm_weight = np.dot( np.dot( w, inv( np.dot(kx.T, t_mat[:, :, lii]) + np.eye(d + 1) * 0.0001)), kx.T) efit_beta = np.dot(hat_y_design0, sm_weight.T) efity_design[ind_beg:ind_end, lii, mii] = np.squeeze( np.dot(x_design[ind_beg:ind_end, :], efit_beta)) gcv[nhii, mii] = np.mean( (y_design[:, :, mii] - efity_design[:, :, mii])**2) flag = np.argmin(gcv, axis=0) # optimal bandwidth return flag