-
Notifications
You must be signed in to change notification settings - Fork 179
Expand file tree
/
Copy pathtest_proba_basic.py
More file actions
341 lines (260 loc) · 10.7 KB
/
test_proba_basic.py
File metadata and controls
341 lines (260 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
"""Non-suite tests for probability distribution objects."""
# copyright: skpro developers, BSD-3-Clause License (see LICENSE file)
# adapted from sktime
__author__ = ["fkiraly"]
import numpy as np
import pandas as pd
import pytest
from skbase.utils.dependencies import _check_soft_dependencies
from skpro.tests.test_switch import run_test_module_changed
@pytest.mark.skip(reason="Undiagnosed failure. Skipping until resolved. See #918.")
def test_proba_example():
"""Test one subsetting case for BaseDistribution."""
from skpro.distributions.normal import Normal
n = Normal(mu=[[0, 1], [2, 3], [4, 5]], sigma=1)
assert n.shape == (3, 2)
one_row = n.loc[[1]]
assert isinstance(one_row, Normal)
assert one_row.shape == (1, 2)
@pytest.mark.parametrize("subsetter", ["loc", "iloc"])
def test_proba_subsetters_loc_iloc(subsetter):
"""Test one subsetting case for BaseDistribution."""
from skpro.distributions.normal import Normal
n = Normal(mu=[[0, 1], [2, 3], [4, 5]], sigma=1)
assert n.shape == (3, 2)
# should result in 2D array distribution (1, 1)
nss = getattr(n, subsetter)[1, [1]]
assert isinstance(nss, Normal)
assert nss.shape == (1, 1)
assert nss.mu.shape == (1, 1)
nss = getattr(n, subsetter)[[1], 1]
assert isinstance(nss, Normal)
assert nss.shape == (1, 1)
assert nss.mu.shape == (1, 1)
# should result in scalar distribution
nss = getattr(n, subsetter)[1, 1]
assert isinstance(nss, Normal)
assert nss.shape == ()
nss = getattr(n, subsetter)[1, 1]
assert isinstance(nss, Normal)
assert nss.shape == ()
def test_proba_subsetters_at_iat():
"""Test one subsetting case for BaseDistribution."""
from skpro.distributions.normal import Normal
n = Normal(mu=[[0, 1], [2, 3], [4, 5]], sigma=1)
# should result in scalar distribution
nss = n.iat[1, 1]
assert isinstance(nss, Normal)
assert nss.shape == ()
assert nss == n.iloc[1, 1]
nss = n.at[1, 1]
assert isinstance(nss, Normal)
assert nss.shape == ()
assert nss == n.loc[1, 1]
def test_proba_index_coercion():
"""Test index coercion for BaseDistribution."""
from skpro.distributions.normal import Normal
n = Normal(mu=[[0, 1], [2, 3], [4, 5]], sigma=1, columns=["foo", "bar"])
assert n.shape == (3, 2)
assert isinstance(n.index, pd.Index)
assert isinstance(n.columns, pd.Index)
assert n.index.equals(pd.RangeIndex(3))
assert n.columns.equals(pd.Index(["foo", "bar"]))
n = Normal(mu=[[0, 1], [2, 3], [4, 5]], sigma=1, index=["2", 1, 0])
assert n.shape == (3, 2)
assert isinstance(n.index, pd.Index)
assert isinstance(n.columns, pd.Index)
assert n.index.equals(pd.Index(["2", 1, 0]))
assert n.columns.equals(pd.RangeIndex(2))
# this should coerce to a 2D array of shape (1, 3)
n = Normal(0, 1, columns=[1, 2, 3])
assert n.shape == (1, 3)
assert isinstance(n.index, pd.Index)
assert isinstance(n.columns, pd.Index)
assert n.index.equals(pd.RangeIndex(1))
assert n.columns.equals(pd.Index([1, 2, 3]))
@pytest.mark.skipif(
not _check_soft_dependencies("matplotlib", severity="none"),
reason="skip if matplotlib is not available",
)
@pytest.mark.parametrize("fun", ["pdf", "ppf", "cdf"])
def test_proba_plotting(fun):
"""Test that plotting functions do not crash and return ax as expected."""
from matplotlib.axes import Axes
from matplotlib.figure import Figure
from skpro.distributions.normal import Normal
# default case, 2D distribution with n_columns>1
n = Normal(mu=[[0, 1], [2, 3], [4, 5]], sigma=1)
fig, ax = n.plot(fun=fun)
assert isinstance(fig, Figure)
assert isinstance(ax, np.ndarray)
assert ax.shape == n.shape
assert all([isinstance(a, Axes) for a in ax.flatten()])
assert all([a.get_figure() == fig for a in ax.flatten()])
# 1D case requires special treatment of axes
n = Normal(mu=[[1], [2], [3]], sigma=1)
fig, ax = n.plot(fun=fun)
assert isinstance(fig, Figure)
assert isinstance(ax, type(ax))
assert ax.shape == (n.shape[0],)
assert all([isinstance(a, Axes) for a in ax.flatten()])
assert all([a.get_figure() == fig for a in ax.flatten()])
# scalar case
n = Normal(mu=1, sigma=1)
ax = n.plot(fun=fun)
assert isinstance(ax, Axes)
@pytest.mark.skipif(
not _check_soft_dependencies("matplotlib", severity="none"),
reason="skip if matplotlib is not available",
)
def test_discrete_pmf_plotting():
"""Test that discrete PMF plotting uses stem plots."""
from matplotlib.axes import Axes
from skpro.distributions.binomial import Binomial
# Test Binomial PMF plotting
n = Binomial(n=10, p=0.5)
ax = n.plot(fun="pmf")
assert isinstance(ax, Axes)
# Check that stem plot was used (should have containers)
assert len(ax.containers) > 0, "Stem plot should be used for discrete PMF"
# For small distributions, check that all support points are plotted.
# Binomial(n=10) has support [0, 1, 2, ..., 10] = 11 points.
# A StemContainer always has exactly 3 children (markerline, stemlines,
# baseline) regardless of the number of data points. The correct way to
# count plotted points is via stemlines.get_segments().
stem_container = ax.containers[0]
assert hasattr(
stem_container, "stemlines"
), "Expected StemContainer from stem plot, check _plot_single uses ax.stem"
n_plotted = len(stem_container.stemlines.get_segments())
assert n_plotted > 5, (
f"Should plot at multiple support points, got {n_plotted}. "
"Binomial(n=10) has 11 support points (0..10)."
)
def test_to_df_parametric():
"""Tests coercion to DataFrame via get_params_df and to_df."""
from skpro.distributions.normal import Normal
cols = ["foo", "bar"]
# default case, 2D distribution with n_columns>1
n = Normal(mu=[[0, 1], [2, 3], [4, 5]], sigma=1, columns=cols)
param_names = n.get_param_names()
params_df = n.get_params_df()
for k, v in params_df.items():
assert k in param_names
assert isinstance(v, pd.DataFrame)
assert (v.index == n.index).all()
assert (v.columns == n.columns).all()
all_params_df = n.to_df()
assert isinstance(all_params_df, pd.DataFrame)
assert (all_params_df.index == n.index).all()
assert isinstance(all_params_df.columns, pd.MultiIndex)
level0_vals = all_params_df.columns.get_level_values(0).unique()
level1_vals = all_params_df.columns.get_level_values(1).unique()
assert (level0_vals == n.columns).all()
for ix in level1_vals:
assert ix in param_names
assert ix not in ["index", "columns"]
# scalar case
n = Normal(mu=2, sigma=3)
param_names = n.get_param_names()
params_df = n.get_params_df()
for k, v in params_df.items():
assert k in param_names
assert isinstance(v, pd.DataFrame)
assert (v.index == pd.RangeIndex(1)).all()
assert (v.columns == pd.RangeIndex(1)).all()
all_params_df = n.to_df()
assert isinstance(all_params_df, pd.DataFrame)
assert (all_params_df.index == pd.RangeIndex(1)).all()
assert not isinstance(all_params_df.columns, pd.MultiIndex)
for ix in all_params_df.columns:
assert ix in param_names
assert ix not in ["index", "columns"]
def test_head_tail():
"""Test head and tail utility functions."""
from skpro.distributions.normal import Normal
cols = ["foo", "bar"]
# default case, 2D distribution with n_columns>1
n = Normal(mu=[[0, 1], [2, 3], [4, 5]], sigma=1, columns=cols)
nh = n.head(2)
assert isinstance(nh, Normal)
assert nh.shape == (2, 2)
assert (nh.columns == n.columns).all()
assert (nh.index == n.index[:2]).all()
nh2 = n.head()
assert isinstance(nh2, Normal)
assert nh2.shape == (3, 2)
assert (nh2.columns == n.columns).all()
assert (nh2.index == n.index).all()
nt = n.tail(2)
assert isinstance(nt, Normal)
assert nt.shape == (2, 2)
assert (nt.columns == n.columns).all()
assert (nt.index == n.index[1:]).all()
nt2 = n.tail()
assert isinstance(nt2, Normal)
assert nt2.shape == (3, 2)
assert (nt2.columns == n.columns).all()
assert (nt2.index == n.index).all()
# scalar case
n = Normal(mu=2, sigma=3)
nh = n.head()
assert nh.ndim == 0
nt = n.tail(42)
assert nt.ndim == 0
@pytest.mark.skipif(
not run_test_module_changed("skpro.distributions"),
reason="run only if skpro.distributions has been changed",
)
def test_multiindex_loc_indexing():
"""Test that loc indexing works with MultiIndex and Index objects."""
from skpro.distributions.normal import Normal
index = pd.MultiIndex.from_tuples([("a", 1), ("a", 2), ("b", 1)])
columns = pd.Index(["x", "y"])
dist = Normal(mu=[[0, 1], [2, 3], [4, 5]], sigma=1, index=index, columns=columns)
x = pd.DataFrame([[0.1, 0.2], [0.3, 0.4], [0.5, 0.6]], index=index, columns=columns)
subset = dist.loc[x.index, x.columns]
assert subset.shape == dist.shape
assert subset.index.equals(dist.index)
assert subset.columns.equals(dist.columns)
result = dist.quantile(0.5)
assert result.shape == dist.shape
@pytest.mark.skipif(
not run_test_module_changed("skpro.distributions"),
reason="run only if skpro.distributions has been changed",
)
def test_pmf_support_method():
"""Test the _pmf_support method for different distribution types."""
from skpro.distributions.binomial import Binomial
from skpro.distributions.delta import Delta
from skpro.distributions.empirical import Empirical
from skpro.distributions.normal import Normal
# Test continuous distribution (Normal) - should return empty array
normal = Normal(mu=0, sigma=1)
support = normal._pmf_support(-1, 1)
assert isinstance(support, np.ndarray)
assert len(support) == 0
# Test discrete distribution with default integer support (Binomial)
binomial = Binomial(n=5, p=0.5)
support = binomial._pmf_support(0, 5)
assert isinstance(support, np.ndarray)
assert len(support) > 0
assert all(isinstance(x, (int, np.integer)) for x in support)
assert all(x >= 0 for x in support)
# Test Empirical distribution
spl = pd.Series([1.5, 2.5, 3.5])
empirical = Empirical(spl)
support = empirical._pmf_support(1, 4)
assert isinstance(support, np.ndarray)
assert len(support) > 0
assert 1.5 in support or 2.5 in support or 3.5 in support
# Test Delta distribution
delta = Delta(c=2.0)
support = delta._pmf_support(1, 3)
assert isinstance(support, np.ndarray)
assert len(support) == 1
assert support[0] == 2.0
# Test Delta outside bounds
support = delta._pmf_support(3, 4)
assert isinstance(support, np.ndarray)
assert len(support) == 0