Finite-Context Modeling for DNA Sequences
about
This project implements a Finite-Context Model (FCM) — a k-order Markov chain — to model, compress, and generate symbolic sequences, developed for a university Algorithmic Information Theory course. The core idea is that for each position in a sequence, the model looks at the preceding k characters (the context) and estimates the probability of the next character based on how often it followed that context during training. Laplace smoothing with a tunable parameter alpha prevents zero-probability contexts from breaking the model. The information content of each symbol is then -log2(P(c|context)), and the average bits-per-symbol (BPS) across a sequence is the key metric: lower BPS means the model captured more of the sequence's regularities.
The C++17 core consists of two programs. fcm.cpp trains a model from an input sequence using hash maps for O(1) average-case context lookups, computes per-symbol and average BPS, and serializes the trained model to a binary file for reuse. generator.cpp produces new synthetic sequences by sampling from the trained model's probability distributions using weighted random sampling; if it encounters a context never seen during training, it falls back to the most similar known context via Levenshtein edit distance, keeping generation statistically plausible rather than crashing or defaulting to a uniform distribution.
The primary application domain is DNA sequences (alphabet A, T, G, C), though the implementation is alphabet-agnostic. A Python and Jupyter analysis layer validates and extends the C++ core: an independent entropy calculator computes Shannon and conditional entropy as ground-truth references, a complexity profiler runs the FCM across parameter grids and visualizes per-position bit complexity to reveal repetitive versus high-entropy regions, and a Zstandard benchmark provides a real-world compression baseline. A full hyperparameter search sweeps context order k (1-10) and smoothing alpha (10^-1 to 10^-10) across 100 configurations per sequence, with an end-to-end benchmark script using hyperfine to compare real versus generated sequence BPS for fidelity assessment.
highlights
- ·Built an efficient C++17 FCM engine with hash-map-based O(1) context lookups, Laplace smoothing, and binary model serialization
- ·Implemented a sequence generator with Levenshtein-distance fallback for unseen contexts, ensuring statistically plausible generation
- ·Validated results against independent Shannon/conditional entropy calculations and benchmarked against the Zstandard compressor
- ·Ran a 100-configuration hyperparameter grid search (context order x smoothing) with hyperfine-based performance benchmarking
- ·Achieved a top grade of 18/20
stack
Core
Analysis
Theory