DeepLLM Quiz
Statistics and Probability

Bag of words, n-grams and the limits of counting models of text

10 questions on bag-of-words models, naive Bayes text classification, smoothing, autoregressive tables, bigrams and trigrams, and why they generate incoherent text.

  • 10 questions
  • Stopwatch
  • Instant feedback

Shown publicly with your score. Don’t use your full real name.

Sample questions

Play now ↑

Answers stay hidden. Start the quiz to play them.

  1. Sample 1

    A training text has 200 words, and the word 'excellent' occurs 10 times. What probability does the bag-of-words model give to 'excellent'?

    • 0.5
    • 10
    • 0.05

How it works

  • One question per screen. An answer is final once sent.
  • You see right away whether you were right, with a short explanation.
  • The stopwatch runs: on equal scores, the faster player ranks first.
  • Only your best attempt counts, so replaying is never risky.

About this quiz

Long before neural networks wrote paragraphs, researchers built models of text out of nothing but counting. Tally how often each word occurs, and you have a model of language. Tally how often each pair of words occurs together, and you have a better one. These simple models are still worth knowing, because they show clearly what a language model has to do, where counting fails and why more powerful machinery was needed. This quiz explores them, with small examples and a few calculations.

You will start with the simplest model of all, the bag of words, which pretends that the words of a text are drawn independently from one fixed distribution and so ignores their order completely. You will see how the best possible fit is just the proportion of times each word occurs, and how the same idea gives a simple text classifier, for instance one that separates positive from negative reviews. The classifier assumes the words are independent inside each class but gives each class its own distribution. You will also see the trap of a word never seen in training, which would otherwise zero out the whole calculation, and the standard remedy.

Next comes the step towards order: models that predict each word from the words before it. In general, every word depends on all earlier ones, but the tables needed grow exponentially with the length of the history. Restricting each word to depend on the last one or two words gives the bigram and trigram models, with tables that can be estimated from counts of pairs and triples. You will do a size calculation to see the table grow, and think about what happens when such a model is run to generate text, and why it gets incoherent.

Some points are easy to get wrong. A bag-of-words model has no notion of word order at all, which is a strength for simple classification and a fatal weakness for anything else. Smoothing is not a technicality but a necessity. And making the history longer cannot rescue counting models, because the size of the tables explodes.

The quiz suits learners who want to see what language models improved upon, and practitioners who use simple baselines. It needs only counting. There are 10 questions, each with an explanation, and it takes about four minutes. These counting models remain useful as baselines and as teaching tools. A bag-of-words classifier is quick to build, easy to interpret and often surprisingly competitive on simple tasks, so it is a good first thing to try before reaching for something heavier. Understanding exactly why n-gram models run out of road, through the exponential growth of their tables, makes it much easier to appreciate what neural language models changed: they replace giant tables with learned functions that share statistical strength across similar contexts.

Questions and answers

›How long does the quiz take?

About 3 to 4 minutes for 10 questions, at roughly 20 seconds each. An explanation follows every answer, with the counting shown for the numerical examples.

›Do I need to know probability?

Only the basic idea that a probability can be estimated as a proportion of cases. If you have seen the product rule, it helps with the autoregressive part, but the questions explain everything they use and need nothing beyond division and multiplication.

›What is a bag-of-words model?

It assumes that the words of a text are drawn independently from the same distribution, so the joint distribution fully factorises into a product of one-word distributions. Its graph has no links. It ignores the order of the words entirely, which is where the name comes from, as if the words were shaken out of a bag.

›How is it fitted to data?

The distribution is a table with the probability of each possible word. The maximum likelihood solution is to set each probability to the fraction of times the word occurs in the training text. No iteration is needed, and the fit is just counting and dividing.

›How does the naive Bayes text classifier work?

It keeps a separate table of word probabilities for each class and a prior probability for each class, all estimated from frequencies in labelled text. The words are treated as independent within each class. For a new passage, the entries for its words are multiplied together for each class, and Bayes' theorem converts these into class probabilities.

›Why is smoothing needed?

If a word occurs in a new text but never occurred in the training text for some class, its estimated probability is zero, which would make the product for that class zero whatever the other words say. Smoothing assigns a small amount of probability uniformly across all entries after training, which avoids zeros.

›What is an n-gram model?

It is an autoregressive model in which each word depends only on the previous few words. With one previous word it is a bigram model, with two a trigram model, and so on. The conditional tables are estimated from the frequencies of groups of adjacent words in a large body of text.

›Why can't n-grams be extended to long histories?

The tables needed to store the conditional probabilities grow exponentially with the number of previous words used. Going much beyond trigrams is prohibitively expensive. Yet language has long-range dependencies, and without them the model cannot produce coherent text.

›Can these models generate text?

Yes. Given a starting pair of words, a trigram model can sample the next word from the counts, then use the latest two words to sample another, and so on. The result is locally plausible but incoherent over longer stretches, because each word depends on only two predecessors.

More quizzes