Sub topic 18
Tracking Topics Over Time
This chapter assumes you understand basic LDA, BERTopic, tokenisation, and document embeddings. It also assumes that each document has a reliable date or time period.
Topic modelling over time asks how themes change across a timeline. A topic can become more common, gain new words, split into related themes, or disappear. The method you use depends on your topic model.
Two ways to model change
LDA and BERTopic use different definitions of a topic, so their time-based methods differ.
| Method | How time is used | Main result |
|---|---|---|
| Dynamic LDA | Updates topic-word probabilities across ordered time slices | A topic's changing word distribution |
| BERTopic | Fits topics and calculates their representation in each time period | Topic frequency and changing representative words |
Dynamic LDA treats the same topic as evolving gradually. For example, a “health” topic might contain clinic and doctor in 2019, then contain vaccine and booster in 2021.
BERTopic usually creates topics from the full collection first. Its topics_over_time operation then measures how each topic is represented in each period. It does not automatically prove that a topic in one period is the same semantic topic in another period.
A dynamic LDA model
Split documents into ordered time slices. A time slice might be a month, quarter, or year. Each slice needs enough documents to estimate meaningful word distributions.
from gensim import corpora
from gensim.models.ldaseqmodel import LdaSeqModel
docs = [
["market", "bank", "loan"],
["market", "trade", "price"],
["virus", "clinic", "health"],
["virus", "vaccine", "health"],
]
dictionary = corpora.Dictionary(docs)
corpus = [dictionary.doc2bow(doc) for doc in docs]
# Two documents in January, followed by two documents in February.
time_slice = [2, 2]
model = LdaSeqModel(
corpus=corpus,
time_slice=time_slice,
id2word=dictionary,
num_topics=2,
passes=10,
)
for time_index in range(len(time_slice)):
words = model.print_topics(time=time_index, topic=0, top_terms=5)
print(f"Period {time_index}:", words)The model estimates a document-topic distribution for each document and a topic-word distribution for each time slice. The topic-word distributions are linked across adjacent slices, which encourages gradual change.
BERTopic over time
BERTopic can calculate topic representations for supplied timestamps after fitting the model.
from bertopic import BERTopic
docs = [
"banks issued loans and watched market prices",
"trade affected the market and prices",
"clinics treated patients during a virus outbreak",
"new vaccines helped control the virus",
]
timestamps = [2020, 2020, 2021, 2021]
topic_model = BERTopic(min_topic_size=2, verbose=False)
topics, _ = topic_model.fit_transform(docs)
topics_over_time = topic_model.topics_over_time(
docs,
timestamps,
topics,
global_tuning=True,
evolution_tuning=True,
)
print(topics_over_time.head())global_tuning keeps topic representations related to the global corpus. evolution_tuning encourages representations in neighbouring periods to remain comparable.
Comparing periods safely
Track several signals:
- topic prevalence, or the number of documents assigned to a topic
- representative words and their weights
- example documents from each period
- topic coherence within each period
- similarity between topic representations in adjacent periods
Do not compare topic number 2 in one independently trained model with topic number 2 in another. Topic identifiers are labels, not permanent identities. Compare word distributions, embeddings, and example documents instead.
Exercise
Why should you avoid interpreting a rise in topic prevalence as proof that an event caused the rise?
Show answer
Topic prevalence shows an association in the collection. It does not establish causation, and changes may result from sampling, publishing volume, or changes in vocabulary.