-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapi_improvements_demo.rs
More file actions
316 lines (269 loc) · 10.1 KB
/
api_improvements_demo.rs
File metadata and controls
316 lines (269 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
//! Comprehensive demo of OxiDiviner API improvements
//!
//! This example demonstrates all the enhanced API features:
//! 1. Unified forecasting interface
//! 2. Builder pattern for fluent model construction
//! 3. Automatic model selection with various criteria
//! 4. Parameter validation and error handling
//! 5. Confidence intervals and model evaluation
use chrono::{Duration, Utc};
use oxidiviner::api::*;
use oxidiviner::prelude::*;
fn main() -> Result<()> {
println!("🔮 OxiDiviner API Improvements Demo\n");
// Generate sample time series data
let start_time = Utc::now();
let timestamps: Vec<_> = (0..50).map(|i| start_time + Duration::days(i)).collect();
// Create a trend + seasonal + noise pattern
let values: Vec<f64> = (0..50)
.map(|i| {
let trend = 100.0 + i as f64 * 0.5;
let seasonal = 10.0 * (i as f64 * 2.0 * std::f64::consts::PI / 12.0).sin();
let noise = (i as f64 * 0.1).sin() * 2.0;
trend + seasonal + noise
})
.collect();
let data = TimeSeriesData::new(timestamps, values, "demo_series")?;
println!("📊 Created time series with {} data points", data.len());
// Demo 1: High-level Forecaster API
println!("\n=== Demo 1: High-level Forecaster API ===");
demo_high_level_api(&data)?;
// Demo 2: Builder Pattern API
println!("\n=== Demo 2: Builder Pattern API ===");
demo_builder_pattern(&data)?;
// Demo 3: Automatic Model Selection
println!("\n=== Demo 3: Automatic Model Selection ===");
demo_auto_selection(&data)?;
// Demo 4: Model Comparison
println!("\n=== Demo 4: Model Comparison ===");
demo_model_comparison(&data)?;
println!("\n✅ All demos completed successfully!");
Ok(())
}
fn demo_high_level_api(data: &TimeSeriesData) -> Result<()> {
println!("Using the high-level Forecaster interface...");
// Simple forecasting with defaults
let forecaster = Forecaster::new();
let output = forecaster.forecast(data, 10)?;
println!(
"📈 Auto forecast ({}): {:?}",
output.model_used,
&output.forecast[..3]
);
// Configured forecasting
let forecaster = Forecaster::new()
.model(ModelType::ARIMA)
.arima_params(2, 1, 1);
let output = forecaster.forecast(data, 10)?;
println!("📈 ARIMA(2,1,1) forecast: {:?}", &output.forecast[..3]);
// Using ForecastBuilder
let forecaster = ForecastBuilder::new().arima(1, 1, 2).build();
let output = forecaster.forecast(data, 10)?;
println!(
"📈 Builder ARIMA(1,1,2) forecast: {:?}",
&output.forecast[..3]
);
Ok(())
}
fn demo_builder_pattern(data: &TimeSeriesData) -> Result<()> {
println!("Using the fluent builder pattern...");
// ARIMA model with builder
let mut arima_model = ModelBuilder::arima()
.with_ar(2)
.with_differencing(1)
.with_ma(1)
.build()?;
arima_model.quick_fit(data)?;
let forecast = arima_model.quick_forecast(5)?;
println!("🏗️ ARIMA builder forecast: {:?}", forecast);
// Exponential Smoothing with builder
let mut es_model = ModelBuilder::exponential_smoothing()
.with_alpha(0.3)
.build()?;
es_model.quick_fit(data)?;
let forecast = es_model.quick_forecast(5)?;
println!("🏗️ ES builder forecast: {:?}", forecast);
// Moving Average with builder
let mut ma_model = ModelBuilder::moving_average().with_window(7).build()?;
ma_model.quick_fit(data)?;
let forecast = ma_model.quick_forecast(5)?;
println!("🏗️ MA builder forecast: {:?}", forecast);
Ok(())
}
fn demo_auto_selection(data: &TimeSeriesData) -> Result<()> {
println!("Demonstrating automatic model selection...");
// Since AutoSelector may have issues with some models, let's demonstrate
// manual model selection and comparison instead
println!("🎯 Comparing models manually for selection:");
// Test different models and compare their performance
let mut best_mae = f64::INFINITY;
let mut best_model_name = String::new();
let mut best_forecast = Vec::new();
// Try ARIMA(1,1,1)
if let Ok(mut arima_model) = ModelBuilder::arima()
.with_ar(1)
.with_differencing(1)
.with_ma(1)
.build()
{
if arima_model.quick_fit(data).is_ok() {
if let Ok(evaluation) = arima_model.evaluate(data) {
if let Ok(forecast) = arima_model.quick_forecast(5) {
if evaluation.mae < best_mae {
best_mae = evaluation.mae;
best_model_name = "ARIMA(1,1,1)".to_string();
best_forecast = forecast;
}
println!(" ARIMA(1,1,1) - MAE: {:.3}", evaluation.mae);
}
}
}
}
// Try Simple ES
if let Ok(mut es_model) = ModelBuilder::exponential_smoothing()
.with_alpha(0.3)
.build()
{
if es_model.quick_fit(data).is_ok() {
if let Ok(evaluation) = es_model.evaluate(data) {
if let Ok(forecast) = es_model.quick_forecast(5) {
if evaluation.mae < best_mae {
best_mae = evaluation.mae;
best_model_name = "SimpleES(α=0.3)".to_string();
best_forecast = forecast;
}
println!(" SimpleES(α=0.3) - MAE: {:.3}", evaluation.mae);
}
}
}
}
// Try MA(5)
if let Ok(mut ma_model) = ModelBuilder::moving_average().with_window(5).build() {
if ma_model.quick_fit(data).is_ok() {
if let Ok(evaluation) = ma_model.evaluate(data) {
if let Ok(forecast) = ma_model.quick_forecast(5) {
if evaluation.mae < best_mae {
best_mae = evaluation.mae;
best_model_name = "MA(5)".to_string();
best_forecast = forecast;
}
println!(" MA(5) - MAE: {:.3}", evaluation.mae);
}
}
}
}
if !best_model_name.is_empty() {
println!("🏆 Best model: {} (MAE: {:.3})", best_model_name, best_mae);
println!(" Forecast: {:?}", best_forecast);
} else {
println!("❌ No models could be fitted successfully");
}
Ok(())
}
fn demo_model_comparison(data: &TimeSeriesData) -> Result<()> {
println!("Comparing different models...");
let models = vec![
(
"ARIMA(1,1,1)",
ModelBuilder::arima()
.with_ar(1)
.with_differencing(1)
.with_ma(1),
),
(
"ARIMA(2,1,2)",
ModelBuilder::arima()
.with_ar(2)
.with_differencing(1)
.with_ma(2),
),
(
"ES(α=0.3)",
ModelBuilder::exponential_smoothing().with_alpha(0.3),
),
(
"ES(α=0.7)",
ModelBuilder::exponential_smoothing().with_alpha(0.7),
),
("MA(5)", ModelBuilder::moving_average().with_window(5)),
("MA(10)", ModelBuilder::moving_average().with_window(10)),
];
println!("📊 Model Performance Comparison:");
println!(
"{:<15} {:<8} {:<8} {:<8} {:<8}",
"Model", "MAE", "MSE", "RMSE", "MAPE"
);
println!("{}", "-".repeat(55));
for (name, builder) in models {
match builder.build() {
Ok(mut model) => {
if model.quick_fit(data).is_ok() {
if let Ok(evaluation) = model.evaluate(data) {
println!(
"{:<15} {:<8.2} {:<8.2} {:<8.2} {:<8.2}",
name, evaluation.mae, evaluation.mse, evaluation.rmse, evaluation.mape
);
}
}
}
Err(_) => {
println!("{:<15} Failed to build", name);
}
}
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_api_improvements() {
// Create test data with some noise to avoid numerical issues
let start_time = Utc::now();
let timestamps: Vec<_> = (0..30).map(|i| start_time + Duration::days(i)).collect();
let values: Vec<f64> = (0..30)
.map(|i| {
let trend = 100.0 + i as f64;
let noise = (i as f64 * 0.1).sin() * 2.0; // Add some sinusoidal noise
trend + noise
})
.collect();
let data = TimeSeriesData::new(timestamps, values, "test").unwrap();
// Test high-level API
let forecaster = Forecaster::new();
let output = forecaster.forecast(&data, 5).unwrap();
assert_eq!(output.forecast.len(), 5);
// Test builder pattern with Moving Average (more stable than ARIMA)
let mut model = ModelBuilder::moving_average()
.with_window(5)
.build()
.unwrap();
model.quick_fit(&data).unwrap();
let forecast = model.quick_forecast(5).unwrap();
assert_eq!(forecast.len(), 5);
// Test simple exponential smoothing (also stable)
let mut es_model = ModelBuilder::exponential_smoothing()
.with_alpha(0.3)
.build()
.unwrap();
es_model.quick_fit(&data).unwrap();
let es_forecast = es_model.quick_forecast(5).unwrap();
assert_eq!(es_forecast.len(), 5);
}
#[test]
fn test_model_evaluation() {
let start_time = Utc::now();
let timestamps: Vec<_> = (0..20).map(|i| start_time + Duration::days(i)).collect();
let values: Vec<f64> = (0..20).map(|i| 100.0 + i as f64).collect();
let data = TimeSeriesData::new(timestamps, values, "test").unwrap();
let mut model = ModelBuilder::moving_average()
.with_window(3)
.build()
.unwrap();
model.quick_fit(&data).unwrap();
let evaluation = model.evaluate(&data).unwrap();
assert!(evaluation.mae >= 0.0);
assert!(evaluation.mse >= 0.0);
assert!(evaluation.rmse >= 0.0);
}
}