Skip to content

WIP/BUG/REF: correlation_tools - fix value in clip_evals, add fudge term - #3716

Open
josef-pkt wants to merge 1 commit into
statsmodels:mainfrom
josef-pkt:bugref_corrpsd_clipevals
Open

josef-pkt wants to merge 1 commit into
statsmodels:mainfrom
josef-pkt:bugref_corrpsd_clipevals

Conversation

@josef-pkt

Copy link
Copy Markdown
Member

This was supposed to fix #3227 value ignore in clip_evals.

Include fix for non or slow convergence in tests\test_corrpsd.py:test_corrpsd_threshold (test run print convergence warnings. I tried up to 1000 iterations, but we only seem to approach the threshold in the eigenvalue for corr_nearest from below.

  • add extra convergence check directly on the trial corr matrix
  • add a fudge term 1e-16

Now, convergence is very fast in test_corrpsd_threshold with smallest eigenvalue possibly a bit above threshold.

However, unit test against R fail now. I'm not sure it's really testing the same things, e.g. R has ones on diagonal, i.e. corr_nearest, while the test cases uses cov_nearest with diagonal values slightly above one.
distance to original matrix is smaller in the new version than in the R reference numbers.
This looks more like a reason to adjust the unit tests, than the code.

About the fudge term (I just made it up):

  • it is only relevant for eigenvalues around zero. multiplicative fudge factor doesn't work if threshold is zero
  • idea is to adjust or clip eigenvalues to something slightly higher than the target threshold, so we cross the threshold instead of just approaching it slowly. This might introduce a small distortion in the converged result, but works well and fast in the test cases.

status: currently fails in comparison to R, I need to get the changes out of the way, and haven't looked and decided yet whether to adjust the unit tests or the code.

@josef-pkt

josef-pkt commented May 27, 2017 •

Copy link
Copy Markdown
Member Author

test failures for reference (with relevant line breaks added)


======================================================================
FAIL: statsmodels.stats.tests.test_corrpsd.TestCorrPSD1.test_cov_nearest
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/travis/miniconda2/envs/statsmodels-test/lib/python3.5/site-packages/nose/case.py", line 198, in runTest
    self.test(*self.arg)
  File "/home/travis/miniconda2/envs/statsmodels-test/lib/python3.5/site-packages/statsmodels-0.8.0-py3.5-linux-x86_64.egg/
statsmodels/stats/tests/test_corrpsd.py", line 144, in test_cov_nearest
    assert_allclose(d, res_r.normF, rtol=0.0015)
  File "/home/travis/miniconda2/envs/statsmodels-test/lib/python3.5/site-packages/numpy/testing/utils.py", line 1359, in assert_allclose
    verbose=verbose, header=header)
  File "/home/travis/miniconda2/envs/statsmodels-test/lib/python3.5/site-packages/numpy/testing/utils.py", line 713, in assert_array_compare
    raise AssertionError(msg)
AssertionError: 
Not equal to tolerance rtol=0.0015, atol=0

(mismatch 100.0%)
 x: array(0.07350470296792169)
 y: array(0.0743805226512533)

======================================================================
FAIL: statsmodels.stats.tests.test_corrpsd.TestCorrPSD1.test_nearest
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/travis/miniconda2/envs/statsmodels-test/lib/python3.5/site-packages/nose/case.py", line 198, in runTest
    self.test(*self.arg)
  File "/home/travis/miniconda2/envs/statsmodels-test/lib/python3.5/site-packages/statsmodels-0.8.0-py3.5-linux-x86_64.egg/
statsmodels/stats/tests/test_corrpsd.py", line 114, in test_nearest
    assert_allclose(d, res_r.normF, rtol=0.0015)
  File "/home/travis/miniconda2/envs/statsmodels-test/lib/python3.5/site-packages/numpy/testing/utils.py", line 1359, in assert_allclose
    verbose=verbose, header=header)
  File "/home/travis/miniconda2/envs/statsmodels-test/lib/python3.5/site-packages/numpy/testing/utils.py", line 713, in assert_array_compare
    raise AssertionError(msg)
AssertionError: 
Not equal to tolerance rtol=0.0015, atol=0

(mismatch 100.0%)
 x: array(0.07350470296792169)
 y: array(0.0743805226512533)

======================================================================
FAIL: statsmodels.stats.tests.test_corrpsd.TestCovPSD.test_cov_nearest
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/travis/miniconda2/envs/statsmodels-test/lib/python3.5/site-packages/nose/case.py", line 198, in runTest
    self.test(*self.arg)
  File "/home/travis/miniconda2/envs/statsmodels-test/lib/python3.5/site-packages/statsmodels-0.8.0-py3.5-linux-x86_64.egg/
statsmodels/stats/tests/test_corrpsd.py", line 165, in test_cov_nearest
    assert_almost_equal(y, res_r.mat, decimal=3)
  File "/home/travis/miniconda2/envs/statsmodels-test/lib/python3.5/site-packages/numpy/testing/utils.py", line 497, in assert_almost_equal
    return assert_array_almost_equal(actual, desired, decimal, err_msg)
  File "/home/travis/miniconda2/envs/statsmodels-test/lib/python3.5/site-packages/numpy/testing/utils.py", line 892, in assert_array_almost_equal
    precision=decimal)
  File "/home/travis/miniconda2/envs/statsmodels-test/lib/python3.5/site-packages/numpy/testing/utils.py", line 713, in assert_array_compare
    raise AssertionError(msg)
AssertionError: 
Arrays are not almost equal to 3 decimals

(mismatch 69.44444444444444%)
 x: array([[ 1.015,  0.484,  0.643,  0.486,  0.647,  0.816],
       [ 0.484,  1.019,  0.515,  0.244,  0.676,  0.736],
       [ 0.643,  0.515,  1.01 ,  0.598,  0.582,  0.744],...
 y: array([[ 1.01 ,  0.486,  0.643,  0.489,  0.645,  0.812],
       [ 0.486,  1.01 ,  0.514,  0.248,  0.674,  0.73 ],
       [ 0.643,  0.514,  1.01 ,  0.597,  0.582,  0.745],...

----------------------------------------------------------------------
Ran 5831 tests in 793.521s

FAILED (SKIP=54, failures=3)

x_new = x_psd.copy()
x_new[diag_idx, diag_idx] = 1
else:
raise

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this raise was likely left during debugging

@josef-pkt josef-pkt added this to the 0.14 milestone Feb 17, 2022
@josef-pkt josef-pkt mentioned this pull request Feb 17, 2022
16 of 20 tasks
@josef-pkt

Copy link
Copy Markdown
Member Author

There are still tests that fail randomly, every once in a while

self = <statsmodels.stats.tests.test_corrpsd.Test_Factor object at 0x11ac62890>
dm = 1

    @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

...

>       assert dense_rslt.Converged is sparse_rslt.Converged
E       AssertionError: assert True is False
E        +  where True = {'Converged': True, 'objective_values': [3712.8066033365567, 3712.8066033365567, 2075.359362157222, 2443.4377375064287...sage': 'Converged successfully', 'corr': <statsmodels.stats.correlation_tools.FactoredPSDMatrix object at 0x11a492920>}.Converged
E        +  and   False = {'Converged': False, 'objective_values': [3712.8066033365567, 3712.8066033365567, 1849.1167518228647, 3197.55135887747...': 'spg_optim did not converge', 'corr': <statsmodels.stats.correlation_tools.FactoredPSDMatrix object at 0x11aed34c0>}.Converged

@josef-pkt josef-pkt modified the milestones: 0.14, 0.15 Apr 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: clip_evals uses wrong indicator

1 participant