Week 2: Computational Methods before LLM & LLM Technical Basics

AI for Social Science Research · Fall 2026

Di Zhou

2026-09-09

Plan of Today

  • Lecture & Discussion: Methods before genAI Part 1 ~50 min — Word Frequency, Topic Models, Word Embeddings
  • Break: 10 min
  • Lecture & Discussion: Methods before genAI Part 2 ~40 min — Dependency Parsing, BERT & Transformers, Agent-based Models
  • Lab ~60 min — Word-embedding and BERT in Python

Logistics

Reading Presentations Start Next Week!

20~30 min presentation, 10~15 min discussion. Using “guiding questions” on the syllabus as a starting point.

Week Topic Date Group
3 Text, Image & Video Annotation Sep. 16 Zakaria, Seukyoung, Daye
4 Simulating Subjects & Agents Sep. 23 Woojung, YJ
5 AutoResearch Sep. 30 Duhui, Chun-Ching, Yujia
6 Qualitative Research Oct. 7 Beichen, Jayla

Where We Left Off Last Week: Methods before GenAI

This Week’s Readings

Reading Measure Targets, Methods
Karell & Freedman (2019), Rhetorics of Radicalism Identifying themes, topic modeling
Kozlowski, Taddy & Evans (2019), Geometry of Culture Quantifying meanings & measuring stereotypes, word embeddings
Stuhler (2022), Who Does What to Whom Extracting information & relations, dependency parsing
Lai et al. (2024), Ideology of YouTube Videos Quantifying meaning (ideologies), BERT Classification

This Week’s Lecture:

1. Word Frequency and tf-idf

Word Frequency and tf-idf: Definition & Use

  • A method to identify distinctive words in a corpus.
  • tf-idf = how often a word appears in this document (term frequency) × how rare it is across documents (inverse document frequency).
  • Good for: identifying keywords or distictive words, a baseline for classification.
  • Blind to: word order, negation (“not free” vs. “free”), context, meaning, and relations between words.

1. Word Frequency and tf-idf: Mathematical Definition

2. Topic Modeling

Topic Modeling: Key Idea

  • Every document is seen as a mixture of a set of “topics.”
  • Every topic is seen as a weighted word list.
  • The model jointly estimates document-topic and topic-word distributions from co-occurrence patterns across the whole corpus.

Source: AIML

Topic Modeling: An Unsupervised Approach

  • You provide the corpus & the number of topics K.
  • The algorithm returns the topics and the topic mixture for each document.
  • You read the topics and name them.
  • There’s no “right” number of K: Researchers make judgements based on interpretability, stability, and gains in theoretical insights.
  • Outcomes of topic modeling (e.g. prevelance of topics, topic ratios) can be used as variables for downstream analysis.

Source: Karell and Freedman 2019

Two Kinds of Topic Models: LDA vs. STM

  • LDA (Latent Dirichlet Allocation): treats documents as exchangeable — no covariates.
  • STM (Structural Topic Model): uses document metadata as covariates (e.g., year, source) in up to two places:
    • Topical prevalence: covariates shift the Dirichlet prior on a document’s topic proportions, so topics can be systematically more/less prevalent by group.
    • Topical content: covariates can shift the word distribution within a topic, so the same topic can be “expressed” with different vocabulary by group

Source: Chen et al. 2024

3. Word Embeddings

Word Embeddings: The Geometry of Semantic Meaning

Each word is converted into a vector in a high-dimensional space, with interesting properties:

  1. Words with similar meanings are close together in the vector space.

Source: Huang 2024

Word Embeddings: The Geometry of Semantic Meaning

  1. Word embeddings can capture analogies:
    • Embedding (“men”) - Embedding(“women”) + Embedding(“queen”) ≈ Embedding(“king”)
    • Geometric relationships between word vectors embody semantic connections: gender, verb tense, and country-to-capital relations

Source: Huang 2024

The Geometry of Culture (Kozlowski et al. 2019)

  • Word embeddings “encodes” “collective undertanding” of cultural concepts (words)
  • (Re)construct cultural dimensions from word embedding:
    1. Identify concept pairs,
    2. Compute the average difference vector,
    3. Project other words onto that dimension.
  • We can identify:
    1. Cultural dimension-specific meaning of a word along multiple cultural dimensions, and how they change over time;
    2. How relationships between cultural dimensions change over time.

Source: Kozlowski et al. 2019

