29
29
30
30
import pymc as pm
31
31
32
- from pymc .distributions .discrete import _OrderedLogistic , _OrderedProbit
32
+ from pymc .distributions .discrete import OrderedLogistic , OrderedProbit
33
33
from pymc .logprob .basic import icdf , logcdf , logp
34
34
from pymc .logprob .utils import ParameterValueError
35
35
from pymc .pytensorf import floatX
@@ -481,26 +481,6 @@ def test_orderedlogistic_dimensions(shape):
481
481
assert np .allclose (ologp , expected )
482
482
483
483
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
-
504
484
class TestMoments :
505
485
@pytest .mark .parametrize (
506
486
"p, size, expected" ,
@@ -857,14 +837,12 @@ def test_implied_degenerate_shape(self):
857
837
assert x .eval ().shape == (1 ,)
858
838
859
839
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 )
868
846
869
847
@pytest .mark .parametrize (
870
848
"eta, cutpoints, expected" ,
@@ -881,22 +859,34 @@ def test_shape_inputs(self, eta, cutpoints, expected):
881
859
"""
882
860
This test checks when providing different shapes for `eta` parameters.
883
861
"""
884
- categorical = _OrderedLogistic .dist (
862
+ categorical = OrderedLogistic .dist (
885
863
eta = eta ,
886
864
cutpoints = cutpoints ,
887
865
)
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
890
868
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
891
874
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 )
900
890
901
891
@pytest .mark .parametrize (
902
892
"eta, cutpoints, sigma, expected" ,
@@ -914,10 +904,28 @@ def test_shape_inputs(self, eta, cutpoints, sigma, expected):
914
904
"""
915
905
This test checks when providing different shapes for `eta` and `sigma` parameters.
916
906
"""
917
- categorical = _OrderedProbit .dist (
907
+ categorical = OrderedProbit .dist (
918
908
eta = eta ,
919
909
cutpoints = cutpoints ,
920
910
sigma = sigma ,
921
911
)
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