From 66d764aa419e4f789be10481058a7e95e5d155f5 Mon Sep 17 00:00:00 2001 From: Hemmi Shinichi Date: Mon, 28 Sep 2026 23:50:58 +0900 Subject: [PATCH 1/3] Introduce PedAnovaBuilder for PedAnovaImportanceEvaluator --- rustuna_importance/src/lib.rs | 2 +- rustuna_importance/src/ped_anova.rs | 192 ++++++++++++++++++++++++++-- rustuna_pyo3/src/importance.rs | 9 +- 3 files changed, 185 insertions(+), 18 deletions(-) diff --git a/rustuna_importance/src/lib.rs b/rustuna_importance/src/lib.rs index 68774a13..a523097e 100644 --- a/rustuna_importance/src/lib.rs +++ b/rustuna_importance/src/lib.rs @@ -12,4 +12,4 @@ pub(crate) mod test_utils; pub use common::{ get_param_importances, get_param_importances_with, ImportanceEvaluator, ImportanceOptions, }; -pub use ped_anova::PedAnovaImportanceEvaluator; +pub use ped_anova::{PedAnovaBuilder, PedAnovaImportanceEvaluator}; diff --git a/rustuna_importance/src/ped_anova.rs b/rustuna_importance/src/ped_anova.rs index a7a302a8..00109326 100644 --- a/rustuna_importance/src/ped_anova.rs +++ b/rustuna_importance/src/ped_anova.rs @@ -88,7 +88,148 @@ impl Default for PedAnovaImportanceEvaluator { } } +/// Builder for [`PedAnovaImportanceEvaluator`], following the API style of +/// [`std::thread::Builder`]. +/// +/// # Examples +/// +/// ``` +/// use rustuna_importance::PedAnovaImportanceEvaluator; +/// +/// let evaluator = PedAnovaImportanceEvaluator::builder() +/// .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(); +/// ``` +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, + evaluate_on_local: true, + n_steps: 50, + prior_weight: 1.0, + 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. + pub fn n_steps(self, n_steps: usize) -> Self { + Self { n_steps, ..self } + } + + /// Sets the prior weight used by the Parzen estimators. + 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 + } + } + + /// Builds the evaluator. + /// + /// # Errors + /// + /// Returns an error when the configuration is invalid. + pub fn build(self) -> Result { + 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", + )); + } + 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 { + /// Returns a builder for creating an evaluator with an explicit configuration. + /// + /// This is the counterpart of [`std::thread::Builder`]: settings are configured by + /// chaining methods and the evaluator is created with [`PedAnovaBuilder::build`]. + /// + /// # Examples + /// + /// ``` + /// use rustuna_importance::PedAnovaImportanceEvaluator; + /// + /// let evaluator = PedAnovaImportanceEvaluator::builder() + /// .target_quantile(0.3) + /// .n_steps(100) + /// .build() + /// .unwrap(); + /// # let _ = evaluator; + /// ``` + pub fn builder() -> PedAnovaBuilder { + PedAnovaBuilder::new() + } + /// Creates a PED-ANOVA evaluator. /// /// `target_quantile` selects the top fraction of completed trials used as the target region. @@ -105,20 +246,11 @@ impl PedAnovaImportanceEvaluator { region_quantile: f64, evaluate_on_local: bool, ) -> Result { - 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, - }) + Self::builder() + .target_quantile(target_quantile) + .region_quantile(region_quantile) + .evaluate_on_local(evaluate_on_local) + .build() } fn get_top_quantile_trials<'a>( @@ -599,6 +731,38 @@ mod tests { Ok(()) } + #[test] + fn test_builder() -> Result<()> { + // A full configuration through the builder. + let evaluator = PedAnovaImportanceEvaluator::builder() + .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) + .build()?; + let study = test_utils::get_study(42, 20, ObjectiveType::Single, Direction::Minimize)?; + assert!(!evaluator.evaluate(&study)?.is_empty()); + + // The same configuration as `new`, with the internal defaults for the rest. + let from_new = PedAnovaImportanceEvaluator::new(0.3, 1.0, true)?; + let from_builder = PedAnovaImportanceEvaluator::builder() + .target_quantile(0.3) + .region_quantile(1.0) + .evaluate_on_local(true) + .build()?; + assert_eq!(from_new.evaluate(&study)?, from_builder.evaluate(&study)?); + + // Invalid configurations are rejected by `build`. + assert!(PedAnovaImportanceEvaluator::builder() + .target_quantile(0.5) + .region_quantile(0.3) + .build() + .is_err()); + Ok(()) + } + #[test] fn test_conditional() -> Result<()> { let study = test_utils::get_study(42, 20, ObjectiveType::Conditional, Direction::Minimize)?; diff --git a/rustuna_pyo3/src/importance.rs b/rustuna_pyo3/src/importance.rs index d138b6db..9145d469 100644 --- a/rustuna_pyo3/src/importance.rs +++ b/rustuna_pyo3/src/importance.rs @@ -56,9 +56,12 @@ impl PyPedAnovaImportanceEvaluator { region_quantile: f64, evaluate_on_local: bool, ) -> PyResult { - let evaluator = - PedAnovaImportanceEvaluator::new(target_quantile, region_quantile, evaluate_on_local) - .map_err(err_to_exceptions)?; + let evaluator = PedAnovaImportanceEvaluator::builder() + .target_quantile(target_quantile) + .region_quantile(region_quantile) + .evaluate_on_local(evaluate_on_local) + .build() + .map_err(err_to_exceptions)?; if region_quantile != 1.0 && !evaluate_on_local { PyErr::warn( py, From 3611f4787ffd1c7419acc31d8ff0cdd626fbe206 Mon Sep 17 00:00:00 2001 From: Hemmi Shinichi Date: Mon, 5 Oct 2026 11:21:40 +0900 Subject: [PATCH 2/3] Remove the builder() entry point from PedAnovaImportanceEvaluator --- rustuna_importance/src/ped_anova.rs | 33 ++++++----------------------- rustuna_pyo3/src/importance.rs | 5 +++-- 2 files changed, 9 insertions(+), 29 deletions(-) diff --git a/rustuna_importance/src/ped_anova.rs b/rustuna_importance/src/ped_anova.rs index 00109326..884f1b2f 100644 --- a/rustuna_importance/src/ped_anova.rs +++ b/rustuna_importance/src/ped_anova.rs @@ -94,9 +94,9 @@ impl Default for PedAnovaImportanceEvaluator { /// # Examples /// /// ``` -/// use rustuna_importance::PedAnovaImportanceEvaluator; +/// use rustuna_importance::PedAnovaBuilder; /// -/// let evaluator = PedAnovaImportanceEvaluator::builder() +/// let evaluator = PedAnovaBuilder::new() /// .target_quantile(0.2) /// .region_quantile(0.9) /// .evaluate_on_local(false) @@ -209,27 +209,6 @@ impl PedAnovaBuilder { } impl PedAnovaImportanceEvaluator { - /// Returns a builder for creating an evaluator with an explicit configuration. - /// - /// This is the counterpart of [`std::thread::Builder`]: settings are configured by - /// chaining methods and the evaluator is created with [`PedAnovaBuilder::build`]. - /// - /// # Examples - /// - /// ``` - /// use rustuna_importance::PedAnovaImportanceEvaluator; - /// - /// let evaluator = PedAnovaImportanceEvaluator::builder() - /// .target_quantile(0.3) - /// .n_steps(100) - /// .build() - /// .unwrap(); - /// # let _ = evaluator; - /// ``` - pub fn builder() -> PedAnovaBuilder { - PedAnovaBuilder::new() - } - /// Creates a PED-ANOVA evaluator. /// /// `target_quantile` selects the top fraction of completed trials used as the target region. @@ -246,7 +225,7 @@ impl PedAnovaImportanceEvaluator { region_quantile: f64, evaluate_on_local: bool, ) -> Result { - Self::builder() + PedAnovaBuilder::new() .target_quantile(target_quantile) .region_quantile(region_quantile) .evaluate_on_local(evaluate_on_local) @@ -734,7 +713,7 @@ mod tests { #[test] fn test_builder() -> Result<()> { // A full configuration through the builder. - let evaluator = PedAnovaImportanceEvaluator::builder() + let evaluator = PedAnovaBuilder::new() .target_quantile(0.3) .region_quantile(0.9) .evaluate_on_local(false) @@ -747,7 +726,7 @@ mod tests { // The same configuration as `new`, with the internal defaults for the rest. let from_new = PedAnovaImportanceEvaluator::new(0.3, 1.0, true)?; - let from_builder = PedAnovaImportanceEvaluator::builder() + let from_builder = PedAnovaBuilder::new() .target_quantile(0.3) .region_quantile(1.0) .evaluate_on_local(true) @@ -755,7 +734,7 @@ mod tests { assert_eq!(from_new.evaluate(&study)?, from_builder.evaluate(&study)?); // Invalid configurations are rejected by `build`. - assert!(PedAnovaImportanceEvaluator::builder() + assert!(PedAnovaBuilder::new() .target_quantile(0.5) .region_quantile(0.3) .build() diff --git a/rustuna_pyo3/src/importance.rs b/rustuna_pyo3/src/importance.rs index 9145d469..f9653fd5 100644 --- a/rustuna_pyo3/src/importance.rs +++ b/rustuna_pyo3/src/importance.rs @@ -6,7 +6,8 @@ use pyo3::PyResult; use rustuna_core::trial::PersistedTrial; use rustuna_importance::{ - get_param_importances_with, ImportanceEvaluator, ImportanceOptions, PedAnovaImportanceEvaluator, + get_param_importances_with, ImportanceEvaluator, ImportanceOptions, PedAnovaBuilder, + PedAnovaImportanceEvaluator, }; use crate::exception::err_to_exceptions; @@ -56,7 +57,7 @@ impl PyPedAnovaImportanceEvaluator { region_quantile: f64, evaluate_on_local: bool, ) -> PyResult { - let evaluator = PedAnovaImportanceEvaluator::builder() + let evaluator = PedAnovaBuilder::new() .target_quantile(target_quantile) .region_quantile(region_quantile) .evaluate_on_local(evaluate_on_local) From 0f80088522c1f80a1d7547e8d58a244423de8d4e Mon Sep 17 00:00:00 2001 From: Hemmi Shinichi Date: Wed, 18 Nov 2026 17:16:46 +0900 Subject: [PATCH 3/3] Validate n_steps and prior_weight in PedAnovaBuilder --- rustuna_importance/src/ped_anova.rs | 75 +++++++++++++++++++++-------- 1 file changed, 56 insertions(+), 19 deletions(-) diff --git a/rustuna_importance/src/ped_anova.rs b/rustuna_importance/src/ped_anova.rs index 884f1b2f..fffb40e8 100644 --- a/rustuna_importance/src/ped_anova.rs +++ b/rustuna_importance/src/ped_anova.rs @@ -77,14 +77,9 @@ pub struct PedAnovaImportanceEvaluator { impl Default for PedAnovaImportanceEvaluator { fn default() -> Self { - Self { - 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, - } + PedAnovaBuilder::new() + .build() + .expect("the default configuration of PedAnovaBuilder is valid") } } @@ -106,6 +101,7 @@ impl Default for PedAnovaImportanceEvaluator { /// .build() /// .unwrap(); /// ``` +#[derive(Clone, Debug)] pub struct PedAnovaBuilder { target_quantile: f64, region_quantile: f64, @@ -161,11 +157,15 @@ impl PedAnovaBuilder { } /// 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, @@ -186,7 +186,9 @@ impl PedAnovaBuilder { /// /// # Errors /// - /// Returns an error when the configuration is invalid. + /// 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 { if !(0.0 < self.target_quantile && self.target_quantile < self.region_quantile @@ -197,6 +199,21 @@ impl PedAnovaBuilder { "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, @@ -724,21 +741,41 @@ mod tests { let study = test_utils::get_study(42, 20, ObjectiveType::Single, Direction::Minimize)?; assert!(!evaluator.evaluate(&study)?.is_empty()); - // The same configuration as `new`, with the internal defaults for the rest. - let from_new = PedAnovaImportanceEvaluator::new(0.3, 1.0, true)?; - let from_builder = PedAnovaBuilder::new() - .target_quantile(0.3) + // 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!(from_new.evaluate(&study)?, from_builder.evaluate(&study)?); + 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`. - assert!(PedAnovaBuilder::new() - .target_quantile(0.5) - .region_quantile(0.3) - .build() - .is_err()); + 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(()) }