Part VII: Building Intelligent Temporal Systems
Chapter 30: Temporal Reasoning and Causality

Temporal Reasoning and Causality

Beyond prediction to why and what-if: temporal logic, causal discovery, treatment effects over time, and counterfactuals.

"For thirty chapters they trained me to answer one question, what comes next, and I answered it so well that everyone forgot it was the small question. Then a clinician asked me whether the patient improved because of the drug or merely after it, and I had nothing, because correlation had been my whole education and the arrow of cause was a direction I had never been taught to read. I could continue any curve you handed me, but I could not tell you which curve the world would have followed had you acted differently, and that counterfactual, the road not taken, turned out to be the only road anyone actually cared about. I have learned that prediction without causation is a confident finger pointing at a coincidence, and that the bravest thing a temporal model can say is not here is what happens next but here is what would have happened instead."

A Forecaster Who Finally Learned to Ask Why

Chapter Overview

Part VII opens here, and with it the book turns from systems that learn the shape of time to systems that reason about it. Everything before this point, the statistical models of Part II, the deep networks of Part III, the foundation models of Part III, the decision-makers of Part VI, shares a single quiet limitation: they are extraordinarily good at saying what will happen next and almost silent on why it happens or what would happen instead. A model that forecasts a patient's deterioration is useful; a model that can say the deterioration is being driven by this variable, and would be averted by that intervention, is the difference between an observer and an agent that can act with reasons. This chapter complements the learned and statistical machinery you have built with explicit reasoning about time and about cause.

Four threads run through the chapter, each a different way of going beyond the predictive curve. Temporal logic (linear temporal logic, LTL, and signal temporal logic, STL) gives you a precise language for specifying temporal properties, "the alarm must fire within five minutes of the breach and stay on until acknowledged", and a way to monitor a running signal against that specification rather than merely forecasting it. Causal discovery for time series asks what the causal graph behind a multivariate series actually is, moving from Granger's predictive notion of influence to modern algorithms that recover directed temporal causal structure. Causal effect estimation over time asks how large an intervention's effect is and how it evolves, the treatment-effect question at the heart of medicine, economics, and policy. Counterfactual and what-if analysis asks the hardest question of all: what would this series have done had we intervened differently, the road not taken made computable.

Causality matters here for a reason larger than completeness: it is what makes a temporal decision trustworthy. A policy learned in Part VI optimizes a return, but a return estimated from observational data can reward a model for exploiting a confounded correlation that vanishes the moment the model acts on it. The treatment that appears beneficial because sicker patients received it, the marketing spend that appears to drive sales it merely followed, the sensor that predicts failure only because it shares a hidden cause with the failure: each is a trap that prediction alone cannot see and that causal reasoning is built to escape. When a model's outputs will drive interventions, the question is no longer how well it fits the past but whether its claimed effects would survive being acted upon.

This is also a homecoming for an idea the book introduced early. Granger causality first appeared in Chapter 6 as a predictive test between series, and the chapter showed even then that predictive precedence is not the same as cause. Here that gap is finally closed: Granger's notion becomes one node in a richer causal-discovery toolkit, and the predictive influence of Chapter 6 is set beside the structural causation that observational forecasting can never quite reach. The chapter thus pays off a debt the book has carried since Part II, and arms the agents and systems of the rest of Part VII to reason, not merely to predict.

Prerequisites

This chapter assumes the multivariate and econometric machinery of Chapter 6: Multivariate and Econometric Models, and Granger causality in particular, because causal discovery here begins exactly where Granger's predictive test of influence leaves off and then moves past it to recover directed causal structure. It draws on the latent-state thinking of Chapter 7: State-Space Models and Filtering, since reasoning about hidden causes and unobserved confounders is the same habit of mind that treats the observed series as a window onto a latent process you must infer rather than see. And it connects directly to the sequential decision making of Part VI, beginning with Chapter 22: Markov Decision Processes, because an intervention is an action and a treatment effect is the value of taking it, so the counterfactual reasoning of this chapter is the rigorous foundation under any claim that a learned policy will help rather than merely correlate with help. Readers wanting to refresh multivariate time-series notation, the probability behind expectations and conditioning, or the decision-making vocabulary these methods build on will find the refreshers through the Table of Contents.

Remember the Chapter as One Sentence

If you keep one idea from this chapter, keep this: prediction tells you what is likely to happen next, but reasoning about time and cause tells you why it happens and what would happen if you intervened, and only the latter makes a temporal decision trustworthy. Temporal logic (LTL and STL) lets you specify and monitor temporal properties precisely; causal discovery moves from Granger's predictive influence to algorithms like PCMCI that recover the directed causal graph of a multivariate series; causal effect estimation measures how an intervention's effect unfolds over time; and counterfactual what-if analysis computes the road not taken, the baseline a series would have followed had you acted differently, which is the single number a confounded correlation can never give you.

