-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTabNet Financial Predictive Models
More file actions
371 lines (293 loc) · 13.7 KB
/
TabNet Financial Predictive Models
File metadata and controls
371 lines (293 loc) · 13.7 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
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
import numpy as np
import pandas as pd
import torch
from pytorch_tabnet.tab_model import TabNetRegressor, TabNetClassifier
from sklearn.preprocessing import StandardScaler, LabelEncoder
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, roc_auc_score
import yfinance as yf
import ta
import matplotlib.pyplot as plt
import seaborn as sns
import warnings
warnings.filterwarnings('ignore')
class FinancialDataProcessor:
"""Process and prepare financial data for TabNet models"""
def __init__(self):
self.scaler = StandardScaler()
self.label_encoder = LabelEncoder()
def fetch_stock_data(self, symbols, period='2y'):
"""Fetch stock data from Yahoo Finance"""
data = {}
for symbol in symbols:
ticker = yf.Ticker(symbol)
df = ticker.history(period=period)
df['Symbol'] = symbol
data[symbol] = df
return pd.concat(data.values(), ignore_index=True)
def create_technical_indicators(self, df):
"""Create technical indicators for stock data"""
# Price-based indicators
df['SMA_10'] = ta.trend.sma_indicator(df['Close'], window=10)
df['SMA_20'] = ta.trend.sma_indicator(df['Close'], window=20)
df['EMA_12'] = ta.trend.ema_indicator(df['Close'], window=12)
df['EMA_26'] = ta.trend.ema_indicator(df['Close'], window=26)
# Momentum indicators
df['RSI'] = ta.momentum.rsi(df['Close'])
df['MACD'] = ta.trend.macd_diff(df['Close'])
df['MACD_signal'] = ta.trend.macd_signal(df['Close'])
df['Stoch_K'] = ta.momentum.stoch(df['High'], df['Low'], df['Close'])
df['Stoch_D'] = ta.momentum.stoch_signal(df['High'], df['Low'], df['Close'])
# Volatility indicators
df['BB_upper'] = ta.volatility.bollinger_hband(df['Close'])
df['BB_lower'] = ta.volatility.bollinger_lband(df['Close'])
df['BB_width'] = df['BB_upper'] - df['BB_lower']
df['ATR'] = ta.volatility.average_true_range(df['High'], df['Low'], df['Close'])
# Volume indicators
df['Volume_SMA'] = ta.volume.volume_sma(df['Close'], df['Volume'])
df['OBV'] = ta.volume.on_balance_volume(df['Close'], df['Volume'])
# Price features
df['Price_Change'] = df['Close'].pct_change()
df['High_Low_Ratio'] = df['High'] / df['Low']
df['Close_Open_Ratio'] = df['Close'] / df['Open']
return df
def create_target_variables(self, df, target_type='next_day_return'):
"""Create target variables for prediction"""
if target_type == 'next_day_return':
df['Target'] = df['Close'].pct_change().shift(-1)
elif target_type == 'price_direction':
df['Target'] = (df['Close'].shift(-1) > df['Close']).astype(int)
elif target_type == 'volatility':
df['Target'] = df['Close'].rolling(window=10).std().shift(-1)
return df
def prepare_features(self, df):
"""Prepare features for model training"""
# Select numeric columns (excluding target and date-related columns)
feature_cols = [col for col in df.columns if df[col].dtype in ['float64', 'int64']
and col not in ['Target']]
# Handle missing values
df[feature_cols] = df[feature_cols].fillna(method='ffill').fillna(method='bfill')
# Encode categorical variables if any
categorical_cols = [col for col in df.columns if df[col].dtype == 'object'
and col not in ['Date']]
for col in categorical_cols:
df[col] = self.label_encoder.fit_transform(df[col].astype(str))
feature_cols.append(col)
return df, feature_cols
class TabNetStockPredictor:
"""TabNet model for stock price prediction"""
def __init__(self, model_params=None):
self.model_params = model_params or {
'n_d': 64, 'n_a': 64, 'n_steps': 5,
'gamma': 1.5, 'n_independent': 2, 'n_shared': 2,
'lambda_sparse': 1e-4, 'optimizer_fn': torch.optim.Adam,
'optimizer_params': dict(lr=0.02),
'scheduler_params': dict(step_size=50, gamma=0.9),
'scheduler_fn': torch.optim.lr_scheduler.StepLR,
'verbose': 1
}
self.model = TabNetRegressor(**self.model_params)
self.processor = FinancialDataProcessor()
def prepare_data(self, symbols, period='2y', target_type='next_day_return'):
"""Prepare data for training"""
# Fetch and process data
df = self.processor.fetch_stock_data(symbols, period)
df = self.processor.create_technical_indicators(df)
df = self.processor.create_target_variables(df, target_type)
# Prepare features
df, feature_cols = self.processor.prepare_features(df)
# Remove rows with NaN targets
df = df.dropna(subset=['Target'])
X = df[feature_cols].values
y = df['Target'].values
return X, y, feature_cols
def train(self, X, y, test_size=0.2, validation_split=0.2):
"""Train the TabNet model"""
# Split data
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=test_size, random_state=42, shuffle=False
)
# Further split training data for validation
X_train, X_val, y_train, y_val = train_test_split(
X_train, y_train, test_size=validation_split, random_state=42
)
# Train model
self.model.fit(
X_train, y_train,
eval_set=[(X_val, y_val)],
max_epochs=200,
patience=20,
batch_size=1024,
virtual_batch_size=128,
drop_last=False
)
return X_test, y_test
def evaluate(self, X_test, y_test):
"""Evaluate model performance"""
predictions = self.model.predict(X_test)
metrics = {
'MAE': mean_absolute_error(y_test, predictions),
'MSE': mean_squared_error(y_test, predictions),
'RMSE': np.sqrt(mean_squared_error(y_test, predictions)),
'R2': r2_score(y_test, predictions),
'Directional_Accuracy': np.mean(np.sign(y_test) == np.sign(predictions))
}
return metrics, predictions
def plot_feature_importance(self, feature_names):
"""Plot feature importance from TabNet"""
importance = self.model.feature_importances_
plt.figure(figsize=(12, 8))
indices = np.argsort(importance)[-20:] # Top 20 features
plt.barh(range(len(indices)), importance[indices])
plt.yticks(range(len(indices)), [feature_names[i] for i in indices])
plt.xlabel('Feature Importance')
plt.title('TabNet Feature Importance - Top 20 Features')
plt.tight_layout()
plt.show()
class TabNetCreditRiskClassifier:
"""TabNet model for credit risk classification"""
def __init__(self, model_params=None):
self.model_params = model_params or {
'n_d': 32, 'n_a': 32, 'n_steps': 4,
'gamma': 1.3, 'n_independent': 2, 'n_shared': 2,
'lambda_sparse': 1e-3, 'optimizer_fn': torch.optim.Adam,
'optimizer_params': dict(lr=0.02),
'scheduler_params': dict(step_size=50, gamma=0.9),
'scheduler_fn': torch.optim.lr_scheduler.StepLR,
'verbose': 1
}
self.model = TabNetClassifier(**self.model_params)
def create_synthetic_credit_data(self, n_samples=10000):
"""Create synthetic credit data for demonstration"""
np.random.seed(42)
# Generate features
data = {
'age': np.random.normal(40, 12, n_samples),
'income': np.random.exponential(50000, n_samples),
'credit_score': np.random.normal(650, 100, n_samples),
'debt_to_income': np.random.beta(2, 5, n_samples),
'employment_length': np.random.exponential(5, n_samples),
'loan_amount': np.random.exponential(25000, n_samples),
'loan_term': np.random.choice([12, 24, 36, 48, 60], n_samples),
'num_credit_lines': np.random.poisson(3, n_samples),
'num_delinquencies': np.random.poisson(0.5, n_samples),
'home_ownership': np.random.choice([0, 1, 2], n_samples, p=[0.3, 0.6, 0.1])
}
df = pd.DataFrame(data)
# Create target based on logical rules
risk_score = (
-0.01 * df['credit_score'] +
0.02 * df['debt_to_income'] * 100 +
0.001 * df['loan_amount'] / df['income'] * 100 +
0.1 * df['num_delinquencies'] +
np.random.normal(0, 0.5, n_samples)
)
df['default'] = (risk_score > np.percentile(risk_score, 80)).astype(int)
return df
def train(self, df, target_col='default', test_size=0.2):
"""Train the credit risk model"""
feature_cols = [col for col in df.columns if col != target_col]
X = df[feature_cols].values
y = df[target_col].values
# Split data
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=test_size, random_state=42, stratify=y
)
# Further split for validation
X_train, X_val, y_train, y_val = train_test_split(
X_train, y_train, test_size=0.2, random_state=42, stratify=y_train
)
# Train model
self.model.fit(
X_train, y_train,
eval_set=[(X_val, y_val)],
max_epochs=200,
patience=20,
batch_size=1024,
virtual_batch_size=128,
drop_last=False
)
return X_test, y_test, feature_cols
def evaluate(self, X_test, y_test):
"""Evaluate classification performance"""
predictions = self.model.predict(X_test)
probabilities = self.model.predict_proba(X_test)[:, 1]
metrics = {
'Accuracy': accuracy_score(y_test, predictions),
'Precision': precision_score(y_test, predictions),
'Recall': recall_score(y_test, predictions),
'F1': f1_score(y_test, predictions),
'AUC_ROC': roc_auc_score(y_test, probabilities)
}
return metrics, predictions, probabilities
class TabNetPortfolioAnalyzer:
"""TabNet model for portfolio risk analysis"""
def __init__(self):
self.processor = FinancialDataProcessor()
def create_portfolio_features(self, symbols, weights=None, period='2y'):
"""Create portfolio-level features"""
if weights is None:
weights = np.ones(len(symbols)) / len(symbols)
# Fetch individual stock data
portfolio_data = []
for symbol in symbols:
ticker = yf.Ticker(symbol)
df = ticker.history(period=period)
df['Symbol'] = symbol
df['Weight'] = weights[symbols.index(symbol)]
portfolio_data.append(df)
# Combine data
combined_df = pd.concat(portfolio_data, ignore_index=True)
# Calculate portfolio returns
portfolio_returns = []
dates = combined_df.index.get_level_values(0).unique()
for date in dates:
day_data = combined_df.loc[date]
if len(day_data) == len(symbols):
returns = day_data['Close'].pct_change().fillna(0)
portfolio_return = np.sum(returns * weights)
portfolio_returns.append(portfolio_return)
return np.array(portfolio_returns)
def main():
"""Main function to demonstrate TabNet financial models"""
print("🚀 TabNet Financial Predictive Models Demo")
print("=" * 50)
# 1. Stock Price Prediction
print("\n📈 Stock Price Prediction")
print("-" * 30)
stock_predictor = TabNetStockPredictor()
symbols = ['AAPL', 'GOOGL', 'MSFT', 'AMZN']
try:
X, y, feature_names = stock_predictor.prepare_data(symbols, period='1y')
X_test, y_test = stock_predictor.train(X, y)
metrics, predictions = stock_predictor.evaluate(X_test, y_test)
print("Stock Prediction Metrics:")
for metric, value in metrics.items():
print(f" {metric}: {value:.4f}")
# Plot feature importance
stock_predictor.plot_feature_importance(feature_names)
except Exception as e:
print(f"Stock prediction demo failed: {e}")
# 2. Credit Risk Classification
print("\n🏦 Credit Risk Assessment")
print("-" * 30)
credit_classifier = TabNetCreditRiskClassifier()
try:
# Generate synthetic credit data
credit_data = credit_classifier.create_synthetic_credit_data()
X_test, y_test, feature_cols = credit_classifier.train(credit_data)
metrics, predictions, probabilities = credit_classifier.evaluate(X_test, y_test)
print("Credit Risk Metrics:")
for metric, value in metrics.items():
print(f" {metric}: {value:.4f}")
except Exception as e:
print(f"Credit risk demo failed: {e}")
print("\n✅ Demo completed successfully!")
print("\nNext steps:")
print("1. Customize model parameters for your specific use case")
print("2. Implement additional evaluation metrics")
print("3. Add model interpretability analysis")
print("4. Set up automated model retraining pipeline")
if __name__ == "__main__":
main()