Newton for one-counter loops, and accelerated unrolling for slowly converging loops

Open
#313 2 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
25/100
Issue type
Feature
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
backend

Research direction

Start by reading the existing lower-bound strategies in prodigy/analysis/lowerbounds/, especially the earlier newton.py and onecounter.py implementation mentioned in the issue, then inspect how strategies are registered and how PGF truncation represents bounded supports. The issue is complete when the one-counter Newton strategy and the truncation-based acceleration option produce sound lower bounds and residual mass, with the stated upper bounds and moments, and are benchmarked against plain unrolling on the named examples.

Written by the indexing model from the issue text.

Description

area: analysis enhancement priority: medium topic: loop strategies

Use Newton iteration as an option to speed up unrolling.

Observation

The forward semantics of a while loop is linear in the distribution, so Newton on the loop's fixed-point equation over distributions is just the exact solve of (I − T) G = G_0 (what the symbolic strategies do). But for structured loops the infinite linear system collapses to a finite polynomial (non-linear) equation, and there Newton's quadratic convergence beats Kleene:

  • One-counter loops while (x > c) { body } where every iteration changes the counter by at least −1 (skip-free downwards) with a step distribution that does not depend on the counter (homogeneous), plus reward variables. By homogeneity and skip-freeness, the first passage from c + 1 + k down to c is the sum of k + 1 independent one-level passages, so the generating function U(Y) of the rewards until exit is the least solution of u = f(u, Y) with f(u, Y) = Σ P(k, r) Y^r u^(k+1) (non-negative coefficients). Started in x = c + m the output is U(Y)^m, so the output distribution is G(U(Y), Y).
  • Newton N(v) = v + (1 − f_u(v))^(−1) (f(v) − v) from v = 0 converges to U from below; by convexity v ≤ N(v) ≤ U coefficient-wise for every 0 ≤ v ≤ U, so rounding down after each step is sound (truncated power series in the queried reward, ball arithmetic with FLINT arb_series, keep lower endpoints). Upper bounds by Park induction on the scalar equation at Y = 1 (any w with f(w, 1) ≤ w bounds U(1)); moments from U'(1) = f_Y / (1 − f_u).
  • An earlier version of prodigy/analysis/lowerbounds had exactly this as a mode (newton.py with onecounter.py: detection of one-counter loops, scalar and power-series Newton, Kleene as a switch for comparison); it was not merged with the other lower-bound modes. The maintainer has the code; it can be the starting point.
  • Checking applicability is syntactic: guard x > c, counter decreases by at most 1 per iteration on every path, probabilities independent of the counter (detect_one_counter). Cost per step: Newton needs f, f_u and one series division, about 2–3× a Kleene step on the same truncation, but converges quadratically (and with rate 1/2 per step in the critical case f_u(U) = 1, where Kleene converges only like 1/k). So when a loop is one-counter, Newton is the better choice; otherwise fall back to the options below.

Options (for loops that are not one-counter)

  1. Exact solve on a truncation: restrict the loop to a finite set of states S (states leaving S count as unaccounted mass), solve the finite linear system exactly (rational arithmetic; this is the Newton step of the truncated system). The result is the occupation measure of the truncated loop, a sound lower bound with residual mass = 1 − mass found; grow S adaptively.
  2. Iteration doubling: compute T^(2^k) by repeated squaring on a finite truncation, so k steps give 2^k Kleene iterations.
  3. Compare cost: one exact solve on |S| states is O(|S|^3) (sparse: less) versus Kleene's O(nnz) per iteration times the number of iterations; worthwhile when the iteration count needed is ≫ |S|.

Questions

  • Which state truncations fit Prodigy's PGF representation (bounded supports per variable, as the sandwich's numerical exploration does in lowerbounds/)?
  • How to report the residual soundly (as for unrolling iterates).

Done when

  • A loop strategy newton for one-counter loops (lower bounds with residual mass, Park upper bounds, moments), registered like the other strategies, with Kleene as comparison on the same truncation.
  • A strategy option (e.g. unroll:accelerate=solve) for loops whose relevant state space can be truncated, with sound lower bounds and residual mass, on all three engines; a benchmark against plain unrolling on the slowly converging examples in pgfexamples/inference/loopy/hard_distributions.
  • Related: #224 (unrolling performance), #188 (recursion, where Newton proper applies).

🤖 Generated with Claude Code

Dominant language
Python
Stars
6
Forks
2
Avg merge
1h 52m
Merged PRs (30d)
187

Getting set up

  • Ships a Dockerfile or Docker Compose file
  • No pull request template
  • No contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from LKlinke/Prodigy

All issues in LKlinke/Prodigy

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.