Skip to content

perf: optimize Greenwood Laplace statistic with Numba#128

Open
tahmazidik wants to merge 5 commits into
mainfrom
feature/numba-optimization
Open

perf: optimize Greenwood Laplace statistic with Numba#128
tahmazidik wants to merge 5 commits into
mainfrom
feature/numba-optimization

Conversation

@tahmazidik

@tahmazidik tahmazidik commented Jul 18, 2026

Copy link
Copy Markdown
Contributor

Summary

Optimize the Greenwood Laplace goodness-of-fit statistic using Numba to reduce execution time and avoid unnecessary intermediate arrays

Part of #122

Quick changelog

  • Added Numba as a project dependency
  • Reimplemented the Greenwood Laplace statistic in a JIT-compiled function
  • Calculated the Laplace cumulative distribution function directly inside the compiled loop
  • Added input conversion and one-dimensional sample validation
  • Added regression tests comparing the optimized implementation with the SciPy reference implementation
  • Added a reproducible benchmark comparing both implementations

What’s new?

The Greenwood Laplace statistic is now calculated in a Numba-compiled function

The implementation sorts the input sample, calculates the Laplace cumulative distribution function directly, and accumulates squared spacings in a single loop. This reduces Python overhead and avoids creating unnecessary intermediate arrays with np.concatenate and np.diff

The implementation now also validates that the input sample is one-dimensional and raises a clear ValueError for multidimensional input

Regression tests were added to compare the optimized implementation with the previous SciPy-based formula. The tests cover a single observation, unsorted samples, repeated values, different location and scale parameters, and integer NumPy arrays

A benchmark was added for a sample of 10,000 values and 1,000 executions. The Numba function is called once before measurement so that JIT compilation time is not included in the benchmark

Benchmark results:

  • SciPy reference implementation: 0.000285595 seconds per call
  • Numba implementation: 0.000218485 seconds per call
  • Speedup: approximately 1.31x

@tahmazidik
tahmazidik marked this pull request as ready for review July 20, 2026 10:57
@tahmazidik
tahmazidik requested review from alex98247 and f1i3g3 July 20, 2026 10:57
@f1i3g3 f1i3g3 added enhancement New feature or request python Pull requests that update python code 1.1.0 labels Jul 20, 2026
@f1i3g3

f1i3g3 commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

That's cool! Further experimentation is needed.

@f1i3g3 f1i3g3 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

С Numba создаётся новый массив или там уже всё оптимизировано? Иначе может получиться, что для образца ещё и сборщик мусора считается.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Сортировку можно вынести отдельно и вызывать до замеров.

Comment on lines +384 to +385
if rvs_array.ndim != 1:
raise ValueError("Sample must be one-dimensional.")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

?

Comment thread tests/distributions/test_laplace.py Outdated
KolmogorovSmirnovLaplaceGofStatistic,
KuiperLaplaceGofStatistic,
WatsonLaplaceGofStatistic,
_greenwood_laplace_statistic,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Если метод выносится в паблик, лучше именовать его в CamelCase.

@tahmazidik
tahmazidik requested a review from f1i3g3 July 22, 2026 17:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

1.1.0 enhancement New feature or request python Pull requests that update python code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants