Expected-Cost Analysis for Probabilistic Programs and Semantics-Level Adaption of Optional Stopping Theorems

Di Wang, Jan Hoffmann, Thomas W. RepsarXiv 2021

Abstract

Analyzing the execution cost of probabilistic programs requires navigating the complexities of stochastic processes. Traditional methods often struggle with non-terminating traces or negative costs, leading to potentially unsound bounds. This paper introduces a rigorous framework that adapts the Optional Stopping Theorem (OST) from probability theory into a Hoare-style program logic. By developing a "semantics-friendly" version of the OST, the authors provide a sound method for deriving expected-cost bounds for complex, recursive probabilistic code.

Introduction

In classical program analysis, determining execution cost is a fundamental task. However, in probabilistic programming—where programs utilize random draws and coin flips—cost becomes an expected value. Calculating this value accurately is notoriously difficult, particularly when programs may not terminate or when costs can be negative (e.g., "tick(-1)").

A recent study by Di Wang, Jan Hoffmann, and Thomas Reps addresses these challenges by bridging the gap between formal program semantics and probability theory. Their work focuses on the "potential method," a form of amortized analysis, and its relationship to Martingales—sequences of random variables where the expected future value equals the current value.

The Problem: The Pitfall of Optional Stopping

The primary obstacle in probabilistic cost analysis is the "Optional Stopping" problem. While a Martingale's expectation remains constant at any fixed step, this property does not automatically hold at the moment a program terminates (the "stopping time"). Standard analysis techniques can inadvertently produce mathematically valid but practically "unsound" bounds if they fail to account for non-terminating traces or varying costs.

While mathematicians use Optional Stopping Theorems to resolve these issues, the requirements for these theorems—such as "uniform integrability"—are often too abstract for direct application to source code. There is a significant "semantic gap" between the abstract requirements of probability theory and the concrete properties of a programming language.

A Semantics-Friendly Optional Stopping Theorem

The core contribution of this research is a new version of the OST (Theorem 3.12) designed specifically for program analysis. Instead of abstract mathematical conditions, this theorem utilizes properties that are more natural to verify in a programming context:

  1. Controlled Non-Termination: The probability of the program failing to terminate must be bounded.
  2. The Product Principle: The authors identify that soundness depends on the product of the probability of non-termination and the "expected gap" in potential vanishing as time approaches infinity.
  3. Bounded Updates: The program must exhibit bounded changes in state (e.g., variables do not jump to infinity in a single step).

By replacing global properties like uniform integrability with these local, checkable properties, the authors make it possible to apply the OST directly to program logic.

The APPL Framework and Hoare-Style Logic

To formalize their approach, the authors define APPL, an imperative probabilistic language supporting general recursion and continuous distributions (e.g., uniform(a, b)). They model program execution as a Markov chain, allowing them to treat accumulated costs and state potential as a stochastic process.

The framework introduces a set of Hoare-style inference rules (e.g., {Pre} Statement {Post}). These rules allow for the compositional derivation of expected-cost bounds. If a bound can be derived using these rules, it is mathematically guaranteed to be a sound upper limit on the program's expected cost.

Key Insights and Results

The research successfully unifies the potential method from amortized analysis with the Martingale approach from probability theory. Key results include:

  • Handling Negative Costs: Unlike simpler "ranking function" approaches, this framework soundly handles programs where costs can decrease.
  • Formal Soundness: The authors provide a rigorous proof of the logic's soundness, ensuring that derived bounds are reliable.
  • Practical Extension: Theorem 3.12 extends the reach of cost analysis to programs where state changes are bounded by polynomials, a common characteristic in real-world software.

Conclusion and Future Work

This work provides a vital theoretical bridge for the probabilistic programming community. By translating abstract probability theorems into concrete program logic, it enables the sound analysis of complex stochastic behaviors.

Future research may focus on extending this theory to handle lower bounds more comprehensively, expanding the language scope to include complex data structures or functional paradigms, and developing automated tools to infer potential functions for practical engineering applications.

Summary updated

BibTeX
@misc{wang2021expected,
  author = {Di Wang and Jan Hoffmann and Thomas W. Reps},
  title = {{Expected-Cost Analysis for Probabilistic Programs and Semantics-Level Adaption of Optional Stopping Theorems}},
  year = {2021},
  eprint = {2103.16105},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2103.16105},
}
Figure