def test_stack_product(): X = np.random.standard_normal((5, 30)) Y = np.random.standard_normal((5, 30)) Z = np.random.standard_normal((5, 31)) U = np.random.standard_normal((6, 30)) stack = vstack([X, Y]) assert_raises(ValueError, vstack, [X, Z]) assert_raises(ValueError, hstack, [X, U]) np.testing.assert_allclose( stack.linear_map(np.arange(30))[:5], np.dot(X, np.arange(30))) np.testing.assert_allclose( stack.linear_map(np.arange(30))[5:], np.dot(Y, np.arange(30))) np.testing.assert_allclose( stack.affine_map(np.arange(30))[:5], np.dot(X, np.arange(30))) np.testing.assert_allclose( stack.affine_map(np.arange(30))[5:], np.dot(Y, np.arange(30))) np.testing.assert_allclose( stack.adjoint_map(np.arange(10)), np.dot(X.T, np.arange(5)) + np.dot(Y.T, np.arange(5, 10))) _hstack = hstack([X, Y, Z]) _hstack.linear_map(np.arange(91)) _hstack.affine_map(np.arange(91)) _hstack.adjoint_map(np.arange(5)) b = np.random.standard_normal(5) XA = rr.affine_transform(X, b) _product = product([XA, Y]) np.testing.assert_allclose( _product.linear_map(np.arange(60))[:5], np.dot(X, np.arange(30))) np.testing.assert_allclose( _product.linear_map(np.arange(60))[5:], np.dot(Y, np.arange(30, 60))) np.testing.assert_allclose( _product.affine_map(np.arange(60))[:5], np.dot(X, np.arange(30)) + b) np.testing.assert_allclose( _product.affine_map(np.arange(60))[5:], np.dot(Y, np.arange(30, 60))) np.testing.assert_allclose( _product.adjoint_map(np.arange(10))[:30], np.dot(X.T, np.arange(5))) np.testing.assert_allclose( _product.adjoint_map(np.arange(10))[30:], np.dot(Y.T, np.arange(5, 10))) scale_prod = scalar_multiply(_product, 2) np.testing.assert_allclose(scale_prod.linear_map(np.arange(60)), 2 * _product.linear_map(np.arange(60))) np.testing.assert_allclose(scale_prod.affine_map(np.arange(60)), 2 * _product.affine_map(np.arange(60))) np.testing.assert_allclose(scale_prod.adjoint_map(np.arange(60)), 2 * _product.adjoint_map(np.arange(60)))
def fit(self, solve_args={'min_its': 200}): Y, M, p, weights = self.Y, self.M, self.Y.shape[ 0], self.feature_weights Y0 = Y - Y.mean() coef = solve_sqrt_lasso(M.T, Y0, weights=weights, solve_args=solve_args)[0] self.active = active = coef != 0 self.inactive = inactive = ~active if active.sum(): jumps = self.merge_intervals(M.slices[active]) M_inactive = copy(M) M_inactive.update_slices(M.slices[inactive]) X = M.form_matrix(M.slices[active])[0] R = np.identity(p) - X.dot(np.linalg.pinv(X)) L_inactive = M_inactive.dot(rr.astransform(R)) L = lasso.sqrt_lasso(X, Y0, weights[active]) L.fit(solve_args={'min_its': 200}, lasso_solution=coef[active]) C = L.constraints.linear_part.dot(X.T) L_inactive_neg = ra.scalar_multiply(L_inactive, -1.) irrep = L_inactive.dot( X.dot(-np.diag(L.active_signs).dot(L.constraints.offset))) full_lin = ra.vstack([L_inactive, L_inactive_neg, C]) full_offset = np.hstack([ irrep + L._multiplier * weights[inactive], -irrep + L._multiplier * weights[inactive], L.constraints.offset ]) full_con = constraints(full_lin, full_offset, covariance=L._sigma_hat**2 * np.identity(p)) print full_con(Y - Y.mean()) fit = X.dot(coef[active]) + Y.mean() segments = np.array([jumps[:-1], jumps[1:]]).T Xr = M.form_matrix(segments)[0] relaxed_fit = Xr.dot(np.linalg.pinv(Xr).dot(Y)) + Y.mean() # S = L.summary('onesided') # pS = pd.DataFrame(S) # pS['interval'] = [M.slices[i] for i in np.nonzero(active)[0]] # pS = pS.reindex(columns=['interval', 'pval', 'lasso', 'onestep']) pS = None else: fit = relaxed_fit = Y.mean() * np.ones(self.p) pS = None segments = np.array([[0, self.p]]) return fit, relaxed_fit, pS, segments
def test_stack_product(): X = np.random.standard_normal((5, 30)) Y = np.random.standard_normal((5, 30)) Z = np.random.standard_normal((5, 31)) U = np.random.standard_normal((6, 30)) stack = vstack([X, Y]) assert_raises(ValueError, vstack, [X, Z]) assert_raises(ValueError, hstack, [X, U]) np.testing.assert_allclose(stack.linear_map(np.arange(30))[:5], np.dot(X, np.arange(30))) np.testing.assert_allclose(stack.linear_map(np.arange(30))[5:], np.dot(Y, np.arange(30))) np.testing.assert_allclose(stack.affine_map(np.arange(30))[:5], np.dot(X, np.arange(30))) np.testing.assert_allclose(stack.affine_map(np.arange(30))[5:], np.dot(Y, np.arange(30))) np.testing.assert_allclose(stack.adjoint_map(np.arange(10)), np.dot(X.T, np.arange(5)) + np.dot(Y.T, np.arange(5, 10))) _hstack = hstack([X, Y, Z]) _hstack.linear_map(np.arange(91)) _hstack.affine_map(np.arange(91)) _hstack.adjoint_map(np.arange(5)) b = np.random.standard_normal(5) XA = rr.affine_transform(X, b) _product = product([XA,Y]) np.testing.assert_allclose(_product.linear_map(np.arange(60))[:5], np.dot(X, np.arange(30))) np.testing.assert_allclose(_product.linear_map(np.arange(60))[5:], np.dot(Y, np.arange(30, 60))) np.testing.assert_allclose(_product.affine_map(np.arange(60))[:5], np.dot(X, np.arange(30)) + b) np.testing.assert_allclose(_product.affine_map(np.arange(60))[5:], np.dot(Y, np.arange(30, 60))) np.testing.assert_allclose(_product.adjoint_map(np.arange(10))[:30], np.dot(X.T, np.arange(5))) np.testing.assert_allclose(_product.adjoint_map(np.arange(10))[30:], np.dot(Y.T, np.arange(5, 10))) scale_prod = scalar_multiply(_product, 2) np.testing.assert_allclose(scale_prod.linear_map(np.arange(60)), 2 * _product.linear_map(np.arange(60))) np.testing.assert_allclose(scale_prod.affine_map(np.arange(60)), 2 * _product.affine_map(np.arange(60))) np.testing.assert_allclose(scale_prod.adjoint_map(np.arange(60)), 2 * _product.adjoint_map(np.arange(60)))