Urban Analytics & Transportation

Crowd Monitoring in Madinah

A model of camera counts built one term at a time, then used to fill hidden cameras, gaps and the next 15 minutes. In seven parts.

What this is

Thirteen cameras in Madinah count people every minute. The table they produce has holes: a camera that is off for a whole day, an outage of a few minutes, and the next 15 minutes, which have not happened yet. This tutorial builds one model of the table with pyINLA, one term at a time, and then uses it to fill every kind of hole.

People who manage crowds ask three questions of a camera network: what is happening where no camera is working, what happened during an outage, and what will the next 15 minutes look like. Behind them is a fourth, which no single prediction answers: what is normal here, and how far from normal is today? The model answers all four, because the typical day is one of its terms and today's departure from it is another.

How the model grows

The model describes the logarithm of the count as a sum:

$$\log(\text{count}) = \text{levels} + \text{typical day} + \text{today} + \text{noise}$$

Each part adds one piece and asks the same questions: what do the data show, which term describes it, how is it written in pyINLA, what does the fit say, and what can the model still not do. The last question leads to the next part. Along the way the standard deviation of what the model leaves unexplained falls from 1.31 to 0.15.

parttitlewhat it doesnotebook
1The Counts and Their Holesthe table, the rhythm of a day, the four kinds of holes, and holes with known truth01_data.ipynb
2How Big, How Busythe first terms: a level for each camera and for each day02_levels.ipynb
3The Typical Daya smooth curve over the day, on a clock that follows the prayers03_typical_day.ipynb
4Each Camera's Own Shapea curve for each camera, and a second one around the Friday prayer04_camera_shapes.ipynb
5What Is Different Todaywhat the typical day leaves over: deviations that last minutes and are partly shared05_today.ipynb
6Filling the Holespredictive distributions for a hidden camera and for the next 15 minutes06_filling_holes.ipynb
7Checking the Predictionscoverage of the intervals, a forecast that fails, and what each term explained07_checking.ipynb

How to run it

1. Install. Any recent Python, in a fresh environment:

pip install pyinla numpy pandas scipy matplotlib jupyter

The first fit downloads the INLA program by itself. This happens once.

2. Get the code. Download crowd-monitoring.zip and unpack it. It holds the seven notebooks and the helper crowd.py.

3. Get the data. The data are the files train.csv and test.csv of the Madinah Ethical AI Competition on Kaggle. They are not redistributed here: download them from the competition page into the folder data/, so that the folder looks like this:

crowd-monitoring/
    crowd.py
    01_data.ipynb  ...  07_checking.ipynb
    data/
        train.csv
        test.csv

4. Run. Open the notebooks in this folder and run them in order, 1 to 7:

jupyter notebook

Each notebook reads the files the earlier ones saved in work/, which the first one creates. Parts 1 to 4 take seconds to a minute or two. The full model of Part 5 and the forecasts of Part 6 take about ten minutes together, on four threads of a laptop. The environment variable THREADS changes the number of threads, for example THREADS=2:1.

The pages were run with pyINLA 0.1.17, and the notebooks were checked again in a fresh environment with 0.1.18. Expect small differences in the last digits: a fit on several threads does not repeat exactly.