Word Embeddings: Some Technical Points

  1. Word embeddings can be trained using various algorithms, including word2vec, GloVe, and fastText.
  2. Basically, the models are trained to predict words from their neighbors, or neighbors from the word, and some methods also use co-occurrence patterns.
  3. Traditional word embeddings are static: each word has a single vector, regardless of context. Newer models (BERT, GPT) produce contextualized embeddings: the vector for a word depends on the sentence it appears in using attention mechanisms, enabled by transformer architectures.
  4. The concept of word embedding are important for understanding the innerwork of LLMs (Large Language Models). LLMs use embeddings to represent tokens in a high-dimensional space, which are then processed by LLMs to generate predictions or outputs.

Break

  • Come back in 10 minutes

4. NER and Dependency Parsing

NER and Dependency Parsing

Instead of treating a sentence as a bag of words, use its grammar to recover “event structure of texts” (who did what to whom?).

  • Named entity recognition (NER): tag the spans of text that are people, organizations, places, dates, money. (“Los Angeles” → place; “Tuesday” → date.)
  • Dependency parsing: label the grammatical link between every pair of words — which noun is the subject of which verb, which noun is its object.
  • Motif extraction: combine NER and dependency parsing to extract the actions, treatments, agents, patients, characterizations, and possessions of an entity of interest (Stuhler 2022).

5. Supervised Machine Learning and Pretained Transformers

Supervised Machine Learning: Overview

  • Train a model to predict a label from features based on hand-coded/already-labelled training data.
  • Various supervised ML algorithms:
    1. Linear regression,
    2. Logistic regression,
    3. Naive Bayes,
    4. Random forest,
    5. Support vector machine,
    6. Neural networks, such as fine-tuned transformers (BERT, GPT, etc.)

Source: Devopedia

BERT and GPT: Pretrained Transformers

  • Transformers are a type of neural network architecture that has become the foundation for LLMs and genAI. They are particularly effective at capturing long-range contextual information.

  • Attention mechanisms are a key component of transformers. They allow the model to weigh the importance of different pieces of the input information when making predictions.

  • Different genAI models contain billions to hundreds of billions of parameters (weights) that fill in a fixed transformer architecture. These weights are learned during the pretraining phase on large corpora of training data.

BERT and GPT: Pretrained Transformers - How They Work in Plain English

Source: 3Blue1Brown

  • For an input vector, a transformer computes a contextualized embedding for each token in the input sequence. → This step “understands” the meaning of each token in the context it appears in.

  • This “taking into account the context” step is done by attention: each token’s embedding is reshaped by other tokens in its context, and their weights (importance) are determined by the attention mechanism (via its Query, Key, and Value matrices).

BERT looks at both preceding and following tokens, while GPT models only look at preceding tokens.

Applying BERT for Classification

Source: LinkedIn

Two ways to use BERT for Supervised ML/Classification:

  1. As a feature extractor: pre-trained BERT, use its output vectors as inputs to a plain logistic regression.
  2. Fine-tuning: keep training BERT’s own weights on your own labeled dataset. Better, slower, needs more tuning.

Both are in today’s lab codes.

6. Agent-Based Models

Agent-Based Models (ABMs)

Key idea: Specify simple rules for individual behavior, run the interaction forward, and see what pattern emerges at the population level.

  • ABMs are used to study the effects of social interaction.
  • ABMs do not use text as data.
  • ABMs are not machine learning: they are simulations, not predictive models.
  • Two classic examples: Schelling’s segregation model (1971) and Axelrod’s dissemination of culture (1997).

The simulation shows that segregation emerges when every agent prefers to have at least 35% of its neighbors be like itself (Schelling 1971)

Review

Today We Covered:

  • Note how empirical computational research ususally combines multiple methods: Topic modeling or unsupervised learning may be used to construct variables, which are then used in a supervised ML model. Topic models or tf-idf might be used to interpret results of motif extraction, etc.
  • What we have not covered but are important modifications to facilitate statistical inference/hypothesis testing: structural topic models, embedding regression, causal inference with text data, etc.

Next Week Onwards: How GenAI May Replace Some of These Methods

  • Each computational method is a specialist pipeline: You learn various methods in order to reach various measurement goals.
  • An LLM is a generalist. It can perform various tasks with very similar pipelines. We will begin by looking at how LLMs can be used for text, image, and video annotation.

Lab

Open the lab page: Lab 2: Computational Methods in Python