File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -58,6 +58,46 @@ def test_read_evidence_file(self):
5858 evidence = read_evidence_file ("examples/simple_model.evid" )
5959 self .assertEqual (evidence , {1 : 1 })
6060
61+ def test_invalid_network_type (self ):
62+ content = "\n " .join (
63+ [
64+ "INVALID" ,
65+ "1" ,
66+ "2" ,
67+ "0" ,
68+ ]
69+ )
70+ with self .assertRaises (ValueError ):
71+ read_model_from_string (content )
72+
73+ def test_scope_size_mismatch (self ):
74+ content = "\n " .join (
75+ [
76+ "MARKOV" ,
77+ "2" ,
78+ "2 2" ,
79+ "1" ,
80+ "2 0" ,
81+ "2" ,
82+ "0.5 0.5" ,
83+ ]
84+ )
85+ with self .assertRaises (ValueError ):
86+ read_model_from_string (content )
87+
88+ def test_missing_table_entries (self ):
89+ content = "\n " .join (
90+ [
91+ "MARKOV" ,
92+ "1" ,
93+ "2" ,
94+ "1" ,
95+ "1 0" ,
96+ ]
97+ )
98+ with self .assertRaises (ValueError ):
99+ read_model_from_string (content )
100+
61101
62102if __name__ == "__main__" :
63103 unittest .main ()
Original file line number Diff line number Diff line change 1616 initial_state ,
1717 collect_message ,
1818 process_message ,
19+ apply_evidence ,
1920)
2021
2122
@@ -91,6 +92,45 @@ def test_message_normalization(self):
9192 for msg in var_msgs :
9293 self .assertAlmostEqual (float (msg .sum ()), 1.0 , places = 6 )
9394
95+ def test_zero_message_handling (self ):
96+ content = "\n " .join (
97+ [
98+ "MARKOV" ,
99+ "1" ,
100+ "2" ,
101+ "2" ,
102+ "1 0" ,
103+ "1 0" ,
104+ "2" ,
105+ "0.0 0.0" ,
106+ "2" ,
107+ "0.7 0.3" ,
108+ ]
109+ )
110+ model = read_model_from_string (content )
111+ bp = BeliefPropagation (model )
112+ state = initial_state (bp )
113+ collect_message (bp , state , normalize = True )
114+ process_message (bp , state , normalize = True , damping = 0.0 )
115+ self .assertAlmostEqual (float (state .message_in [0 ][0 ].sum ()), 0.0 , places = 6 )
116+ self .assertAlmostEqual (float (state .message_out [0 ][1 ].sum ()), 0.0 , places = 6 )
117+
118+ def test_evidence_out_of_range_zeros_factor (self ):
119+ content = "\n " .join (
120+ [
121+ "MARKOV" ,
122+ "1" ,
123+ "2" ,
124+ "1" ,
125+ "1 0" ,
126+ "2" ,
127+ "0.4 0.6" ,
128+ ]
129+ )
130+ model = read_model_from_string (content )
131+ bp = apply_evidence (BeliefPropagation (model ), {1 : 5 })
132+ self .assertAlmostEqual (float (bp .factors [0 ].values .sum ()), 0.0 , places = 6 )
133+
94134
95135if __name__ == "__main__" :
96136 unittest .main ()
You can’t perform that action at this time.
0 commit comments