Performance¶
performance
¶
Performance optimization module for HeavyTails library.
This module contains performance-critical functions and optimization TODO items that will be tracked as GitHub Issues.
DistributionCache
¶
Intelligent caching system for distribution computations.
Uses LRU (Least Recently Used) caching to store results of expensive computations. Particularly useful for: - Repeated PDF/CDF evaluations with same parameters - Quantile calculations for common probabilities - Parameter fitting results
Attributes:
| Name | Type | Description |
|---|---|---|
max_size |
Maximum number of cached entries (default: 1000) |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_size
|
int
|
Maximum number of entries to cache |
1000
|
Source code in heavytails/performance.py
cached_cdf
¶
Cached CDF evaluation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
distribution
|
str
|
Name of the distribution class |
required |
x
|
float
|
Point at which to evaluate CDF |
required |
**params
|
Any
|
Distribution parameters |
{}
|
Returns:
| Type | Description |
|---|---|
float
|
CDF value at x |
Examples:
Source code in heavytails/performance.py
cached_pdf
¶
Cached PDF evaluation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
distribution
|
str
|
Name of the distribution class |
required |
x
|
float
|
Point at which to evaluate PDF |
required |
**params
|
Any
|
Distribution parameters |
{}
|
Returns:
| Type | Description |
|---|---|
float
|
PDF value at x |
Examples:
>>> cache = DistributionCache()
>>> pdf1 = cache.cached_pdf('Pareto', 2.0, alpha=2.5, xm=1.0)
>>> pdf2 = cache.cached_pdf('Pareto', 2.0, alpha=2.5, xm=1.0) # Retrieved from cache
Source code in heavytails/performance.py
cached_ppf
¶
Cached PPF (quantile) evaluation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
distribution
|
str
|
Name of the distribution class |
required |
u
|
float
|
Probability at which to evaluate PPF (0 < u < 1) |
required |
**params
|
Any
|
Distribution parameters |
{}
|
Returns:
| Type | Description |
|---|---|
float
|
Quantile value at probability u |
Examples:
Source code in heavytails/performance.py
clear_cache
¶
get_cache_info
¶
Get cache statistics.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary containing cache hit/miss statistics for each function |
Source code in heavytails/performance.py
PPFOptimizer
dataclass
¶
Optimized quantile function (PPF) computation using hybrid root-finding.
Uses Newton-Raphson when PDF is available, with fallback to Brent's method for robustness. Includes caching for frequently used quantiles.
Attributes:
| Name | Type | Description |
|---|---|---|
max_iterations |
int
|
Maximum iterations for root-finding (default: 100) |
tolerance |
float
|
Convergence tolerance (default: 1e-9) |
cache_size |
int
|
Size of LRU cache for quantiles (default: 128) |
optimize_ppf
¶
Create optimized PPF using hybrid root-finding methods.
Tries Newton-Raphson first (if PDF available), falls back to Brent's method for robustness. Caches frequently used quantiles.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cdf_func
|
Callable[[float], float]
|
Cumulative distribution function F(x) |
required |
pdf_func
|
Callable[[float], float] | None
|
Probability density function f(x), optional |
None
|
Returns:
| Type | Description |
|---|---|
Callable[[float], float]
|
Optimized PPF function that takes u in (0,1) and returns x |
Examples:
>>> optimizer = PPFOptimizer()
>>> from heavytails import Pareto
>>> dist = Pareto(alpha=2.5, xm=1.0)
>>> fast_ppf = optimizer.optimize_ppf(dist.cdf, dist.pdf)
>>> x = fast_ppf(0.5)
Source code in heavytails/performance.py
PerformanceBenchmarks
¶
Comprehensive performance benchmarking suite.
Tracks performance metrics for distribution operations including: - PDF/CDF evaluation speed - Sampling performance - Parallel vs serial performance - Cache effectiveness
Attributes:
| Name | Type | Description |
|---|---|---|
benchmarks |
dict[str, Any]
|
Dictionary storing benchmark results |
Source code in heavytails/performance.py
benchmark_cache_effectiveness
¶
Benchmark cache effectiveness for repeated evaluations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
distribution
|
str
|
Name of the distribution class |
required |
n_evaluations
|
int
|
Total number of evaluations to perform |
required |
n_unique
|
int
|
Number of unique evaluation points |
required |
**params
|
Any
|
Distribution parameters |
{}
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary containing cache performance metrics |
Examples:
>>> benchmarks = PerformanceBenchmarks()
>>> results = benchmarks.benchmark_cache_effectiveness(
... 'Pareto', n_evaluations=10000, n_unique=100, alpha=2.5, xm=1.0
... )
Source code in heavytails/performance.py
benchmark_pdf_evaluation
¶
Benchmark PDF evaluation performance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
distribution
|
str
|
Name of the distribution class |
required |
n_points
|
int
|
Number of points to evaluate |
required |
**params
|
Any
|
Distribution parameters |
{}
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary containing timing results |
Examples:
>>> benchmarks = PerformanceBenchmarks()
>>> results = benchmarks.benchmark_pdf_evaluation('Pareto', 10000, alpha=2.5, xm=1.0)
Source code in heavytails/performance.py
benchmark_sampling
¶
Benchmark sampling performance across different sample sizes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
distribution
|
str
|
Name of the distribution class |
required |
sizes
|
list[int]
|
List of sample sizes to benchmark |
required |
**params
|
Any
|
Distribution parameters |
{}
|
Returns:
| Type | Description |
|---|---|
dict[int, dict[str, Any]]
|
Dictionary containing timing results for each size |
Examples:
>>> benchmarks = PerformanceBenchmarks()
>>> results = benchmarks.benchmark_sampling('Pareto', [100, 1000, 10000], alpha=2.5, xm=1.0)
Source code in heavytails/performance.py
print_results
¶
Print benchmark results in a readable format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
results
|
dict[str, Any] | None
|
Results dictionary to print (default: all stored benchmarks) |
None
|
Source code in heavytails/performance.py
run_all_benchmarks
¶
Execute comprehensive benchmark suite for specified distributions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
distributions
|
list[str] | None
|
List of distribution names to benchmark (default: ['Pareto', 'StudentT', 'LogNormal']) |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary containing all benchmark results |
Examples:
Source code in heavytails/performance.py
parallel_sampling
¶
Parallel random number generation for large samples using multiprocessing.
Splits the sampling task across multiple CPU cores for faster generation. Particularly beneficial for large samples (n > 10000) and distributions with expensive sampling procedures.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
distribution
|
str
|
Name of the distribution class (e.g., 'Pareto', 'StudentT') |
required |
n
|
int
|
Number of samples to generate |
required |
n_cores
|
int | None
|
Number of CPU cores to use (default: all available cores) |
None
|
seed
|
int | None
|
Random seed for reproducibility (default: None) |
None
|
**params
|
Any
|
Distribution parameters |
{}
|
Returns:
| Type | Description |
|---|---|
list[float]
|
List of n random samples from the distribution |
Examples:
>>> from heavytails.performance import parallel_sampling
>>> samples = parallel_sampling('Pareto', n=100000, n_cores=4, seed=42, alpha=2.5, xm=1.0)
>>> len(samples)
100000
Note
- For small n (< 1000), overhead may outweigh benefits
- Uses different random seeds for each worker to ensure independence
- Results are deterministic when seed is provided
Source code in heavytails/performance.py
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 | |
robust_log_gamma
¶
Numerically stable log-gamma function.
Currently uses math.lgamma which works for most cases but can fail for: - Very large arguments (> 1e308) - Arguments very close to negative integers
Future enhancements could include: - Stirling's approximation for large x - Series expansions for problematic regions - Complex plane extensions - Error bounds and accuracy guarantees
Source code in heavytails/performance.py
vectorized_cdf_evaluation
¶
Vectorized CDF evaluation for array inputs.
.. note::
See :func:vectorized_pdf_evaluation. Prefer calling dist.cdf
directly, which takes an array.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
distribution
|
str
|
Name of the distribution class (e.g., 'Pareto', 'StudentT') |
required |
x_array
|
list[float]
|
Array of x values to evaluate |
required |
**params
|
Any
|
Distribution parameters |
{}
|
Returns:
| Type | Description |
|---|---|
list[float]
|
List of CDF values evaluated at each point in x_array |
Examples:
>>> from heavytails.performance import vectorized_cdf_evaluation
>>> x = [1.0, 2.0, 3.0, 4.0, 5.0]
>>> cdf_values = vectorized_cdf_evaluation('Pareto', x, alpha=2.5, xm=1.0)
Source code in heavytails/performance.py
vectorized_pdf_evaluation
¶
Vectorized PDF evaluation for array inputs.
.. note::
This is now a thin wrapper. dist.pdf takes an array and returns one,
so looking a distribution up by name and handing it the points is all
that is left to do -- prefer calling the method directly. It used to go
through np.vectorize, which NumPy's own documentation describes as
provided for convenience rather than performance, and which was a
Python loop either way.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
distribution
|
str
|
Name of the distribution class (e.g., 'Pareto', 'StudentT') |
required |
x_array
|
list[float]
|
Array of x values to evaluate |
required |
**params
|
Any
|
Distribution parameters (e.g., alpha=2.5, xm=1.0) |
{}
|
Returns:
| Type | Description |
|---|---|
list[float]
|
List of PDF values evaluated at each point in x_array |
Examples:
>>> from heavytails.performance import vectorized_pdf_evaluation
>>> x = [1.0, 2.0, 3.0, 4.0, 5.0]
>>> pdf_values = vectorized_pdf_evaluation('Pareto', x, alpha=2.5, xm=1.0)