scirs2_interpolate/extrapolation_modules/
config.rs1use scirs2_core::numeric::{Float, FromPrimitive};
7use std::collections::HashMap;
8
9use super::types::{ARFittingMethod, AdaptiveSelectionCriterion, EnsembleCombinationStrategy};
10
11#[derive(Debug, Clone)]
13pub struct ExtrapolationParameters<T: Float> {
14 pub exponential_rate: T,
16
17 pub exponential_offset: T,
19
20 pub power_exponent: T,
22
23 pub power_scale: T,
25
26 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(), 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 pub fn new() -> Self {
45 Self::default()
46 }
47
48 pub fn with_exponential_rate(mut self, rate: T) -> Self {
50 self.exponential_rate = rate;
51 self
52 }
53
54 pub fn with_exponential_offset(mut self, offset: T) -> Self {
56 self.exponential_offset = offset;
57 self
58 }
59
60 pub fn with_power_exponent(mut self, exponent: T) -> Self {
62 self.power_exponent = exponent;
63 self
64 }
65
66 pub fn with_power_scale(mut self, scale: T) -> Self {
68 self.power_scale = scale;
69 self
70 }
71
72 pub fn with_period(mut self, period: T) -> Self {
74 self.period = period;
75 self
76 }
77}
78
79#[derive(Debug, Clone)]
81pub struct ConfidenceExtrapolationConfig<T: Float> {
82 pub bootstrap_samples: usize,
84
85 pub confidence_level: T,
87
88 pub max_extrapolation_ratio: T,
90
91 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#[derive(Debug, Clone)]
108pub struct ConfidenceExtrapolationResult<T: Float> {
109 pub value: T,
111 pub lower_bound: T,
113 pub upper_bound: T,
115 pub confidence_level: T,
117}
118
119#[derive(Debug, Clone)]
121pub struct EnsembleExtrapolationConfig<T: Float> {
122 pub methods: Vec<super::types::ExtrapolationMethod>,
124 pub combination_strategy: EnsembleCombinationStrategy,
126 pub weights: Option<Vec<T>>,
128 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#[derive(Debug, Clone)]
149pub struct AdaptiveExtrapolationConfig {
150 pub selection_criterion: AdaptiveSelectionCriterion,
152 pub local_window_size: usize,
154 pub minimum_confidence: f64,
156 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#[derive(Debug, Clone)]
173pub struct AutoregressiveExtrapolationConfig<T: Float> {
174 pub ar_order: usize,
176 pub fitting_method: ARFittingMethod,
178 pub max_steps: usize,
180 pub trend_adjustment: bool,
182 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#[derive(Debug, Clone)]
200pub struct ExtrapolationConfigBuilder<T: Float> {
201 pub parameters: ExtrapolationParameters<T>,
203 pub confidence_config: Option<ConfidenceExtrapolationConfig<T>>,
205 pub ensemble_config: Option<EnsembleExtrapolationConfig<T>>,
207 pub adaptive_config: Option<AdaptiveExtrapolationConfig>,
209 pub ar_config: Option<AutoregressiveExtrapolationConfig<T>>,
211 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 pub fn new() -> Self {
231 Self::default()
232 }
233
234 pub fn with_parameters(mut self, parameters: ExtrapolationParameters<T>) -> Self {
236 self.parameters = parameters;
237 self
238 }
239
240 pub fn with_confidence(mut self, config: ConfidenceExtrapolationConfig<T>) -> Self {
242 self.confidence_config = Some(config);
243 self
244 }
245
246 pub fn with_ensemble(mut self, config: EnsembleExtrapolationConfig<T>) -> Self {
248 self.ensemble_config = Some(config);
249 self
250 }
251
252 pub fn with_adaptive(mut self, config: AdaptiveExtrapolationConfig) -> Self {
254 self.adaptive_config = Some(config);
255 self
256 }
257
258 pub fn with_autoregressive(mut self, config: AutoregressiveExtrapolationConfig<T>) -> Self {
260 self.ar_config = Some(config);
261 self
262 }
263
264 pub fn with_custom_parameter(mut self, name: String, value: T) -> Self {
266 self.custom_parameters.insert(name, value);
267 self
268 }
269
270 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#[derive(Debug, Clone)]
285pub struct ExtrapolationConfig<T: Float> {
286 pub parameters: ExtrapolationParameters<T>,
288 pub confidence_config: Option<ConfidenceExtrapolationConfig<T>>,
290 pub ensemble_config: Option<EnsembleExtrapolationConfig<T>>,
292 pub adaptive_config: Option<AdaptiveExtrapolationConfig>,
294 pub ar_config: Option<AutoregressiveExtrapolationConfig<T>>,
296 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 pub fn new() -> Self {
316 Self::default()
317 }
318
319 pub fn builder() -> ExtrapolationConfigBuilder<T> {
321 ExtrapolationConfigBuilder::new()
322 }
323
324 pub fn has_confidence(&self) -> bool {
326 self.confidence_config.is_some()
327 }
328
329 pub fn has_ensemble(&self) -> bool {
331 self.ensemble_config.is_some()
332 }
333
334 pub fn has_adaptive(&self) -> bool {
336 self.adaptive_config.is_some()
337 }
338
339 pub fn has_autoregressive(&self) -> bool {
341 self.ar_config.is_some()
342 }
343
344 pub fn get_custom_parameter(&self, name: &str) -> Option<&T> {
346 self.custom_parameters.get(name)
347 }
348}