---
# System prepended metadata

title: Lorenzo's Item Response NBA notebook

---

# Lorenzo's Item Response NBA notebook


## NBA Foul Analysis with Item Response Theory

Based on Austin Rochford's blog post NBA Foul Calls and Bayesian Item Response Theory.

This tutorial shows an application of Bayesian Item Response Theory to NBA basketball foul calls data using PyMC3. Our scenario is that we observe a binary outcome (calling a foul or not) from an interaction (a basketball play) of two agents with two different roles (the player committing the alleged foul and the player disadvantaged in the play). Moreover, each committing or disadvantaged agent is an individual who might be observed several times (say LeBron James observed committing a foul in more than one play). Then it might be that the agent's role and the abilities of the single individual player contribute to the observed outcome. Thus, we would like to estimate how much each individual's (latent) ability and role as a committing or disadvantaged agent contribute to the observed outcome.

Item response theory involves a common and complex multi-agent interaction scenario, with hierarchical structures between the two committing/disadvantaged agents and individuals. Despite its complexity, Bayesian Item Response Theory combined with modern, powerful statistical software allows for quite elegant and practical modelling options. One of these options employs the Rasch model, which we now discuss in more detail.

We sourced our data from the official NBA Last Two Minutes Reports with game data between 2017 and 2021. In this dataset, each row $k$ is one play involving two players, the committing and the disadvantaged, where a foul has either been called or not. So we model the probability $p_k$ that a referee calls a foul in play $k$ as a function of the players involved. So we define two latent variables for each player, namely:

- θ: which estimates the player's ability to have a foul called when disadvantaged, and
- b: which estimates the player's ability to have a foul not called when committing.

Note that the higher these player's parameters, the better the outcome for the player's team. We then estimate these two parameters using a standard Rasch model by assuming the log-odds-ratio of $p_k$ equals $θ-b$ for the corresponding players involved in play $k$. Also, we place hierarchical hyperpriors on θ and b to account for shared abilities between players and a vastly different number of observations for different players.

We divide this analysis into four sections:

1. Data collection and processing, 
1. Definition and implementation of the Rasch model, 
1. Posterior sampling and convergence checks,
1. Analysis of the posterior results.

