← Back to 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.
- 21,022 rows, one per camera and minute, on six days
- 10,637 counts the model may see; the rest are holes or blanks
- ten terms, each added for a reason that the data show
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:
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.
| part | title | what it does | notebook |
|---|---|---|---|
| 1 | The Counts and Their Holes | the table, the rhythm of a day, the four kinds of holes, and holes with known truth | 01_data.ipynb |
| 2 | How Big, How Busy | the first terms: a level for each camera and for each day | 02_levels.ipynb |
| 3 | The Typical Day | a smooth curve over the day, on a clock that follows the prayers | 03_typical_day.ipynb |
| 4 | Each Camera's Own Shape | a curve for each camera, and a second one around the Friday prayer | 04_camera_shapes.ipynb |
| 5 | What Is Different Today | what the typical day leaves over: deviations that last minutes and are partly shared | 05_today.ipynb |
| 6 | Filling the Holes | predictive distributions for a hidden camera and for the next 15 minutes | 06_filling_holes.ipynb |
| 7 | Checking the Predictions | coverage of the intervals, a forecast that fails, and what each term explained | 07_checking.ipynb |
How to run it
1. Install. Any recent Python, in a fresh environment:
pip install pyinla numpy pandas scipy matplotlib jupyterThe 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.csv4. Run. Open the notebooks in this folder and run them in order, 1 to 7:
jupyter notebookEach 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.