@@ -705,29 +705,53 @@ def test_variable_type(self):
705
705
706
706
class TestSamplePPCW (SeededTest ):
707
707
def test_sample_posterior_predictive_w (self ):
708
- data0 = np .random .normal (0 , 1 , size = 500 )
708
+ data0 = np .random .normal (0 , 1 , size = 50 )
709
+ warning_msg = "The number of samples is too small to check convergence reliably"
709
710
710
711
with pm .Model () as model_0 :
711
712
mu = pm .Normal ("mu" , mu = 0 , sigma = 1 )
712
713
y = pm .Normal ("y" , mu = mu , sigma = 1 , observed = data0 )
713
- trace_0 = pm .sample ()
714
+ with pytest .warns (UserWarning , match = warning_msg ):
715
+ trace_0 = pm .sample (10 , tune = 0 , chains = 2 , return_inferencedata = False )
714
716
idata_0 = az .from_pymc3 (trace_0 )
715
717
716
718
with pm .Model () as model_1 :
717
719
mu = pm .Normal ("mu" , mu = 0 , sigma = 1 , shape = len (data0 ))
718
720
y = pm .Normal ("y" , mu = mu , sigma = 1 , observed = data0 )
719
- trace_1 = pm .sample ()
721
+ with pytest .warns (UserWarning , match = warning_msg ):
722
+ trace_1 = pm .sample (10 , tune = 0 , chains = 2 , return_inferencedata = False )
720
723
idata_1 = az .from_pymc3 (trace_1 )
721
724
725
+ with pm .Model () as model_2 :
726
+ # Model with no observed RVs.
727
+ mu = pm .Normal ("mu" , mu = 0 , sigma = 1 )
728
+ with pytest .warns (UserWarning , match = warning_msg ):
729
+ trace_2 = pm .sample (10 , tune = 0 , return_inferencedata = False )
730
+
722
731
traces = [trace_0 , trace_1 ]
723
732
idatas = [idata_0 , idata_1 ]
724
733
models = [model_0 , model_1 ]
725
734
726
735
ppc = pm .sample_posterior_predictive_w (traces , 100 , models )
727
- assert ppc ["y" ].shape == (100 , 500 )
736
+ assert ppc ["y" ].shape == (100 , 50 )
728
737
729
738
ppc = pm .sample_posterior_predictive_w (idatas , 100 , models )
730
- assert ppc ["y" ].shape == (100 , 500 )
739
+ assert ppc ["y" ].shape == (100 , 50 )
740
+
741
+ with model_0 :
742
+ ppc = pm .sample_posterior_predictive_w ([idata_0 .posterior ], None )
743
+ assert ppc ["y" ].shape == (20 , 50 )
744
+
745
+ with pytest .raises (ValueError , match = "The number of traces and weights should be the same" ):
746
+ pm .sample_posterior_predictive_w ([idata_0 .posterior ], 100 , models , weights = [0.5 , 0.5 ])
747
+
748
+ with pytest .raises (ValueError , match = "The number of models and weights should be the same" ):
749
+ pm .sample_posterior_predictive_w ([idata_0 .posterior ], 100 , models )
750
+
751
+ with pytest .raises (
752
+ ValueError , match = "The number of observed RVs should be the same for all models"
753
+ ):
754
+ pm .sample_posterior_predictive_w ([trace_0 , trace_2 ], 100 , [model_0 , model_2 ])
731
755
732
756
733
757
@pytest .mark .parametrize (
0 commit comments