55
66from probnum .quad .integration_measures import GaussianMeasure , LebesgueMeasure
77
8- # Tests shared by all measures start here
98
10-
11- def test_density_call_shape (x , measure ):
12- expected_shape = (x .shape [0 ],)
13- assert measure (x ).shape == expected_shape
14-
15-
16- @pytest .mark .parametrize ("n_sample" , [1 , 2 , 5 ])
17- def test_sample_shape (measure , n_sample , rng ):
18- input_dim = measure .input_dim
19- res = measure .sample (n_sample = n_sample , rng = rng )
20- assert res .shape == (n_sample , input_dim )
21-
22-
23- # Tests for Gaussian measure start here
24-
25-
26- def test_gaussian_diagonal_covariance (input_dim ):
9+ # Tests for Gaussian measure
10+ def test_gaussian_diagonal_covariance (input_dim : int ):
2711 """Check that diagonal covariance matrices are recognised as diagonal."""
2812 mean = np .full ((input_dim ,), 0.0 )
2913 cov = np .eye (input_dim )
@@ -52,6 +36,13 @@ def test_gaussian_mean_shape_1d(mean, cov):
5236 assert measure .cov .size == 1
5337
5438
39+ @pytest .mark .parametrize ("neg_dim" , [0 , - 1 , - 10 , - 100 ])
40+ def test_gaussian_negative_dimension (neg_dim ):
41+ """Make sure that a negative dimension raises ValueError."""
42+ with pytest .raises (ValueError ):
43+ GaussianMeasure (0 , 1 , neg_dim )
44+
45+
5546def test_gaussian_param_assignment (input_dim : int ):
5647 """Check that diagonal mean and covariance for higher dimensions are extended
5748 correctly."""
@@ -66,24 +57,15 @@ def test_gaussian_param_assignment(input_dim: int):
6657 assert np .array_equal (measure .cov , np .eye (input_dim ))
6758
6859
69- def test_gaussian_param_assignment_scalar ():
60+ def test_gaussian_scalar ():
7061 """Check that the 1d Gaussian case works."""
7162 measure = GaussianMeasure (0.5 , 1.5 )
7263 assert measure .mean == 0.5
7364 assert measure .cov == 1.5
7465
7566
76- @pytest .mark .parametrize ("wrong_dim" , [0 , - 1 , - 10 , - 100 ])
77- def test_gaussian_wrong_dimension_raises (wrong_dim ):
78- """Make sure that a non-positive dimension raises ValueError."""
79- with pytest .raises (ValueError ):
80- GaussianMeasure (0 , 1 , wrong_dim )
81-
82-
83- # Tests for Lebesgue measure start here
84-
85-
86- def test_lebesgue_input_dim_assignment (input_dim : int ):
67+ # Tests for Lebesgue measure
68+ def test_lebesgue_dim_correct (input_dim : int ):
8769 """Check that dimensions are handled correctly."""
8870 domain1 = (0.0 , 1.87 )
8971 measure11 = LebesgueMeasure (domain = domain1 )
@@ -100,70 +82,40 @@ def test_lebesgue_input_dim_assignment(input_dim: int):
10082 assert measure22 .input_dim == input_dim
10183
10284
103- def test_lebesgue_normalization_value (input_dim : int ):
85+ @pytest .mark .parametrize ("domain_a" , [0 , np .full ((3 ,), 0 ), np .full ((13 ,), 0 )])
86+ @pytest .mark .parametrize ("domain_b" , [np .full ((4 ,), 1.2 ), np .full ((14 ,), 1.2 )])
87+ @pytest .mark .parametrize ("input_dim" , [- 10 , - 2 , 0 , 2 , 12 , 122 ])
88+ def test_lebesgue_dim_incorrect (domain_a , domain_b , input_dim ):
89+ """Check that ValueError is raised if domain limits have mismatching dimensions or
90+ dimension is not positive."""
91+ with pytest .raises (ValueError ):
92+ LebesgueMeasure (domain = (domain_a , domain_b ), input_dim = input_dim )
93+
94+
95+ def test_lebesgue_normalization (input_dim : int ):
10496 """Check that normalization constants are handled properly when not equal to one."""
10597 domain = (0 , 2 )
106-
107- # normalized
10898 measure = LebesgueMeasure (domain = domain , input_dim = input_dim , normalized = True )
99+
109100 volume = 2 ** input_dim
110- assert measure .normalization_constant == 1.0 / volume
101+ assert measure .normalization_constant == 1 / volume
102+
111103
112- # not normalized
113- measure = LebesgueMeasure (domain = domain , input_dim = input_dim , normalized = False )
114- assert measure .normalization_constant == 1.0
104+ @pytest .mark .parametrize ("domain" , [(0 , np .Inf ), (- np .Inf , 0 ), (- np .Inf , np .Inf )])
105+ def test_lebesgue_normalization_raises (domain , input_dim : int ):
106+ """Check that exception is raised when normalization is not possible."""
107+ with pytest .raises (ValueError ):
108+ LebesgueMeasure (domain = domain , input_dim = input_dim , normalized = True )
115109
116110
117- def test_lebesgue_normalization_value_unnormalized (input_dim : int ):
111+ def test_lebesgue_unnormalized (input_dim : int ):
118112 """Check that normalization constants are handled properly when equal to one."""
119113 measure1 = LebesgueMeasure (domain = (0 , 1 ), input_dim = input_dim , normalized = True )
120114 measure2 = LebesgueMeasure (domain = (0 , 1 ), input_dim = input_dim , normalized = False )
121-
122- assert measure1 .normalized
123- assert not measure2 .normalized
124115 assert measure1 .normalization_constant == measure2 .normalization_constant
125116
126117
127- @pytest .mark .parametrize ("wrong_input_dim" , [- 5 , - 1 , 0 ])
128- def test_lebesgue_non_positive_input_dim_raises (wrong_input_dim ):
129- # non positive input dimenions are not allowed
130- with pytest .raises (ValueError ):
131- LebesgueMeasure (input_dim = wrong_input_dim , domain = (0 , 1 ))
132-
133-
134- @pytest .mark .parametrize (
135- "domain" ,
136- [
137- (0.0 , np .ones (4 )),
138- (np .ones (4 ), 0.0 ),
139- (np .zeros (4 ), np .ones (3 )),
140- (np .zeros ([4 , 1 ]), np .ones (4 )),
141- (np .zeros (4 ), np .ones ([4 , 1 ])),
142- (np .zeros ([4 , 1 ]), np .ones ([4 , 1 ])),
143- ],
144- )
145- def test_lebesgue_domain_shape_mismatch_raises (domain ):
146- # left and right bounds of domain are not consistent
147- with pytest .raises (ValueError ):
148- LebesgueMeasure (domain = domain )
149-
150-
151- @pytest .mark .parametrize (
152- "input_dim,domain" ,
153- [
154- (1 , (np .zeros (3 ), np .ones (3 ))),
155- (2 , (np .zeros (3 ), np .ones (3 ))),
156- (5 , (np .zeros (3 ), np .ones (3 ))),
157- ],
158- )
159- def test_lebesgue_domain_input_dim_mismatch_raises (input_dim , domain ):
160- # input dimension does not agree with domain shapes
161- with pytest .raises (ValueError ):
162- LebesgueMeasure (input_dim = input_dim , domain = domain )
163-
164-
165- @pytest .mark .parametrize ("domain" , [(0 , np .Inf ), (- np .Inf , 0 ), (- np .Inf , np .Inf )])
166- def test_lebesgue_normalization_raises (domain , input_dim : int ):
167- """Check that exception is raised when normalization is not possible."""
168- with pytest .raises (ValueError ):
169- LebesgueMeasure (domain = domain , input_dim = input_dim , normalized = True )
118+ # Tests for all integration measures
119+ def test_density_call (x , measure ):
120+ expected_shape = (x .shape [0 ],)
121+ assert measure (x ).shape == expected_shape
0 commit comments