liveBook · Manning

Conditional probability

As explained in Natural Language Processing in Action, Second Edition

Conditional probability is the probability of an event given that another event or condition is known to hold.

Conditional probability describes how likely an event is when another event or condition is already known. In language generation, it gives the probabilities of possible next words based on the words that came before. section 10.2

Choosing the next word

Starting with <SOS> and “LLMs,” a generative model ranks possible next tokens, chooses one, then conditions the next choice on the expanded sequence. Temperature controls how often it explores less-probable options. section 10.2

A small autocomplete experiment makes the calculation concrete:

  • Fit scikit-learn’s CountVectorizer to bigrams from sentences in 100 Wikipedia articles, calculate conditional probabilities for possible second words, and use Python’s random.choice to autocomplete sentences. section 10.4
Figure 10.2 Stochastic chameleons deciding words one at a time
figure

Trace one path through the branching probability tree used to select words from left to right. section 10.2

How Natural Language Processing in Action, Second Edition covers it

How deep it goes
Explains next-word distributions, sequential token choice, and temperature, without developing a general probability formula.
Examples
Uses an SOS-prefixed sentence, a token probability tree, Wikipedia bigrams, CountVectorizer, and random-choice autocomplete.
What it assumes
Assumes basic Python, sentence data, bigrams, and scikit-learn CountVectorizer while introducing conditional distributions through word prediction.
What you can do after
Build it from scratch by calculating bigram conditional probabilities and sampling autocomplete words with Python.
How it connects
Connects conditional probability to language models, generative models, token selection, and temperature.

Also explained in

Compare how 11 books explain conditional probability →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Natural Language Processing in Action, Second Edition. The section links point to the parts of the book it draws on.