@@ -1930,21 +1930,16 @@ def test_logpdf_4x4(self):
1930
1930
1931
1931
class TestSpecialOrthoGroup :
1932
1932
def test_reproducibility (self ):
1933
- np .random .seed (514 )
1934
- x = special_ortho_group .rvs (3 )
1935
- expected = np .array ([[- 0.99394515 , - 0.04527879 , 0.10011432 ],
1936
- [0.04821555 , - 0.99846897 , 0.02711042 ],
1937
- [0.09873351 , 0.03177334 , 0.99460653 ]])
1938
- assert_array_almost_equal (x , expected )
1939
-
1940
- random_state = np .random .RandomState (seed = 514 )
1941
- x = special_ortho_group .rvs (3 , random_state = random_state )
1933
+ x = special_ortho_group .rvs (3 , random_state = np .random .default_rng (514 ))
1934
+ expected = np .array ([[- 0.93200988 , 0.01533561 , - 0.36210826 ],
1935
+ [0.35742128 , 0.20446501 , - 0.91128705 ],
1936
+ [0.06006333 , - 0.97875374 , - 0.19604469 ]])
1942
1937
assert_array_almost_equal (x , expected )
1943
1938
1944
1939
def test_invalid_dim (self ):
1945
1940
assert_raises (ValueError , special_ortho_group .rvs , None )
1946
1941
assert_raises (ValueError , special_ortho_group .rvs , (2 , 2 ))
1947
- assert_raises (ValueError , special_ortho_group .rvs , 1 )
1942
+ assert_raises (ValueError , special_ortho_group .rvs , - 1 )
1948
1943
assert_raises (ValueError , special_ortho_group .rvs , 2.5 )
1949
1944
1950
1945
def test_frozen_matrix (self ):
@@ -1979,8 +1974,9 @@ def test_haar(self):
1979
1974
dim = 5
1980
1975
samples = 1000 # Not too many, or the test takes too long
1981
1976
ks_prob = .05
1982
- np .random .seed (514 )
1983
- xs = special_ortho_group .rvs (dim , size = samples )
1977
+ xs = special_ortho_group .rvs (
1978
+ dim , size = samples , random_state = np .random .default_rng (513 )
1979
+ )
1984
1980
1985
1981
# Dot a few rows (0, 1, 2) with unit vectors (0, 2, 4, 3),
1986
1982
# effectively picking off entries in the matrices of xs.
@@ -1997,6 +1993,14 @@ def test_haar(self):
1997
1993
ks_tests = [ks_2samp (proj [p0 ], proj [p1 ])[1 ] for (p0 , p1 ) in pairs ]
1998
1994
assert_array_less ([ks_prob ]* len (pairs ), ks_tests )
1999
1995
1996
+ def test_one_by_one (self ):
1997
+ # Test that the distribution is a delta function at the identity matrix
1998
+ # when dim=1
1999
+ assert_allclose (special_ortho_group .rvs (1 , size = 1000 ), 1 , rtol = 1e-13 )
2000
+
2001
+ def test_zero_by_zero (self ):
2002
+ assert_equal (special_ortho_group .rvs (0 , size = 4 ).shape , (4 , 0 , 0 ))
2003
+
2000
2004
2001
2005
class TestOrthoGroup :
2002
2006
def test_reproducibility (self ):
@@ -2015,7 +2019,7 @@ def test_reproducibility(self):
2015
2019
def test_invalid_dim (self ):
2016
2020
assert_raises (ValueError , ortho_group .rvs , None )
2017
2021
assert_raises (ValueError , ortho_group .rvs , (2 , 2 ))
2018
- assert_raises (ValueError , ortho_group .rvs , 1 )
2022
+ assert_raises (ValueError , ortho_group .rvs , - 1 )
2019
2023
assert_raises (ValueError , ortho_group .rvs , 2.5 )
2020
2024
2021
2025
def test_frozen_matrix (self ):
@@ -2085,6 +2089,20 @@ def test_haar(self):
2085
2089
ks_tests = [ks_2samp (proj [p0 ], proj [p1 ])[1 ] for (p0 , p1 ) in pairs ]
2086
2090
assert_array_less ([ks_prob ]* len (pairs ), ks_tests )
2087
2091
2092
+ def test_one_by_one (self ):
2093
+ # Test that the 1x1 distribution gives ±1 with equal probability.
2094
+ dim = 1
2095
+ xs = ortho_group .rvs (dim , size = 5000 , random_state = np .random .default_rng (514 ))
2096
+ assert_allclose (np .abs (xs ), 1 , rtol = 1e-13 )
2097
+ k = np .sum (xs > 0 )
2098
+ n = len (xs )
2099
+ res = stats .binomtest (k , n )
2100
+ low , high = res .proportion_ci (confidence_level = 0.95 )
2101
+ assert low < 0.5 < high
2102
+
2103
+ def test_zero_by_zero (self ):
2104
+ assert_equal (special_ortho_group .rvs (0 , size = 4 ).shape , (4 , 0 , 0 ))
2105
+
2088
2106
@pytest .mark .slow
2089
2107
def test_pairwise_distances (self ):
2090
2108
# Test that the distribution of pairwise distances is close to correct.
@@ -2290,7 +2308,7 @@ def test_reproducibility(self):
2290
2308
def test_invalid_dim (self ):
2291
2309
assert_raises (ValueError , unitary_group .rvs , None )
2292
2310
assert_raises (ValueError , unitary_group .rvs , (2 , 2 ))
2293
- assert_raises (ValueError , unitary_group .rvs , 1 )
2311
+ assert_raises (ValueError , unitary_group .rvs , - 1 )
2294
2312
assert_raises (ValueError , unitary_group .rvs , 2.5 )
2295
2313
2296
2314
def test_frozen_matrix (self ):
@@ -2319,17 +2337,22 @@ def test_haar(self):
2319
2337
# the complex plane, are uncorrelated.
2320
2338
2321
2339
# Generate samples
2322
- dim = 5
2323
- samples = 1000 # Not too many, or the test takes too long
2324
- np .random .seed (514 ) # Note that the test is sensitive to seed too
2325
- xs = unitary_group .rvs (dim , size = samples )
2326
-
2327
- # The angles "x" of the eigenvalues should be uniformly distributed
2328
- # Overall this seems to be a necessary but weak test of the distribution.
2329
- eigs = np .vstack ([scipy .linalg .eigvals (x ) for x in xs ])
2330
- x = np .arctan2 (eigs .imag , eigs .real )
2331
- res = kstest (x .ravel (), uniform (- np .pi , 2 * np .pi ).cdf )
2332
- assert_ (res .pvalue > 0.05 )
2340
+ for dim in (1 , 5 ):
2341
+ samples = 1000 # Not too many, or the test takes too long
2342
+ # Note that the test is sensitive to seed too
2343
+ xs = unitary_group .rvs (
2344
+ dim , size = samples , random_state = np .random .default_rng (514 )
2345
+ )
2346
+
2347
+ # The angles "x" of the eigenvalues should be uniformly distributed
2348
+ # Overall this seems to be a necessary but weak test of the distribution.
2349
+ eigs = np .vstack ([scipy .linalg .eigvals (x ) for x in xs ])
2350
+ x = np .arctan2 (eigs .imag , eigs .real )
2351
+ res = kstest (x .ravel (), uniform (- np .pi , 2 * np .pi ).cdf )
2352
+ assert_ (res .pvalue > 0.05 )
2353
+
2354
+ def test_zero_by_zero (self ):
2355
+ assert_equal (unitary_group .rvs (0 , size = 4 ).shape , (4 , 0 , 0 ))
2333
2356
2334
2357
2335
2358
class TestMultivariateT :
0 commit comments