๐ markrel
Markov Chain Document Relevance โ School your documents with Markov chains!
๐ What is markrel? A fast, interpretable Python library that uses Markov chains to predict document relevance. Like a school of mackerel navigating the seas, markrel traces probabilistic paths through similarity space to find the most relevant documents.
๐๐๐
๐๐๐๐
๐๐๐๐๐ โ Your documents
๐๐๐๐ swimming through
๐๐๐ relevance space!
๐ Table of Contents
- ๐ Overview
- โก Quick Start
- ๐ Tutorial
- ๐ฏ Why markrel?
- ๐ Benchmarks
- ๐ง Installation
- ๐จ How It Works
- โ Advantages & Use Cases
- โ Limitations
- ๐ Calibrating External Scorers
- ๐ API Reference
- ๐ License
๐ Overview
markrel predicts whether a document is relevant to a query using Markov chains and similarity metrics. It's designed for:
- ๐ Semantic search re-ranking โ Filter top-k results with learned relevance
- ๐ง Document classification โ Sort documents by relevance to topics
- ๐ค Response selection โ Pick best answers from candidate pool
- โก High-throughput filtering โ Process 50K+ documents/second
Key Features
| Feature | Description |
|---|---|
| ๐ 8 Similarity Metrics | Cosine, Euclidean, Jaccard, Overlap, Dice, Manhattan, Chebyshev, Dot Product |
| ๐ง Markov Chain Learning | Learns P(relevance) from your data, not generic rules |
| ๐ฏ 3 Optimization Modes | Tune for F1, Recall, or Precision based on your needs |
| โก Fast Inference | 50K+ samples/second after training |
| ๐ง Embedding Agnostic | Works with BERT, OpenAI, sentence-transformers, or TF-IDF |
| ๐ Interpretable | See exactly why a document was flagged as relevant |
โก Quick Start (3 Minutes)
1. Install
pip install markrel
2. Train & Predict
from markrel import MarkovRelevanceModel
# Your data: queries, documents, and relevance labels
queries = ["machine learning tutorial", "baking recipes", "neural networks"]
documents = ["intro to ML", "best chocolate cake", "deep learning guide"]
labels = [1, 0, 1] # 1 = relevant, 0 = not relevant
# Create and train (using optimal config from benchmarks)
model = MarkovRelevanceModel(
metrics=["euclidean"], # Best single metric
n_bins=35, # Optimized for F1
bin_strategy="uniform"
)
model.fit(queries, documents, labels)
# Predict relevance
probs = model.predict_proba(
["deep learning", "pasta recipes"],
["neural networks", "italian cooking"]
)
print(probs) # [0.82, 0.15]
3. Use with Modern Embeddings
from sentence_transformers import SentenceTransformer
# Load BGE-M3 (best model per benchmarks)
encoder = SentenceTransformer('BAAI/bge-m3')
# Encode your texts
query_emb = encoder.encode(["what is ML?"])
doc_emb = encoder.encode(["machine learning is..."])
# Train with embeddings (disable TF-IDF)
model = MarkovRelevanceModel(
metrics=["euclidean"],
use_text_vectorizer=False # Use your embeddings
)
model.fit(query_emb, doc_emb, [1])
That's it! ๐ You now have a relevance model trained on your data.
๐ Tutorial: Complete Walkthrough
Step 1: Prepare Your Data
Markrel needs (query, document, label) triples:
# Example: Question-Answer Relevance Dataset
queries = [
"What is machine learning?",
"How does photosynthesis work?",
"Best pizza recipe?",
"Explain neural networks",
"Types of pasta?",
]
documents = [
"Machine learning is a subset of AI...",
"Photosynthesis converts sunlight into energy...",
"Authentic Neapolitan pizza requires...",
"Neural networks are computing systems...",
"Popular pasta types include spaghetti...",
]
# Labels: 1 = relevant, 0 = not relevant
labels = [1, 1, 0, 1, 0]
Step 2: Choose Your Configuration
Based on our benchmarks, here are recommended configs:
# Option A: Balanced (Best F1)
model = MarkovRelevanceModel(
metrics=["euclidean"],
n_bins=35,
bin_strategy="uniform"
)
# Option B: Catch Everything (Best Recall)
model = MarkovRelevanceModel(
metrics=["euclidean"],
n_bins=7,
bin_strategy="uniform"
)
# Option C: Strict Filtering (Best Precision)
model = MarkovRelevanceModel(
metrics=["cosine", "euclidean"],
n_bins=24,
bin_strategy="uniform"
)
Step 3: Train the Model
# Train on your data
model.fit(queries, documents, labels)
# Inspect what the model learned
print(model.summary())
Step 4: Make Predictions
# Get probability scores
probabilities = model.predict_proba(
new_queries,
new_documents
)
# Apply threshold (default 0.5, or optimized threshold from benchmarks)
threshold = 0.251 # F1-optimized threshold
predictions = probabilities >= threshold
# Or use built-in prediction with custom threshold
predictions = model.predict(
new_queries,
new_documents,
threshold=0.251
)
Step 5: Advanced Usage with Embeddings
For best results, use modern embeddings:
from sentence_transformers import SentenceTransformer
import numpy as np
# Load encoder (BGE-M3 recommended)
encoder = SentenceTransformer('BAAI/bge-m3')
# Large-scale training
train_queries = encoder.encode(train_query_texts)
train_docs = encoder.encode(train_doc_texts)
test_queries = encoder.encode(test_query_texts)
test_docs = encoder.encode(test_doc_texts)
# Train markrel
model = MarkovRelevanceModel(
metrics=["euclidean"],
n_bins=35,
use_text_vectorizer=False # Important!
)
model.fit(train_queries, train_docs, train_labels)
# Batch prediction (fast!)
probs = model.predict_proba(test_queries, test_docs)
Complete Example: Email Classifier
from markrel import MarkovRelevanceModel
from sentence_transformers import SentenceTransformer
# Load data
emails = ["Urgent: Project deadline moved up", "Weekly team newsletter", "Invoice #1234 payment required"]
queries = ["urgent project emails", "team updates", "billing notifications"]
labels = [1, 0, 1] # Which emails are relevant to which query
# Encode with BGE-M3
encoder = SentenceTransformer('BAAI/bge-m3')
email_emb = encoder.encode(emails)
query_emb = encoder.encode(queries)
# Train relevance classifier
model = MarkovRelevanceModel(
metrics=["euclidean"],
n_bins=35,
use_text_vectorizer=False
)
model.fit(query_emb, email_emb, labels)
# Classify new emails
new_emails = encoder.encode([
"RE: Project timeline discussion",
"Your Amazon order has shipped",
"URGENT: Server outage in production"
])
search_query = encoder.encode(["urgent project emails"])
relevance_scores = model.predict_proba(search_query, new_emails)
print(f"Email relevance scores: {relevance_scores}")
# Output: [0.78, 0.12, 0.91]
๐ฏ Why markrel?
The Problem
Traditional document relevance uses:
- Fixed thresholds: "Cosine > 0.7 = relevant" (ignores domain-specific patterns)
- Linear scoring: Assumes similarity linearly predicts relevance
- Black-box models: Can't explain why a document was selected
The Solution
Markrel uses Markov chains to learn non-linear relevance patterns:
Similarity Score โ Bin Mapping โ P(Relevance)
0.95 โโโ Bin 9 โโโ P(rel)=0.92 โ Highly relevant
0.75 โโโ Bin 7 โโโ P(rel)=0.68 โ Maybe relevant
0.45 โโโ Bin 4 โโโ P(rel)=0.23 โ Probably not
0.15 โโโ Bin 1 โโโ P(rel)=0.05 โ Not relevant
Each bin learns its own probability from your training data, capturing domain-specific patterns.
๐ Benchmarks
WikiQA Question-Answer Relevance
Results on 6,165 test samples (4.8% positive class):
| Optimization | F1 | Recall | Precision | Config | Use Case |
|---|---|---|---|---|---|
| Balanced | 0.370 | 0.362 | 0.379 | 35 bins, euclidean | General purpose |
| Recall | 0.091 | 1.000 | 0.048 | 7 bins, euclidean | Catch all relevant |
| Precision | 0.007 | 0.003 | 1.000 | 24 bins, cos+euc | Strict filtering |
Embedding Model Comparison
| Model | F1 | AUC | Speed |
|---|---|---|---|
| BGE-M3 โญ | 0.343 | 0.815 | 51K/s |
| RoBERTa-large | 0.323 | 0.828 | 54K/s |
| MiniLM-L6 | 0.322 | 0.799 | 61K/s |
Winner: BGE-M3 for accuracy, MiniLM for speed.
๐ง Installation
From PyPI (when published)
pip install markrel
From Source
git clone https://github.com/yourusername/markrel.git
cd markrel
pip install -e .
Development Install
pip install -e ".[dev]"
pytest tests/ -v
Dependencies
numpy >= 1.20.0
scikit-learn >= 1.0.0
Optional for embeddings:
sentence-transformers >= 2.0.0
๐จ How It Works
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ ๐ markrel Pipeline โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
๐ฅ INPUT ๐ง PROCESSING
โโโโโโโโ โโโโโโโโโโโ
โโโโโโโโโโโโ โโโโโโโโโโโโโโโ
โ Query โโโโ โ Embed with โ
โ "What โ โ โ BGE-M3 โ
โ is ML?" โ โ โ (1024-dim) โ
โโโโโโโโโโโโ โ โโโโโโโโโโโโโโโ
โ โ
โโโโโโโโโโโโ โ โผ
โ Document โโโโ โโโโโโโโโโโโโโโ
โ "Machine โ โ Similarity โ
โ learning โ โ Computation โ
โโโโโโโโโโโโ โโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโ
โ ๐ Markov โ
โ Chain โ
โ โ
โ Bin 0: 5% โ
โ Bin 4: 23% โ
โ Bin 7: 68% โ
โ Bin 9: 92% โ
โโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโ
โ P(Relevant) โ
โ 0.75 โ
โโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโ
โ โ
Relevantโ
โโโโโโโโโโโโโโโ
๐ค OUTPUT: Probability + Prediction
The Markov Chain
Unlike fixed thresholds, markrel learns a probability for each similarity bin:
Similarity: 0.0 โโโ 0.2 โโโ 0.4 โโโ 0.6 โโโ 0.8 โโโ 1.0
โ โ โ โ โ โ
โผ โผ โผ โผ โผ โผ
Bin: [Bin0] [Bin1] [Bin2] [Bin3] [Bin4] [Bin5]
โ โ โ โ โ โ
P(Relevant): 0.05 0.12 0.35 0.68 0.89 0.95
โ โ โ โ โ โ
๐ซ โ ๏ธ โ ๏ธ โ โ โ
Not Rel. Maybe Likely Highly
Relevant Relevant Relevant
โ Advantages & Use Cases
โ Advantages
| Feature | Benefit |
|---|---|
| ๐ฏ Domain Adaptable | Learns from YOUR data, not generic assumptions |
| ๐ Non-linear | Captures complex similarityโrelevance patterns |
| ๐ง Tunable | Optimize for F1, Recall, or Precision |
| โก Fast | 50K+ samples/second after training |
| ๐ Interpretable | See P(relevance) per bin; debug predictions |
| ๐งฉ Embedding Agnostic | Use BERT, OpenAI, or TF-IDF |
| ๐ฆ Lightweight | No GPU required; pure NumPy |
โ Best Use Cases
| Use Case | Why markrel Works |
|---|---|
| ๐ Semantic Search Re-ranking | Fast second-stage filtering of retrieved docs |
| ๐ง Email Classification | Learn relevance patterns from your mail |
| ๐ Document Similarity | Semantic matching beyond keywords |
| ๐ค Chatbot Responses | Select best response from candidates |
| โก Real-time Filtering | High-throughput with low latency |
โ Limitations
| Limitation | Solution |
|---|---|
| Requires labeled data | Use transfer learning or synthetic labels |
| Class imbalance | Use Recall-optimized config for rare positives |
| No native ranking | Pair with BM25 for initial retrieval |
| Single-pair only | Use cross-encoders for document sets |
๐ Calibrating External Scorers (e.g. Jev)
If you're using an external relevance judge โ an LLM-based reranker like Jev,
a cross-encoder, or any model that outputs a probability โ that probability
is calibrated to its own judgment task, not to your domain's actual base
rate. JevCalibrator reuses markrel's bin-based Markov chain to learn a
recalibration curve, P(actually relevant | external score), fit against
your own ground-truth labels:
from markrel.integrations import JevCalibrator
# jev_probs: raw P(relevant) from an external scorer, per training pair
# labels: independent ground-truth relevance labels for the same pairs
cal = JevCalibrator(n_bins=20, bin_strategy="quantile")
cal.fit(jev_probs, labels)
# Recalibrated probability for a new score
cal.predict_proba([0.83])
It can also combine the recalibrated score with a markrel embedding-based
MarkovRelevanceModel using the same Bayesian odds-product logic markrel
uses internally to combine its own metrics โ useful as a cheap first-pass
filter that only escalates borderline cases to the external scorer.
Note: the labels used to fit JevCalibrator must be independent of the
scorer being calibrated (e.g. human review, click-through data). Calibrating
a scorer against labels it produced itself just reproduces the scorer.
Full walkthrough, API table, and cascade example: docs/jev-calibrator.md.
๐ API Reference
MarkovRelevanceModel
from markrel import MarkovRelevanceModel
model = MarkovRelevanceModel(
metrics=["euclidean"], # Similarity metrics to use
n_bins=35, # Number of bins (10-50)
bin_strategy="uniform", # "uniform" or "quantile"
smoothing=1.0, # Laplace smoothing
combine_rule="bayesian", # "bayesian" or "mean"
use_text_vectorizer=True # Auto-vectorize text
)
Methods:
fit(queries, documents, labels)โ Train the modelpredict_proba(queries, documents)โ Get relevance probabilities [0-1]predict(queries, documents, threshold=0.5)โ Binary predictions {0, 1}summary()โ Model statisticsget_metric_probabilities(metric)โ Bin probabilities
๐ License
MIT License โ See LICENSE for details.
๐ About the Name
Markrel = Markov Chain + Relevance
Like a school of mackerel swimming through the ocean, markrel navigates the sea of documents, tracing probabilistic paths to find the most relevant matches. Each fish (document) follows the currents (similarity scores) toward their destination (relevance). ๐๐๐
Ready to school your documents? Get started with Quick Start โ
๐
๐๐๐
๐๐๐๐๐
๐๐๐
๐
Release files for markrel 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| markrel-0.2.0.tar.gz | 1.8 MB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| markrel-0.2.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 1.8 MB
Release files / markrel-0.2.0.tar.gz
| Download URL | markrel-0.2.0.tar.gz |
|---|---|
| Size | 1.8 MB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
08dddb119927d9ecba904059faa7d7ca6964e354e1c519c99da5a8c01607c8fb
|
|
BLAKE2b-256 checksum How to use checksums |
a90b108b2bcedaa04dc401fbf1b754d073ced274905961e0ad61172c68d7cece
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 25, 2026.
Transparency logRelease files / markrel-0.2.0-py3-none-any.whl
| Download URL | markrel-0.2.0-py3-none-any.whl |
|---|---|
| Size | 39.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
299a74327e82d890b846f3893c41b9c3a9e9cb50b41316628b394d7c56cbaf7a
|
|
BLAKE2b-256 checksum How to use checksums |
695d1049da6c99e40da18e103ab42a0218050ef0db0f1b1f205ed7a1f6850738
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 25, 2026.
Transparency log