Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: stats.levy_stable: incorrect logpdf values for beta == +-1 #20636

Open
tjhunter opened this issue May 3, 2024 · 0 comments
Open

BUG: stats.levy_stable: incorrect logpdf values for beta == +-1 #20636

tjhunter opened this issue May 3, 2024 · 0 comments
Labels
defect A clear bug or issue that prevents SciPy from being installed or used as expected scipy.stats

Comments

@tjhunter
Copy link

tjhunter commented May 3, 2024

Describe your issue.

Hello,
I am implementing Levy's alpha-stable distribution for the JAX framework, using scipy as reference and I found some discrepancies.

The tail values of scipy's levy_stable.logpdf are incorrect for alpha in (1,2], beta in {-1, +1} and |x| >= 10. In particular, they do not obey the expected exponential law.

See Nolan (2020), Proposition 3.1 page 100 for the correct tail approximation. I have implemented it (in jax) here:
https://github.com/tjhunter/levy-stable-jax/blob/b3e3e05cc572b68742a8d1f0223fd88230836f30/src/levy_stable_jax/distribution.py#L169

Here is an example. In blue is the theoretical tail behaviour (implemented by levy-stable-jax), in orange scipy

image

Link to the notebook that created the plot:
https://github.com/tjhunter/levy-stable-jax/blob/main/notebooks/scipy_issues.ipynb

My suggestion to resolve this issue would be to bypass the quadrature-based implementation in C for |beta| == 1 and to directly implement the tail approximation as in `levy-stable-jax1. Happy to discuss a possible PR.

Reproducing Code Example

import seaborn as sns
from scipy.stats import levy_stable as sp_levy_stable
import numpy as np

sp_levy_stable.parameterization = "S0"
(alpha, beta) = (1.6, 1.0)
xs = np.linspace(-15, 10, 100)
sns.lineplot(x=xs,            y=sp_levy_stable.logpdf(xs,alpha=alpha, beta=beta))

Error message

N/A

SciPy/NumPy/Python version and system information

1.13.0 1.26.4 sys.version_info(major=3, minor=11, micro=9, releaselevel='final', serial=0)
Build Dependencies:
  blas:
    detection method: pkgconfig
    found: true
    include directory: /usr/local/include
    lib directory: /usr/local/lib
    name: openblas
    openblas configuration: USE_64BITINT=0 DYNAMIC_ARCH=1 DYNAMIC_OLDER= NO_CBLAS=
      NO_LAPACK= NO_LAPACKE= NO_AFFINITY=1 USE_OPENMP= ZEN MAX_THREADS=64
    pc file directory: /usr/local/lib/pkgconfig
    version: 0.3.26.dev
  lapack:
    detection method: pkgconfig
    found: true
    include directory: /usr/local/include
    lib directory: /usr/local/lib
    name: openblas
    openblas configuration: USE_64BITINT=0 DYNAMIC_ARCH=1 DYNAMIC_OLDER= NO_CBLAS=
      NO_LAPACK= NO_LAPACKE= NO_AFFINITY=1 USE_OPENMP= ZEN MAX_THREADS=64
    pc file directory: /usr/local/lib/pkgconfig
    version: 0.3.26.dev
  pybind11:
    detection method: config-tool
    include directory: unknown
    name: pybind11
    version: 2.12.0
Compilers:
  c:
    commands: cc
    linker: ld.bfd
    name: gcc
    version: 10.2.1
  c++:
    commands: c++
    linker: ld.bfd
    name: gcc
    version: 10.2.1
  cython:
    commands: cython
    linker: cython
    name: cython
    version: 3.0.10
  fortran:
    commands: gfortran
    linker: ld.bfd
    name: gcc
    version: 10.2.1
  pythran:
    include directory: ../../tmp/pip-build-env-ilw0s1wu/overlay/lib/python3.11/site-packages/pythran
    version: 0.15.0
Machine Information:
  build:
    cpu: x86_64
    endian: little
    family: x86_64
    system: linux
  cross-compiled: false
  host:
    cpu: x86_64
    endian: little
    family: x86_64
    system: linux
Python Information:
  path: /opt/python/cp311-cp311/bin/python
  version: '3.11'
@tjhunter tjhunter added the defect A clear bug or issue that prevents SciPy from being installed or used as expected label May 3, 2024
@lucascolley lucascolley changed the title BUG: incorrect logpdf values in scipy.stats.levy_stable for beta == +-1 BUG: stats.levy_stable: incorrect logpdf values for beta == +-1 May 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
defect A clear bug or issue that prevents SciPy from being installed or used as expected scipy.stats
Projects
None yet
Development

No branches or pull requests

2 participants