⚡️ Speed up method BetaBinomial.pdf by 74%
#68
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
📄 74% (0.74x) speedup for
BetaBinomial.pdfinquantecon/distributions.py⏱️ Runtime :
4.15 milliseconds→2.39 milliseconds(best of25runs)📝 Explanation and details
The optimized code achieves a 73% speedup by replacing SciPy's
binomandbetafunctions with custom Numba-JIT-compiled implementations that use log-gamma calculations.Key optimizations:
Numba JIT compilation: The core computation is moved into
_pdf_numba, a JIT-compiled function that executes at near-C speed. This eliminates Python interpreter overhead for the inner loops.Efficient log-gamma approach: Instead of calling
scipy.special.binomandscipy.special.beta(which have additional overhead), the optimized code usesmath.lgammadirectly and computes results viaexp(lgamma(...)). This is numerically stable and faster.Loop fusion: The original code creates intermediate arrays through vectorized operations (
binom(n, k) * beta(...) / beta(...)), while the optimized code computes everything in tight loops within JIT-compiled functions, reducing memory allocations and improving cache locality.Cached compilation: The
cache=Trueparameter ensures the JIT compilation overhead is paid only once, making subsequent calls extremely fast (as seen intest_pdf_jit_compilationwhere the second call drops from 14.0μs to 3.88μs).Performance characteristics:
Workload impact:
Since
function_referencesis not available, we cannot determine if this function is in a hot path. However, the annotated tests show the optimization is particularly effective when:The optimization maintains identical numerical behavior to the original (using the same gamma function generalization for non-integer n), ensuring correctness while delivering substantial performance gains.
✅ Correctness verification report:
⚙️ Click to see Existing Unit Tests
test_distributions.py::TestBetaBinomial.test_pdf🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-BetaBinomial.pdf-mjvx03e4and push.