question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`stats.norminvgauss` incorrect implementation?

See original GitHub issue

Hi,

Mentioned in the latest release of TensorFlow Probability, it has been noted that scipy.stats.norminvgauss has not been implemented in the same way as R or the original mathematical paper.

It seems the loc and scale parameters should actually be mu and delta. Is there a way to update this so that non-standard cases (other than loc=0, scale =1) make mathematical sense?

Note that `scipy.stats.norminvgauss` implements the distribution as a
      location-scale family. However, in the original paper, and other
      implementations (such as R) do not implement it this way. Thus the
      `scale` parameters here and scipy don't match unless `scale = 1`.
  Warning: As mentioned above, this distribution is __not__ a location-scale
  family.

Link here: https://github.com/tensorflow/probability/blob/main/tensorflow_probability/python/distributions/normal_inverse_gaussian.py

Docs: https://www.tensorflow.org/probability/api_docs/python/tfp/distributions/NormalInverseGaussian

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:16 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
Nishan-Pradhancommented, Aug 31, 2021

@mdhaber Thanks! That works really well!

1reaction
mdhabercommented, Aug 26, 2021

Very close, but don’t use norminvg to calculate the PPF and ISF. The functions will only be evaluated at x = tol/10, so you can code in correct values yourself. It is not very sensitive to these values being super accurate - e.g., for PPF, return any value x at which norminvg.cdf(x) is very small (ideally tol/10).

Something like

class CustomNIG:

    def pdf(self, x):
        return norminvg.pdf(x)

    def cdf(self, x):
        return norminvg.cdf(x)

    def ppf(self, x):
        return -2  # norminvg.cdf(-2) ~ 0

    def isf(self, x):
        return 2  # norminvg.sf(2) ~ 0


dist = CustomNIG()
norminvg_fast = stats.NumericalInverseHermite(dist, tol=1e-6)

ppf = norminvg_fast.ppf(data)

This will make NumericalInverseHermite form an approximation H of the inverse of norminvg.cdf that is valid between -2 and 2. The round-trip tolerance (i.e. abs(x - norminvg.cdf(H(x)))) will be controlled at mesh points and midpoints between mesh points to tol=1e-6.

Read more comments on GitHub >

github_iconTop Results From Across the Web

scipy.stats.norminvgauss — SciPy v1.9.3 Manual
A Normal Inverse Gaussian continuous random variable. As an instance of the rv_continuous class, norminvgauss object inherits from it a collection of generic ......
Read more >
oryx.distributions.NormalInverseGaussian | Oryx | TensorFlow
Python bool describing behavior when a stat is undefined. Stats return +/- infinity when it makes sense. E.g., the variance of a Cauchy...
Read more >
SciPy 1.8.0 Release Notes
A new scipy.stats.sampling submodule that leverages the UNU.RAN C library to sample from ... #14568: `stats.norminvgauss` incorrect implementation?
Read more >
Scipy/Scipy: Scipy 1.1.0 - NASA/ADS
The method switches between two implementations depending on the problem ... Added the normal inverse Gaussian distribution as scipy.stats.norminvgauss.
Read more >
scipy.stats._continuous_distns — Copulas 0.6.0 documentation
The location-scale-based parameterization implemented in SciPy is based on [2]_, where :math:`a ... "Values replaced by NaN to avoid incorrect results.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found