Alternative Data and NLP in Quant Strategies › Natural Language Processing for Finance
Quantitative FinanceAll chapters Alternative Data and NLP in Quant Strategies › Natural Language Processing for FinanceOne section · Sections in this chapter4 / 18 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
Text represents the largest and most underexploited category of alternative data. Earnings call transcripts, news articles, analyst reports, social media posts, regulatory filings, and patent documents collectively contain enormous information that humans cannot process at scale. Natural language processing provides the tools to extract structured insights from this unstructured text.
From Text to Numbers
The fundamental challenge in text analytics is representation: how do we convert words and sentences into numerical features that machine learning models can process? This problem is more complex than it might initially appear because language is inherently high-dimensional, context-dependent, and ambiguous. Every sentence carries meaning through the individual words it contains, their arrangement, their relationships to one another, and the broader context in which they appear. Our goal is to capture as much of this meaning as possible in a numerical form that algorithms can manipulate.
The bag-of-words model is the simplest approach. It treats a document as an unordered collection of words, ignoring grammar and word order entirely. Under this representation, each unique word in the vocabulary becomes a separate feature dimension, and documents are represented as vectors of word counts or frequencies. The resulting vector space has as many dimensions as there are unique words in the corpus, which can easily reach tens of thousands of dimensions for realistic text collections.
Consider two short sentences about a stock:
- "Revenue growth exceeded expectations"
- "Expectations exceeded by revenue growth"
A bag-of-words representation would treat these identically: both contain the same words with the same frequencies. The model captures what topics are discussed but not how they relate. This limitation reveals a fundamental tradeoff. Simpler models like bag-of-words are computationally efficient and interpretable. However, they discard the relationships conveyed by word order. More sophisticated approaches attempt to recover this lost information, but at the cost of greater complexity.
import pandas as pd
from sklearn.feature_extraction.text import CountVectorizer
# Sample financial text snippets
documents = [
"Revenue growth exceeded analyst expectations for Q3",
"The company missed earnings estimates significantly",
"Strong revenue growth drove share price higher",
"Analysts downgraded the stock after weak earnings",
]
# Create bag-of-words representation
vectorizer = CountVectorizer()
bow_matrix = vectorizer.fit_transform(documents)
# Get feature names (vocabulary)
feature_names = vectorizer.get_feature_names_out()
vocab_size = len(feature_names)
# Display the bag-of-words matrix
bow_df = pd.DataFrame(
bow_matrix.toarray(),
columns=feature_names,
index=[f"Doc {i + 1}" for i in range(len(documents))],
)Bag-of-Words Representation:
after analyst analysts company downgraded drove earnings estimates exceeded expectations for growth higher missed price q3 revenue share significantly stock strong the weak
Doc 1 0 1 0 0 0 0 0 0 1 1 1 1 0 0 0 1 1 0 0 0 0 0 0
Doc 2 0 0 0 1 0 0 1 1 0 0 0 0 0 1 0 0 0 0 1 0 0 1 0
Doc 3 0 0 0 0 0 1 0 0 0 0 0 1 1 0 1 0 1 1 0 0 1 0 0
Doc 4 1 0 1 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 1
Vocabulary size: 23 unique wordsThe bag-of-words matrix shows how documents are represented as vectors of word counts. Document 1 and Document 3 both score highly for revenue and growth, while Documents 2 and 4 share earnings vocabulary. However, This representation cannot distinguish between exceeded expectations (positive) and missed estimates (negative) because word order and grammatical relationships are completely ignored. The vocabulary size of 30 unique words determines the dimensionality of the feature space.

