Week 4 Due Fri, midnight (Week 4)

PC Lab 3 — Creating a Factor from Text Data

A sophisticated hedge fund wants to know whether Twitter can predict returns: clean real financial tweets, run sentiment analysis with modern NLP, and build a media-attention factor.

Week 4 materials
Solution
Published after the deadline

The setting

Word travels fast. This time it’s a sophisticated hedge fund, and the manager has been watching single tweets move billion-dollar valuations.

“I want to know if Twitter has predictive power. Take this sample of financial tweets: run a proper sentiment analysis on it — and then build me a firm-level measure of media attention. If firms the internet talks about earn different returns… that may be a great factor idea.”

Your goals: clean real text data (messier than any price series you’ve met), do sentiment analysis with modern NLP, and take a first shot at factor construction — the bridge from last week’s lecture to the data-driven finance of this one.

Background

This week’s lecture, Empirical Asset Pricing, covers factors, how they’re built, and the factor zoo. For the text part you’ll meet new tools: nltk, ekphrasis, word clouds, and pretrained transformers (FinBERT, Twitter-RoBERTa) or LLM-based classification.

The data

Data_PCLab3_Twitter_Stock_Sentiment.csv: real financial tweets, two columns — Text (the tweet) and Sentiment (a 0/1 label of unknown quality; part of the fun is deciding whether to trust it). For the optional factor part, the daily Fama–French 5 factors file is provided too.

Your assignment

Part 1 — Know your sample (Task 1)

  • Import the tweets and describe the sample: how many tweets? Words per tweet and their distribution? Average sentiment?

Part 2 — Clean and visualize (Task 2)

  • The usual text-cleaning pipeline: remove URLs, mentions, hashtags, emojis; tokenize, lemmatize, drop stopwords. For tweet-specific quirks (elongated words, emoticons, hashtags) try ekphrasis.
  • Plot a word cloud for positive and negative tweets separately.
  • How many unique words are left?

Part 3 — Sentiment analysis (Task 3)

  • The dataset ships with a sentiment label — should you trust it?
  • Produce your own sentiment with either:
    • a pretrained transformer (e.g. Twitter-RoBERTa, FinBERT), or
    • an LLM (zero-shot / few-shot classification).
  • Compare your classification to the original labels: how do these methods perform on financial tweets?

Part 4 — Measuring media attention (Task 4)

  • Using the ticker list from last week’s scraping exercise, count tweets per stock and rank stocks by media attention — total, positive vs. negative, disagreement… be creative.
  • Build 10 portfolios sorted on your preferred attention measure: is there a correlation between media attention and returns? Could Twitter attention be a factor?
  • Optional: regress each stock’s excess returns on the Fama–French 5 factors, keep the residuals (the abnormal returns traditional factors can’t explain), and redo the plot with residuals. Does attention explain what FF5 cannot?

Deliverables

Grading

  1. Submitting before the deadline;
  2. Code quality — comments, readability, use of functions;
  3. Notebook structure — well organized, explain what and why;
  4. Completing the tasks and innovating — produce less, but more useful output.

Model solution

A complete model solution — code, output, charts, and commentary — will be published on this page after the submission deadline.

Stuck, or curious about something here? Ask on the course forum →