55
66from probnum .quad .integration_measures import GaussianMeasure , LebesgueMeasure
77
8+ # Tests shared by all measures start here
89
9- # Tests for Gaussian measure
10- def test_gaussian_diagonal_covariance (input_dim : int ):
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 ):
1127 """Check that diagonal covariance matrices are recognised as diagonal."""
1228 mean = np .full ((input_dim ,), 0.0 )
1329 cov = np .eye (input_dim )
@@ -36,13 +52,6 @@ def test_gaussian_mean_shape_1d(mean, cov):
3652 assert measure .cov .size == 1
3753
3854
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-
4655def test_gaussian_param_assignment (input_dim : int ):
4756 """Check that diagonal mean and covariance for higher dimensions are extended
4857 correctly."""
@@ -57,15 +66,24 @@ def test_gaussian_param_assignment(input_dim: int):
5766 assert np .array_equal (measure .cov , np .eye (input_dim ))
5867
5968
60- def test_gaussian_scalar ():
69+ def test_gaussian_param_assignment_scalar ():
6170 """Check that the 1d Gaussian case works."""
6271 measure = GaussianMeasure (0.5 , 1.5 )
6372 assert measure .mean == 0.5
6473 assert measure .cov == 1.5
6574
6675
67- # Tests for Lebesgue measure
68- def test_lebesgue_dim_correct (input_dim : int ):
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 ):
6987 """Check that dimensions are handled correctly."""
7088 domain1 = (0.0 , 1.87 )
7189 measure11 = LebesgueMeasure (domain = domain1 )
@@ -82,40 +100,70 @@ def test_lebesgue_dim_correct(input_dim: int):
82100 assert measure22 .input_dim == input_dim
83101
84102
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 ):
103+ def test_lebesgue_normalization_value (input_dim : int ):
96104 """Check that normalization constants are handled properly when not equal to one."""
97105 domain = (0 , 2 )
98- measure = LebesgueMeasure (domain = domain , input_dim = input_dim , normalized = True )
99106
107+ # normalized
108+ measure = LebesgueMeasure (domain = domain , input_dim = input_dim , normalized = True )
100109 volume = 2 ** input_dim
101- assert measure .normalization_constant == 1 / volume
102-
110+ assert measure .normalization_constant == 1.0 / volume
103111
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 )
112+ # not normalized
113+ measure = LebesgueMeasure (domain = domain , input_dim = input_dim , normalized = False )
114+ assert measure .normalization_constant == 1.0
109115
110116
111- def test_lebesgue_unnormalized (input_dim : int ):
117+ def test_lebesgue_normalization_value_unnormalized (input_dim : int ):
112118 """Check that normalization constants are handled properly when equal to one."""
113119 measure1 = LebesgueMeasure (domain = (0 , 1 ), input_dim = input_dim , normalized = True )
114120 measure2 = LebesgueMeasure (domain = (0 , 1 ), input_dim = input_dim , normalized = False )
121+
122+ assert measure1 .normalized
123+ assert not measure2 .normalized
115124 assert measure1 .normalization_constant == measure2 .normalization_constant
116125
117126
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
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 )
0 commit comments