8000 Fix zero in Halton, fix Van Der Corput and fix tests · scikit-optimize/scikit-optimize@79ae9a5 · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Feb 28, 2024. It is now read-only.

Commit 79ae9a5

Browse files
committed
Fix zero in Halton, fix Van Der Corput and fix tests
1 parent 2cde042 commit 79ae9a5

File tree

4 files changed

+41
-33
lines changed

4 files changed

+41
-33
lines changed

skopt/sampler/halton.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
class Halton(InitialPointGenerator):
1212
"""Creates `Halton` sequence samples.
13+
1314
In statistics, Halton sequences are sequences used to generate
1415
points in space for numerical methods such as Monte Carlo simulations.
1516
Although these sequences are deterministic, they are of low discrepancy,
@@ -23,16 +24,17 @@ class Halton(InitialPointGenerator):
2324
Parameters
2425
----------
2526
min_skip : int
26-
minimum skipped seed number. When `min_skip != max_skip`
27+
Minimum skipped seed number. When `min_skip != max_skip`
2728
a random number is picked.
2829
max_skip : int
29-
maximum skipped seed number. When `min_skip != max_skip`
30+
Maximum skipped seed number. When `min_skip != max_skip`
3031
a random number is picked.
3132
primes : tuple, default=None
3233
The (non-)prime base to calculate values along each axis. If
3334
empty or None, growing prime values starting from 2 will be used.
35+
3436
"""
35-
def __init__(self, min_skip=-1, max_skip=-1, primes=None):
37+
def __init__(self, min_skip=0, max_skip=0, primes=None):
3638
self.primes = primes
3739
self.min_skip = min_skip
3840
self.max_skip = max_skip
@@ -62,7 +64,8 @@ def generate(self, dimensions, n_samples, random_state=None):
6264
Returns
6365
-------
6466
np.array, shape=(n_dim, n_samples)
65-
Halton set
67+
Halton set.
68+
6669
"""
6770
rng = check_random_state(random_state)
6871
if self.primes is None:
@@ -81,10 +84,11 @@ def generate(self, dimensions, n_samples, random_state=None):
8184

8285
primes = primes[:n_dim]
8386
assert len(primes) == n_dim, "not enough primes"
84-
if self.min_skip < 0 and self.max_skip < 0:
85-
skip = max(primes)
86-
elif self.min_skip == self.max_skip:
87+
88+
if self.min_skip == self.max_skip:
8789
skip = self.min_skip
90+
elif self.min_skip < 0 and self.max_skip < 0:
91+
skip = max(primes)
8892
elif self.min_skip < 0 or self.max_skip < 0:
8993
skip = np.max(self.min_skip, self.max_skip)
9094
else:
@@ -101,8 +105,7 @@ def generate(self, dimensions, n_samples, random_state=None):
101105

102106

103107
def _van_der_corput_samples(idx, number_base=2):
104-
"""
105-
Create `Van Der Corput` low discrepancy sequence samples.
108+
"""Create `Van Der Corput` low discrepancy sequence samples.
106109
107110
A van der Corput sequence is an example of the simplest one-dimensional
108111
low-discrepancy sequence over the unit interval; it was first described in
@@ -125,10 +128,11 @@ def _van_der_corput_samples(idx, number_base=2):
125128
-------
126129
float, numpy.ndarray
127130
Van der Corput samples.
131+
128132
"""
129133
assert number_base > 1
130134

131-
idx = np.asarray(idx).flatten() + 1
135+
idx = np.asarray(idx).flatten()
132136
out = np.zeros(len(idx), dtype=float)
133137

134138
base = float(number_base)

