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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
// External imports
use crate::constants::{EXTENDED_INDICATORS, PRICE_DENORM_CLIP_MIN, TECHNICAL_INDICATORS};
use anyhow::{Context, Result};
use burn::tensor::backend::Backend;
use polars::prelude::*;
use rand::Rng;
// Internal imports
use super::step_1_tensor_preparation;
use super::step_3_lstm_model_arch::TimeSeriesLstm;
/// Single-step prediction from the model
pub fn predict_next_step<B: Backend>(
model: &TimeSeriesLstm<B>,
df: DataFrame,
device: &B::Device,
use_extended_features: bool,
) -> Result<f64> {
// Choose which feature set to use
let feature_columns = if use_extended_features {
&EXTENDED_INDICATORS[..]
} else {
&TECHNICAL_INDICATORS[..]
};
// Validate required columns
if !df.is_empty() {
for col in feature_columns {
if !df.schema().contains(col) {
return Err(anyhow::anyhow!("Missing required column: {}", col));
}
}
}
// Clone the DataFrame to avoid modifications affecting the original
let prediction_df = df.clone();
// Build sequences tensor with horizon 1
let (features, _) = step_1_tensor_preparation::dataframe_to_tensors::<B>(
&prediction_df,
crate::constants::SEQUENCE_LENGTH,
1,
device,
use_extended_features,
None,
)
.context("Tensor creation failed for prediction")?;
// Extract the last sequence
let seq_count = features.dims()[0];
let seq = features.clone().narrow(0, seq_count - 1, 1);
// Forward pass for single-step prediction
let pred_tensor = model.forward(seq);
// Extract scalar prediction
let data = pred_tensor.to_data().convert::<f32>();
let slice = data.as_slice::<f32>().unwrap();
let value = slice[0];
Ok(value as f64)
}
/// Generate multiple future predictions using recursive forecasting with error correction
pub fn generate_forecast_with_correction<B: Backend>(
model: &TimeSeriesLstm<B>,
df: DataFrame,
forecast_horizon: usize,
device: &B::Device,
use_extended_features: bool,
error_correction_alpha: f64,
) -> Result<Vec<f64>> {
let mut predictions = Vec::with_capacity(forecast_horizon);
// Keep track of recent prediction errors for correction
let mut recent_errors = Vec::new();
let max_error_history = 5; // Number of recent errors to consider
// Choose which feature set to use
let feature_columns = if use_extended_features {
&EXTENDED_INDICATORS[..]
} else {
&TECHNICAL_INDICATORS[..]
};
// Get the schema once at the beginning
let schema = df.schema();
// Start with the original DataFrame
let mut current_data = df.clone();
for step in 0..forecast_horizon {
// Make a prediction for the next step
let pred_df = current_data.clone();
let uncorrected_prediction =
predict_next_step(model, pred_df, device, use_extended_features)?;
// Apply error correction if we have historical errors
let corrected_prediction = if !recent_errors.is_empty() {
let mean_error: f64 = recent_errors.iter().sum::<f64>() / recent_errors.len() as f64;
uncorrected_prediction - (error_correction_alpha * mean_error)
} else {
uncorrected_prediction
};
predictions.push(corrected_prediction);
// Extract needed data from the current DataFrame
let height = current_data.height();
let mut column_data = Vec::new();
// Create a new row with the predicted value
for (name, dtype) in schema.iter() {
let series = match name.as_str() {
"close" => Series::new(name.clone(), &[corrected_prediction]),
"symbol" | "time" => {
if let Ok(col) = current_data.column(name) {
let last_idx = if height > 0 { height - 1 } else { 0 };
let last_val = if height > 0 {
col.get(last_idx).unwrap_or(AnyValue::Null)
} else {
AnyValue::Null
};
Series::new(name.clone(), &[last_val.to_string()])
} else {
Series::new(name.clone(), &[""])
}
}
_ => {
if feature_columns.contains(&name.as_str()) {
if let Ok(col) = current_data.column(name) {
if let Ok(f64_col) = col.f64() {
let last_idx = if height > 0 { height - 1 } else { 0 };
let last_val = if f64_col.len() > 0 {
f64_col.get(last_idx).unwrap_or(0.0)
} else {
0.0
};
Series::new(name.clone(), &[last_val])
} else {
Series::new(name.clone(), &[0.0])
}
} else {
Series::new(name.clone(), &[0.0])
}
} else {
match dtype {
DataType::Float64 => Series::new(name.clone(), &[0.0f64]),
DataType::Int64 => Series::new(name.clone(), &[0i64]),
DataType::String => Series::new(name.clone(), &[""]),
_ => Series::new(name.clone(), &[0.0f64]),
}
}
}
};
column_data.push(series.into_column());
}
// Create new row and add it to the data
let new_row = DataFrame::new(column_data)?;
let next_data = current_data.vstack(&new_row)?;
// Completely replace the current data with the new data
current_data = next_data;
// Update error history if we have actual values
if step + 1 < forecast_horizon {
if let Ok(actual_series) = df.column("close") {
if let Ok(actual_f64) = actual_series.f64() {
// The true_idx should be based on the original df height, not the current_data height
// Calculate the index where we should expect the actual value
let df_height = df.height();
let step_index = step;
// Only try to get the value if we're still within bounds of the original df
if step_index < df_height {
if let Some(actual) = actual_f64.get(step_index) {
let error = corrected_prediction - actual;
recent_errors.push(error);
// Keep only the most recent errors
if recent_errors.len() > max_error_history {
recent_errors.remove(0);
}
}
}
}
}
}
}
Ok(predictions)
}
/// Convert predictions back to original scale using Z-score denormalization
/// with volatility constraints based on historical data
pub fn denormalize_z_score_predictions(
predictions: Vec<f64>,
original_df: &DataFrame,
column: &str,
) -> Result<Vec<f64>> {
// Get the original series
let series = original_df.column(column)?;
let f64_series = series.f64()?;
// Get mean and std for the series
let mean = f64_series.mean().unwrap_or(0.0);
let std = f64_series.std(1).unwrap_or(1.0);
// Avoid division by zero
let std = if std < f64::EPSILON { 1.0 } else { std };
// Calculate historical daily volatility
let daily_volatility = calculate_historical_volatility(original_df, column)?;
println!(
"Historical daily volatility: {:.2}%",
daily_volatility * 100.0
);
// Calculate realistic per-minute volatility constraint
// Typical stock prices move around 0.5-1% of daily volatility per minute
let minute_volatility_factor = daily_volatility * 0.007; // 0.7% of daily volatility per minute
// Denormalize the predictions
let mut denormalized = Vec::with_capacity(predictions.len());
let mut prev_value = f64_series.get(f64_series.len() - 1).unwrap_or(mean);
for (_i, &pred) in predictions.iter().enumerate() {
// Basic denormalization using the Z-score formula: x = z*std + mean
let raw_value = (pred * std) + mean;
// Calculate max allowed change based on historical volatility
let max_allowed_change = prev_value * minute_volatility_factor;
// Apply volatility constraints
let constrained_value = if (raw_value - prev_value).abs() > max_allowed_change {
// Limit the change to the maximum allowed
if raw_value > prev_value {
prev_value + max_allowed_change
} else {
prev_value - max_allowed_change
}
} else {
raw_value
};
// Prevent negative prices
let final_value = constrained_value.max(PRICE_DENORM_CLIP_MIN);
denormalized.push(final_value);
// Update prev_value for next iteration
prev_value = final_value;
}
Ok(denormalized)
}
/// Calculate the historical volatility from a DataFrame
fn calculate_historical_volatility(df: &DataFrame, price_column: &str) -> Result<f64> {
if df.height() < 10 {
// Not enough data for reliable calculation
return Ok(0.02); // Return a default 2% daily volatility
}
let series = df.column(price_column)?;
let f64_series = series.f64()?;
// Calculate daily returns
let mut returns = Vec::new();
let mut prev_value = f64_series.get(0).unwrap_or(0.0);
for i in 1..f64_series.len() {
if let Some(curr) = f64_series.get(i) {
if prev_value > 0.0 {
let ret = (curr - prev_value) / prev_value;
returns.push(ret);
}
prev_value = curr;
}
}
if returns.is_empty() {
return Ok(0.02); // Default if no valid returns
}
// Calculate standard deviation of returns
let mean_return = returns.iter().sum::<f64>() / returns.len() as f64;
let variance = returns
.iter()
.map(|&r| (r - mean_return).powi(2))
.sum::<f64>()
/ returns.len() as f64;
let daily_vol = variance.sqrt();
// Cap volatility to reasonable range
let capped_vol = daily_vol.min(0.05).max(0.005);
Ok(capped_vol)
}
/// Convert predictions back to original scale (reverse min-max normalization)
pub fn denormalize_predictions(
predictions: Vec<f64>,
original_df: &DataFrame,
column: &str,
) -> Result<Vec<f64>> {
// Get the original series
let series = original_df.column(column)?;
// Get min and max values for the series
let f64_series = series.f64()?;
let min = f64_series.min().unwrap_or(0.0);
let max = f64_series.max().unwrap_or(1.0);
// Avoid division by zero
let range = if (max - min).abs() < f64::EPSILON {
1.0
} else {
max - min
};
// Denormalize the predictions
let denormalized = predictions
.iter()
.map(|&p| (p * range) + min)
.map(|p| p.max(PRICE_DENORM_CLIP_MIN)) // Prevent negative prices
.collect();
Ok(denormalized)
}
/// Direct multi-step (multi-output) prediction: returns all future steps at once
pub fn predict_multi_step_direct<B: Backend>(
model: &TimeSeriesLstm<B>,
df: DataFrame,
device: &B::Device,
forecast_horizon: usize,
) -> Result<Vec<f64>> {
// Direct multi-step: build features from last SEQUENCE_LENGTH rows
let seq_len = crate::constants::SEQUENCE_LENGTH;
// Ensure we use only the standard indicator set to match model input dimensions
let indicator_names: Vec<String> = TECHNICAL_INDICATORS
.iter()
.map(|&s| s.to_string())
.collect();
// Select only technical indicators
let mut df_sel = df
.select(&indicator_names)
.context("Failed to select technical indicators for prediction")?;
// Drop nulls
df_sel = df_sel
.drop_nulls::<String>(None)
.context("Failed to drop null values")?;
// Compute available rows after drop
let n_rows_sel = df_sel.height();
if n_rows_sel < seq_len {
return Err(anyhow::anyhow!(format!(
"DataFrame has too few rows ({}) for sequence_length ({})",
n_rows_sel, seq_len
)));
}
// Count the number of features to ensure correct tensor dimensions
let n_features = TECHNICAL_INDICATORS.len();
// Extract the last seq_len rows
let start = n_rows_sel - seq_len;
// Build feature buffer [1, seq_len, n_features]
let mut buf = Vec::with_capacity(seq_len * n_features);
// Populate buffer with feature values
for row in start..n_rows_sel {
for &col in TECHNICAL_INDICATORS.iter() {
let val = df_sel.column(col)?.f64()?.get(row).unwrap_or(0.0) as f32;
buf.push(val);
}
}
// Verify buffer size matches expected dimensions
if buf.len() != seq_len * n_features {
return Err(anyhow::anyhow!(format!(
"Feature buffer size mismatch: got {} elements, expected {} (seq_len={}, n_features={})",
buf.len(), seq_len * n_features, seq_len, n_features
)));
}
// Create tensor with correct shape
let shape = burn::tensor::Shape::new([1, seq_len, n_features]);
let features = burn::tensor::Tensor::<B, 1>::from_floats(buf.as_slice(), device).reshape(shape);
// Forward pass through the model
let output = model.forward(features); // [1, forecast_horizon]
// Convert to Vec<f64>
let data = output.to_data().convert::<f32>();
let slice = data.as_slice::<f32>().unwrap();
// Ensure we have enough predictions
let pred_count = slice.len();
if pred_count < forecast_horizon {
println!(
"Warning: Model returned fewer predictions ({}) than requested forecast horizon ({})",
pred_count, forecast_horizon
);
}
Ok(slice.iter().map(|&v| v as f64).collect())
}
/// Ensemble forecasting that combines multiple prediction strategies
pub fn ensemble_forecast<B: Backend>(
model: &TimeSeriesLstm<B>,
df: DataFrame,
device: &B::Device,
forecast_horizon: usize,
) -> Result<Vec<f64>> {
// Create a copy of the DataFrame to avoid modifications to the original
let df_copy = df.clone();
// Check if we have extended features available by verifying all columns exist
let _has_all_extended_features = EXTENDED_INDICATORS
.iter()
.all(|&col| df_copy.schema().contains(col));
// Safety check - force standard features if any extended features are missing
let use_extended_features = false; // Disable extended features to ensure model works
println!(
"Using {} features for forecasting",
if use_extended_features {
"extended"
} else {
"standard"
}
);
// Strategy 1: Direct multi-step prediction (using standard features only)
let direct_predictions =
predict_multi_step_direct(model, df_copy.clone(), device, forecast_horizon)?;
// Strategy 2: Recursive prediction with standard features
let recursive_predictions = generate_forecast_with_correction(
model,
df_copy.clone(),
forecast_horizon,
device,
false, // Use standard features
0.3, // Error correction alpha
)?;
// For extended predictions, we just clone the recursive predictions for now
// This is a placeholder for future implementation
let _extended_predictions = recursive_predictions.clone();
// Combine predictions using weighted average
// Adjusted weights to favor direct predictions more (they're typically more stable)
let weights = [0.6, 0.4, 0.0]; // Give more weight to direct predictions
let mut ensemble_predictions = Vec::with_capacity(forecast_horizon);
for i in 0..forecast_horizon {
let direct = direct_predictions.get(i).copied().unwrap_or(0.0);
let recursive = recursive_predictions.get(i).copied().unwrap_or(0.0);
// Dynamic weights based on position in forecast horizon
// Direct predictions tend to be better for early predictions
// While recursive might capture trends better later (but with higher variance)
let position_factor = i as f64 / forecast_horizon as f64;
// Start with heavy direct weight, gradually shift toward recursive
let direct_weight = weights[0] - (position_factor * 0.1); // Decrease weight for later predictions
let recursive_weight = weights[1] + (position_factor * 0.1); // Increase for later predictions
// Normalize weights to sum to 1.0
let total_weight = direct_weight + recursive_weight;
let direct_weight = direct_weight / total_weight;
let recursive_weight = recursive_weight / total_weight;
// Compute weighted average (only using direct and recursive)
let ensemble = (direct * direct_weight) + (recursive * recursive_weight);
ensemble_predictions.push(ensemble);
}
// CRITICAL FIX: Denormalize the predictions before applying constraints
// The predictions from the model are in normalized scale and need to be converted back
let denormalized_predictions =
denormalize_z_score_predictions(ensemble_predictions, &df_copy, "close")?;
// Apply post-processing to limit maximum percentage change between consecutive predictions
let smoothed_predictions = apply_max_change_constraint(denormalized_predictions, df_copy)?;
Ok(smoothed_predictions)
}
/// Apply a constraint on the maximum percentage change between consecutive predictions
fn apply_max_change_constraint(predictions: Vec<f64>, df: DataFrame) -> Result<Vec<f64>> {
if predictions.is_empty() {
return Ok(predictions);
}
// Configuration parameters
let max_percent_change_per_minute = 0.0025; // 0.25% maximum change per minute (reduced from 0.5%)
// Get the actual last known price from the dataframe
let last_known_price = if let Ok(close_series) = df.column("close") {
if let Ok(f64_series) = close_series.f64() {
if f64_series.len() > 0 {
f64_series.get(f64_series.len() - 1).unwrap_or(180.0) // Use 180 as fallback
} else {
180.0 // Default to 180 as a reasonable price for AAPL
}
} else {
180.0
}
} else {
180.0
};
println!("Last known price from data: ${:.2}", last_known_price);
// Calculate mean and standard deviation of predictions to identify trend direction
let pred_mean = predictions.iter().sum::<f64>() / predictions.len() as f64;
let _pred_std = (predictions
.iter()
.map(|x| (x - pred_mean).powi(2))
.sum::<f64>()
/ predictions.len() as f64)
.sqrt();
// Calculate mean price (just as an example - not used in current implementation)
let _mean_price = predictions.iter().sum::<f64>() / predictions.len() as f64;
// Start from the last known price
let mut prev_price = last_known_price;
let mut smoothed = Vec::with_capacity(predictions.len());
let mut rng = rand::rng();
// Calculate the overall trend from the model predictions
// Instead of blindly following the trend, extract just the direction
let original_first_pred = predictions.first().unwrap_or(&pred_mean);
let original_last_pred = predictions.last().unwrap_or(&pred_mean);
let overall_trend = if original_last_pred > original_first_pred {
1.0
} else {
-1.0
};
// Significantly reduce trend factor to prevent continuous uptrend
let trend_factor = 0.0002 * overall_trend; // Much smaller trend factor (0.02% per minute)
println!(
"Starting price constraint from ${:.2} with max change of {:.2}%",
prev_price,
max_percent_change_per_minute * 100.0
);
// Track consecutive moves in same direction to force reversals
let mut consecutive_same_direction = 0;
let mut last_direction = 0.0;
// Keep track of mean and starting price for mean reversion
let start_price = prev_price;
let mean_reversion_strength = 0.2; // Strength of mean reversion effect
for (_i, &pred) in predictions.iter().enumerate() {
// Calculate trend direction from raw prediction compared to previous one
let mut direction = if _i > 0 {
if pred > predictions[_i - 1] {
1.0
} else {
-1.0
}
} else {
if pred > pred_mean {
1.0
} else {
-1.0
}
};
// Force direction changes to create more realistic price movements
if direction == last_direction {
consecutive_same_direction += 1;
} else {
consecutive_same_direction = 0;
}
// Force reversal if too many consecutive moves in same direction (more aggressive)
if consecutive_same_direction > 3 + (rng.random::<f64>() * 3.0) as i32 {
direction = -direction;
consecutive_same_direction = 0;
}
last_direction = direction;
// Increase randomness for more volatility (up to 70% of max allowed change)
let random_factor = (rng.random::<f64>() * 2.0 - 1.0) * max_percent_change_per_minute * 0.7;
// Apply mean reversion - push price back toward mean/start price when it deviates too much
let price_deviation = (prev_price / start_price) - 1.0;
let mean_reversion =
-price_deviation * mean_reversion_strength * max_percent_change_per_minute;
// Calculate percent change combining trend, randomness, mean reversion and market patterns
let percent_change =
// Base change - minimal trend + randomness + mean reversion
(trend_factor + random_factor + mean_reversion) *
// Increase volatility at market open and close (U-shaped volatility)
(1.0 + 0.5 * (1.0 - (((_i as f64 / predictions.len() as f64) * 2.0 - 1.0).powi(2))));
// Every ~15-30 minutes, introduce a small reversal for realism
let time_based_reversal =
if _i % (15 + (rng.random::<f64>() * 15.0) as usize) == 0 && _i > 0 {
-1.0 * direction * max_percent_change_per_minute * rng.random::<f64>() * 0.8
} else {
0.0
};
// Calculate next price with constraints
let next_price = prev_price * (1.0 + percent_change + time_based_reversal);
// Apply min/max constraints
let max_up_change = prev_price * (1.0 + max_percent_change_per_minute);
let max_down_change = prev_price * (1.0 - max_percent_change_per_minute);
let constrained_price = next_price.min(max_up_change).max(max_down_change);
smoothed.push(constrained_price);
prev_price = constrained_price;
}
Ok(smoothed)
}
// Function that uses random number generation
pub fn add_market_noise(predictions: &mut [f64], max_percent_change_per_minute: f64) {
let mut rng = rand::rng();
// Track consecutive price movements in the same direction
let mut prev_direction = 0.0;
let mut consecutive_same_direction = 0;
for idx in 0..predictions.len() {
// Apply various market microstructure effects
// 1. Mean reversion after consecutive movements in the same direction
let direction = if idx > 0 {
if predictions[idx] > predictions[idx - 1] {
1.0
} else {
-1.0
}
} else {
0.0
};
if direction == prev_direction && direction != 0.0 {
consecutive_same_direction += 1;
} else {
consecutive_same_direction = 0;
}
if consecutive_same_direction > 3 + (rng.random::<f64>() * 3.0) as i32 {
// Mean reversion - add opposite force
let reversion = -direction * max_percent_change_per_minute * 0.6;
predictions[idx] *= 1.0 + reversion;
consecutive_same_direction = 0;
}
// 2. Random noise (market microstructure)
let random_factor = (rng.random::<f64>() * 2.0 - 1.0) * max_percent_change_per_minute * 0.7;
predictions[idx] *= 1.0 + random_factor;
// 3. Ensure no negative prices
if predictions[idx] < 0.0 {
predictions[idx] = 0.01;
}
// Store current direction for next iteration
prev_direction = direction;
// 4. Time-based patterns (e.g., reversals at round time marks)
let time_based_reversal =
if idx % (15 + (rng.random::<f64>() * 15.0) as usize) == 0 && idx > 0 {
-1.0 * direction * max_percent_change_per_minute * rng.random::<f64>() * 0.8
} else {
0.0
};
predictions[idx] *= 1.0 + time_based_reversal;
}
}