Chapter Roadmap

Once you have worked through the four sections, the Hands-On Lab below chains them into a single investigation of one multivariate series. Each lab step maps to a section, so the lab doubles as a review: by the time you finish you will have monitored a signal against the STL specification of Section 30.1, recovered a causal graph with the PCMCI method of Section 30.2, estimated a time-varying treatment effect from Section 30.3, and computed a counterfactual baseline following Section 30.4, turning a single dataset from something you can predict into something you can explain and intervene upon.

Hands-On Lab: Why, Not Just What

Duration: about 120 to 180 minutes Difficulty: Intermediate to Advanced

Objective

Take one multivariate time series and move it through the full arc of temporal reasoning, from specifying what should be true of it, to discovering why its variables move together, to measuring the effect of an intervention, to imagining what would have happened without that intervention. You will first write a signal temporal logic specification and monitor the running signal against it, computing the quantitative robustness of Section 30.1 rather than merely forecasting the series. You will then recover a directed causal graph from the multivariate series with the PCMCI algorithm of Section 30.2, contrasting the structure it finds with the bare predictive influence a Granger test would report. Next you will estimate a time-varying treatment effect under time-varying confounding using the methods of Section 30.3, watching a naive regression mislead before a confounding-aware estimator corrects it. Finally you will construct a counterfactual baseline for an intervention following Section 30.4, so the deliverable is not a forecast of what happened but an estimate of what would have happened instead. The single thread is that prediction is the start of the question, not the end: a series you can only continue is a series you do not yet understand.

What You'll Practice

  • Writing a signal temporal logic specification and computing its quantitative robustness on a running signal, the monitoring discipline of Section 30.1.
  • Recovering a directed temporal causal graph from a multivariate series with PCMCI, the discovery method of Section 30.2.
  • Estimating a treatment effect under time-varying confounding and seeing why a naive regression fails, following Section 30.3.
  • Constructing a counterfactual baseline for an intervention and reading the gap as the causal effect, following Section 30.4.
  • Distinguishing predictive precedence from structural causation throughout, the thread that ties the chapter together.

Setup

You need a Python environment with numpy and pandas for the series, tigramite for the PCMCI causal discovery of Section 30.2, and CausalImpact (or dowhy) for the counterfactual baseline of Section 30.4. Use a multivariate series with a known or suspected intervention point, such as a clinical series where a treatment begins at a fixed time or a sensor series before and after a configuration change. The one discipline that governs the whole lab is to keep predictive influence and structural causation separate in your head: a variable that helps forecast another is not thereby its cause, and every step here exists to make that distinction operational. The code below computes the quantitative robustness of a simple signal temporal logic property of Section 30.1, the signed margin by which a running signal satisfies or violates a specification.

import numpy as np

def stl_robustness_eventually_below(signal, threshold, window):
    """Robustness of 'eventually within `window` the signal drops below `threshold`'.
    Positive means the property holds with margin; negative means it is violated."""
    n = len(signal)
    rho = np.full(n, -np.inf)
    for t in range(n):
        horizon = signal[t:t + window]                 # the look-ahead window from time t
        rho[t] = np.max(threshold - horizon)           # best satisfaction margin inside it
    return rho                                          # signed robustness per timestep
Setup: a quantitative robustness monitor for a bounded "eventually" signal temporal logic property, returning the signed margin by which the running signal satisfies the specification of Section 30.1 at each timestep.

Steps

Step 1: Specify and monitor a temporal property

Write a signal temporal logic specification for a property the series should obey, for example that a monitored channel must fall below a safe threshold within a bounded window of any breach, and compute its quantitative robustness over the running signal using the monitor above, the runtime-monitoring task of Section 30.1. Read where the robustness goes negative as the moments the specification is violated, a stronger statement than any forecast of the raw value.

Step 2: Discover the causal graph with PCMCI

Run the PCMCI algorithm of Section 30.2 on the multivariate series with tigramite to recover a directed graph of lagged causal links, and contrast the edges it keeps with the dense web of predictive correlations a pairwise Granger test would report. Note which Granger-significant links PCMCI prunes once it conditions on the rest of the graph, the difference between predictive influence and conditional causal structure.

Step 3: Estimate a time-varying treatment effect

Pick a treatment variable and an outcome and estimate the treatment's effect over time under time-varying confounding, following Section 30.3. First fit a naive regression of outcome on treatment and observe the biased estimate, then apply a confounding-aware estimator such as inverse-probability weighting or a marginal structural model and watch the effect correct, making concrete why sequential confounding defeats ordinary regression.

Step 4: Compute a counterfactual baseline

For the intervention point in the series, construct the counterfactual baseline of Section 30.4, the trajectory the outcome would have followed had the intervention not occurred, using a structural or Bayesian method such as CausalImpact fitted on the pre-intervention period and control series. Read the gap between the observed series and the counterfactual baseline as the estimated causal effect, with its uncertainty band.

Step 5: Assemble the causal narrative

Put the four results together into one account of the series: what property it must satisfy and when it failed (Step 1), what causes what (Step 2), how large the intervention's effect is (Step 3), and what would have happened without it (Step 4). Write the conclusion as a causal statement, not a predictive one, and mark every place where a predictive shortcut would have led you to the wrong answer.

Expected Output

The lab produces one series understood four ways. Step 1 yields a robustness trace whose negative excursions pinpoint exactly when and by how much the signal temporal logic specification was violated, a monitored property rather than a forecast. Step 2 yields a directed causal graph that is markedly sparser than the correlation web, with several Granger-significant links pruned once PCMCI conditions on the full graph, the visible gap between predictive influence and causal structure. Step 3 shows two treatment-effect estimates side by side, the biased naive regression and the corrected confounding-aware estimate, demonstrating that the choice of estimator, not just the data, determines whether the effect is real. Step 4 delivers a counterfactual baseline with an uncertainty band and an effect read off as the gap from the observed series, the road not taken made into a number. The reader finishes able to specify, monitor, discover, estimate, and counterfactually reason over a temporal series, and, more importantly, able to tell at each step whether a claim is merely predictive or genuinely causal, which is the line that separates a forecast from an explanation.

Right Tool: Causal Reasoning Off the Shelf

The from-scratch robustness monitor of the setup is worth writing once so the quantitative semantics of signal temporal logic stop being abstract, but you should rarely reimplement these methods in production. The tigramite library implements PCMCI and its variants behind a few lines that take a multivariate array and return a directed causal graph with link strengths and lags, collapsing the discovery of Section 30.2 into a single run_pcmci call. For effect estimation and counterfactuals, dowhy exposes the whole identify-estimate-refute workflow of Section 30.3 through a uniform causal-model API, and Google's CausalImpact fits the structural-time-series counterfactual baseline of Section 30.4 from a pre-period and a set of controls in a handful of lines. The discipline the chapter teaches still governs the libraries: a causal graph is only as valid as its assumption of no hidden confounders, and a counterfactual is only as trustworthy as the controls that stand in for the world without the intervention. The library computes the estimate; it does not certify the assumptions, and the assumptions are where causal claims live or die.

Stretch Goals

  • Inject a hidden common cause into a synthetic version of the series and rerun the PCMCI discovery of Section 30.2, observing how an unobserved confounder produces a spurious direct link and how the latent-variant of the algorithm responds, the failure mode every causal-discovery claim must guard against.
  • Replace the marginal structural model of Section 30.3 with a neural sequential treatment-effect estimator such as a counterfactual recurrent network and compare the time-varying effect curves, weighing flexibility against the transparency of the weighting approach.
  • Vary the control series feeding the counterfactual baseline of Section 30.4 and watch the estimated effect and its uncertainty band move, exposing how much of a causal-impact conclusion rests on the unverifiable assumption that the controls were unaffected by the intervention.

What's Next?

This chapter gave temporal systems the ability to reason about why and what-if, not merely to predict what comes next, and that reasoning is precisely what an agent needs before it acts. Chapter 31: Temporal AI Agents takes the next step, assembling the predictive models of the earlier parts, the decision-making of Part VI, and the causal and temporal-logic reasoning built here into systems that perceive a stream, reason over time about cause and consequence, and act, then monitor their own behavior against temporal specifications and revise. The temporal-logic monitors of Section 30.1 become the agent's safety constraints, the causal graphs of Section 30.2 become its model of how its actions propagate, and the counterfactual reasoning of Section 30.4 becomes its way of evaluating a plan it has not yet executed. The full path through Part VII and the rest of the book is laid out in the Table of Contents.

Bibliography & Further Reading

Temporal Logic and Interval Reasoning

Pnueli, A. "The Temporal Logic of Programs." 18th Annual Symposium on Foundations of Computer Science (FOCS), 46-57, 1977. doi.org/10.1109/SFCS.1977.32

The foundational paper that brought temporal logic into computer science, introducing the linear-time operators for reasoning about how propositions evolve over time, the LTL foundation of Section 30.1.

📄 Paper

Maler, O., Nickovic, D. "Monitoring Temporal Properties of Continuous Signals." FORMATS/FTRTFT, LNCS 3253, 152-166, 2004. doi.org/10.1007/978-3-540-30206-3_12

