Sequences are not IID
In sequence learning, chronology is part of the feature schema: define the cutoff first, then construct inputs, labels, and splits around it.
Long support conversations must be routed while preserving temporal context.
This chapter moves from name the timeline to split whole futures. We will make the idea concrete in a small code example before placing it back inside a production system with a declared latency, cost, and operator-capacity budget.
Train on earlier cutoffs and validate on later cutoffs. Keep related entities or overlapping windows together when their shared history would otherwise leak.
Verify the behavior illustrated in causal_window.py, then measure decision utility, slice reliability, and system latency.
A bidirectional model and globally normalized features use future events unavailable at authorization. An unsafe release must degrade to a named baseline or the last known-good version.
Build the idea one layer at a time.
A sequence is more than a bag of rows. Swap two card transactions and the amounts stay the same, but the story—and often the risk—changes.
IID modeling assumes examples can be shuffled without changing their meaning. Sequence data breaks that assumption in two ways: nearby observations influence one another, and the order carries information. A failed login after a password reset is different from the same failed login before it. Validation must therefore move forward in time, not randomly mix past and future.
The unit of prediction matters too. Windows from one customer share history, so scattering overlapping windows across train and test quietly duplicates evidence. Start by writing a causal contract: identify the entity, sort by event time, define what is known at the decision cutoff, and place the label strictly after that cutoff.
- event time
- padding/masks
- hidden state
- BPTT
Follow the reasoning before touching the code.
- 01
Name the timeline
Choose one entity key and distinguish event time from arrival time. State whether late events can revise history or are ignored after the decision.
- 02
Cut a causal example
For a decision at time t, include only events observable by t. Predict an outcome in an explicit future horizon, such as an account takeover in the next 24 hours.
- 03
Split whole futures
Train on earlier cutoffs and validate on later cutoffs. Keep related entities or overlapping windows together when their shared history would otherwise leak.
events = [
{'time': 2, 'amount': 12},
{'time': 5, 'amount': 80},
{'time': 9, 'amount': 30},
]
decision_time = 6
history = [e for e in events if e['time'] <= decision_time]
future = [e for e in events if decision_time < e['time'] <= 10]
label = any(e['amount'] > 25 for e in future)
assert [e['time'] for e in history] == [2, 5]
print(history, label)Build one history using only events available at the decision time.
What to notice
- The event at time 9 contributes to the label but never appears in the input history.
- Randomly splitting windows made from this timeline could place nearly identical histories on both sides of validation.
YOUR TURN Pause and predict before revealing the answer.
Move the decision time from 6 to 9. Which part of the example must change besides the input window?
Need a hint?
The forecast horizon is anchored to the cutoff, not to the last row in the file.
Check your reasoning
The history now contains all three events, and the label must be recomputed from events after time 9. Reusing the old label would mix two different prediction contracts.
Neural networks
Connect the chapter to the course’s quantitative spine.
In sequence learning, chronology is part of the feature schema: define the cutoff first, then construct inputs, labels, and splits around it. The lstm cell-state update below is the shared course-level lens that helps quantify the surrounding system. Name each symbol, check its units or shape, and use only values that were available at the production cutoff.
LSTM cell-state update
| Symbol | Meaning / shape / unit |
|---|---|
c_t | cell state |
f_t | forget gate |
i_t | input gate |
g_t | candidate state |
Open derivation and numerical substitution
Start from the production quantity being optimized, substitute the observed values with their declared units, then isolate the model-controlled term. Preserve shape annotations at each step so broadcasting or aggregation cannot silently change the result.
- Write the named inputs: c_t, f_t, i_t, g_t.
- Substitute one small, hand-checkable batch before vectorizing.
- Calculate an independent reference value and compare within a declared tolerance.
# equation → code contract
inputs = validate_shapes_and_units(batch)
value = compute_c11(inputs)
assert is_finite(value)Change one assumption and make the tradeoff visible.
Use the worked chapter as your prediction, then change one input in the lab and explain why the output moves. The lab runs predefined TypeScript and never executes learner code, so the focus stays on the concept rather than setup.
BPTT context lab
Change truncation length and compare retained context with activation memory.
Assumption: Single recurrent layer; memory normalized to the 256-token configuration.
Open nonvisual data table
| Item | Computed state | Interpretation |
|---|---|---|
| 8-token signal | retained | 91.0% |
| 16-token signal | retained | 90.1% |
| 32-token signal | retained | 88.2% |
| 64-token signal | retained | 84.3% |
| 128-token signal | truncated | 5.0% |
Trace the complete operating path.
Carry the chapter’s central idea into the system: In sequence learning, chronology is part of the feature schema: define the cutoff first, then construct inputs, labels, and splits around it. The path below shows where that reasoning needs an artifact, a test, an owner, or a fallback.
- 01
Turn “Name the timeline” into a written invariant and a failing test.
- 02
Promote causal_window.py from illustration to a versioned implementation with representative fixtures.
- 03
Use “Split whole futures” as the release checkpoint: Train on earlier cutoffs and validate on later cutoffs. Keep related entities or overlapping windows together when their shared history would otherwise leak.
- 04
Serve or materialize behind explicit a declared latency, cost, and operator-capacity budget.
- 05
Join telemetry to mature outcomes and retain a rollback path.
Observability
Join service health, input quality, prediction distributions, slice behavior, and mature outcomes by exact version.
Cost
Measure storage, preprocessing, compute, queueing, and human review under a representative arrival pattern.
Failure modes
A bidirectional model and globally normalized features use future events unavailable at authorization. Add a detector, owner, mitigation, and stop condition for this class of failure.
Alternatives
Compare a rule, a simpler statistical baseline, and a different system boundary before adding model complexity.
Explain the contract, not just the vocabulary.
Causal stateful risk model
Specify an LSTM with temporal splits, masking, BPTT diagnostics, and replay/online state parity.
- Causal sequence dataset
- Baseline and recurrent comparison
- Gradient/state diagnostics
- Late-event and state policy
Read primary material with a purpose.
Return to the opening failure.
Define causal cutoffs, keyed state, reset/TTL, duplicates, late events, and replay/online parity.
For this lesson, the release evidence is the worked code example, a hand-checked formal result, deterministic simulation output, a ≥80% checkpoint, the production rubric, and a named fallback. The course resolves when the system can produce a masked sequence model with bptt, recurrent gates, and attention precursors.