def test_corr_nearest_factor_sparse(self, dm): # Test that result is the same if the input is dense or sparse d = 200 # Generate a test matrix of factors X = np.zeros((d, dm), dtype=np.float64) x = np.linspace(0, 2 * np.pi, d) rs = np.random.RandomState(10) for j in range(dm): X[:, j] = np.sin(x * (j + 1)) + rs.randn(d) # Get the correlation matrix _project_correlation_factors(X) X *= 0.7 mat = np.dot(X, X.T) np.fill_diagonal(mat, 1) # Threshold it mat.flat[np.abs(mat.flat) < 0.35] = 0.0 smat = sparse.csr_matrix(mat) dense_rslt = corr_nearest_factor(mat, dm, maxiter=10000) sparse_rslt = corr_nearest_factor(smat, dm, maxiter=10000) mat_dense = dense_rslt.corr.to_matrix() mat_sparse = sparse_rslt.corr.to_matrix() assert dense_rslt.Converged is sparse_rslt.Converged assert dense_rslt.Converged is True assert_allclose(mat_dense, mat_sparse, rtol=.25, atol=1e-3)
def test_corr_nearest_factor_sparse(self): d = 100 for dm in 1,2: # Generate a test matrix of factors X = np.zeros((d,dm), dtype=np.float64) x = np.linspace(0, 2*np.pi, d) for j in range(dm): X[:,j] = np.sin(x*(j+1)) # Get the correlation matrix _project_correlation_factors(X) X *= 0.7 mat = np.dot(X, X.T) np.fill_diagonal(mat, 1) # Threshold it mat *= (np.abs(mat) >= 0.4) smat = sparse.csr_matrix(mat) fac_dense = corr_nearest_factor(smat, dm).corr mat_dense = fac_dense.to_matrix() fac_sparse = corr_nearest_factor(smat, dm).corr mat_sparse = fac_sparse.to_matrix() assert_allclose(mat_dense, mat_sparse, rtol=0.25, atol=1e-3)
def test_corr_nearest_factor_sparse(self, dm): # Test that result is the same if the input is dense or sparse d = 100 # Generate a test matrix of factors X = np.zeros((d, dm), dtype=np.float64) x = np.linspace(0, 2 * np.pi, d) np.random.seed(10) for j in range(dm): X[:, j] = np.sin(x * (j + 1)) + 1e-10 * np.random.randn(d) # Get the correlation matrix _project_correlation_factors(X) X *= 0.7 mat = np.dot(X, X.T) np.fill_diagonal(mat, 1) # Threshold it mat *= (np.abs(mat) >= 0.35) smat = sparse.csr_matrix(mat) try: rslt = corr_nearest_factor(mat, dm, maxiter=10000) assert rslt.Converged is True mat_dense = rslt.corr.to_matrix() rslt = corr_nearest_factor(smat, dm, maxiter=10000) assert rslt.Converged is True mat_sparse = rslt.corr.to_matrix() assert_allclose(mat_dense, mat_sparse, rtol=.25, atol=1e-3) except AssertionError as err: if PLATFORM_WIN32: pytest.xfail('Known to randomly fail on Win32') raise err
def test_corr_nearest_factor_sparse(self): d = 100 for dm in 1, 2: # Generate a test matrix of factors X = np.zeros((d, dm), dtype=np.float64) x = np.linspace(0, 2 * np.pi, d) np.random.seed(10) for j in range(dm): X[:, j] = np.sin(x * (j + 1)) + 1e-10 * np.random.randn(d) # Get the correlation matrix _project_correlation_factors(X) X *= 0.7 mat = np.dot(X, X.T) np.fill_diagonal(mat, 1) # Threshold it mat *= (np.abs(mat) >= 0.4) smat = sparse.csr_matrix(mat) rslt = corr_nearest_factor(smat, dm) assert_equal(rslt.Converged, True) mat_dense = rslt.corr.to_matrix() rslt = corr_nearest_factor(smat, dm) assert_equal(rslt.Converged, True) mat_sparse = rslt.corr.to_matrix() assert_allclose(mat_dense, mat_sparse, rtol=0.25, atol=1e-3)
def test_corr_nearest_factor_sparse(self): d = 100 for dm in 1,2: # Generate a test matrix of factors X = np.zeros((d,dm), dtype=np.float64) x = np.linspace(0, 2*np.pi, d) np.random.seed(10) for j in range(dm): X[:,j] = np.sin(x*(j+1)) + 1e-10 * np.random.randn(d) # Get the correlation matrix _project_correlation_factors(X) X *= 0.7 mat = np.dot(X, X.T) np.fill_diagonal(mat, 1) # Threshold it mat *= (np.abs(mat) >= 0.4) smat = sparse.csr_matrix(mat) rslt = corr_nearest_factor(smat, dm) assert_equal(rslt.Converged, True) mat_dense = rslt.corr.to_matrix() rslt = corr_nearest_factor(smat, dm) assert_equal(rslt.Converged, True) mat_sparse = rslt.corr.to_matrix() assert_allclose(mat_dense, mat_sparse, rtol=0.25, atol=1e-3)
def test_corr_nearest_factor(self, dm): objvals = [ np.array([6241.8, 6241.8, 579.4, 264.6, 264.3]), np.array([2104.9, 2104.9, 710.5, 266.3, 286.1]) ] d = 100 # Construct a test matrix with exact factor structure X = np.zeros((d, dm), dtype=np.float64) x = np.linspace(0, 2 * np.pi, d) np.random.seed(10) for j in range(dm): X[:, j] = np.sin(x * (j + 1)) + 1e-10 * np.random.randn(d) _project_correlation_factors(X) assert np.isfinite(X).all() X *= 0.7 mat = np.dot(X, X.T) np.fill_diagonal(mat, 1.) # Try to recover the structure rslt = corr_nearest_factor(mat, dm, maxiter=10000) err_msg = 'rank=%d, niter=%d' % (dm, len(rslt.objective_values)) assert_allclose(rslt.objective_values[:5], objvals[dm - 1], rtol=0.5, err_msg=err_msg) assert rslt.Converged mat1 = rslt.corr.to_matrix() assert_allclose(mat, mat1, rtol=0.25, atol=1e-3, err_msg=err_msg)
def test_corr_nearest_factor(self, dm): objvals = [np.array([6241.8, 6241.8, 579.4, 264.6, 264.3]), np.array([2104.9, 2104.9, 710.5, 266.3, 286.1])] d = 100 # Construct a test matrix with exact factor structure X = np.zeros((d, dm), dtype=np.float64) x = np.linspace(0, 2 * np.pi, d) np.random.seed(10) for j in range(dm): X[:, j] = np.sin(x * (j + 1)) + 1e-10 * np.random.randn(d) _project_correlation_factors(X) assert np.isfinite(X).all() X *= 0.7 mat = np.dot(X, X.T) np.fill_diagonal(mat, 1.) # Try to recover the structure rslt = corr_nearest_factor(mat, dm, maxiter=10000) err_msg = 'rank=%d, niter=%d' % (dm, len(rslt.objective_values)) assert_allclose(rslt.objective_values[:5], objvals[dm - 1], rtol=0.5, err_msg=err_msg) assert rslt.Converged mat1 = rslt.corr.to_matrix() assert_allclose(mat, mat1, rtol=0.25, atol=1e-3, err_msg=err_msg)
def test_corr_nearest_factor_sparse(self, dm): # Test that result is the same if the input is dense or sparse d = 100 # Generate a test matrix of factors X = np.zeros((d, dm), dtype=np.float64) x = np.linspace(0, 2 * np.pi, d) rs = np.random.RandomState(10) for j in range(dm): X[:, j] = np.sin(x * (j + 1)) + 1e-10 * rs.randn(d) # Get the correlation matrix _project_correlation_factors(X) X *= 0.7 mat = np.dot(X, X.T) np.fill_diagonal(mat, 1) # Threshold it mat.flat[np.abs(mat.flat) < 0.35] = 0.0 # Replace line below which left signed 0 # mat *= (np.abs(mat) >= 0.35) smat = sparse.csr_matrix(mat) try: dense_rslt = corr_nearest_factor(mat, dm, maxiter=10000) sparse_rslt = corr_nearest_factor(smat, dm, maxiter=10000) mat_dense = dense_rslt.corr.to_matrix() mat_sparse = sparse_rslt.corr.to_matrix() assert dense_rslt.Converged is True assert sparse_rslt.Converged is True assert_allclose(mat_dense, mat_sparse, rtol=.25, atol=1e-3) except AssertionError as err: if PLATFORM_WIN32: pytest.xfail('Known to randomly fail on Win32') # Some debugging information for CI runs that randomly fail print(dense_rslt.objective_values) print(sparse_rslt.objective_values) locs = np.where(~np.isclose(mat_dense, mat_sparse, rtol=.25, atol=1e-3)) print(mat_sparse[locs]) print(mat_dense[locs]) raise err
def test_corr_nearest_factor(self): d = 100 for dm in 1,2: # Construct a test matrix with exact factor structure X = np.zeros((d,dm), dtype=np.float64) x = np.linspace(0, 2*np.pi, d) for j in range(dm): X[:,j] = np.sin(x*(j+1)) _project_correlation_factors(X) X *= 0.7 mat = np.dot(X, X.T) np.fill_diagonal(mat, 1.) # Try to recover the structure rslt = corr_nearest_factor(mat, dm) assert_equal(rslt.Converged, True) mat1 = rslt.corr.to_matrix() assert_allclose(mat, mat1, rtol=0.25, atol=1e-3)
def test_corr_nearest_factor(self): d = 100 for dm in 1,2: # Construct a test matrix with exact factor structure X = np.zeros((d,dm), dtype=np.float64) x = np.linspace(0, 2*np.pi, d) for j in range(dm): X[:,j] = np.sin(x*(j+1)) _project_correlation_factors(X) X *= 0.7 mat = np.dot(X, X.T) np.fill_diagonal(mat, 1.) # Try to recover the structure rslt = corr_nearest_factor(mat, dm) C = rslt.corr mat1 = C.to_matrix() assert(np.abs(mat - mat1).max() < 1e-3)