Skip to content

Latest commit

 

History

9 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Financial Genomics

Python 3.9+ License MIT Status: prototype

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.


Key idea

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.


Status

Everything below the discretization and k-mer layer is implemented but not yet validated against real market data. Concretely, right now:

  • data/ and results/ 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 in src/ (VolatilityAdaptiveDiscretizer, src.backtest.strategy, src.backtest.metrics.PerformanceReport), so those test files fail to collect. test_kmer.py collects 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.


Architecture

┌─────────────────────────────────────────────────────────────────┐
│                    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)

Installation

From source (recommended for research)

git clone https://github.com/Abhi183/financial-genomics.git
cd financial-genomics
pip install -e .

Dependencies only

pip install -r requirements.txt

Python 3.9 or higher is required. PyTorch 2.0+ is needed for the LSTM model; CPU-only training is supported.


Quick start

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)

Usage

# 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-missing

Repository structure

financial-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

Mathematical framework

Log-return discretization

Given price series ${P_t}$, compute log-returns and rolling volatility:

$$r_t = \ln!\left(\frac{P_t}{P_{t-1}}\right), \qquad \hat{\sigma}_t = \sqrt{\frac{1}{w}\sum_{i=0}^{w-1}r_{t-i}^2}$$

The standardized return $z_t = r_t / \hat{\sigma}_t$ is thresholded into four regimes:

$$s_t = \begin{cases} \text{A} & z_t < -\tau \ \text{C} & |z_t| \le \varepsilon \ \text{G} & \varepsilon < z_t \le \tau \ \text{T} & z_t > \tau \end{cases}$$

where $\tau$ and $\varepsilon$ are fitted as quantile boundaries on the training set.

K-mer enrichment

For a k-mer $w$ of length $k$, the observed frequency is $f_w$ and the expected frequency under a null permutation distribution is $\mu_w \pm \sigma_w$. The enrichment z-score is:

$$Z_w = \frac{f_w - \mu_w}{\sigma_w}$$

The p-value is estimated by permutation: shuffle the sequence $N = 1000$ times and count how often the shuffled frequency exceeds $f_w$.

Markov transition matrix

The first-order transition probability from symbol $i$ to symbol $j$ is:

$$T_{ij} = \frac{c_{ij}}{\sum_{j'} c_{ij'}}, \qquad \sum_{j} T_{ij} = 1$$

where $c_{ij}$ counts consecutive pairs $(s_t = i,, s_{t+1} = j)$ in the training sequence.

Kelly position sizing

Given estimated win-rate $p$ and mean win/loss ratio $b = \bar{w} / \bar{l}$:

$$f^* = \frac{p \cdot b - (1 - p)}{b} = p - \frac{1-p}{b}$$

A fractional Kelly $f = \alpha f^*$ with $\alpha = 0.5$ is used in practice to reduce variance.


License

MIT. See LICENSE for details.

About

Financial Genomics — volatility-adaptive mapping of stock returns to ACGT alphabet + LSTM-based 'market grammar' learning. Full paper, ML pipeline, backtesting engine.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages