Skip to content

doc(gh-2187): mathematical details for GaussianRandomWalk - #2266

Merged
juanitorduz merged 2 commits into
pyro-ppl:masterfrom
jabrailkhalil:doc/gh-2187-gaussian-random-walk
Sep 14, 2026
Merged

juanitorduz merged 2 commits into
pyro-ppl:masterfrom
jabrailkhalil:doc/gh-2187-gaussian-random-walk

Conversation

@jabrailkhalil

Copy link
Copy Markdown
Contributor

Summary

Adds mathematical explanations and implementation details for GaussianRandomWalk to the class, sample, log_prob, mean, and variance docstrings, as tracked in gh-2187.

  • Class: joint density of the random walk over R^n, defined via initial position and independent Normal increments.
  • sample: cumulative sum of standard Normal draws.
  • log_prob: sum of univariate Normal log densities over the initial position and increments.
  • mean and variance: E[X_t] = 0 and Var(X_t) = (t+1) * scale^2, matching the implementation.

Math details were checked numerically against the implementation (log_prob, mean, variance, batched scale shapes).

Testing

  • ruff check and ruff format --check on the changed file: pass.
  • ty check and codespell: pass.
  • pytest test/test_distributions.py -k "random_walk or RandomWalk": 62 passed, 42 skipped (missing optional deps), 2 xfailed, on local CPU before opening the PR.

Add class, sample, log_prob, mean, and variance docstrings with
mathematical derivations for the Gaussian random walk distribution,
following the docstring style of the other pyro-pplgh-2187 doc PRs.
@github-actions

Copy link
Copy Markdown

Benchmark report

this PR doc/gh-2187-gaussian-random-walk at a1c8d453 vs baseline master at 93041d1e

+ run time:     7 faster
- compile time: 1 slower, 0 faster

Significant changes (8)

                               ──────── run time ────────     ────── compile time ──────
  benchmark                    baseline   this PR       Δ     baseline   this PR       Δ
────────────────────────────────────────────────────────────────────────────────────────
- dirichlet_log_prob             637 µs    617 µs   -3.2%     455.8 ms  574.3 ms  +26.0%
+ normal_sample                 24.7 ms   22.1 ms  -10.7%     206.6 ms  204.0 ms   -1.3%
+ hmc_logistic_regression      816.3 ms  775.4 ms   -5.0%       3.54 s    3.38 s   -4.8%
+ nuts_dense_mass_funnel         1.30 s    1.18 s   -9.2%       2.84 s    2.71 s   -4.5%
+ nuts_eight_schools             1.33 s    1.19 s  -10.6%       2.95 s    2.65 s  -10.1%
+ nuts_logistic_regression       1.21 s    1.13 s   -6.4%       3.62 s    3.37 s   -7.1%
+ nuts_vectorized_chains         2.84 s    2.66 s   -6.3%       3.16 s    2.93 s   -7.4%
+ svi_autodelta_map_logistic   340.2 ms  315.9 ms   -7.1%       3.44 s    3.28 s   -4.7%

Red is slower, green is faster; a row is coloured by the worse of its two columns. A delta in parentheses cleared the threshold on a measurement below the resolution floor, so it is shown without being called a change. † marks a benchmark that could not be compared — see below.

Full results

distributions

                                 ───────── run time ────────     ────── compile time ──────
  benchmark                      baseline  this PR         Δ     baseline   this PR       Δ
───────────────────────────────────────────────────────────────────────────────────────────
  biject_to_constraints            4.1 ms   4.1 ms     +2.1%     359.7 ms  384.3 ms   +6.9%
  categorical_log_prob             2.2 ms   2.3 ms     +5.0%      70.7 ms   72.4 ms   +2.4%
- dirichlet_log_prob               637 µs   617 µs     -3.2%     455.8 ms  574.3 ms  +26.0%
  dirichlet_sample                44.7 ms  44.8 ms     +0.2%     863.4 ms  872.0 ms   +1.0%
  gamma_log_prob                   2.2 ms   2.1 ms     -0.9%       2.51 s    2.79 s  +11.1%
  gamma_sample                    18.9 ms  19.6 ms     +4.0%     830.8 ms  829.7 ms   -0.1%
  lkj_cholesky_sample              5.4 ms   5.4 ms     +0.8%       1.19 s    1.21 s   +1.2%
  mixture_same_family_log_prob     989 µs   987 µs     -0.2%      86.2 ms   89.7 ms   +4.1%
  multivariate_normal_log_prob     375 µs   331 µs  (-11.6%)     570.7 ms  580.3 ms   +1.7%
  normal_log_prob                  684 µs   653 µs     -4.5%      57.7 ms   57.6 ms   -0.2%
+ normal_sample                   24.7 ms  22.1 ms    -10.7%     206.6 ms  204.0 ms   -1.3%
  stick_breaking_transform         6.3 ms   6.4 ms     +1.7%     207.4 ms  240.3 ms  +15.9%
  student_t_log_prob               3.0 ms   3.1 ms     +3.6%      88.0 ms   92.5 ms   +5.2%
  truncated_normal_log_prob        775 µs   827 µs   (+6.8%)      54.6 ms   53.7 ms   -1.6%

handlers

                                  ──────── run time ───────     ────── compile time ──────
  benchmark                       baseline   this PR      Δ     baseline   this PR       Δ
