@@ -87,6 +87,7 @@ class Holder(object):
87
87
0.7445177382760834 , 0.7766852947049376 , 0.8107916469252828 , 1.01
88
88
]).reshape (6 ,6 , order = 'F' )
89
89
90
+
90
91
def test_corr_psd ():
91
92
# test positive definite matrix is unchanged
92
93
x = np .array ([[1 , - 0.2 , - 0.9 ], [- 0.2 , 1 , - 0.2 ], [- 0.9 , - 0.2 , 1 ]])
@@ -122,7 +123,6 @@ def test_nearest(self):
122
123
#print evals[0] / 1e-7 - 1
123
124
assert_allclose (evals [0 ], 1e-7 , rtol = 1e-6 )
124
125
125
-
126
126
def test_clipped (self ):
127
127
x = self .x
128
128
res_r = self .res
@@ -190,37 +190,30 @@ def setup_class(cls):
190
190
cls .x = x
191
191
cls .res = cov1_r
192
192
193
- def test_corrpsd_threshold ():
193
+
194
+ @pytest .mark .parametrize ('threshold' , [0 , 1e-15 , 1e-10 , 1e-6 ])
195
+ def test_corrpsd_threshold (threshold ):
194
196
x = np .array ([[1 , - 0.9 , - 0.9 ], [- 0.9 , 1 , - 0.9 ], [- 0.9 , - 0.9 , 1 ]])
195
197
196
- #print np.linalg.eigvalsh(x)
197
- for threshold in [0 , 1e-15 , 1e-10 , 1e-6 ]:
198
+ y = corr_nearest (x , n_fact = 100 , threshold = threshold )
199
+ evals = np .linalg .eigvalsh (y )
200
+ assert_allclose (evals [0 ], threshold , rtol = 1e-6 , atol = 1e-15 )
198
201
199
- y = corr_nearest (x , n_fact = 100 , threshold = threshold )
200
- evals = np .linalg .eigvalsh (y )
201
- #print 'evals', evals, threshold
202
- assert_allclose (evals [0 ], threshold , rtol = 1e-6 , atol = 1e-15 )
202
+ y = corr_clipped (x , threshold = threshold )
203
+ evals = np .linalg .eigvalsh (y )
204
+ assert_allclose (evals [0 ], threshold , rtol = 0.25 , atol = 1e-15 )
203
205
204
- y = corr_clipped (x , threshold = threshold )
205
- evals = np .linalg .eigvalsh (y )
206
- #print 'evals', evals, threshold
207
- assert_allclose (evals [0 ], threshold , rtol = 0.25 , atol = 1e-15 )
206
+ y = cov_nearest (x , method = 'nearest' , n_fact = 100 , threshold = threshold )
207
+ evals = np .linalg .eigvalsh (y )
208
+ assert_allclose (evals [0 ], threshold , rtol = 1e-6 , atol = 1e-15 )
208
209
209
- y = cov_nearest (x , method = 'nearest' , n_fact = 100 , threshold = threshold )
210
- evals = np .linalg .eigvalsh (y )
211
- #print 'evals', evals, threshold
212
- #print evals[0] / threshold - 1
213
- assert_allclose (evals [0 ], threshold , rtol = 1e-6 , atol = 1e-15 )
210
+ y = cov_nearest (x , n_fact = 100 , threshold = threshold )
211
+ evals = np .linalg .eigvalsh (y )
212
+ assert_allclose (evals [0 ], threshold , rtol = 0.25 , atol = 1e-15 )
214
213
215
- y = cov_nearest (x , n_fact = 100 , threshold = threshold )
216
- evals = np .linalg .eigvalsh (y )
217
- #print 'evals', evals, threshold
218
- #print evals[0] / threshold - 1
219
- assert_allclose (evals [0 ], threshold , rtol = 0.25 , atol = 1e-15 )
220
214
221
215
class Test_Factor (object ):
222
216
223
-
224
217
def test_corr_nearest_factor_arrpack (self ):
225
218
226
219
# regression results for svds call
@@ -283,74 +276,67 @@ def test_corr_nearest_factor_arrpack(self):
283
276
assert_allclose (u , dsign * u2 , rtol = 1e-6 , atol = 1e-14 )
284
277
assert_allclose (s , s2 , rtol = 1e-6 )
285
278
286
-
287
- def test_corr_nearest_factor (self ):
279
+ @ pytest . mark . parametrize ( 'dm' , [ 1 , 2 ])
280
+ def test_corr_nearest_factor (self , dm ):
288
281
289
282
objvals = [np .array ([6241.8 , 6241.8 , 579.4 , 264.6 , 264.3 ]),
290
283
np .array ([2104.9 , 2104.9 , 710.5 , 266.3 , 286.1 ])]
291
284
292
285
d = 100
293
286
294
- for dm in 1 ,2 :
295
-
296
- # Construct a test matrix with exact factor structure
297
- X = np .zeros ((d ,dm ), dtype = np .float64 )
298
- x = np .linspace (0 , 2 * np .pi , d )
299
- np .random .seed (10 )
300
- for j in range (dm ):
301
- X [:,j ] = np .sin (x * (j + 1 )) + 1e-10 * np .random .randn (d )
302
-
303
- _project_correlation_factors (X )
304
- assert np .isfinite (X ).all ()
305
- X *= 0.7
306
- mat = np .dot (X , X .T )
307
- np .fill_diagonal (mat , 1. )
308
-
309
- # Try to recover the structure
310
- rslt = corr_nearest_factor (mat , dm )
311
- err_msg = 'rank=%d, niter=%d' % (dm , len (rslt .objective_values ))
312
- assert_allclose (rslt .objective_values [:5 ], objvals [dm - 1 ],
313
- rtol = 0.5 , err_msg = err_msg )
314
- assert_equal (rslt .Converged , True , err_msg = err_msg )
315
- mat1 = rslt .corr .to_matrix ()
316
- assert_allclose (mat , mat1 , rtol = 0.25 , atol = 1e-3 , err_msg = err_msg )
317
-
287
+ # Construct a test matrix with exact factor structure
288
+ X = np .zeros ((d , dm ), dtype = np .float64 )
289
+ x = np .linspace (0 , 2 * np .pi , d )
290
+ np .random .seed (10 )
291
+ for j in range (dm ):
292
+ X [:, j ] = np .sin (x * (j + 1 )) + 1e-10 * np .random .randn (d )
318
293
319
- # Test that we get the same result if the input is dense or sparse
320
- def test_corr_nearest_factor_sparse (self ):
294
+ _project_correlation_factors (X )
295
+ assert np .isfinite (X ).all ()
296
+ X *= 0.7
297
+ mat = np .dot (X , X .T )
298
+ np .fill_diagonal (mat , 1. )
321
299
300
+ # Try to recover the structure
301
+ rslt = corr_nearest_factor (mat , dm , maxiter = 10000 )
302
+ err_msg = 'rank=%d, niter=%d' % (dm , len (rslt .objective_values ))
303
+ assert_allclose (rslt .objective_values [:5 ], objvals [dm - 1 ],
304
+ rtol = 0.5 , err_msg = err_msg )
305
+ assert_equal (rslt .Converged , True , err_msg = err_msg )
306
+ mat1 = rslt .corr .to_matrix ()
307
+ assert_allclose (mat , mat1 , rtol = 0.25 , atol = 1e-3 , err_msg = err_msg )
308
+
309
+ @pytest .mark .parametrize ('dm' , [1 , 2 ])
310
+ def test_corr_nearest_factor_sparse (self , dm ):
311
+ # Test that result is the same if the input is dense or sparse
322
312
d = 100
323
313
324
- for dm in 1 ,2 :
325
-
326
- # Generate a test matrix of factors
327
- X = np .zeros ((d ,dm ), dtype = np .float64 )
328
- x = np .linspace (0 , 2 * np .pi , d )
329
- np .random .seed (10 )
330
- for j in range (dm ):
331
- X [:,j ] = np .sin (x * (j + 1 )) + 1e-10 * np .random .randn (d )
332
-
333
- # Get the correlation matrix
334
- _project_correlation_factors (X )
335
- X *= 0.7
336
- mat = np .dot (X , X .T )
337
- np .fill_diagonal (mat , 1 )
314
+ # Generate a test matrix of factors
315
+ X = np .zeros ((d , dm ), dtype = np .float64 )
316
+ x = np .linspace (0 , 2 * np .pi , d )
317
+ np .random .seed (10 )
318
+ for j in range (dm ):
319
+ X [:, j ] = np .sin (x * (j + 1 )) + 1e-10 * np .random .randn (d )
338
320
339
- # Threshold it
340
- mat *= (np .abs (mat ) >= 0.35 )
341
- smat = sparse .csr_matrix (mat )
321
+ # Get the correlation matrix
322
+ _project_correlation_factors (X )
323
+ X *= 0.7
324
+ mat = np .dot (X , X .T )
325
+ np .fill_diagonal (mat , 1 )
342
326
343
- rslt = corr_nearest_factor ( smat , dm )
344
- assert_equal ( rslt . Converged , True )
345
- mat_dense = rslt . corr . to_matrix ( )
327
+ # Threshold it
328
+ mat *= ( np . abs ( mat ) >= 0.35 )
329
+ smat = sparse . csr_matrix ( mat )
346
330
347
- rslt = corr_nearest_factor (smat , dm )
348
- assert_equal ( rslt .Converged , True )
349
- mat_sparse = rslt .corr .to_matrix ()
331
+ rslt = corr_nearest_factor (mat , dm , maxiter = 10000 )
332
+ assert rslt .Converged is True
333
+ mat_dense = rslt .corr .to_matrix ()
350
334
351
- assert_allclose (mat_dense , mat_sparse , rtol = 0.25 ,
352
- atol = 1e-3 )
335
+ rslt = corr_nearest_factor (smat , dm , maxiter = 10000 )
336
+ assert rslt .Converged is True
337
+ mat_sparse = rslt .corr .to_matrix ()
353
338
339
+ assert_allclose (mat_dense , mat_sparse , rtol = .25 , atol = 1e-3 )
354
340
355
341
# Test on a quadratic function.
356
342
def test_spg_optim (self ):
@@ -419,52 +405,49 @@ def test_solve(self):
419
405
sr2 = np .linalg .solve (mat , rhs )
420
406
assert_almost_equal (sr1 , sr2 )
421
407
422
- def test_cov_nearest_factor_homog (self ):
408
+ @pytest .mark .parametrize ('dm' , [1 , 2 ])
409
+ def test_cov_nearest_factor_homog (self , dm ):
423
410
424
411
d = 100
425
412
426
- for dm in 1 ,2 :
427
-
428
- # Construct a test matrix with exact factor structure
429
- X = np .zeros ((d ,dm ), dtype = np .float64 )
430
- x = np .linspace (0 , 2 * np .pi , d )
431
- for j in range (dm ):
432
- X [:,j ] = np .sin (x * (j + 1 ))
433
- mat = np .dot (X , X .T )
434
- np .fill_diagonal (mat , np .diag (mat ) + 3.1 )
435
-
436
- # Try to recover the structure
437
- rslt = cov_nearest_factor_homog (mat , dm )
438
- mat1 = rslt .to_matrix ()
413
+ # Construct a test matrix with exact factor structure
414
+ X = np .zeros ((d ,dm ), dtype = np .float64 )
415
+ x = np .linspace (0 , 2 * np .pi , d )
416
+ for j in range (dm ):
417
+ X [:,j ] = np .sin (x * (j + 1 ))
418
+ mat = np .dot (X , X .T )
419
+ np .fill_diagonal (mat , np .diag (mat ) + 3.1 )
439
420
440
- assert_allclose (mat , mat1 , rtol = 0.25 , atol = 1e-3 )
421
+ # Try to recover the structure
422
+ rslt = cov_nearest_factor_homog (mat , dm )
423
+ mat1 = rslt .to_matrix ()
441
424
425
+ assert_allclose (mat , mat1 , rtol = 0.25 , atol = 1e-3 )
442
426
443
- # Check that dense and sparse inputs give the same result
444
- def test_cov_nearest_factor_homog_sparse (self ):
427
+ @pytest .mark .parametrize ('dm' , [1 , 2 ])
428
+ def test_cov_nearest_factor_homog_sparse (self , dm ):
429
+ # Check that dense and sparse inputs give the same result
445
430
446
431
d = 100
447
432
448
- for dm in 1 ,2 :
449
-
450
- # Construct a test matrix with exact factor structure
451
- X = np .zeros ((d ,dm ), dtype = np .float64 )
452
- x = np .linspace (0 , 2 * np .pi , d )
453
- for j in range (dm ):
454
- X [:,j ] = np .sin (x * (j + 1 ))
455
- mat = np .dot (X , X .T )
456
- np .fill_diagonal (mat , np .diag (mat ) + 3.1 )
433
+ # Construct a test matrix with exact factor structure
434
+ X = np .zeros ((d ,dm ), dtype = np .float64 )
435
+ x = np .linspace (0 , 2 * np .pi , d )
436
+ for j in range (dm ):
437
+ X [:,j ] = np .sin (x * (j + 1 ))
438
+ mat = np .dot (X , X .T )
439
+ np .fill_diagonal (mat , np .diag (mat ) + 3.1 )
457
440
458
- # Fit to dense
459
- rslt = cov_nearest_factor_homog (mat , dm )
460
- mat1 = rslt .to_matrix ()
441
+ # Fit to dense
442
+ rslt = cov_nearest_factor_homog (mat , dm )
443
+ mat1 = rslt .to_matrix ()
461
444
462
- # Fit to sparse
463
- smat = sparse .csr_matrix (mat )
464
- rslt = cov_nearest_factor_homog (smat , dm )
465
- mat2 = rslt .to_matrix ()
445
+ # Fit to sparse
446
+ smat = sparse .csr_matrix (mat )
447
+ rslt = cov_nearest_factor_homog (smat , dm )
448
+ mat2 = rslt .to_matrix ()
466
449
467
- assert_allclose (mat1 , mat2 , rtol = 0.25 , atol = 1e-3 )
450
+ assert_allclose (mat1 , mat2 , rtol = 0.25 , atol = 1e-3 )
468
451
469
452
def test_corr_thresholded (self ):
470
453
0 commit comments