Repository navigation
Introduce PedAnovaBuilder for PedAnovaImportanceEvaluator #262
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -77,6 +77,47 @@ pub struct PedAnovaImportanceEvaluator { | |||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| impl Default for PedAnovaImportanceEvaluator { | ||||||||||||||||||||||||||||||||||||||||||||
| fn default() -> Self { | ||||||||||||||||||||||||||||||||||||||||||||
| PedAnovaBuilder::new() | ||||||||||||||||||||||||||||||||||||||||||||
| .build() | ||||||||||||||||||||||||||||||||||||||||||||
| .expect("the default configuration of PedAnovaBuilder is valid") | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| /// Builder for [`PedAnovaImportanceEvaluator`], following the API style of | ||||||||||||||||||||||||||||||||||||||||||||
| /// [`std::thread::Builder`]. | ||||||||||||||||||||||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||||||||||||||||||||||
| /// # Examples | ||||||||||||||||||||||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||||||||||||||||||||||
| /// ``` | ||||||||||||||||||||||||||||||||||||||||||||
| /// use rustuna_importance::PedAnovaBuilder; | ||||||||||||||||||||||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||||||||||||||||||||||
| /// let evaluator = PedAnovaBuilder::new() | ||||||||||||||||||||||||||||||||||||||||||||
| /// .target_quantile(0.2) | ||||||||||||||||||||||||||||||||||||||||||||
| /// .region_quantile(0.9) | ||||||||||||||||||||||||||||||||||||||||||||
| /// .evaluate_on_local(false) | ||||||||||||||||||||||||||||||||||||||||||||
| /// .n_steps(100) | ||||||||||||||||||||||||||||||||||||||||||||
| /// .prior_weight(2.0) | ||||||||||||||||||||||||||||||||||||||||||||
| /// .min_n_trials_in_regime(3) | ||||||||||||||||||||||||||||||||||||||||||||
| /// .build() | ||||||||||||||||||||||||||||||||||||||||||||
| /// .unwrap(); | ||||||||||||||||||||||||||||||||||||||||||||
| /// ``` | ||||||||||||||||||||||||||||||||||||||||||||
| #[derive(Clone, Debug)] | ||||||||||||||||||||||||||||||||||||||||||||
| pub struct PedAnovaBuilder { | ||||||||||||||||||||||||||||||||||||||||||||
| target_quantile: f64, | ||||||||||||||||||||||||||||||||||||||||||||
| region_quantile: f64, | ||||||||||||||||||||||||||||||||||||||||||||
| evaluate_on_local: bool, | ||||||||||||||||||||||||||||||||||||||||||||
| n_steps: usize, | ||||||||||||||||||||||||||||||||||||||||||||
| prior_weight: f64, | ||||||||||||||||||||||||||||||||||||||||||||
| min_n_trials_in_regime: usize, | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| impl Default for PedAnovaBuilder { | ||||||||||||||||||||||||||||||||||||||||||||
| fn default() -> Self { | ||||||||||||||||||||||||||||||||||||||||||||
| Self::new() | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| impl PedAnovaBuilder { | ||||||||||||||||||||||||||||||||||||||||||||
| /// Creates a builder with the default configuration. | ||||||||||||||||||||||||||||||||||||||||||||
| pub fn new() -> Self { | ||||||||||||||||||||||||||||||||||||||||||||
| Self { | ||||||||||||||||||||||||||||||||||||||||||||
| target_quantile: 0.1, | ||||||||||||||||||||||||||||||||||||||||||||
| region_quantile: 1.0, | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -86,6 +127,102 @@ impl Default for PedAnovaImportanceEvaluator { | |||||||||||||||||||||||||||||||||||||||||||
| min_n_trials_in_regime: 2, | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| /// Sets the top fraction of completed trials used as the target region. | ||||||||||||||||||||||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||||||||||||||||||||||
| /// For example, `0.1` evaluates which parameters were important for achieving the top | ||||||||||||||||||||||||||||||||||||||||||||
| /// 10% of observed objective values. | ||||||||||||||||||||||||||||||||||||||||||||
| pub fn target_quantile(self, target_quantile: f64) -> Self { | ||||||||||||||||||||||||||||||||||||||||||||
| Self { | ||||||||||||||||||||||||||||||||||||||||||||
| target_quantile, | ||||||||||||||||||||||||||||||||||||||||||||
| ..self | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| /// Sets the reference region against which the target region is compared. | ||||||||||||||||||||||||||||||||||||||||||||
| pub fn region_quantile(self, region_quantile: f64) -> Self { | ||||||||||||||||||||||||||||||||||||||||||||
| Self { | ||||||||||||||||||||||||||||||||||||||||||||
| region_quantile, | ||||||||||||||||||||||||||||||||||||||||||||
| ..self | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| /// Sets whether the reference density is estimated from the explored region (`true`) | ||||||||||||||||||||||||||||||||||||||||||||
| /// or from the full search space (`false`). | ||||||||||||||||||||||||||||||||||||||||||||
| pub fn evaluate_on_local(self, evaluate_on_local: bool) -> Self { | ||||||||||||||||||||||||||||||||||||||||||||
| Self { | ||||||||||||||||||||||||||||||||||||||||||||
| evaluate_on_local, | ||||||||||||||||||||||||||||||||||||||||||||
| ..self | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| /// Sets the number of grid steps used by the Parzen estimators. | ||||||||||||||||||||||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||||||||||||||||||||||
| /// Must be at least 2. | ||||||||||||||||||||||||||||||||||||||||||||
| pub fn n_steps(self, n_steps: usize) -> Self { | ||||||||||||||||||||||||||||||||||||||||||||
| Self { n_steps, ..self } | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| /// Sets the prior weight used by the Parzen estimators. | ||||||||||||||||||||||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||||||||||||||||||||||
| /// Must be finite and non-negative. | ||||||||||||||||||||||||||||||||||||||||||||
| pub fn prior_weight(self, prior_weight: f64) -> Self { | ||||||||||||||||||||||||||||||||||||||||||||
| Self { | ||||||||||||||||||||||||||||||||||||||||||||
| prior_weight, | ||||||||||||||||||||||||||||||||||||||||||||
| ..self | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| /// Sets the minimum number of completed trials for a search regime to contribute to | ||||||||||||||||||||||||||||||||||||||||||||
| /// the importance. | ||||||||||||||||||||||||||||||||||||||||||||
| pub fn min_n_trials_in_regime(self, min_n_trials_in_regime: usize) -> Self { | ||||||||||||||||||||||||||||||||||||||||||||
| Self { | ||||||||||||||||||||||||||||||||||||||||||||
| min_n_trials_in_regime, | ||||||||||||||||||||||||||||||||||||||||||||
| ..self | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+159
to
+184
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
In Optuna, the latter three are kept as instance attributes so they can still be adjusted if necessary, but are deliberately omitted from the constructor arguments to avoid encouraging customization: https://github.com/optuna/optuna/blob/v5.0.0/optuna/importance/_ped_anova/evaluator.py#L177-L182 Could we follow the same approach here by removing the builder methods for |
||||||||||||||||||||||||||||||||||||||||||||
| /// Builds the evaluator. | ||||||||||||||||||||||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||||||||||||||||||||||
| /// # Errors | ||||||||||||||||||||||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||||||||||||||||||||||
| /// Returns an error when the configuration is invalid, i.e., unless | ||||||||||||||||||||||||||||||||||||||||||||
| /// `0.0 < target_quantile < region_quantile <= 1.0`, `n_steps >= 2`, and `prior_weight` is | ||||||||||||||||||||||||||||||||||||||||||||
| /// finite and non-negative. | ||||||||||||||||||||||||||||||||||||||||||||
| pub fn build(self) -> Result<PedAnovaImportanceEvaluator> { | ||||||||||||||||||||||||||||||||||||||||||||
| if !(0.0 < self.target_quantile | ||||||||||||||||||||||||||||||||||||||||||||
| && self.target_quantile < self.region_quantile | ||||||||||||||||||||||||||||||||||||||||||||
| && self.region_quantile <= 1.0) | ||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||
| return Err(Error::with_reason( | ||||||||||||||||||||||||||||||||||||||||||||
| ErrorKind::ImportanceEvaluatorError, | ||||||||||||||||||||||||||||||||||||||||||||
| "condition 0.0 < `target_quantile` < `region_quantile` <= 1.0 must be satisfied", | ||||||||||||||||||||||||||||||||||||||||||||
| )); | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| if self.n_steps < 2 { | ||||||||||||||||||||||||||||||||||||||||||||
| return Err(Error::with_reason( | ||||||||||||||||||||||||||||||||||||||||||||
| ErrorKind::ImportanceEvaluatorError, | ||||||||||||||||||||||||||||||||||||||||||||
| format!("`n_steps` must be at least 2, but got {}", self.n_steps), | ||||||||||||||||||||||||||||||||||||||||||||
| )); | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| if !(self.prior_weight.is_finite() && self.prior_weight >= 0.0) { | ||||||||||||||||||||||||||||||||||||||||||||
| return Err(Error::with_reason( | ||||||||||||||||||||||||||||||||||||||||||||
| ErrorKind::ImportanceEvaluatorError, | ||||||||||||||||||||||||||||||||||||||||||||
| format!( | ||||||||||||||||||||||||||||||||||||||||||||
| "`prior_weight` must be finite and non-negative, but got {}", | ||||||||||||||||||||||||||||||||||||||||||||
| self.prior_weight | ||||||||||||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||||||||||||
| )); | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| Ok(PedAnovaImportanceEvaluator { | ||||||||||||||||||||||||||||||||||||||||||||
| target_quantile: self.target_quantile, | ||||||||||||||||||||||||||||||||||||||||||||
| region_quantile: self.region_quantile, | ||||||||||||||||||||||||||||||||||||||||||||
| evaluate_on_local: self.evaluate_on_local, | ||||||||||||||||||||||||||||||||||||||||||||
| n_steps: self.n_steps, | ||||||||||||||||||||||||||||||||||||||||||||
| prior_weight: self.prior_weight, | ||||||||||||||||||||||||||||||||||||||||||||
| min_n_trials_in_regime: self.min_n_trials_in_regime, | ||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| impl PedAnovaImportanceEvaluator { | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -105,20 +242,11 @@ impl PedAnovaImportanceEvaluator { | |||||||||||||||||||||||||||||||||||||||||||
| region_quantile: f64, | ||||||||||||||||||||||||||||||||||||||||||||
| evaluate_on_local: bool, | ||||||||||||||||||||||||||||||||||||||||||||
| ) -> Result<Self> { | ||||||||||||||||||||||||||||||||||||||||||||
| if !(0.0 < target_quantile && target_quantile < region_quantile && region_quantile <= 1.0) { | ||||||||||||||||||||||||||||||||||||||||||||
| return Err(Error::with_reason( | ||||||||||||||||||||||||||||||||||||||||||||
| ErrorKind::ImportanceEvaluatorError, | ||||||||||||||||||||||||||||||||||||||||||||
| "condition 0.0 < `target_quantile` < `region_quantile` <= 1.0 must be satisfied", | ||||||||||||||||||||||||||||||||||||||||||||
| )); | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| Ok(Self { | ||||||||||||||||||||||||||||||||||||||||||||
| target_quantile, | ||||||||||||||||||||||||||||||||||||||||||||
| region_quantile, | ||||||||||||||||||||||||||||||||||||||||||||
| evaluate_on_local, | ||||||||||||||||||||||||||||||||||||||||||||
| n_steps: 50, | ||||||||||||||||||||||||||||||||||||||||||||
| prior_weight: 1.0, | ||||||||||||||||||||||||||||||||||||||||||||
| min_n_trials_in_regime: 2, | ||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||
| PedAnovaBuilder::new() | ||||||||||||||||||||||||||||||||||||||||||||
| .target_quantile(target_quantile) | ||||||||||||||||||||||||||||||||||||||||||||
| .region_quantile(region_quantile) | ||||||||||||||||||||||||||||||||||||||||||||
| .evaluate_on_local(evaluate_on_local) | ||||||||||||||||||||||||||||||||||||||||||||
| .build() | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| fn get_top_quantile_trials<'a>( | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -599,6 +727,58 @@ mod tests { | |||||||||||||||||||||||||||||||||||||||||||
| Ok(()) | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| #[test] | ||||||||||||||||||||||||||||||||||||||||||||
| fn test_builder() -> Result<()> { | ||||||||||||||||||||||||||||||||||||||||||||
| // A full configuration through the builder. | ||||||||||||||||||||||||||||||||||||||||||||
| let evaluator = PedAnovaBuilder::new() | ||||||||||||||||||||||||||||||||||||||||||||
| .target_quantile(0.3) | ||||||||||||||||||||||||||||||||||||||||||||
| .region_quantile(0.9) | ||||||||||||||||||||||||||||||||||||||||||||
| .evaluate_on_local(false) | ||||||||||||||||||||||||||||||||||||||||||||
| .n_steps(100) | ||||||||||||||||||||||||||||||||||||||||||||
| .prior_weight(2.0) | ||||||||||||||||||||||||||||||||||||||||||||
| .min_n_trials_in_regime(3) | ||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+737
to
+739
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||
| .build()?; | ||||||||||||||||||||||||||||||||||||||||||||
| let study = test_utils::get_study(42, 20, ObjectiveType::Single, Direction::Minimize)?; | ||||||||||||||||||||||||||||||||||||||||||||
| assert!(!evaluator.evaluate(&study)?.is_empty()); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| // The builder defaults match the documented defaults of the evaluator. | ||||||||||||||||||||||||||||||||||||||||||||
| let expected = PedAnovaBuilder::new() | ||||||||||||||||||||||||||||||||||||||||||||
| .target_quantile(0.1) | ||||||||||||||||||||||||||||||||||||||||||||
| .region_quantile(1.0) | ||||||||||||||||||||||||||||||||||||||||||||
| .evaluate_on_local(true) | ||||||||||||||||||||||||||||||||||||||||||||
| .n_steps(50) | ||||||||||||||||||||||||||||||||||||||||||||
| .prior_weight(1.0) | ||||||||||||||||||||||||||||||||||||||||||||
| .min_n_trials_in_regime(2) | ||||||||||||||||||||||||||||||||||||||||||||
| .build()?; | ||||||||||||||||||||||||||||||||||||||||||||
| assert_eq!( | ||||||||||||||||||||||||||||||||||||||||||||
| PedAnovaImportanceEvaluator::default().evaluate(&study)?, | ||||||||||||||||||||||||||||||||||||||||||||
| expected.evaluate(&study)? | ||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||
| assert_eq!( | ||||||||||||||||||||||||||||||||||||||||||||
| PedAnovaBuilder::new().build()?.evaluate(&study)?, | ||||||||||||||||||||||||||||||||||||||||||||
| expected.evaluate(&study)? | ||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| // Invalid configurations are rejected by `build`. | ||||||||||||||||||||||||||||||||||||||||||||
| let invalid = [ | ||||||||||||||||||||||||||||||||||||||||||||
| PedAnovaBuilder::new() | ||||||||||||||||||||||||||||||||||||||||||||
| .target_quantile(0.5) | ||||||||||||||||||||||||||||||||||||||||||||
| .region_quantile(0.3), | ||||||||||||||||||||||||||||||||||||||||||||
| PedAnovaBuilder::new().target_quantile(0.0), | ||||||||||||||||||||||||||||||||||||||||||||
| PedAnovaBuilder::new().region_quantile(1.5), | ||||||||||||||||||||||||||||||||||||||||||||
| PedAnovaBuilder::new().n_steps(0), | ||||||||||||||||||||||||||||||||||||||||||||
| PedAnovaBuilder::new().n_steps(1), | ||||||||||||||||||||||||||||||||||||||||||||
| PedAnovaBuilder::new().prior_weight(-1.0), | ||||||||||||||||||||||||||||||||||||||||||||
| PedAnovaBuilder::new().prior_weight(f64::NAN), | ||||||||||||||||||||||||||||||||||||||||||||
| PedAnovaBuilder::new().prior_weight(f64::INFINITY), | ||||||||||||||||||||||||||||||||||||||||||||
| ]; | ||||||||||||||||||||||||||||||||||||||||||||
| for builder in invalid { | ||||||||||||||||||||||||||||||||||||||||||||
| assert!(builder.clone().build().is_err(), "{builder:?}"); | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| assert!(PedAnovaBuilder::new().prior_weight(0.0).build().is_ok()); | ||||||||||||||||||||||||||||||||||||||||||||
| Ok(()) | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| #[test] | ||||||||||||||||||||||||||||||||||||||||||||
| fn test_conditional() -> Result<()> { | ||||||||||||||||||||||||||||||||||||||||||||
| let study = test_utils::get_study(42, 20, ObjectiveType::Conditional, Direction::Minimize)?; | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ditto