The paper introducing signal temporal logic (STL), extending temporal logic to real-valued signals with the quantitative robustness semantics that make Section 30.1's runtime monitoring of continuous streams possible.

📄 Paper

Allen, J. F. "Maintaining Knowledge about Temporal Intervals." Communications of the ACM, 26(11), 832-843, 1983. doi.org/10.1145/182.358434

The paper introducing the interval algebra of thirteen basic relations between time intervals, the qualitative interval-reasoning calculus that complements point-based temporal logic in Section 30.1.

📄 Paper

Causal Inference Foundations

Pearl, J. "Causality: Models, Reasoning, and Inference." 2nd ed., Cambridge University Press, 2009. cambridge.org/core/books/causality

The definitive treatment of structural causal models, do-calculus, and counterfactuals, the conceptual backbone for the effect estimation of Section 30.3 and the counterfactual reasoning of Section 30.4.

📖 Book

Peters, J., Janzing, D., Scholkopf, B. "Elements of Causal Inference: Foundations and Learning Algorithms." MIT Press, 2017. library.oapen.org/handle/20.500.12657/26040

A modern, freely available text tying causal structure to learning algorithms, including time-series causal discovery, the bridge between machine learning and the causal methods of Sections 30.2 and 30.3.

📖 Book (free PDF)

Causal Discovery for Time Series

Runge, J., et al. "Detecting and Quantifying Causal Associations in Large Nonlinear Time Series Datasets (PCMCI)." Science Advances, 5(11), 2019. arXiv:1702.07007. arxiv.org/abs/1702.07007

The paper introducing PCMCI and its momentary conditional independence test for recovering causal graphs from high-dimensional time series, the core causal-discovery algorithm of Section 30.2 and the lab.

📄 Paper

Runge, J., et al. "Tigramite: Causal Inference and Causal Discovery for Time Series Datasets." Software library. github.com/jakobrunge/tigramite

The reference implementation of PCMCI and its variants, the library that collapses the causal-discovery step of Section 30.2 into a few lines over a multivariate array, used directly in the lab.

🔧 Tool

Treatment Effects over Time

Hernan, M. A., Robins, J. M. "Causal Inference: What If." Chapman & Hall/CRC, 2020. hsph.harvard.edu/miguel-hernan/causal-inference-book

The freely available standard text on causal inference from observational data, including time-varying treatments and confounding, the authoritative reference behind the sequential-treatment methods of Section 30.3.

📖 Book (free PDF)

Robins, J. M., Hernan, M. A., Brumback, B. "Marginal Structural Models and Causal Inference in Epidemiology." Epidemiology, 11(5), 550-560, 2000. doi.org/10.1097/00001648-200009000-00011

The paper introducing marginal structural models and inverse-probability weighting for time-varying treatments under time-varying confounding, the confounding-aware estimator at the heart of Section 30.3.

📄 Paper

Bica, I., Alaa, A. M., Jordon, J., van der Schaar, M. "Estimating Counterfactual Treatment Outcomes over Time Through Adversarially Balanced Representations (Counterfactual Recurrent Network)." ICLR, 2020. arXiv:2002.04083. arxiv.org/abs/2002.04083

The paper introducing the Counterfactual Recurrent Network, a neural estimator of sequential treatment effects that balances time-varying confounders, the modern deep approach previewed in Sections 30.3 and 30.4.

📄 Paper

Melnychuk, V., Frauen, D., Feuerriegel, S. "Causal Transformer for Estimating Counterfactual Outcomes." ICML, 2022. arXiv:2204.07258. arxiv.org/abs/2204.07258

The paper introducing the Causal Transformer, bringing long-range attention to sequential counterfactual outcome estimation, the transformer-era successor to the recurrent estimators of Sections 30.3 and 30.4.

📄 Paper

Counterfactual and What-If Analysis

Brodersen, K. H., Gallusser, F., Koehler, J., Remy, N., Scott, S. L. "Inferring Causal Impact Using Bayesian Structural Time-Series Models (CausalImpact)." Annals of Applied Statistics, 9(1), 247-274, 2015. google.github.io/CausalImpact

The paper and library that fit a Bayesian structural-time-series counterfactual baseline from a pre-intervention period and control series, the what-if method of Section 30.4 used directly in the lab.

🔧 Paper & Tool

Sharma, A., Kiciman, E., et al. "DoWhy: An End-to-End Library for Causal Inference." Software library, py-why. py-why.github.io/dowhy

The library that frames causal effect estimation as an identify-estimate-refute workflow with explicit assumption checks, the off-the-shelf engine for the effect and counterfactual estimation of Sections 30.3 and 30.4.

🔧 Tool