Repository navigation
Expand file tree
/
Copy pathexample.py
More file actions
325 lines (249 loc) · 10.4 KB
/
Copy pathexample.py
File metadata and controls
325 lines (249 loc) · 10.4 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
317
318
319
320
321
322
323
324
325
"""
Example script demonstrating key features of the BTC prediction framework.
This script can be run without fetching real data by using synthetic data.
"""
import numpy as np
import pandas as pd
from datetime import datetime, timedelta
def create_synthetic_btc_data(n_samples=1000, start_price=50000):
"""
Create synthetic BTC-like data for testing.
Args:
n_samples: Number of hourly samples
start_price: Starting price
Returns:
pd.DataFrame: Synthetic OHLCV data
"""
print("Creating synthetic BTC data...")
# Generate timestamps
start_date = datetime.now() - timedelta(hours=n_samples)
timestamps = [start_date + timedelta(hours=i) for i in range(n_samples)]
# Generate price with trend and noise
trend = np.linspace(0, 0.2, n_samples) # 20% upward trend
noise = np.random.randn(n_samples) * 0.02 # 2% random noise
returns = trend + noise
# Generate prices
prices = start_price * np.exp(np.cumsum(returns))
# Generate OHLC from close prices
opens = prices * (1 + np.random.randn(n_samples) * 0.005)
highs = np.maximum(prices, opens) * (1 + np.abs(np.random.randn(n_samples)) * 0.01)
lows = np.minimum(prices, opens) * (1 - np.abs(np.random.randn(n_samples)) * 0.01)
closes = prices
volumes = np.random.uniform(100, 1000, n_samples) * 1e6 # Random volume
# Create DataFrame
df = pd.DataFrame({
'open': opens,
'high': highs,
'low': lows,
'close': closes,
'volume': volumes
}, index=timestamps)
print(f"✓ Created {n_samples} samples from {df.index[0]} to {df.index[-1]}")
return df
def example_1_data_and_features():
"""Example 1: Data fetching and feature engineering."""
print("\n" + "="*80)
print("EXAMPLE 1: DATA AND FEATURE ENGINEERING")
print("="*80 + "\n")
# Create synthetic data
df = create_synthetic_btc_data(n_samples=500)
print("\nRaw data sample:")
print(df.head())
print(f"\nData shape: {df.shape}")
# Feature engineering
from feature_engineering import FeatureEngineer
engineer = FeatureEngineer(df)
df_features = engineer.build_all_features(target_horizon=1)
print(f"\nFeatures created: {len(df_features.columns)} features")
print(f"Sample count after cleaning: {len(df_features)}")
print("\nFeature categories:")
feature_names = engineer.get_feature_names(exclude_targets=True)
print(f" - Price features: {len([f for f in feature_names if 'price' in f or 'returns' in f])}")
print(f" - Moving averages: {len([f for f in feature_names if 'sma' in f or 'ema' in f])}")
print(f" - Momentum indicators: {len([f for f in feature_names if 'rsi' in f or 'macd' in f or 'stoch' in f])}")
print(f" - Volatility indicators: {len([f for f in feature_names if 'bb' in f or 'atr' in f or 'volatility' in f])}")
print(f" - Volume indicators: {len([f for f in feature_names if 'volume' in f or 'obv' in f])}")
print(f" - Time features: {len([f for f in feature_names if 'hour' in f or 'day' in f or 'month' in f])}")
print(f" - Lag features: {len([f for f in feature_names if 'lag' in f])}")
return df, df_features, feature_names
def example_2_model_training():
"""Example 2: Training different models."""
print("\n" + "="*80)
print("EXAMPLE 2: MODEL TRAINING")
print("="*80 + "\n")
# Get data and features
df = create_synthetic_btc_data(n_samples=500)
from feature_engineering import FeatureEngineer
engineer = FeatureEngineer(df)
df_features = engineer.build_all_features(target_horizon=1)
# Prepare data
feature_cols = engineer.get_feature_names(exclude_targets=True)
X = df_features[feature_cols].values
y = df_features['target_return'].values
# Split data
train_size = int(0.7 * len(X))
val_size = int(0.15 * len(X))
X_train, y_train = X[:train_size], y[:train_size]
X_val, y_val = X[train_size:train_size+val_size], y[train_size:train_size+val_size]
X_test, y_test = X[train_size+val_size:], y[train_size+val_size:]
print(f"Data splits:")
print(f" Train: {len(X_train)} samples")
print(f" Validation: {len(X_val)} samples")
print(f" Test: {len(X_test)} samples")
print(f" Features: {X.shape[1]}")
# Train Random Forest (fastest for demo)
print("\nTraining Random Forest model...")
from models import RandomForestModel
rf_model = RandomForestModel(n_estimators=50, max_depth=8, task='regression')
rf_model.fit(X_train, y_train, X_val, y_val)
print("✓ Random Forest trained")
# Make predictions
predictions = rf_model.predict(X_test)
print(f"\nPredictions shape: {predictions.shape}")
print(f"Sample predictions: {predictions[:5]}")
print(f"Sample actual values: {y_test[:5]}")
return rf_model, predictions, y_test, feature_cols
def example_3_evaluation():
"""Example 3: Model evaluation."""
print("\n" + "="*80)
print("EXAMPLE 3: MODEL EVALUATION")
print("="*80 + "\n")
# Train model and get predictions
_, predictions, y_test, _ = example_2_model_training()
# Evaluate
from evaluation import ModelEvaluator
evaluator = ModelEvaluator(task='regression')
metrics = evaluator.evaluate(y_test, predictions, model_name='RandomForest')
print("\nEvaluation Metrics:")
print(f" RMSE: {metrics['rmse']:.6f}")
print(f" MAE: {metrics['mae']:.6f}")
print(f" R²: {metrics['r2']:.6f}")
print(f" MAPE: {metrics['mape']:.2f}%")
print(f" Directional Accuracy: {metrics['directional_accuracy']:.2f}%")
return metrics
def example_4_visualization():
"""Example 4: Creating visualizations."""
print("\n" + "="*80)
print("EXAMPLE 4: VISUALIZATION")
print("="*80 + "\n")
import os
os.makedirs('example_plots', exist_ok=True)
# Get data and predictions
df = create_synthetic_btc_data(n_samples=500)
model, predictions, y_test, feature_cols = example_2_model_training()
from visualization import Visualizer
viz = Visualizer()
# 1. Price history
print("Creating price history plot...")
viz.plot_price_history(
df,
title="Synthetic BTC Price Data",
save_path='example_plots/price_history.png'
)
print("✓ Saved to: example_plots/price_history.png")
# 2. Predictions vs Actual
print("\nCreating predictions plot...")
viz.plot_predictions_vs_actual(
y_test,
predictions,
model_name='RandomForest',
save_path='example_plots/predictions.png'
)
print("✓ Saved to: example_plots/predictions.png")
# 3. Feature importance
print("\nCreating feature importance plot...")
importance = model.get_feature_importance()
viz.plot_feature_importance(
feature_cols,
importance,
top_n=20,
save_path='example_plots/feature_importance.png'
)
print("✓ Saved to: example_plots/feature_importance.png")
print("\n✓ All plots saved in 'example_plots/' directory")
def example_5_comparison():
"""Example 5: Comparing multiple models."""
print("\n" + "="*80)
print("EXAMPLE 5: COMPARING MULTIPLE MODELS")
print("="*80 + "\n")
# Get data and features
df = create_synthetic_btc_data(n_samples=500)
from feature_engineering import FeatureEngineer
engineer = FeatureEngineer(df)
df_features = engineer.build_all_features(target_horizon=1)
# Prepare data
feature_cols = engineer.get_feature_names(exclude_targets=True)
X = df_features[feature_cols].values
y = df_features['target_return'].values
# Split data
train_size = int(0.8 * len(X))
X_train, y_train = X[:train_size], y[:train_size]
X_test, y_test = X[train_size:], y[train_size:]
# Train multiple models
from models import RandomForestModel, XGBoostModel
from evaluation import ModelEvaluator
evaluator = ModelEvaluator(task='regression')
print("Training models...")
# Random Forest
print("\n Training Random Forest...")
rf_model = RandomForestModel(n_estimators=50, max_depth=8, task='regression')
rf_model.fit(X_train, y_train)
rf_pred = rf_model.predict(X_test)
evaluator.evaluate(y_test, rf_pred, 'RandomForest')
print(" ✓ Random Forest complete")
# XGBoost
print("\n Training XGBoost...")
xgb_model = XGBoostModel(n_estimators=50, max_depth=6, task='regression')
xgb_model.fit(X_train, y_train)
xgb_pred = xgb_model.predict(X_test)
evaluator.evaluate(y_test, xgb_pred, 'XGBoost')
print(" ✓ XGBoost complete")
# Print comparison
print("\n" + "-"*80)
evaluator.print_results()
# Get best model
best = evaluator.get_best_model(metric='rmse')
print(f"\n✓ Best model by RMSE: {best}")
# Visualize comparison
import os
os.makedirs('example_plots', exist_ok=True)
from visualization import Visualizer
viz = Visualizer()
results_df = evaluator.get_results_df()
viz.plot_model_comparison(
results_df,
metric='rmse',
save_path='example_plots/model_comparison.png'
)
print("✓ Comparison plot saved to: example_plots/model_comparison.png")
def main():
"""Run all examples."""
print("\n" + "="*80)
print("BTC PREDICTION FRAMEWORK - EXAMPLES")
print("="*80)
print("\nThese examples use synthetic data for demonstration.")
print("To use real BTC data, run 'python experiment.py'\n")
try:
# Example 1: Data and Features
example_1_data_and_features()
# Example 3: Evaluation (includes training from example 2)
example_3_evaluation()
# Example 4: Visualization
example_4_visualization()
# Example 5: Model Comparison
example_5_comparison()
print("\n" + "="*80)
print("✓ ALL EXAMPLES COMPLETED SUCCESSFULLY!")
print("="*80)
print("\nNext steps:")
print(" 1. Review plots in 'example_plots/' directory")
print(" 2. Explore individual modules in Python REPL")
print(" 3. Run full experiment: python experiment.py")
print(" 4. See USAGE.md for more examples")
print("="*80 + "\n")
except Exception as e:
print(f"\n✗ Error running examples: {e}")
import traceback
traceback.print_exc()
if __name__ == "__main__":
main()