──────────────────────────────────────────────────────────────────────────────────────────
  initialize_model_hierarchical    44.6 ms   45.9 ms  +2.9%       3.88 s    3.94 s   +1.4%
  log_density_hierarchical          3.8 ms    3.8 ms  +0.0%       1.29 s    1.34 s   +3.5%
  nested_handler_stack              1.5 ms    1.5 ms  -0.2%       620 µs    687 µs  +10.8%
  potential_energy_and_grad          22 µs     23 µs  +4.8%     100.9 ms  105.4 ms   +4.5%
  predictive_forward_sampling     746.9 ms  734.9 ms  -1.6%     204.5 ms  173.0 ms  -15.4%
  trace_seeded_model                852 µs    868 µs  +1.9%     555.4 ms  562.5 ms   +1.3%

mcmc

                             ──────── run time ────────     ────── compile time ─────
  benchmark                  baseline   this PR       Δ     baseline  this PR       Δ
─────────────────────────────────────────────────────────────────────────────────────
+ hmc_logistic_regression    816.3 ms  775.4 ms   -5.0%       3.54 s   3.38 s   -4.8%
+ nuts_dense_mass_funnel       1.30 s    1.18 s   -9.2%       2.84 s   2.71 s   -4.5%
+ nuts_eight_schools           1.33 s    1.19 s  -10.6%       2.95 s   2.65 s  -10.1%
  nuts_hierarchical_glm        5.07 s    5.08 s   +0.2%       5.38 s   5.61 s   +4.3%
+ nuts_logistic_regression     1.21 s    1.13 s   -6.4%       3.62 s   3.37 s   -7.1%
+ nuts_vectorized_chains       2.84 s    2.66 s   -6.3%       3.16 s   2.93 s   -7.4%

svi

                                             ──────── run time ───────     ───── compile time ─────
  benchmark                                  baseline   this PR      Δ     baseline  this PR      Δ
───────────────────────────────────────────────────────────────────────────────────────────────────
+ svi_autodelta_map_logistic                 340.2 ms  315.9 ms  -7.1%       3.44 s   3.28 s  -4.7%
  svi_autodiagonalnormal_hierarchical          1.12 s    1.07 s  -4.3%       5.58 s   5.26 s  -5.7%
  svi_automultivariatenormal_eight_schools   812.2 ms  797.8 ms  -1.8%       4.94 s   4.69 s  -5.0%
  svi_autonormal_logistic                    835.6 ms  802.1 ms  -4.0%       3.81 s   3.66 s  -3.8%
  svi_multi_particle_elbo                      1.56 s    1.56 s  +0.0%       3.85 s   3.74 s  -2.9%
  svi_trace_mean_field_elbo                    1.35 s    1.38 s  +1.8%       5.65 s   5.63 s  -0.5%
Methodology and environment

Each benchmark is set up untimed, then called once with the JAX caches cleared and several more times warm. Run is the fastest warm call; compile is the first call minus that, i.e. the tracing, lowering and XLA compilation the warm calls did not have to pay for.

Both refs were measured on the same runner over 2 interleaved round(s), taking the best observation per benchmark. A result is called neutral when it moves less than ±5% (run) or ±25% (compile), or when the measurement itself is under 1 ms (run) / 50 ms (compile) — a shared CI runner cannot resolve changes below that. Compile time gets the looser band because it is measured once per round rather than best-of-N, and swings by roughly 20% even between two runs of identical code. A delta shown in parentheses did clear its threshold, but on a measurement below the resolution floor, so it is reported without being called a change.

baseline this PR
ref master doc/gh-2187-gaussian-random-walk
commit 93041d1e a1c8d453
numpyro 0.21.0 0.21.0
jax 0.11.1 0.11.1
backend cpu cpu
python 3.14.7 3.14.7

Runner: Linux-6.17.0-1022-azure-x86_64-with-glibc2.39, 4 CPUs.

Produced by this benchmark run.

@juanitorduz

Copy link
Copy Markdown
Collaborator

Thanks for the PR! The math checks out against the implementation (I verified log_prob, variance, and the batched scale case numerically). Two changes requested before merging:

1. Broken inline markup in the class docstring (line 1186)

length :math:`n = ` :attr:`num_steps` starts at :math:`X_0` and evolves

RST requires the inline end-string to be immediately preceded by non-whitespace, so :math:`n = ` does not close where intended. The :math: role swallows through the next backtick and the math node content becomes literally n = ` :attr:`num_steps, which MathJax will render as garbage. make docs does not warn about this, so CI stays green. You can confirm with docutils:

import docutils.core
text = r"length :math:`n = ` :attr:`num_steps` starts at :math:`X_0`"
print(docutils.core.publish_string(text, writer_name="pseudoxml").decode())

Suggested fix:

length :math:`n` (given by :attr:`num_steps`) starts at :math:`X_0` and evolves

2. :rtype: ArrayLike in sample (line 1255) and log_prob (line 1277)

Every other method docstring in continuous.py uses :rtype: jax.Array (e.g. Normal.sample, Normal.log_prob), and both methods are annotated -> Array. Please change both to :rtype: jax.Array.

Optional nits

  • f(\cdot; \mu, \sigma^2) parameterizes the Normal density by variance, while the Normal class docstring in the same file uses f(x; \mu, \sigma) with \sigma as the standard deviation. Using f(\cdot; 0, \sigma) would keep the notation consistent across the two classes.
  • mean / variance say "Calculates the mean of the Gaussian random walk." whereas the rest of the file uses "Calculates the analytical mean." Purely cosmetic.

@juanitorduz juanitorduz left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Smal changes (see above)

@jabrailkhalil

Copy link
Copy Markdown
Contributor Author

Thanks @juanitorduz! I have addressed both requested changes (fixed the inline RST markup and updated the :rtype: annotations to jax.Array), along with the optional notation alignment to Normal.

@juanitorduz juanitorduz left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!

@juanitorduz
juanitorduz merged commit f960bc9 into pyro-ppl:master Sep 14, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review Awaiting review from maintainers documentation Related to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants