@@ -1125,6 +1125,75 @@ class TestMvNormalTau(BaseTestDistributionRandom):
11251125 checks_to_run = ["check_pymc_params_match_rv_op" ]
11261126
11271127
1128+ class TestMvNormalMisc :
1129+ def test_with_chol_rv (self ):
1130+ with pm .Model () as model :
1131+ mu = pm .Normal ("mu" , 0.0 , 1.0 , size = 3 )
1132+ sd_dist = pm .Exponential .dist (1.0 , size = 3 )
1133+ # pylint: disable=unpacking-non-sequence
1134+ chol , _ , _ = pm .LKJCholeskyCov (
1135+ "chol_cov" , n = 3 , eta = 2 , sd_dist = sd_dist , compute_corr = True
1136+ )
1137+ # pylint: enable=unpacking-non-sequence
1138+ mv = pm .MvNormal ("mv" , mu , chol = chol , size = 4 )
1139+ prior = pm .sample_prior_predictive (samples = 10 , return_inferencedata = False )
1140+
1141+ assert prior ["mv" ].shape == (10 , 4 , 3 )
1142+
1143+ def test_with_cov_rv (
1144+ self ,
1145+ ):
1146+ with pm .Model () as model :
1147+ mu = pm .Normal ("mu" , 0.0 , 1.0 , shape = 3 )
1148+ sd_dist = pm .Exponential .dist (1.0 , shape = 3 )
1149+ # pylint: disable=unpacking-non-sequence
1150+ chol , corr , stds = pm .LKJCholeskyCov (
1151+ "chol_cov" , n = 3 , eta = 2 , sd_dist = sd_dist , compute_corr = True
1152+ )
1153+ # pylint: enable=unpacking-non-sequence
1154+ mv = pm .MvNormal ("mv" , mu , cov = pm .math .dot (chol , chol .T ), size = 4 )
1155+ prior = pm .sample_prior_predictive (samples = 10 , return_inferencedata = False )
1156+
1157+ assert prior ["mv" ].shape == (10 , 4 , 3 )
1158+
1159+ def test_issue_3758 (self ):
1160+ np .random .seed (42 )
1161+ ndim = 50
1162+ with pm .Model () as model :
1163+ a = pm .Normal ("a" , sigma = 100 , shape = ndim )
1164+ b = pm .Normal ("b" , mu = a , sigma = 1 , shape = ndim )
1165+ c = pm .MvNormal ("c" , mu = a , chol = np .linalg .cholesky (np .eye (ndim )), shape = ndim )
1166+ d = pm .MvNormal ("d" , mu = a , cov = np .eye (ndim ), shape = ndim )
1167+ samples = pm .sample_prior_predictive (1000 , return_inferencedata = False )
1168+
1169+ for var in "abcd" :
1170+ assert not np .isnan (np .std (samples [var ]))
1171+
1172+ for var in "bcd" :
1173+ std = np .std (samples [var ] - samples ["a" ])
1174+ npt .assert_allclose (std , 1 , rtol = 1e-2 )
1175+
1176+ def test_issue_3829 (self ):
1177+ with pm .Model () as model :
1178+ x = pm .MvNormal ("x" , mu = np .zeros (5 ), cov = np .eye (5 ), shape = (2 , 5 ))
1179+ trace_pp = pm .sample_prior_predictive (50 , return_inferencedata = False )
1180+
1181+ assert np .shape (trace_pp ["x" ][0 ]) == (2 , 5 )
1182+
1183+ def test_issue_3706 (self ):
1184+ N = 10
1185+ Sigma = np .eye (2 )
1186+
1187+ with pm .Model () as model :
1188+ X = pm .MvNormal ("X" , mu = np .zeros (2 ), cov = Sigma , shape = (N , 2 ))
1189+ betas = pm .Normal ("betas" , 0 , 1 , shape = 2 )
1190+ y = pm .Deterministic ("y" , pm .math .dot (X , betas ))
1191+
1192+ prior_pred = pm .sample_prior_predictive (1 , return_inferencedata = False )
1193+
1194+ assert prior_pred ["X" ].shape == (1 , N , 2 )
1195+
1196+
11281197class TestMvStudentTCov (BaseTestDistributionRandom ):
11291198 def mvstudentt_rng_fn (self , size , nu , mu , cov , rng ):
11301199 chi2_samples = rng .chisquare (nu , size = size )
@@ -2366,94 +2435,6 @@ def generate_shapes(include_params=False):
23662435 return data
23672436
23682437
2369- @pytest .mark .skip (reason = "This test is covered by Aesara" )
2370- class TestMvNormal (SeededTest ):
2371- @pytest .mark .parametrize (
2372- ["sample_shape" , "dist_shape" , "mu_shape" , "param" ],
2373- generate_shapes (include_params = True ),
2374- ids = str ,
2375- )
2376- def test_with_np_arrays (self , sample_shape , dist_shape , mu_shape , param ):
2377- dist = pm .MvNormal .dist (mu = np .ones (mu_shape ), ** {param : np .eye (3 )}, shape = dist_shape )
2378- output_shape = to_tuple (sample_shape ) + dist_shape
2379- assert dist .random (size = sample_shape ).shape == output_shape
2380-
2381- @pytest .mark .parametrize (
2382- ["sample_shape" , "dist_shape" , "mu_shape" ],
2383- generate_shapes (include_params = False ),
2384- ids = str ,
2385- )
2386- def test_with_chol_rv (self , sample_shape , dist_shape , mu_shape ):
2387- with pm .Model () as model :
2388- mu = pm .Normal ("mu" , 0.0 , 1.0 , shape = mu_shape )
2389- sd_dist = pm .Exponential .dist (1.0 , shape = 3 )
2390- # pylint: disable=unpacking-non-sequence
2391- chol , corr , stds = pm .LKJCholeskyCov (
2392- "chol_cov" , n = 3 , eta = 2 , sd_dist = sd_dist , compute_corr = True
2393- )
2394- # pylint: enable=unpacking-non-sequence
2395- mv = pm .MvNormal ("mv" , mu , chol = chol , shape = dist_shape )
2396- prior = pm .sample_prior_predictive (samples = sample_shape )
2397-
2398- assert prior ["mv" ].shape == to_tuple (sample_shape ) + dist_shape
2399-
2400- @pytest .mark .parametrize (
2401- ["sample_shape" , "dist_shape" , "mu_shape" ],
2402- generate_shapes (include_params = False ),
2403- ids = str ,
2404- )
2405- def test_with_cov_rv (self , sample_shape , dist_shape , mu_shape ):
2406- with pm .Model () as model :
2407- mu = pm .Normal ("mu" , 0.0 , 1.0 , shape = mu_shape )
2408- sd_dist = pm .Exponential .dist (1.0 , shape = 3 )
2409- # pylint: disable=unpacking-non-sequence
2410- chol , corr , stds = pm .LKJCholeskyCov (
2411- "chol_cov" , n = 3 , eta = 2 , sd_dist = sd_dist , compute_corr = True
2412- )
2413- # pylint: enable=unpacking-non-sequence
2414- mv = pm .MvNormal ("mv" , mu , cov = pm .math .dot (chol , chol .T ), shape = dist_shape )
2415- prior = pm .sample_prior_predictive (samples = sample_shape )
2416-
2417- assert prior ["mv" ].shape == to_tuple (sample_shape ) + dist_shape
2418-
2419- def test_issue_3758 (self ):
2420- np .random .seed (42 )
2421- ndim = 50
2422- with pm .Model () as model :
2423- a = pm .Normal ("a" , sigma = 100 , shape = ndim )
2424- b = pm .Normal ("b" , mu = a , sigma = 1 , shape = ndim )
2425- c = pm .MvNormal ("c" , mu = a , chol = np .linalg .cholesky (np .eye (ndim )), shape = ndim )
2426- d = pm .MvNormal ("d" , mu = a , cov = np .eye (ndim ), shape = ndim )
2427- samples = pm .sample_prior_predictive (1000 )
2428-
2429- for var in "abcd" :
2430- assert not np .isnan (np .std (samples [var ]))
2431-
2432- for var in "bcd" :
2433- std = np .std (samples [var ] - samples ["a" ])
2434- npt .assert_allclose (std , 1 , rtol = 1e-2 )
2435-
2436- def test_issue_3829 (self ):
2437- with pm .Model () as model :
2438- x = pm .MvNormal ("x" , mu = np .zeros (5 ), cov = np .eye (5 ), shape = (2 , 5 ))
2439- trace_pp = pm .sample_prior_predictive (50 )
2440-
2441- assert np .shape (trace_pp ["x" ][0 ]) == (2 , 5 )
2442-
2443- def test_issue_3706 (self ):
2444- N = 10
2445- Sigma = np .eye (2 )
2446-
2447- with pm .Model () as model :
2448- X = pm .MvNormal ("X" , mu = np .zeros (2 ), cov = Sigma , shape = (N , 2 ))
2449- betas = pm .Normal ("betas" , 0 , 1 , shape = 2 )
2450- y = pm .Deterministic ("y" , pm .math .dot (X , betas ))
2451-
2452- prior_pred = pm .sample_prior_predictive (1 )
2453-
2454- assert prior_pred ["X" ].shape == (1 , N , 2 )
2455-
2456-
24572438@pytest .mark .xfail (reason = "This distribution has not been refactored for v4" )
24582439def test_matrix_normal_random_with_random_variables ():
24592440 """
@@ -2492,7 +2473,6 @@ def test_with_np_arrays(self, sample_shape, dist_shape, mu_shape, param):
24922473 output_shape = to_tuple (sample_shape ) + dist_shape
24932474 assert dist .random (size = sample_shape ).shape == output_shape
24942475
2495- @pytest .mark .xfail
24962476 @pytest .mark .parametrize (
24972477 ["sample_shape" , "dist_shape" , "mu_shape" ],
24982478 generate_shapes (include_params = False ),
0 commit comments