Machine Learning¶
GNN Predictor¶
Temporal Graph Neural Network predictor for spectral metric evolution.
Replaces the flat LSTM with a GCN encoder that operates on the actual interbank graph at each timestep, producing graph-level embeddings that feed into a temporal LSTM for spectral metric forecasting.
Architecture: GCNConv layers → global_mean_pool → LSTM → FC → [λ₂, gap, ρ]
- class scr_financial.ml.gnn_predictor.GNNEncoder(in_channels, hidden_channels, num_gcn_layers=3, dropout=0.1)[source]¶
Bases:
ModuleMulti-layer GCN that produces a graph-level embedding.
- forward(x, edge_index, edge_weight=None, batch=None)[source]¶
Define the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.- Parameters:
x (Tensor)
edge_index (Tensor)
edge_weight (Tensor | None)
batch (Tensor | None)
- Return type:
Tensor
- class scr_financial.ml.gnn_predictor.TemporalGNN(node_features, hidden_dim=64, output_dim=3, num_gcn_layers=3, num_lstm_layers=2, dropout=0.1)[source]¶
Bases:
ModuleGNN encoder + LSTM for temporal graph sequences → spectral predictions.
- Parameters:
- forward(graph_sequences)[source]¶
Define the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class scr_financial.ml.gnn_predictor.GNNPredictor(seq_len=10, hidden_dim=64, num_gcn_layers=3, num_lstm_layers=2, dropout=0.1)[source]¶
Bases:
objectDrop-in replacement for SCGPredictor using a temporal GNN.
Supports a progress_callback(epoch, total_epochs, train_loss, test_loss) for real-time UI updates during training.
- Parameters:
- static extract_graph_snapshot(sim)[source]¶
Extract graph snapshot (node features + edges + spectral targets) from sim.
- static extract_features(sim, spectral_fn)[source]¶
Compatibility shim — extract scalar features like SCGPredictor.
SCG Predictor¶
SCG-aware predictor for spectral metric evolution.
Wraps the existing LSTMPredictor to forecast spectral features (algebraic connectivity, spectral gap, spectral radius) from stochastic ABM trajectories.
- class scr_financial.ml.scg_predictor.SCGPredictor(seq_len=10, hidden_dim=32, num_layers=2)[source]¶
Bases:
objectTrain an LSTM on ABM-generated spectral trajectories and predict forward.
- static extract_features(sim, spectral_fn)[source]¶
Extract one feature vector from the current simulation state.
- Parameters:
sim (BankingSystemSimulation)
spectral_fn (callable returning spectral data dict (eigenvalues, gap_size, etc.))
- Return type:
- build_training_data(feature_history)[source]¶
Convert a list of per-step feature dicts into (X, y) arrays.
X: shape (samples, seq_len, n_features) y: shape (samples, n_targets)
Base Predictors¶
Predictors for financial network analysis.
This module provides predictors for forecasting network evolution and market indicators.
- class scr_financial.ml.predictors.NetworkPredictor(method='rf')[source]¶
Bases:
objectPredictor for financial network evolution.
This class implements methods for predicting how the financial network will evolve over time based on historical data and external factors.
- Parameters:
method (str)
- method¶
Prediction method identifier.
- model¶
Underlying sklearn estimator.
- scaler_X¶
Feature scaler.
- scaler_y¶
Target scaler.
- __init__(method='rf')[source]¶
Initialize the network predictor.
- Parameters:
method (str) – Prediction method (‘rf’ for Random Forest, ‘gb’ for Gradient Boosting, ‘elastic’ for Elastic Net).
- Raises:
ValueError – If method is not one of the supported values.
- Return type:
None
- prepare_features(network_history, system_indicators)[source]¶
Prepare features for network prediction.
- predict(X)[source]¶
Make predictions with the fitted model.
- Parameters:
X (ndarray) – Feature matrix.
- Returns:
Predicted values.
- Raises:
RuntimeError – If
fit()has not been called yet.- Return type:
- class scr_financial.ml.predictors.MarketPredictor(input_dim, hidden_dim, output_dim, num_layers=2)[source]¶
Bases:
objectPredictor for market indicators.
This class implements a deep learning model for predicting market indicators based on historical data and network features.
- model¶
Underlying LSTM model.
- scaler_X¶
Feature scaler.
- scaler_y¶
Target scaler.
- seq_length¶
Default sequence length used during prepare_sequences.
- prepare_sequences(features, targets, seq_length)[source]¶
Prepare sequences for LSTM training.
- Parameters:
- Returns:
Tuple (X, y) where X has shape [n_sequences, seq_length, n_features] and y has shape [n_sequences, n_targets].
- Return type:
- train(X, y, batch_size=32, epochs=100, learning_rate=0.001, device='cpu')[source]¶
Train the market predictor.
- Parameters:
X (ndarray) – Sequence data of shape [n_sequences, seq_length, n_features].
y (ndarray) – Target data of shape [n_sequences, n_targets].
batch_size (int) – Batch size for training.
epochs (int) – Number of training epochs.
learning_rate (float) – Learning rate for optimizer.
device (str) – Device to train on (‘cpu’ or ‘cuda’).
- Returns:
Dictionary containing training history.
- Return type:
- predict(features, device='cpu')[source]¶
Make predictions with the trained model.
- Parameters:
- Returns:
Predicted values.
- Raises:
RuntimeError – If
prepare_sequences()has not been called yet.- Return type:
- class scr_financial.ml.predictors.LSTMPredictor(input_dim, hidden_dim, output_dim, num_layers=2)[source]¶
Bases:
ModuleLSTM-based predictor for time series forecasting.
Hidden dimension for LSTM.
- num_layers¶
Number of LSTM layers.
- lstm¶
LSTM layer.
- fc¶
Fully connected output layer.
- forward(x)[source]¶
Forward pass through the model.
Note
Hidden states are re-initialised to zeros on every forward call (stateless inference).
- Parameters:
x (Tensor) – Input tensor of shape [batch_size, seq_length, input_dim].
- Returns:
Output tensor of shape [batch_size, output_dim].
- Return type:
Tensor
Sentiment Model¶
Sentiment analysis model for financial market data.
This module provides an LSTM-based model for analyzing market sentiment from financial time series data.
- class scr_financial.ml.sentiment_model.LSTMSentimentModel(input_dim, hidden_dim, num_layers=2, output_dim=1, dropout=0.2)[source]¶
Bases:
ModuleLSTM-based sentiment analysis model for financial time series.
Dimension of hidden state.
- num_layers¶
Number of LSTM layers.
- lstm¶
LSTM layer.
- dropout¶
Dropout layer.
- fc¶
Fully connected output layer.
- __init__(input_dim, hidden_dim, num_layers=2, output_dim=1, dropout=0.2)[source]¶
Initialize the LSTM sentiment model.
- forward(x)[source]¶
Forward pass through the model.
Note
Hidden states are re-initialised to zeros on every forward call (stateless inference).
- Parameters:
x (Tensor) – Input tensor of shape [batch_size, seq_len, input_dim].
- Returns:
Output tensor of shape [batch_size, output_dim].
- Return type:
Tensor
- scr_financial.ml.sentiment_model.prepare_sentiment_data(data, target, seq_length, train_split=0.8)[source]¶
Prepare data for sentiment model training.
- Parameters:
- Returns:
Tuple containing (X_train, y_train, X_test, y_test).
- Raises:
ValueError – If seq_length is not positive or train_split is outside (0, 1).
- Return type:
Tuple[Tensor, Tensor, Tensor, Tensor]
- scr_financial.ml.sentiment_model.train_sentiment_model(model, X_train, y_train, X_test, y_test, batch_size=64, epochs=100, learning_rate=0.001, device='cpu')[source]¶
Train the sentiment model.
- Parameters:
model (Module) – The model to train.
X_train (Tensor) – Training features.
y_train (Tensor) – Training targets.
X_test (Tensor) – Test features.
y_test (Tensor) – Test targets.
batch_size (int) – Batch size for training.
epochs (int) – Number of training epochs.
learning_rate (float) – Learning rate for optimizer.
device (str) – Device to train on (‘cpu’ or ‘cuda’).
- Returns:
Dictionary containing training history with keys
'train_loss'and'test_loss'.- Return type:
- scr_financial.ml.sentiment_model.predict_sentiment(model, features, seq_length, device='cpu')[source]¶
Predict sentiment score from features.
- Parameters:
- Returns:
Predicted sentiment score.
- Raises:
ValueError – If features has fewer than seq_length time steps.
- Return type: