Skip to content

Update consider_prior Behavior and Remove Support for False #6007

Merged
nabenabe0928 merged 17 commits intooptuna:masterfrom
sawa3030:set-consider-prior-true
Apr 11, 2025
Merged

Update consider_prior Behavior and Remove Support for False #6007
nabenabe0928 merged 17 commits intooptuna:masterfrom
sawa3030:set-consider-prior-true

Conversation

@sawa3030
Copy link
Copy Markdown
Collaborator

@sawa3030 sawa3030 commented Mar 12, 2025

Motivation

  • Update the behavior of consider_prior to always be True.
  • Ref: #6005.
  • note: Make consider_prior positional args in the following PR.

Description of Changes

  • Update the warning message to explicitly state that consider_prior is now always True.
  • Update the code to always consider prior.
  • Set consider_prior = True when constructing _ParzenEstimatorParameters.
  • Delete tests that previously verified behavior when consider_prior = False.

Copy link
Copy Markdown
Contributor

@nabenabe0928 nabenabe0928 left a comment

Choose a reason for hiding this comment

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

Thank you for the PR, I left some preliminary comments.

Comment on lines 300 to 301
) + ("From v4.3.0 onward, `consider_prior` is always `True`.")
warnings.warn(msg, FutureWarning)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[nit] Automatic falling back is more exact in the meaning.

Suggested change
) + ("From v4.3.0 onward, `consider_prior` is always `True`.")
warnings.warn(msg, FutureWarning)
)
warnings.warn(
f"{msg} From v4.3.0 onward, `consider_prior` automatically falls back to `True`.",
FutureWarning,
)

Comment on lines +43 to +46
if parameters.prior_weight is None:
raise ValueError("Prior weight must be specified because consider_prior==True.")
elif parameters.prior_weight <= 0:
raise ValueError("Prior weight must be positive.")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[nit] As consider_prior cannot be used anymore, we can merge these errors.

Suggested change
if parameters.prior_weight is None:
raise ValueError("Prior weight must be specified because consider_prior==True.")
elif parameters.prior_weight <= 0:
raise ValueError("Prior weight must be positive.")
if parameters.prior_weight is None or parameters.prior_weight <= 0:
raise ValueError(f"A positive value must be specified for prior_weight, but got {parameters.prior_weight}.")

Comment on lines 61 to 65
if len(transformed_observations) == 0:
weights = np.array([1.0])
elif parameters.consider_prior:
else:
assert parameters.prior_weight is not None
weights = np.append(weights, [parameters.prior_weight])
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

As prior_weight is already guaranteed to be not None from the earlier line, we can simply code like this:

        weights = np.append(weights, [parameters.prior_weight])

Comment on lines +192 to +193
consider_prior = True
n_kernels = len(observations) + consider_prior
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[nit] An inline comment is sufficient.

Suggested change
consider_prior = True
n_kernels = len(observations) + consider_prior
n_kernels = len(observations) + 1 # NOTE(sawa): +1 for prior.

maxsigma = 1.0 * (high - low + step_or_0)
if parameters.consider_magic_clip:
# TODO(contramundum53): Remove dependency of minsigma on consider_prior.
consider_prior = True
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What about it?

Suggested change
consider_prior = True
n_kernels = len(observations) + 1 # NOTE(sawa): +1 for prior.

Comment on lines +277 to +280
prior_mu = 0.5 * (low + high)
prior_sigma = 1.0 * (high - low + step_or_0)
mus = np.append(mus, [prior_mu])
sigmas = np.append(sigmas, [prior_sigma])
Copy link
Copy Markdown
Contributor

@nabenabe0928 nabenabe0928 Mar 12, 2025

Choose a reason for hiding this comment

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

[nit] Reduced definitions of unused variables.

