scirs2_interpolate/extrapolation_modules/
config.rs

1//! Configuration structures for extrapolation methods
2//!
3//! This module contains all configuration-related types used to customize
4//! extrapolation behavior across different methods and scenarios.
5
6use scirs2_core::numeric::{Float, FromPrimitive};
7use std::collections::HashMap;
8
9use super::types::{ARFittingMethod, AdaptiveSelectionCriterion, EnsembleCombinationStrategy};
10
11/// Parameters for specialized extrapolation methods
12#[derive(Debug, Clone)]
13pub struct ExtrapolationParameters<T: Float> {
14    /// Decay/growth rate for exponential extrapolation
15    pub exponential_rate: T,
16
17    /// Offset for exponential extrapolation
18    pub exponential_offset: T,
19
20    /// Exponent for power law extrapolation
21    pub power_exponent: T,
22
23    /// Scale factor for power law extrapolation
24    pub power_scale: T,
25
26    /// Period for periodic extrapolation
27    pub period: T,
28}
29
30impl<T: Float> Default for ExtrapolationParameters<T> {
31    fn default() -> Self {
32        Self {
33            exponential_rate: T::one(),
34            exponential_offset: T::zero(),
35            power_exponent: -T::one(), // Default to 1/x decay
36            power_scale: T::one(),
37            period: T::from(2.0 * std::f64::consts::PI).unwrap(),
38        }
39    }
40}
41
42impl<T: Float> ExtrapolationParameters<T> {
43    /// Creates default parameters for extrapolation methods
44    pub fn new() -> Self {
45        Self::default()
46    }
47
48    /// Set the decay/growth rate for exponential extrapolation
49    pub fn with_exponential_rate(mut self, rate: T) -> Self {
50        self.exponential_rate = rate;
51        self
52    }
53
54    /// Set the offset for exponential extrapolation
55    pub fn with_exponential_offset(mut self, offset: T) -> Self {
56        self.exponential_offset = offset;
57        self
58    }
59
60    /// Set the exponent for power law extrapolation
61    pub fn with_power_exponent(mut self, exponent: T) -> Self {
62        self.power_exponent = exponent;
63        self
64    }
65
66    /// Set the scale factor for power law extrapolation
67    pub fn with_power_scale(mut self, scale: T) -> Self {
68        self.power_scale = scale;
69        self
70    }
71
72    /// Set the period for periodic extrapolation
73    pub fn with_period(mut self, period: T) -> Self {
74        self.period = period;
75        self
76    }
77}
78
79/// Configuration for confidence-based extrapolation
80#[derive(Debug, Clone)]
81pub struct ConfidenceExtrapolationConfig<T: Float> {
82    /// Number of bootstrap samples for uncertainty estimation
83    pub bootstrap_samples: usize,
84
85    /// Confidence level (e.g., 0.95 for 95% confidence intervals)
86    pub confidence_level: T,
87
88    /// Maximum extrapolation distance as multiple of domain size
89    pub max_extrapolation_ratio: T,
90
91    /// Whether to use bias correction in bootstrap
92    pub bias_correction: bool,
93}
94
95impl<T: Float> Default for ConfidenceExtrapolationConfig<T> {
96    fn default() -> Self {
97        Self {
98            bootstrap_samples: 1000,
99            confidence_level: T::from(0.95).unwrap(),
100            max_extrapolation_ratio: T::from(0.5).unwrap(),
101            bias_correction: true,
102        }
103    }
104}
105
106/// Result from confidence-based extrapolation
107#[derive(Debug, Clone)]
108pub struct ConfidenceExtrapolationResult<T: Float> {
109    /// Predicted value
110    pub value: T,
111    /// Lower confidence bound
112    pub lower_bound: T,
113    /// Upper confidence bound
114    pub upper_bound: T,
115    /// Confidence level used
116    pub confidence_level: T,
117}
118
119/// Configuration for ensemble extrapolation
120#[derive(Debug, Clone)]
121pub struct EnsembleExtrapolationConfig<T: Float> {
122    /// Methods to include in the ensemble
123    pub methods: Vec<super::types::ExtrapolationMethod>,
124    /// Combination strategy
125    pub combination_strategy: EnsembleCombinationStrategy,
126    /// Weights for weighted combination (if applicable)
127    pub weights: Option<Vec<T>>,
128    /// Whether to include confidence estimation
129    pub include_confidence: bool,
130}
131
132impl<T: Float> Default for EnsembleExtrapolationConfig<T> {
133    fn default() -> Self {
134        Self {
135            methods: vec![
136                super::types::ExtrapolationMethod::Linear,
137                super::types::ExtrapolationMethod::Quadratic,
138                super::types::ExtrapolationMethod::Cubic,
139            ],
140            combination_strategy: EnsembleCombinationStrategy::Mean,
141            weights: None,
142            include_confidence: true,
143        }
144    }
145}
146
147/// Configuration for adaptive extrapolation
148#[derive(Debug, Clone)]
149pub struct AdaptiveExtrapolationConfig {
150    /// Selection criterion for choosing methods
151    pub selection_criterion: AdaptiveSelectionCriterion,
152    /// Number of nearby points to consider for local analysis
153    pub local_window_size: usize,
154    /// Minimum confidence required to use a method
155    pub minimum_confidence: f64,
156    /// Whether to cache method selections for performance
157    pub cache_selections: bool,
158}
159
160impl Default for AdaptiveExtrapolationConfig {
161    fn default() -> Self {
162        Self {
163            selection_criterion: AdaptiveSelectionCriterion::CrossValidationError,
164            local_window_size: 10,
165            minimum_confidence: 0.5,
166            cache_selections: true,
167        }
168    }
169}
170
171/// Configuration for autoregressive extrapolation
172#[derive(Debug, Clone)]
173pub struct AutoregressiveExtrapolationConfig<T: Float> {
174    /// Order of the autoregressive model
175    pub ar_order: usize,
176    /// Fitting method for AR parameters
177    pub fitting_method: ARFittingMethod,
178    /// Maximum number of time steps to extrapolate
179    pub max_steps: usize,
180    /// Whether to apply trend adjustment
181    pub trend_adjustment: bool,
182    /// Regularization parameter for AR fitting
183    pub regularization: T,
184}
185
186impl<T: Float> Default for AutoregressiveExtrapolationConfig<T> {
187    fn default() -> Self {
188        Self {
189            ar_order: 3,
190            fitting_method: ARFittingMethod::YuleWalker,
191            max_steps: 100,
192            trend_adjustment: true,
193            regularization: T::from(1e-6).unwrap_or(T::zero()),
194        }
195    }
196}
197
198/// Configuration builder for complex extrapolation setups
199#[derive(Debug, Clone)]
200pub struct ExtrapolationConfigBuilder<T: Float> {
201    /// Base parameters
202    pub parameters: ExtrapolationParameters<T>,
203    /// Confidence configuration
204    pub confidence_config: Option<ConfidenceExtrapolationConfig<T>>,
205    /// Ensemble configuration
206    pub ensemble_config: Option<EnsembleExtrapolationConfig<T>>,
207    /// Adaptive configuration
208    pub adaptive_config: Option<AdaptiveExtrapolationConfig>,
209    /// Autoregressive configuration
210    pub ar_config: Option<AutoregressiveExtrapolationConfig<T>>,
211    /// Custom method-specific parameters
212    pub custom_parameters: HashMap<String, T>,
213}
214
215impl<T: Float> Default for ExtrapolationConfigBuilder<T> {
216    fn default() -> Self {
217        Self {
218            parameters: ExtrapolationParameters::default(),
219            confidence_config: None,
220            ensemble_config: None,
221            adaptive_config: None,
222            ar_config: None,
223            custom_parameters: HashMap::new(),
224        }
225    }
226}
227
228impl<T: Float> ExtrapolationConfigBuilder<T> {
229    /// Create a new configuration builder
230    pub fn new() -> Self {
231        Self::default()
232    }
233
234    /// Set base extrapolation parameters
235    pub fn with_parameters(mut self, parameters: ExtrapolationParameters<T>) -> Self {
236        self.parameters = parameters;
237        self
238    }
239
240    /// Enable confidence-based extrapolation
241    pub fn with_confidence(mut self, config: ConfidenceExtrapolationConfig<T>) -> Self {
242        self.confidence_config = Some(config);
243        self
244    }
245
246    /// Enable ensemble extrapolation
247    pub fn with_ensemble(mut self, config: EnsembleExtrapolationConfig<T>) -> Self {
248        self.ensemble_config = Some(config);
249        self
250    }
251
252    /// Enable adaptive extrapolation
253    pub fn with_adaptive(mut self, config: AdaptiveExtrapolationConfig) -> Self {
254        self.adaptive_config = Some(config);
255        self
256    }
257
258    /// Enable autoregressive extrapolation
259    pub fn with_autoregressive(mut self, config: AutoregressiveExtrapolationConfig<T>) -> Self {
260        self.ar_config = Some(config);
261        self
262    }
263
264    /// Add custom parameter
265    pub fn with_custom_parameter(mut self, name: String, value: T) -> Self {
266        self.custom_parameters.insert(name, value);
267        self
268    }
269
270    /// Build the final configuration
271    pub fn build(self) -> ExtrapolationConfig<T> {
272        ExtrapolationConfig {
273            parameters: self.parameters,
274            confidence_config: self.confidence_config,
275            ensemble_config: self.ensemble_config,
276            adaptive_config: self.adaptive_config,
277            ar_config: self.ar_config,
278            custom_parameters: self.custom_parameters,
279        }
280    }
281}
282
283/// Complete extrapolation configuration
284#[derive(Debug, Clone)]
285pub struct ExtrapolationConfig<T: Float> {
286    /// Base parameters
287    pub parameters: ExtrapolationParameters<T>,
288    /// Confidence configuration
289    pub confidence_config: Option<ConfidenceExtrapolationConfig<T>>,
290    /// Ensemble configuration
291    pub ensemble_config: Option<EnsembleExtrapolationConfig<T>>,
292    /// Adaptive configuration
293    pub adaptive_config: Option<AdaptiveExtrapolationConfig>,
294    /// Autoregressive configuration
295    pub ar_config: Option<AutoregressiveExtrapolationConfig<T>>,
296    /// Custom method-specific parameters
297    pub custom_parameters: HashMap<String, T>,
298}
299
300impl<T: Float> Default for ExtrapolationConfig<T> {
301    fn default() -> Self {
302        Self {
303            parameters: ExtrapolationParameters::default(),
304            confidence_config: None,
305            ensemble_config: None,
306            adaptive_config: None,
307            ar_config: None,
308            custom_parameters: HashMap::new(),
309        }
310    }
311}
312
313impl<T: Float> ExtrapolationConfig<T> {
314    /// Create a new configuration with default values
315    pub fn new() -> Self {
316        Self::default()
317    }
318
319    /// Create a configuration builder
320    pub fn builder() -> ExtrapolationConfigBuilder<T> {
321        ExtrapolationConfigBuilder::new()
322    }
323
324    /// Check if confidence estimation is enabled
325    pub fn has_confidence(&self) -> bool {
326        self.confidence_config.is_some()
327    }
328
329    /// Check if ensemble methods are enabled
330    pub fn has_ensemble(&self) -> bool {
331        self.ensemble_config.is_some()
332    }
333
334    /// Check if adaptive selection is enabled
335    pub fn has_adaptive(&self) -> bool {
336        self.adaptive_config.is_some()
337    }
338
339    /// Check if autoregressive modeling is enabled
340    pub fn has_autoregressive(&self) -> bool {
341        self.ar_config.is_some()
342    }
343
344    /// Get custom parameter value
345    pub fn get_custom_parameter(&self, name: &str) -> Option<&T> {
346        self.custom_parameters.get(name)
347    }
348}