-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathohlcv_forecasting_example.rs
More file actions
288 lines (246 loc) · 9.29 KB
/
ohlcv_forecasting_example.rs
File metadata and controls
288 lines (246 loc) · 9.29 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
#![allow(deprecated)]
#![allow(clippy::needless_range_loop)]
#![allow(unused_imports)]
use chrono::{DateTime, NaiveDate, TimeZone, Utc};
use oxidiviner::prelude::*;
use oxidiviner::ModelOutput;
use std::error::Error;
use std::path::Path;
// Main function that demonstrates OHLCV data loading and forecasting
fn main() -> std::result::Result<(), Box<dyn Error>> {
println!("OxiDiviner - OHLCV Data Forecasting Example");
println!("============================================\n");
// Load OHLCV data from CSV
println!("Loading AAPL daily OHLCV data...");
let data = load_ohlcv_data("examples/csv/AAPL_daily_ohlcv.csv")?;
println!(
"Data loaded: {} rows from {} to {}",
data.timestamps.len(),
data.timestamps.first().unwrap().date(),
data.timestamps.last().unwrap().date()
);
// Convert to time series for forecasting (using close prices)
let time_series = data.to_time_series(false); // false = use regular close, not adjusted
// Split into training and test sets (last 30 days for testing)
let train_size = time_series.len() - 30;
let (train_data, test_data) = split_time_series(&time_series, train_size)?;
println!(
"\nSplit into training ({} points) and test ({} points) sets",
train_data.len(),
test_data.len()
);
// Forecast using different models
let forecast_horizon = 30; // Forecast the next 30 days
// Create and fit models
println!("\nTraining and evaluating models...");
// 1. Moving Average model
println!("\n1. Moving Average Model (MA):");
let mut ma_model = MAModel::new(7)?; // 7-day moving average
ma_model.fit(&train_data)?;
let ma_output = ma_model.predict(forecast_horizon, Some(&test_data))?;
print_model_evaluation(&ma_output);
// 2. Simple Exponential Smoothing model
println!("\n2. Simple Exponential Smoothing Model (SES):");
let mut ses_model = SimpleESModel::new(0.3)?; // Only the alpha parameter
ses_model.fit(&train_data)?;
let ses_output = ses_model.predict(forecast_horizon, Some(&test_data))?;
print_model_evaluation(&ses_output);
// 3. Holt-Winters model with weekly seasonality
println!("\n3. Holt-Winters Model with Weekly Seasonality:");
let mut hw_model = HoltWintersModel::new(0.2, 0.1, 0.1, 5)?;
hw_model.fit(&train_data)?;
let hw_output = hw_model.predict(forecast_horizon, Some(&test_data))?;
print_model_evaluation(&hw_output);
// 4. Autoregressive model
println!("\n4. Autoregressive Model (AR):");
let mut ar_model = ARModel::new(5, true)?; // AR(5) model with intercept
ar_model.fit(&train_data)?;
let ar_output = ar_model.predict(forecast_horizon, Some(&test_data))?;
print_model_evaluation(&ar_output);
// Compare the first few days of forecasts
println!("\nComparison of first 7 days of forecasts:");
println!("Day | Actual | MA | SES | HW | AR");
println!("----|-----------|-----------|-----------|-----------|----------");
for i in 0..7.min(test_data.len()) {
println!(
"{:3} | {:9.2} | {:9.2} | {:9.2} | {:9.2} | {:9.2}",
i + 1,
test_data.values[i],
ma_output.forecasts[i],
ses_output.forecasts[i],
hw_output.forecasts[i],
ar_output.forecasts[i]
);
}
// Calculate a simple ensemble forecast (average of all models)
println!("\nCalculating ensemble forecast (average of all models)...");
let mut ensemble_forecast = vec![0.0; forecast_horizon.min(test_data.len())];
for i in 0..ensemble_forecast.len() {
ensemble_forecast[i] = (ma_output.forecasts[i]
+ ses_output.forecasts[i]
+ hw_output.forecasts[i]
+ ar_output.forecasts[i])
/ 4.0;
}
// Calculate ensemble error metrics
let ensemble_mae = calculate_mae(
&test_data.values[0..ensemble_forecast.len()],
&ensemble_forecast,
);
let ensemble_rmse = calculate_rmse(
&test_data.values[0..ensemble_forecast.len()],
&ensemble_forecast,
);
println!("\nEnsemble Model Performance:");
println!(" MAE: {:.4}", ensemble_mae);
println!(" RMSE: {:.4}", ensemble_rmse);
// Compare the best individual model with the ensemble
println!("\nEnsemble vs Individual Models:");
let models = [
(
"MA",
ma_output.evaluation.as_ref().unwrap().mae,
ma_output.evaluation.as_ref().unwrap().rmse,
),
(
"SES",
ses_output.evaluation.as_ref().unwrap().mae,
ses_output.evaluation.as_ref().unwrap().rmse,
),
(
"HW",
hw_output.evaluation.as_ref().unwrap().mae,
hw_output.evaluation.as_ref().unwrap().rmse,
),
(
"AR",
ar_output.evaluation.as_ref().unwrap().mae,
ar_output.evaluation.as_ref().unwrap().rmse,
),
("Ensemble", ensemble_mae, ensemble_rmse),
];
// Find the best model based on MAE
let best_model = models
.iter()
.min_by(|a, b| a.1.partial_cmp(&b.1).unwrap())
.unwrap();
println!(
"Best model by MAE: {} (MAE: {:.4}, RMSE: {:.4})",
best_model.0, best_model.1, best_model.2
);
Ok(())
}
// Helper function to calculate Mean Absolute Error
fn calculate_mae(actual: &[f64], forecast: &[f64]) -> f64 {
let sum: f64 = actual
.iter()
.zip(forecast.iter())
.map(|(a, f)| (a - f).abs())
.sum();
sum / actual.len() as f64
}
// Helper function to calculate Root Mean Squared Error
fn calculate_rmse(actual: &[f64], forecast: &[f64]) -> f64 {
let sum: f64 = actual
.iter()
.zip(forecast.iter())
.map(|(a, f)| (a - f).powi(2))
.sum();
(sum / actual.len() as f64).sqrt()
}
// Helper function to load OHLCV data from a CSV file
fn load_ohlcv_data(file_path: &str) -> std::result::Result<OHLCVData, Box<dyn Error>> {
// Extract symbol from filename
let path = Path::new(file_path);
let file_stem = path.file_stem().unwrap().to_str().unwrap();
let symbol = file_stem.split('_').next().unwrap_or("UNKNOWN");
// Read the content of the CSV file
let content = std::fs::read_to_string(file_path)?;
let lines: Vec<&str> = content.lines().collect();
// Skip header row
let data_lines = &lines[1..];
let mut timestamps = Vec::with_capacity(data_lines.len());
let mut open = Vec::with_capacity(data_lines.len());
let mut high = Vec::with_capacity(data_lines.len());
let mut low = Vec::with_capacity(data_lines.len());
let mut close = Vec::with_capacity(data_lines.len());
let mut volume = Vec::with_capacity(data_lines.len());
for line in data_lines {
let parts: Vec<&str> = line.split(',').collect();
if parts.len() < 6 {
continue; // Skip malformed lines
}
// Parse timestamp: format is "YYYY-MM-DD HH:MM:SS UTC"
let timestamp_str = parts[0].trim();
let timestamp = match Utc.datetime_from_str(timestamp_str, "%Y-%m-%d %H:%M:%S %Z") {
Ok(dt) => dt,
Err(_) => continue, // Skip lines with invalid timestamps
};
// Parse price and volume data
let open_val = match parts[1].trim().parse::<f64>() {
Ok(val) => val,
Err(_) => continue,
};
let high_val = match parts[2].trim().parse::<f64>() {
Ok(val) => val,
Err(_) => continue,
};
let low_val = match parts[3].trim().parse::<f64>() {
Ok(val) => val,
Err(_) => continue,
};
let close_val = match parts[4].trim().parse::<f64>() {
Ok(val) => val,
Err(_) => continue,
};
let volume_val = match parts[5].trim().parse::<f64>() {
Ok(val) => val,
Err(_) => continue,
};
timestamps.push(timestamp);
open.push(open_val);
high.push(high_val);
low.push(low_val);
close.push(close_val);
volume.push(volume_val);
}
if timestamps.is_empty() {
return Err("No valid data found in CSV file".into());
}
Ok(OHLCVData {
symbol: symbol.to_string(),
timestamps,
open,
high,
low,
close,
volume,
adjusted_close: None,
})
}
// Helper function to split a time series into training and test sets
fn split_time_series(
data: &TimeSeriesData,
train_size: usize,
) -> std::result::Result<(TimeSeriesData, TimeSeriesData), Box<dyn Error>> {
if train_size >= data.len() {
return Err(Box::new(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"Train size must be less than the total data length",
)));
}
let train = data.slice(0, train_size)?;
let test = data.slice(train_size, data.len())?;
Ok((train, test))
}
// Helper function to print model evaluation results
fn print_model_evaluation(output: &ModelOutput) {
if let Some(eval) = &output.evaluation {
println!(" Model: {}", output.model_name);
println!(" MAE: {:.4}", eval.mae);
println!(" RMSE: {:.4}", eval.rmse);
println!(" MAPE: {:.2}%", eval.mape);
} else {
println!(" No evaluation available for {}", output.model_name);
}
}