A research framework that borrows the analytical toolkit of computational genomics — k-mer frequency analysis, transition matrices, motif enrichment tests, sequence grammar models — and points it at discretized financial return series. Daily log-returns get mapped to a four-letter alphabet (A, C, G, T) by volatility-adjusted regime, so a price history turns into a string you can run the same statistical machinery on that you'd use to study DNA. An LSTM trained on those strings learns recurring patterns and outputs a probability distribution over the next regime, which feeds a Kelly-sized trading strategy meant to be evaluated under walk-forward cross-validation.
This is a prototype, not a validated trading system. See Status below.
Markets, like genomes, might contain recurring structural motifs that carry information about what comes next. Define a four-letter alphabet by volatility regime:
| Symbol | Regime | Intuition |
|---|---|---|
| A | Crash | Large negative return — acute stress, analogous to a deletion or frameshift |
| C | Calm | Near-zero return in a low-volatility window — quiescent baseline |
| G | Growth | Moderate positive return — normal drift |
| T | Spike | Large positive return — sudden upside surprise, analogous to an insertion |
A two-year equity history becomes a string like ...GCGCGTATCGGCGACG.... Three-letter motifs such as GCG or TAT should, in principle, appear at frequencies that deviate from random expectation if there's real structure to find. An LSTM trained on these strings learns which patterns predict which next symbol.
Everything below the discretization and k-mer layer is implemented but not yet validated against real market data. Concretely, right now:
data/andresults/are empty in this repo (gitignored) — no downloaded prices, no saved backtest output. Nothing here has actually been run end to end.- The bundled notebook (
notebooks/financial_genomics_pipeline.ipynb) has never been executed — no saved cell outputs. - The test suite doesn't fully pass. Several tests reference class/module names (
ReturnDiscretizer,src.backtest.strategies,src.backtest.report) that don't match what's currently insrc/(VolatilityAdaptiveDiscretizer,src.backtest.strategy,src.backtest.metrics.PerformanceReport), so those test files fail to collect.test_kmer.pycollects but has 8 failing cases out of 54.
An earlier version of this README had a full results table (accuracy, Sharpe, drawdown vs. baselines). Those numbers weren't backed by an actual run — I pulled them together before running any real experiment, they read fine, and I let them sit there longer than they should have. They're gone now. If this project produces real backtest results at some point, they'll go here with the code and data that generated them.
What's real: the discretization logic, k-mer analysis, and metric functions are implemented and mostly covered by passing tests. The LSTM model, backtest engine, and baselines (ARIMA, GARCH, SAX, Matrix Profile) exist as code but haven't been run against live data yet.
┌─────────────────────────────────────────────────────────────────┐
│ Financial Genomics Pipeline │
└─────────────────────────────────────────────────────────────────┘
Price Data (OHLCV)
│
▼
Log Returns r_t = ln(P_t / P_{t-1})
│
▼
Volatility Regimes σ_t = rolling std (window=20)
│
▼
Discretization ──────── threshold on r_t / σ_t ──────────────┐
│ │
│ r/σ < -τ → A (crash) │
│ |r/σ| < ε → C (calm) │
│ 0 < r/σ < τ → G (growth) │
│ r/σ > τ → T (spike) │
└────────────────────────────────────────────────────────┘
│
▼
{A,C,G,T} Sequence "...GCGTATCGGCGACG..."
│
├──── K-mer Analysis ────────────────────────────────────▶
│ • frequency_table (k=1,2,3,4) │
│ • transition matrix (Markov order 1) │
│ • enrichment test (permutation p-values) │
│ • top_kmers (sorted by frequency) │
│ │
└──── LSTM Grammar Model ────────────────────────────────▶
• embedding layer (vocab=4) │
• stacked LSTM (hidden=128, layers=2) │
• linear + softmax → P(next symbol | context) │
│ │
▼ │
Trading Signal ◀───────────────────────────────── ┘
(LONG / SHORT / FLAT, Kelly-sized)
│
▼
Backtest Engine
• walk-forward validation (retrain monthly)
• commission + slippage model
• PerformanceReport (Sharpe, MaxDD, Win Rate)
git clone https://github.com/Abhi183/financial-genomics.git
cd financial-genomics
pip install -e .pip install -r requirements.txtPython 3.9 or higher is required. PyTorch 2.0+ is needed for the LSTM model; CPU-only training is supported.
import numpy as np
from src.features.discretization import VolatilityAdaptiveDiscretizer
from src.features.kmer_analysis import KmerAnalyzer
from src.models.lstm_model import GenomicLSTM, GenomicLSTMTrainer
# 1. Discretize returns into an ACGT sequence
returns = np.load("data/processed/spy_log_returns.npy")
disc = VolatilityAdaptiveDiscretizer(window=20)
sequence = disc.fit_transform(returns) # e.g. "GCGTATCGGCGA..."
# 2. Analyse k-mer grammar
analyzer = KmerAnalyzer(k=3)
print(analyzer.top_kmers(sequence, n=10)) # [(kmer, count), ...]
print(analyzer.transition_matrix(sequence)) # 4x4 stochastic matrix
# 3. Train LSTM on the integer-encoded sequence
int_seq = disc.encode_to_int(sequence)
model = GenomicLSTM(num_classes=4, embedding_dim=8, hidden_size=128, num_layers=2)
trainer = GenomicLSTMTrainer(model)
X, y = trainer.prepare_sequences(int_seq, seq_len=50)
history = trainer.train(X, y, epochs=50, batch_size=64)# Full pipeline: data download → discretize → train → backtest → report
python -m src.pipeline --config configs/config.yaml --mode full
# Just training
python -m src.pipeline --config configs/config.yaml --mode train
# Just backtesting a trained model
python -m src.pipeline --config configs/config.yaml --mode backtest
# Just the summary report
python -m src.pipeline --config configs/config.yaml --mode report
# Run the test suite
pytest tests/ -v --cov=src --cov-report=term-missingfinancial-genomics/
├── configs/
│ └── config.yaml # Master configuration
├── data/
│ ├── raw/ # Downloaded OHLCV data (gitignored)
│ └── processed/ # Computed log-returns and sequences (gitignored)
├── models/ # Saved model checkpoints (gitignored)
├── notebooks/
│ └── financial_genomics_pipeline.ipynb
├── results/ # Backtest reports (gitignored)
├── src/
│ ├── pipeline.py # CLI entry point
│ ├── features/
│ │ ├── discretization.py # VolatilityAdaptiveDiscretizer (ACGT mapping)
│ │ ├── kmer_analysis.py # KmerAnalyzer (frequency, transitions, enrichment)
│ │ └── motif_discovery.py # MotifDiscovery
│ ├── models/
│ │ ├── lstm_model.py # GenomicLSTM, GenomicLSTMTrainer
│ │ └── baselines.py # ARIMA, GARCH, SAX, Matrix Profile baselines
│ ├── backtest/
│ │ ├── engine.py # BacktestEngine, WalkForwardBacktest, BacktestResult
│ │ ├── strategy.py # GenomicTradingStrategy, BuyAndHoldStrategy, GARCHStrategy
│ │ └── metrics.py # sharpe_ratio, max_drawdown, kelly_fraction, PerformanceReport
│ ├── data/
│ │ ├── loader.py # MarketDataLoader (yfinance download)
│ │ └── preprocessor.py
│ └── visualization/
│ └── plots.py # Equity curve, regime heatmap, k-mer bar charts
├── tests/
│ ├── test_discretization.py
│ ├── test_kmer.py
│ ├── test_lstm.py
│ └── test_backtest.py
├── requirements.txt
├── setup.py
└── README.md
Given price series
The standardized return
where
For a k-mer
The p-value is estimated by permutation: shuffle the sequence
The first-order transition probability from symbol
where
Given estimated win-rate
A fractional Kelly
MIT. See LICENSE for details.