Urban Analytics & Transportation · Part 7 of 7

Checking the Predictions

Are the intervals honest, how far off is the median, where does the model fail, and what did each term explain?

Two checks

For the holes we made on 11 and 12 September the true counts are known. There are 3,605 of them. Two questions are asked of every kind of hole:

import json

import numpy as np
import pandas as pd

v = pd.read_pickle("work/part6_holes.pkl")            # the holes, their truth and their predictions
v["inside"] = v.y.between(v.lo, v.hi)                 # is the truth inside the 95% interval?
v["error"] = (v.pred - v.y).abs() / v.y               # distance of the median from the truth
print(v.groupby(["day", "kind"]).agg(holes=("y", "size"), inside=("inside", "mean"),
                                     typical_error=("error", "median")).round(2))
                          holes  inside  typical_error
day        kind                                       
2026-09-11 forecast         855    0.97           0.13
           hidden           622    0.96           0.18
           hidden-future    298    0.93           0.19
           missing          164    0.98           0.06
2026-09-12 forecast        1017    0.96           0.15
           hidden           314    0.92           0.20
           hidden-future    148    0.92           0.20
           missing          187    0.97           0.10

Reading the result.

Where it fails

An average hides the worst cases, so we look at one. At 12:55 on Saturday 12 September, camera A is at the top of the surge that follows the noon prayer:

whole = lambda col: col.round(0).astype(int)
f = v[(v.unit == "cam_A") & (v.day == "2026-09-12") & (v.now == 775)].copy()    # made at 12:55
f["ahead"] = (f.minute_of_day - f.now).astype(int)
f = f[f.ahead.isin([1, 5, 10, 15])]
print(pd.DataFrame({"minutes ahead": f.ahead, "median": whole(f.pred), "from": whole(f.lo),
                    "to": whole(f.hi), "count": whole(f.y)}).to_string(index=False))
 minutes ahead  median  from  to  count
             1     408   328 508    345
             5     395   271 577    281
            10     349   216 563    227
            15     287   168 491    196
The forecast made at 12:55 for camera A, with what happened and the same minutes on the other two Saturdays
Camera A around the noon surge. Teal line: 12 September, as seen by the model up to 12:55. Amber: the forecast with its 95% interval. Teal dots: what happened. Grey: the other two Saturdays at the same minutes of the prayer clock.

Reading the result. The last count the model saw was 404. Its forecast for the next minute is 408, and the count was 345; after 15 minutes the forecast is 287 against a count of 196. The truth stays inside the interval, but near its lower edge all the way.

The grey lines show why. On the other two Saturdays the crowd stayed near its peak a few minutes longer before it left. The model has learned the typical day from those days. On 12 September the crowd had already begun to leave, and the model read the high count as a large crowd that would leave on the usual schedule.

This is the limit of the model, not a fault of the fit: it places every surge by the prayer clock of the days it has seen, and it has no term for a day that runs a few minutes early or late.

What each part explained

Every part saved the standard deviation of what its model left unexplained. Together they show where the variation in the table lies:

part = lambda k: json.load(open(f"work/part{k}.json"))
steps = [("nothing: the sd of the log count", part(2)["sd"])]
steps += [("2: " + name, sd) for name, sd in part(2)["left"].items()]
steps += [("3: the typical day", part(3)["prayer clock, a curve per weekday"][0])]
steps += [("4: " + name, sd) for name, (sd, waic) in part(4).items()]
steps += [("5: what is different today", part(5)["left"])]
print(pd.DataFrame(steps, columns=["after Part", "sd left"]).round(3).to_string(index=False))
                      after Part  sd left
nothing: the sd of the log count    1.315
           2: a level per camera    0.612
          2: and a level per day    0.593
              3: the typical day    0.423
           4: a curve per camera    0.304
  4: and its Friday-midday curve    0.192
 4: and its level on the weekend    0.189
      5: what is different today    0.145

Standard deviations do not add up, but variances do. Of the variance of the log count,

Most of what a camera counts is decided by which camera it is and what time of day it is. What is different today is a small part of the variance. It is also the part that matters most for the next 15 minutes.

The whole model

With every term introduced, the model can be written in one line:

$$\log y_{c,d}(t) = \mu + \underbrace{a_c + b_d + e_{c,w}}_{\text{levels}} + \underbrace{f_{\text{weekday}}(s) + g_c(s) + w(s)\,j_c(s)}_{\text{typical day}} + \underbrace{Z_r(t) + P_r(t) + U_{c,r}(t)}_{\text{today}} + \varepsilon$$
termwhat it ispyINLA modelpart
ac, bdlevel of the camera, of the dayiid2
ftypical Friday and Saturdayrw2, replicate3
gc, jcthe camera's own shape; Friday middayiid with group AR(1); weights4
ec,wlevel of the camera on a weekendiid4
Z, P, Utoday: all cameras, plaza, own cameraiid with group AR(1); weights5
εcounting noiseGaussian with scale5

Limitations

What to take away

References

The code of this part, with the output it printed: 07_checking.ipynb. It reads crowd.py and the files the earlier parts saved in work/.