skopt/sampler/hammersly.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
class Hammersly(InitialPointGenerator):
1313
"""Creates `Hammersley` sequence samples.
14+
1415
The Hammersley set is equivalent to the Halton sequence, except for one
1516
dimension is replaced with a regular grid. It is not recommended to
1617
generate a Hammersley sequence with more than 10 dimension.
@@ -25,16 +26,17 @@ class Hammersly(InitialPointGenerator):
2526
Parameters
2627
----------
2728
min_skip : int, default=-1
28-
minimum skipped seed number. When `min_skip != max_skip` and
29+
Minimum skipped seed number. When `min_skip != max_skip` and
2930
both are > -1, a random number is picked.
3031
max_skip : int, default=-1
31-
maximum skipped seed number. When `min_skip != max_skip` and
32+
Maximum skipped seed number. When `min_skip != max_skip` and
3233
both are > -1, a random number is picked.
3334
primes : tuple, default=None
3435
The (non-)prime base to calculate values along each axis. If
3536
empty, growing prime values starting from 2 will be used.
37+
3638
"""
37-
def __init__(self, min_skip=-1, max_skip=-1, primes=None):
39+
def __init__(self, min_skip=0, max_skip=0, primes=None):
3840
self.primes = primes
3941
self.min_skip = min_skip
4042
self.max_skip = max_skip
@@ -65,7 +67,8 @@ def generate(self, dimensions, n_samples, random_state=None):
6567
Returns
6668
-------
6769
np.array, shape=(n_dim, n_samples)
68-
Hammersley set
70+
Hammersley set.
71+
6972
"""
7073
rng = check_random_state(random_state)
7174
halton = Halton(min_skip=self.min_skip, max_skip=self.max_skip,
@@ -83,7 +86,7 @@ def generate(self, dimensions, n_samples, random_state=None):
8386
[(0., 1.), ] * (n_dim - 1), n_samples,
8487
random_state=rng)).T
8588

86-
out[n_dim - 1] = np.linspace(0, 1, n_samples + 2)[1:-1]
89+
out[n_dim - 1] = np.linspace(0, 1, n_samples + 1)[:-1]
8790
out = space.inverse_transform(out.T)
8891
space.set_transformer(transformer)
8992
return out

skopt/tests/test_plots.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,14 @@ def objective(params):
5252
xi, yi = partial_dependence_1D(res.space, res.models[-1], 0,
5353
samples, n_points=3)
5454
assert_array_almost_equal(xi, xi_)
55-
assert_array_almost_equal(yi, yi_, 1e-3)
55+
assert_array_almost_equal(yi, yi_, 3)
5656

5757
xi_ = [0, 1]
5858
yi_ = [-0.9241087603770617, -0.9240188905968352]
5959
xi, yi = partial_dependence_1D(res.space, res.models[-1], 4,
6060
samples, n_points=3)
6161
assert_array_almost_equal(xi, xi_)
62-
assert_array_almost_equal(yi, yi_, 1e-3)
62+
assert_array_almost_equal(yi, yi_, 3)
6363

6464
xi_ = [0, 1]
6565
yi_ = [1., 10.5, 20.]
@@ -70,7 +70,7 @@ def objective(params):
7070
samples, n_points=3)
7171
assert_array_almost_equal(xi, xi_)
7272
assert_array_almost_equal(yi, yi_)
73-
assert_array_almost_equal(zi, zi_, 1e-3)
73+
assert_array_almost_equal(zi, zi_, 3)
7474

7575
x_min, f_min = expected_minimum_random_sampling(res, random_state=1)
7676
x_min2, f_min2 = expected_minimum(res, random_state=1)

skopt/tests/test_sampler.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -156,27 +156,31 @@ def test_generate():
156156

157157
@pytest.mark.fast_test
158158
def test_van_der_corput():
159-
x = _van_der_corput_samples(range(11), number_base=10)
160-
y = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 0.01, 0.11]
159+
x = _van_der_corput_samples(range(12), number_base=10)
160+
y = [0., 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 0.01, 0.11]
161161
assert_array_equal(x, y)
162162

163-
x = _van_der_corput_samples(range(8), number_base=2)
164-
y = [0.5, 0.25, 0.75, 0.125, 0.625, 0.375, 0.875, 0.0625]
163+
x = _van_der_corput_samples(range(9), number_base=2)
164+
y = [0., 0.5, 0.25, 0.75, 0.125, 0.625, 0.375, 0.875, 0.0625]
165165
assert_array_equal(x, y)
166166

167167

168168
@pytest.mark.fast_test
169169
def test_halton():
170170
h = Halton()
171-
x = h.generate([(0., 1.), ] * 2, 3)
172-
y = np.array([[0.125, 0.625, 0.375], [0.4444, 0.7778, 0.2222]]).T
173-
assert_array_almost_equal(x, y, 1e-3)
171+
x = h.generate([(0., 1.), ], 9)
172+
y = _van_der_corput_samples(range(9), number_base=2)
173+
assert_array_almost_equal(np.array(x).flatten(), y)
174174

175175
h = Halton()
176-
x = h.generate([(0., 1.), ] * 2, 4)
177-
y = np.array([[0.125, 0.625, 0.375, 0.875],
178-
[0.4444, 0.7778, 0.2222, 0.5556]]).T
179-
assert_array_almost_equal(x, y, 1e-3)
176+
x = h.generate([(0., 1.), ] * 2, 6)
177+
y = np.array([[0, 0], [1 / 2, 1 / 3], [1 / 4, 2 / 3], [3 / 4, 1 / 9],
178+
[1 / 8, 4 / 9], [5 / 8, 7 / 9]])
179+
assert_array_almost_equal(x, y)
180+
181+
h = Halton(min_skip=0, max_skip=3)
182+
x = h.generate([(0., 1.), ] * 2, 4, random_state=12345)
183+
assert_array_almost_equal(x, y[2:])
180184

181185
samples = h.generate([(0., 1.), ] * 2, 200)
182186
assert len(samples) == 200
@@ -186,11 +190,8 @@ def test_halton():
186190
@pytest.mark.fast_test
187191
def test_hammersly():
188192
h = Hammersly()
189-
x = h.generate([(0., 1.), ] * 2, 3)
190-
y = np.array([[0.75, 0.125, 0.625], [0.25, 0.5, 0.75]]).T
191-
assert_almost_equal(x, y)
192193
x = h.generate([(0., 1.), ] * 2, 4)
193-
y = np.array([[0.75, 0.125, 0.625, 0.375], [0.2, 0.4, 0.6, 0.8]]).T
194+
y = np.array([[0, 0], [1 / 2, 0.25], [1 / 4, 0.5], [3 / 4, 0.75]])
194195
assert_almost_equal(x, y)
195196

196197
samples = h.generate([(0., 1.), ] * 2, 200)

0 commit comments

Comments
 (0)
0