Skip to content

fix Int distribution midpoint bias in _SearchSpaceTransform and add tests - #6771

Merged
y0z merged 7 commits into
optuna:masterfrom
yen-0:fix/qmcs_bias
Aug 13, 2026
Merged

y0z merged 7 commits into
optuna:masterfrom
yen-0:fix/qmcs_bias

Conversation

@yen-0

@yen-0 yen-0 commented Jul 24, 2026

Copy link
Copy Markdown
Collaborator

Motivation

This PR removes midpoint bias when untransforming IntDistribution values, first mentioned in https://github.com/optuna/optuna/pull/6742 .

Description of the changes

Previously, _SearchSpaceTransform._untransform_numerical_param used np.round(...) for non-log integer distributions. Because NumPy uses round-half-to-even, transformed values that land exactly on step midpoints were systematically pulled toward even indices.
This PR changes non-log IntDistribution untransforming to use equal-width half-open bins instead of ties-to-even rounding with math.floor(trans_param - d.low + 0.5 * d.step) / d.step. Updated _SearchSpaceTransform._untransform_numerical_param for non-log IntDistribution to use equal-width binning instead of np.round(..).

Also added the following tests.

  • tests/test_transform.py: Verifies that midpoint values such as 0.5, 1.5, and 2.5 map using equal-width bins
    rather than ties-to-even rounding.
  • tests/samplers_tests/test_qmc.py: suggest_int(...) regression tests that assert exact balanced coverage after the independently sampled first trial.
  • Also added tests for other callers including RandomSampler, CmaEsSampler, NSGA-II mutation, and NSGA-II crossover.

@c-bata c-bata assigned c-bata and unassigned c-bata Jul 24, 2026
@y0z y0z added this to the v5.0.0 milestone Jul 24, 2026
@c-bata c-bata self-assigned this Jul 24, 2026
@y0z y0z removed this from the v5.0.0 milestone Jul 24, 2026
@yen-0
yen-0 marked this pull request as ready for review July 29, 2026 01:32
@c-bata c-bata added the bug Issue/PR about behavior that is broken. Not for typos/examples/CI/test but for Optuna itself. label Jul 29, 2026
@y0z y0z self-assigned this Jul 29, 2026
@github-actions

github-actions Bot commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

This pull request has not seen any recent activity.

@github-actions github-actions Bot added the stale Exempt from stale bot labeling. label Aug 5, 2026
@c-bata c-bata removed the stale Exempt from stale bot labeling. label Aug 7, 2026

@c-bata c-bata left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I confirmed that this PR fixes the midpoint bias. LGTM.

from collections import Counter
import optuna

optuna.logging.set_verbosity(optuna.logging.WARN)
sampler = optuna.samplers.QMCSampler(scramble=False, seed=0, qmc_type="sobol")
study = optuna.create_study(sampler=sampler)


def objective(trial: optuna.Trial) -> float:
    x = trial.suggest_int("x", 0, 3)
    y = trial.suggest_int("y", 0, 3)
    return x ** 2 + y ** 2


study.optimize(objective, n_trials=17)
qmc_trials = study.trials[1:]
x_counts = Counter(t.params["x"] for t in qmc_trials)
y_counts = Counter(t.params["y"] for t in qmc_trials)
print("x:", dict(sorted(x_counts.items())))
print("y:", dict(sorted(y_counts.items())))

Before:

$ python tmp/optuna_single.py
/Users/c-bata/go/src/github.com/optuna/optuna/tmp/optuna_single.py:5: ExperimentalWarning: QMCSampler is experimental (supported from v3.0.0). The interface can change in the future.
  sampler = optuna.samplers.QMCSampler(scramble=False, seed=0, qmc_type="sobol")
x: {0: 5, 1: 3, 2: 5, 3: 3}
y: {0: 5, 1: 3, 2: 5, 3: 3}

After (This PR):

$ python tmp/optuna_single.py
/Users/c-bata/go/src/github.com/optuna/optuna/tmp/optuna_single.py:5: ExperimentalWarning: QMCSampler is experimental (supported from v3.0.0). The interface can change in the future.
  sampler = optuna.samplers.QMCSampler(scramble=False, seed=0, qmc_type="sobol")
x: {0: 4, 1: 4, 2: 4, 3: 4}
y: {0: 4, 1: 4, 2: 4, 3: 4}

@c-bata c-bata removed their assignment Aug 7, 2026
@c-bata c-bata added this to the v5.0.0 milestone Aug 7, 2026
Comment thread optuna/_transform.py Outdated
Comment on lines +299 to +300
offset = trans_param - (d.low - 0.5 * d.step)
param = int(np.clip(math.floor(offset / d.step) * d.step + d.low, d.low, d.high))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Computing d.low - 0.5 * d.step before subtracting it from trans_param can lose the half-step for large integer bounds. For example, with IntDistribution(2**52 + 1, 2**52 + 11), transforming and then untransforming d.low returns d.low + 1. Could we subtract d.low first, e.g. floor((trans_param - d.low) / d.step + 0.5), and add a large-integer round-trip test?

d = IntDistribution(2**52 + 1, 2**52 + 11)
transform = _SearchSpaceTransform({"x": d})

transform.untransform(transform.transform({"x": d.low}))
# {"x": 4503599627370498}
# expected: 4503599627370497

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the recommendation. I have confirmed that is indeed a problem, and modified the order as proposed.

def test_search_space_transform_int_large_low_round_trip() -> None:
    distribution = IntDistribution(2**52 + 1, 2**52 + 11)
    transform = _SearchSpaceTransform({"x": distribution})

    assert transform.untransform(transform.transform({"x": distribution.low})) == {
        "x": distribution.low
    }

I used this test to test the behaviors and it worked this time.

@y0z
y0z merged commit 5bc1c71 into optuna:master Aug 13, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Issue/PR about behavior that is broken. Not for typos/examples/CI/test but for Optuna itself.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants