A loop sees the cars at one point, over time; a snapshot sees them all at one instant. For each box, which average of the cars’ speeds will v(A) match: the plain mean or the harmonic mean?
Move and reshape the region. q = k\,v holds by construction, since k\,v = \frac{t(A)}{|A|}\frac{d(A)}{t(A)}. The real test is the speed: in moving traffic a loop-shaped box gives the harmonic mean of the crossing speeds, a snapshot the plain average.
// The red-light scenario of Lecture 1, with twelve cars.edieSim = {const v0 =15, T =1.2, s0 =2, a =1.5, b =2, lc =5, dt =0.1, stop =300, red = [16,46], n =12;const x =newArray(n).fill(null), v =newArray(n).fill(v0), rows = [];const idm = (vi, gap, dv) => a * (1- (vi / v0) **4- ((s0 +Math.max(0, vi * T + vi * dv / (2*Math.sqrt(a * b)))) /Math.max(gap,0.1)) **2);for (let k =0; k <=900; k++) {const t = k * dt, isRed = t >= red[0] && t < red[1];for (let i =0; i < n; i++) if (x[i] ===null&& t >=3* i) x[i] =0;const acc =newArray(n).fill(0);for (let i =0; i < n; i++) {if (x[i] ===null) continue;let gap =Infinity, dv =0;if (i >0&& x[i -1] !==null) { gap = x[i -1] - x[i] - lc; dv = v[i] - v[i -1]; }if (isRed && x[i] < stop && stop - x[i] -1< gap) { gap = stop - x[i] -1; dv = v[i]; } acc[i] =Math.max(-8, gap ===Infinity? a * (1- (v[i] / v0) **4) :idm(v[i], gap, dv)); }for (let i =0; i < n; i++) if (x[i] !==null) { v[i] =Math.max(0, v[i] + acc[i] * dt); x[i] += v[i] * dt; }if (k %2===0) for (let i =0; i < n; i++) if (x[i] !==null&& x[i] <=500) rows.push({t:+t.toFixed(1),car: i +1,x: x[i],v: v[i]}); }const cars = d3.groups(rows, r => r.car).map(([, rs]) => rs);return {rows, cars,dtSample:0.2};}
// Edie's d(A) and t(A), exact: each trajectory is a chain of straight pieces between samples, clipped to the box.edieRegion = {const size = {"Square": [20,100],"Thin and long in time (a loop)": [80,4],"Thin and long in space (a snapshot)": [1,440]}[edieShape];const t1 =Math.max(0, edieT - size[0] /2), t2 =Math.min(90, edieT + size[0] /2);const x1 =Math.max(0, edieX - size[1] /2), x2 =Math.min(500, edieX + size[1] /2);const xc = (x1 + x2) /2, tc = (t1 + t2) /2;let dA =0, tA =0;const crossing = [], snapshot = [];for (const car of edieSim.cars) {for (let j =1; j < car.length; j++) {const a = car[j -1], b = car[j], dt = b.t- a.t, dx = b.x- a.x;let lo =Math.max(0, (t1 - a.t) / dt), hi =Math.min(1, (t2 - a.t) / dt);if (dx >1e-9) { lo =Math.max(lo, (x1 - a.x) / dx); hi =Math.min(hi, (x2 - a.x) / dx); }elseif (a.x< x1 || a.x>= x2) hi =-1;if (hi > lo) { tA += (hi - lo) * dt; dA += (hi - lo) * dx; }// the loop: speed of each car as it crosses the middle line inside the time windowif (a.x< xc && b.x>= xc) {const u = (xc - a.x) / dx, t = a.t+ u * dt;if (t >= t1 && t < t2) crossing.push(a.v+ u * (b.v- a.v)); }// the snapshot: speed of each car inside the box at the middle instantif (a.t<= tc && b.t> tc) {const u = (tc - a.t) / dt, x = a.x+ u * dx;if (x >= x1 && x < x2) snapshot.push(a.v+ u * (b.v- a.v)); } } }const area = (t2 - t1) * (x2 - x1);const mean = s => s.length? d3.mean(s) *3.6:NaN;const harm = s => s.length&& s.every(v => v >0) ? s.length/ d3.sum(s, v =>1/ v) *3.6:NaN;return {t1, t2, x1, x2,q: dA / area *3600,k: tA / area *1000,v: tA >0? dA / tA *3.6:NaN, crossing, snapshot,cross: {plain:mean(crossing),harm:harm(crossing)},snap: {plain:mean(snapshot),harm:harm(snapshot)}};}
viewof edieShape =seg(["Square","Thin and long in time (a loop)","Thin and long in space (a snapshot)"], {value:"Thin and long in time (a loop)",label:"Box shape",format: d => d ==="Square"?"Square": d.includes("loop") ?"Loop":"Snapshot"})viewof edieT =knob({min:5,max:85,step:1,value:60,label:"Centre time",format: t =>`${t} s`})viewof edieX =knob({min:40,max:460,step:5,value:300,label:"Centre position",format: x =>`${x} m`})
{// Dashboard: q, k and v(A) of the box; for a loop or a snapshot, also the two averages of the cars' speeds,// with the one that should match v(A) outlined (harmonic for a loop, plain for a snapshot)const r = edieRegion, f = x =>isNaN(x) ?"—": x.toFixed(1);const loop = edieShape.includes("loop"), snap = edieShape.includes("snapshot"), m = loop ? r.cross: r.snap;const tile = (label, value, unit, match =false) => htl.html`<div class=${match ?"stat-tile match":"stat-tile"}> <div class="stat-k">${label}</div><div><span class="stat-v">${value}</span><span class="stat-u">${unit}</span></div></div>`;return htl.html`<div class="stat-tiles">${tile("Flow q", r.q.toFixed(0),"veh/h")}${tile("Density k", r.k.toFixed(1),"veh/km")}${tile("Speed v(A)",f(r.v),"km/h")}${loop || snap ? [tile("Cars counted", (loop ? r.crossing: r.snapshot).length, loop ?"crossed":"on the road"),tile("Harmonic mean",f(m.harm),"km/h", loop),tile("Plain mean",f(m.plain),"km/h", snap)] :""} </div> <div class="stat-note">${loop || snap ?`The two averages are over the cars counted (${loop ?"those that crossed the line":"those on the road at that instant"}); the outlined one should match v(A).`:"A square is neither a loop nor a snapshot: v(A) weighs each car by the time it spends in the box."}</div>`;}
{// the speed key sits inside the plot, in its empty lower-right corner (late and near the start of the road)const speed = {type:"sequential",scheme:"RdYlGn",domain: [0,15],label:"Speed (m/s)"};const plot = Plot.plot({width: plotW,height:!!document.querySelector(".reveal") ?300:320,marginBottom:36,x: {label:"Time (s)",domain: [0,90]},y: {label:"Position (m)",domain: [0,500]},color: speed,marks: [ Plot.line(edieSim.rows, {x:"t",y:"x",z:"car",stroke:"v",strokeWidth:2}), Plot.rect([edieRegion], {x1:"t1",x2:"t2",y1:"x1",y2:"x2",fill: accent,fillOpacity:0.15,stroke: accent,strokeWidth:2}) ] });const key = Plot.legend({color: speed,width:Math.min(170,Math.round(plotW *0.3)),marginLeft:8,marginRight:10,ticks: [0,5,10,15]});return htl.html`<div class="edie-plot" style="position: relative; width: ${plotW}px; max-width: 100%">${plot}<div style="position: absolute; right: 18px; bottom: 46px; padding-top: 2px; border-radius: 6px; background: color-mix(in srgb, var(--bs-body-bg, #fff) 85%, transparent)">${key}</div></div>`;}
What it shows
A thin box along time is what a loop detector sees: its speed is the harmonic mean of the passing speeds. A thin box along the road is a drone snapshot: its speed is their plain average. Slide the loop into the red-light queue (about 250 m, 45 s) and the two part ways: cars stop on the loop, and v(A) counts the time they stand there. A square is neither, and the definitions still work. Lecture 2 proves why the two averages differ.
Reference. Edie, L. C. (1965). Discussion of traffic stream measurements and definitions. Proceedings of the Second International Symposium on the Theory of Road Traffic Flow, London, 139–154.
import {seg, knob, panel, pair} from"../assets/controls.js"theme = Generators.observe(notify => {const read = () =>notify(document.body.classList.contains("quarto-dark") ?"dark":"light");read();const watcher =newMutationObserver(read); watcher.observe(document.body, {attributes:true,attributeFilter: ["class"]});return () => watcher.disconnect();})accent = theme ==="dark"?"#c9a0ff":"#57068c"accentRange = theme ==="dark"? ["#4b3a5c","#ecdcff"] : ["#dccbee","#3a0463"]ink = theme ==="dark"?"#f2f2f2":"black"// Plot width: Plot's default 640 px, except on a phone, where a plot is drawn to fit its card// instead of being shrunk (and its text with it). Changes only when the width actually changes.plotW = Generators.observe(notify => {let last;const read = () => {const main =document.querySelector("main"), fit = main ? main.clientWidth-50:640;const w =!document.querySelector(".reveal") && fit <640?Math.max(240,Math.floor(fit)) :640;if (w !== last) notify(last = w); };read();window.addEventListener("resize", read);return () =>window.removeEventListener("resize", read);})