def test_make_augmented_matrix(): np.random.seed(1) n = 500 x = np.random.uniform(-1, 1, (n, 3)) s = np.dot(x.T, x) y = np.array(list(range(n))) w = np.random.uniform(0, 1, n) nobs, n_columns = x.shape # matrix_sqrt removes redundant rows, # if alpha is zero, then no augmentation is needed alpha = 0 aug_y, aug_x, aug_w = make_augmented_matrix(y, x, alpha * s, w) expected_aug_x = x assert_allclose(aug_x, expected_aug_x) expected_aug_y = y expected_aug_y[:nobs] = y assert_allclose(aug_y, expected_aug_y) expected_aug_w = w assert_allclose(aug_w, expected_aug_w) alpha = 1 aug_y, aug_x, aug_w = make_augmented_matrix(y, x, s, w) rs = matrix_sqrt(alpha * s) # alternative version to matrix_sqrt using cholesky is not available # rs = sp.linalg.cholesky(alpha * s) assert_allclose(np.dot(rs.T, rs), alpha * s) expected_aug_x = np.vstack([x, rs]) assert_allclose(aug_x, expected_aug_x) expected_aug_y = np.zeros(shape=(nobs + n_columns, )) expected_aug_y[:nobs] = y assert_allclose(aug_y, expected_aug_y) expected_aug_w = np.concatenate((w, [1] * n_columns), axis=0) assert_allclose(aug_w, expected_aug_w)
def test_make_augmented_matrix(): np.random.seed(1) n = 500 x = np.random.uniform(-1, 1, (n, 3)) s = np.dot(x.T, x) y = np.array(list(range(n))) w = np.random.uniform(0, 1, n) nobs, n_columns = x.shape # matrix_sqrt removes redundant rows, # if alpha is zero, then no augmentation is needed alpha = 0 aug_y, aug_x, aug_w = make_augmented_matrix(y, x, alpha * s, w) expected_aug_x = x assert_allclose(aug_x, expected_aug_x) expected_aug_y = y expected_aug_y[:nobs] = y assert_allclose(aug_y, expected_aug_y) expected_aug_w = w assert_allclose(aug_w, expected_aug_w) alpha = 1 aug_y, aug_x, aug_w = make_augmented_matrix(y, x, s, w) rs = matrix_sqrt(alpha * s) # alternative version to matrix_sqrt using cholesky is not available # rs = sp.linalg.cholesky(alpha * s) assert_allclose(np.dot(rs.T, rs), alpha * s) expected_aug_x = np.vstack([x, rs]) assert_allclose(aug_x, expected_aug_x) expected_aug_y = np.zeros(shape=(nobs + n_columns,)) expected_aug_y[:nobs] = y assert_allclose(aug_y, expected_aug_y) expected_aug_w = np.concatenate((w, [1] * n_columns), axis=0) assert_allclose(aug_w, expected_aug_w)
def test_get_sqrt(): n = 1000 np.random.seed(1) x = np.random.normal(0, 1, (n, 3)) x2 = np.dot(x.T, x) sqrt_x2 = matrix_sqrt(x2) x2_reconstruction = np.dot(sqrt_x2.T, sqrt_x2) assert_allclose(x2_reconstruction, x2)
def test_get_sqrt(): n = 1000 np.random.seed(1) x = np.random.normal(0, 1, (n, 3)) x2 = np.dot(x.T, x) sqrt_x2 = matrix_sqrt(x2) x2_reconstruction = np.dot(sqrt_x2.T, sqrt_x2) assert_allclose(x2_reconstruction, x2)
def _init(cls): # TODO: CyclicCubicSplines raises when using pandas cc_h = CyclicCubicSplines(np.asarray(data_mcycle['times']), df=[6]) constraints = np.atleast_2d(cc_h.basis.mean(0)) transf = transf_constraints(constraints) exog = cc_h.basis.dot(transf) penalty_matrix = transf.T.dot(cc_h.penalty_matrices[0]).dot(transf) restriction = matrix_sqrt(penalty_matrix) return exog, penalty_matrix, restriction
def _init(cls): # TODO: CyclicCubicSplines raises when using pandas cc_h = CyclicCubicSplines(np.asarray(data_mcycle['times']), df=[6]) constraints = np.atleast_2d(cc_h.basis.mean(0)) transf = transf_constraints(constraints) exog = cc_h.basis.dot(transf) penalty_matrix = transf.T.dot(cc_h.penalty_matrices[0]).dot(transf) restriction = matrix_sqrt(penalty_matrix) return exog, penalty_matrix, restriction
def make_augmented_matrix(endog, exog, penalty_matrix, weights): """augment endog, exog and weights with stochastic restriction matrix Parameters ---------- endog : ndarray response or endogenous variable exog : ndarray design matrix, matrix of exogenous or explanatory variables penalty_matrix : ndarray, 2-Dim square penality matrix for quadratic penalization weights : ndarray weights for WLS Returns ------- endog_aug : ndarray augmented response variable exog_aug : ndarray augmented design matrix weights_aug : ndarray augmented weights for WLS """ y, x, s, = endog, exog, penalty_matrix nobs = x.shape[0] # TODO: needs full because of broadcasting with weights # check what weights should be doing rs = matrix_sqrt(s) x1 = np.vstack([x, rs]) # augmented x n_samp1es_x1 = x1.shape[0] y1 = np.array([0.] * n_samp1es_x1) # augmented y y1[:nobs] = y id1 = np.array([1.] * rs.shape[0]) w1 = np.concatenate([weights, id1]) return y1, x1, w1
def make_augmented_matrix(endog, exog, penalty_matrix, weights): """augment endog, exog and weights with stochastic restriction matrix Parameters ---------- endog : ndarray response or endogenous variable exog : ndarray design matrix, matrix of exogenous or explanatory variables penalty_matrix : ndarray, 2-Dim square penality matrix for quadratic penalization weights : ndarray weights for WLS Returns ------- endog_aug : ndarray augmented response variable exog_aug : ndarray augmented design matrix weights_aug : ndarray augmented weights for WLS """ y, x, s, = endog, exog, penalty_matrix nobs = x.shape[0] # TODO: needs full because of broadcasting with weights # check what weights should be doing rs = matrix_sqrt(s) x1 = np.vstack([x, rs]) # augmented x n_samp1es_x1 = x1.shape[0] y1 = np.array([0.] * n_samp1es_x1) # augmented y y1[:nobs] = y id1 = np.array([1.] * rs.shape[0]) w1 = np.concatenate([weights, id1]) return y1, x1, w1