-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathquick_start.rs
More file actions
202 lines (167 loc) · 6.2 KB
/
quick_start.rs
File metadata and controls
202 lines (167 loc) · 6.2 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
//! Quick Start Guide for OxiDiviner
//!
//! This example demonstrates the easiest ways to get started with forecasting
//! using the improved OxiDiviner API.
use chrono::{DateTime, Duration, Utc};
use oxidiviner::prelude::*;
use oxidiviner::quick;
fn main() -> Result<()> {
println!("🚀 OxiDiviner Quick Start");
println!("=========================\n");
// 1. Generate some sample data
let (timestamps, values) = create_sample_data();
let data = TimeSeriesData::new(timestamps, values, "Sample Data")?;
println!("📊 Created sample data with {} points", data.values.len());
println!(
" Values: {:.2} to {:.2}\n",
data.values.iter().fold(f64::INFINITY, |a, &b| a.min(b)),
data.values.iter().fold(f64::NEG_INFINITY, |a, &b| a.max(b))
);
// 2. Quick forecasting with auto model selection
println!("🤖 Method 1: Auto Model Selection (Easiest)");
println!("--------------------------------------------");
let selector = AutoSelector::with_aic().max_models(5);
match selector.select_best(&data) {
Ok((best_model, score, model_name)) => {
println!("✓ Best model: {} (AIC: {:.2})", model_name, score);
let forecast = best_model.quick_forecast(7)?;
println!("📈 7-day forecast: {:?}\n", &forecast[..3]);
}
Err(e) => println!("⚠️ Auto-selection failed: {}\n", e),
}
// 3. Builder pattern for custom models
println!("🏗️ Method 2: Model Builder (Flexible)");
println!("---------------------------------------");
// Build an ARIMA model
let mut arima = ModelBuilder::arima()
.with_ar(2)
.with_differencing(1)
.with_ma(1)
.build()?;
arima.quick_fit(&data)?;
let arima_forecast = arima.quick_forecast(7)?;
println!("✓ ARIMA(2,1,1) forecast: {:?}", &arima_forecast[..3]);
// Build an Exponential Smoothing model
let mut es = ModelBuilder::exponential_smoothing()
.with_alpha(0.3)
.build()?;
es.quick_fit(&data)?;
let es_forecast = es.quick_forecast(7)?;
println!(
"✓ Exponential Smoothing forecast: {:?}\n",
&es_forecast[..3]
);
// 4. Quick one-liners
println!("⚡ Method 3: One-liner Functions (Fastest)");
println!("-------------------------------------------");
// Use the quick functions for immediate results
let quick_arima = quick::arima(data.clone(), 7)?;
println!("✓ Quick ARIMA: {:?}", &quick_arima[..3]);
let quick_ma = quick::moving_average(data.clone(), 7, Some(5))?;
println!("✓ Quick Moving Average: {:?}", &quick_ma[..3]);
let (auto_forecast, auto_model) = quick::auto_select(data.clone(), 7)?;
println!(
"✓ Auto selection chose: {} -> {:?}\n",
auto_model,
&auto_forecast[..3]
);
// 5. Model evaluation and comparison
println!("📊 Method 4: Model Evaluation");
println!("------------------------------");
// Compare multiple models
let models = vec![
(
"ARIMA(1,1,1)",
ModelBuilder::arima()
.with_ar(1)
.with_differencing(1)
.with_ma(1)
.build()?,
),
(
"ES(α=0.3)",
ModelBuilder::exponential_smoothing()
.with_alpha(0.3)
.build()?,
),
(
"MA(5)",
ModelBuilder::moving_average().with_window(5).build()?,
),
];
for (name, mut model) in models {
match model.quick_fit(&data) {
Ok(()) => {
if let Ok(eval) = model.evaluate(&data) {
println!("✓ {}: MSE={:.4}, MAE={:.4}", name, eval.mse, eval.mae);
}
}
Err(e) => println!("⚠️ {} failed: {}", name, e),
}
}
println!("\n🎉 Quick start complete! You can now:");
println!(" • Use AutoSelector for automatic model selection");
println!(" • Use ModelBuilder for custom configurations");
println!(" • Use quick functions for immediate results");
println!(" • Compare models with built-in evaluation metrics");
Ok(())
}
/// Create sample time series data for demonstration
fn create_sample_data() -> (Vec<DateTime<Utc>>, Vec<f64>) {
let start = Utc::now() - Duration::days(50);
let mut timestamps = Vec::new();
let mut values = Vec::new();
for i in 0..50 {
timestamps.push(start + Duration::days(i));
// Create realistic-looking data with trend and weekly pattern
let trend = 100.0 + 0.5 * i as f64;
let weekly = 10.0 * (2.0 * std::f64::consts::PI * (i % 7) as f64 / 7.0).sin();
let noise = (i % 17) as f64 * 0.3 - 2.5; // Deterministic "noise"
values.push(trend + weekly + noise);
}
(timestamps, values)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_quick_start_example() {
// Test that the quick start example runs without panicking
let result = main();
// The example should complete successfully or fail gracefully
match result {
Ok(()) => println!("Quick start example completed successfully"),
Err(e) => println!("Quick start example failed gracefully: {}", e),
}
}
#[test]
fn test_sample_data_creation() {
let (timestamps, values) = create_sample_data();
assert_eq!(timestamps.len(), 50);
assert_eq!(values.len(), 50);
// Verify timestamps are increasing
for i in 1..timestamps.len() {
assert!(timestamps[i] > timestamps[i - 1]);
}
// Verify values are finite
for &value in &values {
assert!(value.is_finite());
}
}
#[test]
fn test_model_builder_basic() {
// Test that basic model building works
let arima = ModelBuilder::arima()
.with_ar(1)
.with_differencing(1)
.with_ma(1)
.build();
assert!(arima.is_ok());
let es = ModelBuilder::exponential_smoothing()
.with_alpha(0.3)
.build();
assert!(es.is_ok());
let ma = ModelBuilder::moving_average().with_window(5).build();
assert!(ma.is_ok());
}
}