import sys
if sys.platform == "emscripten": # on the course website: install the course package
import micropip, js
await micropip.install(js.location.origin + "/lite/wheels/trafficlab-0.3.0-py3-none-any.whl")
import numpy as np
import pandas as pd
from trafficlab import data, plots
from trafficlab.hw import my_problems, check, submit, whoamiLab 2 · Real Traffic from the Sky
Homework 2 · NGSIM trajectories · time-space diagrams · Edie’s definitions · a virtual loop detector
▶ Start this homework in your browser (nothing to install; your work is saved in this browser).
This notebook is Homework 2 (100 points): the lab (60) and three problems by hand (40). Part A is done in class; finish Parts B and C at home.
This lab is open to everyone. Try it freely; if you are enrolled, open https://trgy7353.pages.dev/login once (NYU login) so that the Submit cell at the end works.
It runs right here on the course website: Python runs inside your browser, with nothing to install. Your work is saved automatically in this browser, on this computer; to move to another computer, use File → Download and upload the file there. To submit: upload a PDF or photo of your hand derivations into this folder (name it derivation...), save the notebook, and run the last cell. You may submit again until the deadline; the last submission counts.
In 2005, the Federal Highway Administration filmed US-101 in Los Angeles from the top of a 36-storey building and turned the video into the position of every vehicle, ten times per second.
A loop detector in the road and a camera looking down at the same lane report the average speed of the same cars. Which reports the higher speed, and by roughly how much? Write down your guess.
Your numbers
Every student gets their own numbers, made from their NetID, and the grader recomputes yours. On the course website your NetID comes from your NYU login.
NETID = whoami() or "your-netid" # filled in from your NYU login
P = my_problems(NETID, hw=2)Part A · In class
A1 · Four minutes of one lane
Lane 2 of US-101 from 8:00 a.m., in metres and seconds: vehicle, t, x (distance along the road), v (speed, m/s).
us101 = data.load("ngsim-us101-0800-4min-v1")
df = us101[us101.lane == 2].reset_index(drop=True)
print(f"{len(df):,} records of {df.vehicle.nunique()} vehicles")
ax = plots.time_space(df, title="US-101, lane 2, from 8:00 a.m.")Question 1 (multiple choice, 6 points). Find the red band in the diagram, read two points off it, and estimate how fast it moves. Which is right?
- A. It moves forward, with the traffic, at about 60 km/h.
- B. It moves backward, against the traffic, at roughly 10–25 km/h.
- C. It stands still: a bottleneck at one place.
- D. It moves backward at about 100 km/h.
A2 · Edie’s definitions
For a region A of the time-space diagram,
\begin{aligned} q(A) &= \frac{d(A)}{|A|}, \\ k(A) &= \frac{t(A)}{|A|}, \\ v(A) &= \frac{d(A)}{t(A)}, \end{aligned}
where d(A) is the total distance and t(A) the total time that vehicles spend in A.
Your turn. Complete edie, then measure your box from P["lab"].
def edie(df, x1, x2, t1, t2, dt=0.1):
"""Flow (veh/h), density (veh/km) and speed (km/h) in the region [x1, x2) x [t1, t2).
Each row of df is one vehicle at one instant, recorded every dt seconds.
Hint: a row inside the region stands for dt seconds of vehicle time,
and for v * dt metres of vehicle distance.
"""
# YOUR CODE HERE: compute total_time t(A), total_distance d(A) and the area |A|
return Noneassert edie(df, 0, 600, 0, 240) is not None, "complete edie() first"
b = P["lab"]
box = (b["edie_x1"], b["edie_x1"] + b["edie_dx"], b["edie_t1"], b["edie_t1"] + b["edie_dt"])
q, k, v = edie(df, *box)
print(f"your box {box}: q = {q:.0f} veh/h, k = {k:.1f} veh/km, v = {v:.1f} km/h; k*v = {k * v:.0f}")
ax = plots.time_space(df); plots.region(ax, *box, label="your box")Question 2 (multiple choice, 6 points). q = k\,v holds exactly in your box, and in any box you try. Why?
- A. It is a coincidence of these data.
- B. By Edie’s definitions, q(A)/k(A) = d(A)/t(A) = v(A) for any region at all.
- C. Because traffic in the box is steady.
- D. Because the samples are 0.1 s apart.
Part B · At home
B1 · A virtual loop detector
A loop detector sits at one point and records each vehicle as it passes. Your turn. Complete virtual_loop, then put your loop at P["lab"]["x0"] for all four minutes.
def virtual_loop(df, x0, t1, t2):
"""What a loop detector at position x0 would report between t1 and t2.
Returns (count, flow in veh/h, time-mean speed in km/h, harmonic-mean speed in km/h).
Hint: for each vehicle, find its first record at or past x0; keep it if it falls in [t1, t2).
"""
# YOUR CODE HERE
return Nonex0 = P["lab"]["x0"]
loop_n, loop_flow, loop_vt, loop_vh = virtual_loop(df, x0, 0, 240)
print(f"loop at {x0:.0f} m: {loop_n} vehicles, {loop_flow:.0f} veh/h")
print(f" time-mean speed {loop_vt:5.1f} km/h")
print(f" harmonic-mean speed {loop_vh:5.1f} km/h")
print(f" Edie space-mean speed, x0 ± 50 m: {edie(df, x0 - 50, x0 + 50, 0, 240)[2]:5.1f} km/h")Question 3 (multiple choice, 6 points). Which of the loop’s two speeds is closer to Edie’s space-mean speed, and why?
- A. The arithmetic (time-mean) speed, because it treats every car equally.
- B. The harmonic mean: it weights each car by the time it spends on the road, as a space-mean speed does. The time-mean is larger by about \sigma_s^2/\bar v_s.
- C. Neither: a loop cannot estimate a space-mean speed.
- D. They are equal, so either.
Now check your prediction from the top: which sensor reported the higher speed?
Going further (not graded). Use lane 1 (us101[us101.lane == 1]): is the wave there too? Move your loop: does the flow change along the road? Should it?
Part C · Problems by hand (40 points)
Use your numbers from my_problems above. Write each derivation on paper (photo or PDF upload) and enter the numbers in the answer sheet. Speeds in m/s unless stated.
P1 · Two lanes, two averages (12). In lane A every car drives at v_A with spacing s_A; in lane B at v_B with spacing s_B. Find the time-mean speed \bar v_t (what a loop across both lanes reports), the space-mean speed \bar v_s (what a photo reports), and the variance of speeds in space \sigma_s^2 (m²/s²). Then compute \bar v_s + \sigma_s^2/\bar v_s and compare it with \bar v_t.
P2 · Trucks fool a loop (12). In 30 s a 1.8 m loop counts your n vehicles and reports your occupancy. Your share of them are 18 m trucks, the rest 4.5 m cars. Find the flow q (veh/h) and the true density k (veh/km, using the true average length). Then the speed a single loop reports if it assumes every vehicle is a car (km/h), and the true speed (km/h). By what fraction is the loop wrong?
P3 · Which average for travel time? (16, stretch). Six vehicles pass a loop at your speeds. Compute their arithmetic mean and harmonic mean (m/s). If these vehicles are typical of the traffic on the section ahead, what is the true average travel time over your L km (minutes)? By what percentage would a travel-time sign that used the arithmetic mean be wrong? Prove on paper why the harmonic mean of speeds measured at a point is the right one for travel time.
- Your answers go to the course website when you run the last cell, together with this notebook and your derivation file. You get a receipt with the time.
- Your numbers are recomputed from your NetID and checked within a small tolerance, step by step, so a slip in one step costs only that step.
- Your functions (
edie,virtual_loop) are run on hidden tests: data you have not seen. Only those functions are run, nothing else from your notebook, with a 2-minute limit. - Your derivations (the PDF or photo) are read by the instructor.
ANSWERS = {
"netid": NETID,
"lab": {"q": q, "k": k, "v": v, "loop_n": loop_n, "loop_vt": loop_vt, "loop_vh": loop_vh,
"Q1": "", "Q2": "", "Q3": ""},
"P1": {"v_t": None, "v_s": None, "var_s": None, "check": None},
"P2": {"q": None, "k": None, "v_cars": None, "v_mixed": None},
"P3": {"v_t": None, "v_s": None, "tt_true": None, "tt_error_pct": None},
}# Save the notebook first (Ctrl+S), then run this cell to hand in.
submit(ANSWERS, hw=2, notebook="lab02-trajectories.ipynb")