2929
3030import pymc as pm
3131
32- from pymc .distributions .discrete import _OrderedLogistic , _OrderedProbit
32+ from pymc .distributions .discrete import OrderedLogistic , OrderedProbit
3333from pymc .logprob .basic import icdf , logcdf , logp
3434from pymc .logprob .utils import ParameterValueError
3535from pymc .pytensorf import floatX
@@ -481,26 +481,6 @@ def test_orderedlogistic_dimensions(shape):
481481 assert np .allclose (ologp , expected )
482482
483483
484- def test_ordered_logistic_probs ():
485- with pm .Model () as m :
486- pm .OrderedLogistic ("ol_p" , cutpoints = np .array ([- 2 , 0 , 2 ]), eta = 0 )
487- pm .OrderedLogistic ("ol_no_p" , cutpoints = np .array ([- 2 , 0 , 2 ]), eta = 0 , compute_p = False )
488- assert len (m .deterministics ) == 1
489-
490- x = pm .OrderedLogistic .dist (cutpoints = np .array ([- 2 , 0 , 2 ]), eta = 0 )
491- assert isinstance (x , TensorVariable )
492-
493-
494- def test_ordered_probit_probs ():
495- with pm .Model () as m :
496- pm .OrderedProbit ("op_p" , cutpoints = np .array ([- 2 , 0 , 2 ]), eta = 0 , sigma = 1 )
497- pm .OrderedProbit ("op_no_p" , cutpoints = np .array ([- 2 , 0 , 2 ]), eta = 0 , sigma = 1 , compute_p = False )
498- assert len (m .deterministics ) == 1
499-
500- x = pm .OrderedProbit .dist (cutpoints = np .array ([- 2 , 0 , 2 ]), eta = 0 , sigma = 1 )
501- assert isinstance (x , TensorVariable )
502-
503-
504484class TestMoments :
505485 @pytest .mark .parametrize (
506486 "p, size, expected" ,
@@ -857,14 +837,12 @@ def test_implied_degenerate_shape(self):
857837 assert x .eval ().shape == (1 ,)
858838
859839
860- class TestOrderedLogistic (BaseTestDistributionRandom ):
861- pymc_dist = _OrderedLogistic
862- pymc_dist_params = {"eta" : 0 , "cutpoints" : np .array ([- 2 , 0 , 2 ])}
863- expected_rv_op_params = {"p" : np .array ([0.11920292 , 0.38079708 , 0.38079708 , 0.11920292 ])}
864- checks_to_run = [
865- "check_pymc_params_match_rv_op" ,
866- "check_rv_size" ,
867- ]
840+ class TestOrderedLogistic :
841+ def test_expected_categorical (self ):
842+ categorical = OrderedLogistic .dist (eta = 0 , cutpoints = np .array ([- 2 , 0 , 2 ]))
843+ p = categorical .owner .inputs [3 ].eval ()
844+ expected_p = np .array ([0.11920292 , 0.38079708 , 0.38079708 , 0.11920292 ])
845+ np .testing .assert_allclose (p , expected_p )
868846
869847 @pytest .mark .parametrize (
870848 "eta, cutpoints, expected" ,
@@ -881,22 +859,34 @@ def test_shape_inputs(self, eta, cutpoints, expected):
881859 """
882860 This test checks when providing different shapes for `eta` parameters.
883861 """
884- categorical = _OrderedLogistic .dist (
862+ categorical = OrderedLogistic .dist (
885863 eta = eta ,
886864 cutpoints = cutpoints ,
887865 )
888- p = categorical .owner .inputs [3 ]. eval ()
889- assert p . shape == expected
866+ p_shape = tuple ( categorical .owner .inputs [- 1 ]. shape . eval () )
867+ assert p_shape == expected
890868
869+ def test_compute_p (self ):
870+ with pm .Model () as m :
871+ pm .OrderedLogistic ("ol_p" , cutpoints = np .array ([- 2 , 0 , 2 ]), eta = 0 )
872+ pm .OrderedLogistic ("ol_no_p" , cutpoints = np .array ([- 2 , 0 , 2 ]), eta = 0 , compute_p = False )
873+ assert len (m .deterministics ) == 1
891874
892- class TestOrderedProbit (BaseTestDistributionRandom ):
893- pymc_dist = _OrderedProbit
894- pymc_dist_params = {"eta" : 0 , "cutpoints" : np .array ([- 2 , 0 , 2 ])}
895- expected_rv_op_params = {"p" : np .array ([0.02275013 , 0.47724987 , 0.47724987 , 0.02275013 ])}
896- checks_to_run = [
897- "check_pymc_params_match_rv_op" ,
898- "check_rv_size" ,
899- ]
875+ x = pm .OrderedLogistic .dist (cutpoints = np .array ([- 2 , 0 , 2 ]), eta = 0 )
876+ assert isinstance (x , TensorVariable )
877+
878+ # Test it works with auto-imputation
879+ with pm .Model () as m :
880+ pm .OrderedLogistic ("ol" , cutpoints = np .array ([- 2 , 0 , 2 ]), eta = 0 , observed = [0 , np .nan , 1 ])
881+ assert len (m .deterministics ) == 2 # One from the auto-imputation, the other from compute_p
882+
883+
884+ class TestOrderedProbit :
885+ def test_expected_categorical (self ):
886+ categorical = OrderedProbit .dist (eta = 0 , cutpoints = np .array ([- 2 , 0 , 2 ]))
887+ p = categorical .owner .inputs [3 ].eval ()
888+ expected_p = np .array ([0.02275013 , 0.47724987 , 0.47724987 , 0.02275013 ])
889+ np .testing .assert_allclose (p , expected_p )
900890
901891 @pytest .mark .parametrize (
902892 "eta, cutpoints, sigma, expected" ,
@@ -914,10 +904,28 @@ def test_shape_inputs(self, eta, cutpoints, sigma, expected):
914904 """
915905 This test checks when providing different shapes for `eta` and `sigma` parameters.
916906 """
917- categorical = _OrderedProbit .dist (
907+ categorical = OrderedProbit .dist (
918908 eta = eta ,
919909 cutpoints = cutpoints ,
920910 sigma = sigma ,
921911 )
922- p = categorical .owner .inputs [3 ].eval ()
923- assert p .shape == expected
912+ p_shape = tuple (categorical .owner .inputs [- 1 ].shape .eval ())
913+ assert p_shape == expected
914+
915+ def test_compute_p (self ):
916+ with pm .Model () as m :
917+ pm .OrderedProbit ("op_p" , cutpoints = np .array ([- 2 , 0 , 2 ]), eta = 0 , sigma = 1 )
918+ pm .OrderedProbit (
919+ "op_no_p" , cutpoints = np .array ([- 2 , 0 , 2 ]), eta = 0 , sigma = 1 , compute_p = False
920+ )
921+ assert len (m .deterministics ) == 1
922+
923+ x = pm .OrderedProbit .dist (cutpoints = np .array ([- 2 , 0 , 2 ]), eta = 0 , sigma = 1 )
924+ assert isinstance (x , TensorVariable )
925+
926+ # Test it works with auto-imputation
927+ with pm .Model () as m :
928+ pm .OrderedProbit (
929+ "op" , cutpoints = np .array ([- 2 , 0 , 2 ]), eta = 0 , sigma = 1 , observed = [0 , np .nan , 1 ]
930+ )
931+ assert len (m .deterministics ) == 2 # One from the auto-imputation, the other from compute_p
0 commit comments