Term Frequency-Inverse Document Frequency
Raw word counts suffer from a significant limitation: common words like "the," "and," and "company" dominate the representation despite carrying little discriminative information. These high-frequency words appear in nearly every document, so their presence tells us almost nothing about what makes any particular document unique or meaningful. If we want our numerical representation to capture the distinctive content of each document, we need a weighting scheme that acknowledges that not all words contribute equally to meaning.
The TF-IDF (Term Frequency-Inverse Document Frequency) weighting scheme addresses this limitation through a simple insight. Words that appear frequently in a specific document but rarely across the broader corpus are likely the most informative features. Words that appear everywhere carry little discriminative power regardless of how often they appear in any single document. This intuition leads to a weighting formula that combines two complementary measurements.
The TF-IDF score for a term in document within corpus combines two components to emphasize distinctive terms while downweighting common ones. It measures both how often a term appears in a specific document (term frequency) and how rare that term is across all documents (inverse document frequency):
where:
- : the term (word) being scored
- : the specific document containing the term
- : the entire corpus (collection of all documents)
- : term frequency, measuring how often term appears in document (typically normalized by document length)
- : inverse document frequency, downweighting common terms by measuring how rare is across the corpus
- : the combined score, higher values indicate terms that are both frequent in the document and rare in the corpus
The multiplication of these two components creates a weighting scheme. Terms that appear frequently in a specific document but rarely across the corpus receive the highest scores, making them the most distinctive features of that document. This multiplicative structure means that both conditions must be satisfied for a term to receive a high weight. A word appearing frequently in one document but also appearing in every other document will have its high term frequency nullified by a low inverse document frequency. Similarly, an extremely rare word that appears only once in a single document receives a limited boost because its term frequency is low despite its high corpus-level rarity.
The inverse document frequency component quantifies how rare or common a term is across the entire corpus. This measurement provides the mechanism for penalizing ubiquitous terms while rewarding distinctive vocabulary. The formula takes a ratio of the total number of documents to the number of documents containing the term, then applies a logarithmic transformation to moderate the scaling behavior.
The inverse document frequency is computed as:
where:
- : the term being evaluated
- : the entire corpus (collection of documents)
- : total number of documents in the corpus (cardinality of set )
- : number of documents containing term (size of the subset where appears)
- : natural logarithm function, so the penalty grows gradually rather than explosively
The IDF formula penalizes common words through a logarithmic ratio that captures distinctiveness. To understand why this formula works, consider the extreme cases at both ends of the term frequency spectrum. If a term appears in all documents, the ratio yields , assigning zero weight because ubiquitous words provide no discriminative power. This makes intuitive sense: if every document contains the word "the," knowing that a particular document contains "the" tells us nothing useful for distinguishing it from other documents. Conversely, if a term appears in only one document, the ratio maximizes , giving high weight to this uniquely identifying term. Such rare terms are precisely the vocabulary that makes a document distinctive.
The logarithm serves an important mathematical purpose beyond simply computing a ratio. Without the logarithmic transformation, the IDF values would grow linearly with corpus size for rare terms, potentially creating extreme weights that dominate the feature representation. The logarithm ensures the penalty grows gradually, not explosively, creating smooth weighting transitions as term frequency increases. This gradient allows IDF to distinguish between moderately common and very common terms without overweighting rare terms. A term appearing in half the documents receives a moderate penalty, while a term appearing in 90% of documents receives a stronger penalty, but the difference remains manageable and interpretable.
from sklearn.feature_extraction.text import TfidfVectorizer
# Create TF-IDF representation
tfidf_vectorizer = TfidfVectorizer()
tfidf_matrix = tfidf_vectorizer.fit_transform(documents)
tfidf_features = tfidf_vectorizer.get_feature_names_out()TF-IDF Representation (selected columns with highest variance):
company estimates missed significantly after analysts downgraded stock weak drove
Doc 1 0.000 0.000 0.000 0.000 0.0 0.0 0.0 0.0 0.0 0.0
Doc 2 0.437 0.437 0.437 0.437 0.0 0.0 0.0 0.0 0.0 0.0
Doc 3 0.000 0.000 0.000 0.000 0.0 0.0 0.0 0.0 0.0 0.4
Doc 4 0.000 0.000 0.000 0.000 0.4 0.4 0.4 0.4 0.4 0.0The TF-IDF weighting scheme emphasizes distinctive terms while downweighting common words that appear across multiple documents. Words like missed, downgraded, and exceeded receive higher scores because they appear in only one or two documents: this makes them discriminative features. In contrast, words like the or and (if present) would receive near-zero weights since they appear everywhere. This transformation converts raw word counts into features that better capture what makes each document unique, which is essential for machine learning models to identify meaningful patterns.
