Skip to content

Commit

Permalink
Merge pull request #6868 from bashtage/test-factor-again
Browse files Browse the repository at this point in the history
TST: Refactor factor tests again
  • Loading branch information
bashtage committed Jul 9, 2020
2 parents 452f8dc + 233f044 commit 0538c0c
Showing 1 changed file with 13 additions and 27 deletions.
40 changes: 13 additions & 27 deletions statsmodels/stats/tests/test_corrpsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
Author: Josef Perktold
"""
from statsmodels.compat.platform import PLATFORM_WIN32

import warnings

import numpy as np
Expand Down Expand Up @@ -305,17 +303,18 @@ def test_corr_nearest_factor(self, dm):
mat1 = rslt.corr.to_matrix()
assert_allclose(mat, mat1, rtol=0.25, atol=1e-3, err_msg=err_msg)

@pytest.mark.slow
@pytest.mark.parametrize('dm', [1, 2])
def test_corr_nearest_factor_sparse(self, dm):
# Test that result is the same if the input is dense or sparse
d = 100
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)) + 1e-10 * rs.randn(d)
X[:, j] = np.sin(x * (j + 1)) + rs.randn(d)

# Get the correlation matrix
_project_correlation_factors(X)
Expand All @@ -325,31 +324,18 @@ def test_corr_nearest_factor_sparse(self, dm):

# 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
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)

# Test on a quadratic function.
def test_spg_optim(self, reset_randomstate):
Expand Down

0 comments on commit 0538c0c

Please sign in to comment.