Build variable length markov models
Blazingly fast top down approeach using the Peres-Shield method
Built with Rust
Documentation • Paper • Author • Literature
Implementation of Variable Length Markov Chains (VLMC) for Python.
Suffix tree building is done top-down using the Peres-Shield order estimation method. It is written in Rust with Python Bindings.
Pre-built packages for many Linux, Windows, and OSX systems are available in PyPI and can be installed with:
pip install vlmc
On uncommon architectures, you may need to first
install Cargo before running pip install vlmc
.
In order to compile from source you will need to install Rust/Cargo and maturin for the python bindings. Maturin is best used within a Python virtual environment:
# activate your desired virtual environment first, then:
pip install maturin
git clone https://github.com/antonio-leitao/vlmc.git
cd vlmc
# build and install the package:
maturin develop --release
Complete documentation is available here
import vlmc
tree = vlmc.VLMC(alphabet_size,max_depth=10)
Parameters:
alphabet_size
: Total number of symbols in the alphabet. This number has to be bigger than the highest integer encountered, else it will cause runtime errors.max_depth
: Maximum depth of tree. Subsequences whose length exceed themax_depth
will not be considered nor counted.
Note fit method returns
None
and notself
. This is by design as to not expose the rust object to python.
data = [
[1,2,3],
[2,3],
[1,0,1],
[2]
]
tree.fit(data)
Arguments:
data
: List of lists containing sequences of discrete values to fit on. Values are assumed to be integers form0
toalphabet_size
. List is expected to be two dimensional.
Given a sequence, returns the longest suffix that is present in the VLMC.
suffix = tree.get_suffix(sequence)
Arguments:
sequence
: list of integers representing a sequence of discrete varaibles.
Returns:
suffix
: longest suffix of sequence that is present in the VLMC.
Gets the total number of occurences of a given sequence of integers.
Will throw a KeyError
if the sequence is not a tree node. Consider using get_suffix
to make sure to get a tree node.
counts = tree.get_counts(sequence)
Arguments:
sequence
: list of integers representing a sequence of discrete varaibles.
Returns:
counts
: integer
Gets the vector of probabilities over the entire alphabet for the given sequence.
Will throw a KeyError
if the sequence is not a tree node. Consider using get_suffix
to make sure to get a tree node.
probabilities = tree.get_distribution(sequence)
Arguments:
sequence
: list of integers representing a sequence of discrete variables.
Returns:
probabilities
: list of floats representing the probability of observing a specific state (index) as the next symbol.
contexts = tree.get_contexts()
Returns:
contexts
: list of relevant contexts according to the Peres-Shield tree prunning method. Contexts are ordered by length.
After experimentation the best possible idea for paralelization would be to create different hashmaps for each sunsequence length.
Hashmaps are then joined from longest to smallest.
The hashmap at max_depth + 1
can be discarded after.
Could be very fast depending on merging algo.