Skip to content

Remove prior_mu from compute_sigmas - #6261

Closed
not522 wants to merge 2 commits into
optuna:masterfrom
not522:fix-compute-sigmas
Closed

not522 wants to merge 2 commits into
optuna:masterfrom
not522:fix-compute-sigmas

Conversation

@not522

@not522 not522 commented Aug 28, 2025

Copy link
Copy Markdown
Member

Motivation

The calculation of sigmas in ParzenEstimator is influenced by the prior's mu, but this is inappropriate for the window computation. This behavior stems from referencing the implementation of hyperopt (hyperopt/hyperopt@5d8b88e).

Description of the changes

This PR removes prior_mu from compute_sigmas.

Benchmark results vary depending on the objective function, and when the optimal values are in the middle of the suggestion ranges, performance slightly deteriorated.

import numpy as np
import matplotlib.pyplot as plt
import optuna

def objective(trial):
    params = np.empty(20)
    for i in range(20):
        params[i] = trial.suggest_float(f"x{i}", -10, 10)
    return np.sum(params ** 2)
    # return np.sum((params - 5) ** 2)

N_STUDIES = 100
N_TRIALS = 100

y = [[] for _ in  range(N_STUDIES)]
for seed in range(N_STUDIES):
    sampler = optuna.samplers.TPESampler(seed=seed)
    study = optuna.create_study(sampler=sampler)
    study.optimize(objective, n_trials=N_TRIALS)
    trials = study.trials
    y[seed].append(trials[0].value)
    for i in range(1, N_TRIALS):
        y[seed].append(min(y[seed][-1], trials[i].value))

y = np.asarray(y)
np.save("master.npy", y)
# np.save("pr.npy", y)
y = np.load("master.npy")
mean = np.mean(y, axis=0)
std = np.std(y, axis=0)
plt.fill_between(range(N_TRIALS), mean + std, mean - std, alpha=0.1)
plt.plot(range(N_TRIALS), mean, label="master")

y = np.load("pr.npy")
mean = np.mean(y, axis=0)
std = np.std(y, axis=0)
plt.fill_between(range(N_TRIALS), mean + std, mean - std, alpha=0.1)
plt.plot(range(N_TRIALS), mean, label="fixed")

plt.xlabel("trial number")
plt.ylabel("loss")
plt.legend()
plt.savefig("sigma.png")

Σx^2 / Σ(x-5)^2

sigma0 sigma5

The internal state of the ParzenEstimator was verified using the following code:

import numpy as np
import matplotlib.pyplot as plt
import optuna
import optunahub

def objective(trial):
    params = np.empty(20)
    for i in range(20):
        params[i] = trial.suggest_float(f"x{i}", -10, 10)
    return np.sum(params ** 2)

module = optunahub.load_module(
    package="visualization/tpe_acquisition_visualizer",
)
tpe_acquisition_visualizer = module.TPEAcquisitionVisualizer()
sampler = optuna.samplers.TPESampler(seed=0)
study = optuna.create_study(sampler=sampler)
study.optimize(objective, n_trials=11, callbacks=[tpe_acquisition_visualizer])
fig = tpe_acquisition_visualizer.plot(study, 10, "x0")
fig.savefig(f"x0_10.png")
plt.close(fig)

master / PR

x0_10_master x0_10_pr

@not522 not522 added the bug Issue/PR about behavior that is broken. Not for typos/examples/CI/test but for Optuna itself. label Aug 28, 2025
@not522
not522 marked this pull request as ready for review August 28, 2025 11:40
@github-actions

github-actions Bot commented Sep 4, 2025

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 Sep 4, 2025
@y0z

y0z commented Sep 8, 2025

Copy link
Copy Markdown
Member

@nabenabe0928 @fusawa-yugo
Could you review this PR?

@y0z y0z removed the stale Exempt from stale bot labeling. label Sep 8, 2025
@nabenabe0928

Copy link
Copy Markdown
Contributor

I'd recommend revisiting this PR once the bandwidth selection becomes stable.

@not522

not522 commented Sep 11, 2025

Copy link
Copy Markdown
Member Author

I discussed with @y0z as well, we concluded that performance concerns warrant postponing this change until the next major release.

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