Suggested change
prior_mu = 0.5 * (low + high)
prior_sigma = 1.0 * (high - low + step_or_0)
mus = np.append(mus, [prior_mu])
sigmas = np.append(sigmas, [prior_sigma])
# Add the stats for non-informative prior.
mus = np.append(mus, [0.5 * (low + high)])
sigmas = np.append(sigmas, [1.0 * (high - low + step_or_0)])

],
[
np.asarray([-0.4, 0.4, 0.41, 0.42]),
{"prior": False, "magic_clip": True, "endpoints": True},
Copy link
Copy Markdown
Contributor

@nabenabe0928 nabenabe0928 Mar 12, 2025

Choose a reason for hiding this comment

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

I believe we can remove unit tests for prior=False as the tests for prior=True already cover the functionality.
But please check carefully whether the removed tests are really covered by the existing tests.

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 your review. Since this is the only test in test_calculate that uses magic_clip = True, I believe it should be retained.

@sawa3030 sawa3030 marked this pull request as ready for review March 13, 2025 01:09
@sawa3030
Copy link
Copy Markdown
Collaborator Author

@nabenabe0928 Thank you for your advice. Everything should be updated now. PTAL!

@c-bata
Copy link
Copy Markdown
Member

c-bata commented Mar 17, 2025

@gen740 @nabenabe0928 Could you review this PR?

# counts.astype(float) is necessary for weight calculation in ParzenEstimator.
return _ScottParzenEstimator(
param_name, rounded_dist, counts.astype(np.float64), consider_prior, prior_weight
param_name, rounded_dist, counts.astype(np.float64), True, prior_weight
Copy link
Copy Markdown
Contributor

@nabenabe0928 nabenabe0928 Mar 19, 2025

Choose a reason for hiding this comment

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

[nit] It would be better to pass arguments with keys as consider_prior is hard-coded.

Suggested change
param_name, rounded_dist, counts.astype(np.float64), True, prior_weight
param_name=param_name,
dist=rounded_dist,
counts=counts.astype(np.float64),
consider_prior=True,
prior_weight=prior_weight,


self._parzen_estimator_parameters = _ParzenEstimatorParameters(
consider_prior,
True,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could you use the keyword args here for more clarity?

Suggested change
True,
consider_prior=True,

)
assert len(mpe._mixture_distribution.weights) == max(len(mus) + int(prior), 1)
assert len(mpe._mixture_distribution.weights) == max(
len(mus) + 1, 1
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We don't need the max operator.

Suggested change
len(mus) + 1, 1
len(mus) + 1

Copy link
Copy Markdown
Contributor

@nabenabe0928 nabenabe0928 left a comment

Choose a reason for hiding this comment

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

I left some comments except for the unit tests of PED-ANOVA.

)
assert len(mpe._mixture_distribution.weights) == max(len(mus) + int(prior), 1)
assert len(mpe._mixture_distribution.weights) == max(
len(mus) + 1, 1
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Same here. We don't need the max operator.

Suggested change
len(mus) + 1, 1
len(mus) + 1

@c-bata c-bata assigned not522 and unassigned gen740 Mar 21, 2025
@c-bata
Copy link
Copy Markdown
Member

c-bata commented Mar 21, 2025

Let me reassign the reviewer since gen740 is busy for a while.
@not522 Could you review this PR?

@sawa3030
Copy link
Copy Markdown
Collaborator Author

@nabenabe0928 Thank you for your review. I updated the code based on your suggestion and I also made some changes to the unit tests of PED-ANOVA. PTAL!

@sawa3030
Copy link
Copy Markdown
Collaborator Author

@nabenabe0928 @not522 I would really appreciate it if you could take a moment to review the code. Thank you!

Copy link
Copy Markdown
Member

@not522 not522 left a comment

Choose a reason for hiding this comment

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

How about remove consider_prior from _ParzenEstimatorParameters? Since it is an internal class, we can break the compatibility.

@c-bata c-bata added the compatibility Change that breaks compatibility. label Mar 28, 2025
@sawa3030
Copy link
Copy Markdown
Collaborator Author

@not522 Thank you for your advice. I decided to remove consider_prior from _ParzenEstimatorParameters, _ParzenEstimator, and _ScottParzenEstimator. PTAL!


class _ParzenEstimatorParameters(NamedTuple):
consider_prior: bool
prior_weight: float | None
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.

How about remove None from the prior_weight type?

Suggested change
prior_weight: float | None
prior_weight: float

)

self._parzen_estimator_parameters = _ParzenEstimatorParameters(
consider_prior=True,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could you simply remove only this line and keep the keyword argument as is?

# Prior is used for regularization.
self._consider_prior = True
# Control the regularization effect.
# Control the regularization effect of prior.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
# Control the regularization effect of prior.
# Control the regularization effect by prior.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I still need to check the changes in this file.

@sawa3030
Copy link
Copy Markdown
Collaborator Author

sawa3030 commented Apr 2, 2025

I’ve made some modifications based on your suggestion. PTAL! @not522 @nabenabe0928

Copy link
Copy Markdown
Member

@not522 not522 left a comment

Choose a reason for hiding this comment

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

Thank you for your update. It's almost LGTM.
I left nit comments. Please check them.

raise ValueError("Prior weight must be specified when consider_prior==True.")
elif parameters.prior_weight <= 0:
raise ValueError("Prior weight must be positive.")
if parameters.prior_weight is None or parameters.prior_weight <= 0:
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.

Suggested change
if parameters.prior_weight is None or parameters.prior_weight <= 0:
if parameters.prior_weight <= 0:


n_kernels = len(observations) + parameters.consider_prior
n_kernels = len(observations) + 1 # NOTE(sawa3030): +1 for prior.
assert parameters.prior_weight is not None
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.

Suggested change
assert parameters.prior_weight is not None

@sawa3030
Copy link
Copy Markdown
Collaborator Author

sawa3030 commented Apr 3, 2025

@not522 Thank you for your support. I've made the updates accordingly.

Copy link
Copy Markdown
Member

@not522 not522 left a comment

Choose a reason for hiding this comment

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

LGTM!

@not522 not522 removed their assignment Apr 3, 2025
counts=counts,
consider_prior=False,
prior_weight=0.0,
prior_weight=1.0,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I believe this part should still test prior_weight=0.0 to check whether PED-ANOVA works with prior_weight==0.0.

counts=_counts,
consider_prior=False,
prior_weight=0.0,
prior_weight=1.0,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
prior_weight=1.0,
prior_weight=0.0,

Copy link
Copy Markdown
Contributor

@nabenabe0928 nabenabe0928 left a comment

Choose a reason for hiding this comment

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

Sorry for the late response 🙇
I left some comments on the test file for PED-ANOVA.
Basically, PED-ANOVA tests prior_weight=0.0 just because the original paper uses prior_weight=0.0 actually.

counts=_counts,
consider_prior=False,
prior_weight=0.0,
prior_weight=1.0,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
prior_weight=1.0,
prior_weight=0.0,

@sawa3030 sawa3030 force-pushed the set-consider-prior-true branch from 6be7b57 to a8e0ab2 Compare April 8, 2025 12:34
@sawa3030 sawa3030 force-pushed the set-consider-prior-true branch from a8e0ab2 to 6329097 Compare April 8, 2025 23:18
@sawa3030
Copy link
Copy Markdown
Collaborator Author

sawa3030 commented Apr 9, 2025

@nabenabe0928 I modified _ParzenEstimator to support prior_weight = 0.0. However, I'm a bit concerned that this change implicitly allow consider_prior = False. Thank you very much for your continued support.

@nabenabe0928
Copy link
Copy Markdown
Contributor

I'm a bit concerned that this change implicitly allows consider_prior = False.

In fact, PED-ANOVA was originally expected to be used without prior and prior_weight=0.0 is an edge case, so it should be tested anyways although only advanced users use prior_weight=0.0.

@sawa3030
Copy link
Copy Markdown
Collaborator Author

@nabenabe0928 Thank you for your advice. I think I made the necessary updates. PTAL!

Copy link
Copy Markdown
Contributor

@nabenabe0928 nabenabe0928 left a comment

Choose a reason for hiding this comment

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

Thank you very much for the hard work on this PR, LGTM:)

@nabenabe0928 nabenabe0928 merged commit 5770d7a into optuna:master Apr 11, 2025
14 checks passed
@nabenabe0928 nabenabe0928 added this to the v4.4.0 milestone Apr 16, 2025
@sawa3030 sawa3030 deleted the set-consider-prior-true branch November 14, 2025 07:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

compatibility Change that breaks compatibility.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants