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.

truncnorm gives incorrect means and variances

See original GitHub issue

truncnorm.mean and truncnorm.var appear to give incorrect outputs when loc or scale are array-like and contain more than one number:

>>> import scipy.stats as stats
>>> stats.truncnorm.mean(-1, 2, loc=0)
0.229637179091329
>>> stats.truncnorm.mean(-1, 2, loc=[0])
array([0.22963718])
>>> stats.truncnorm.mean(-1, 2, loc=[0, 0]) # expect to get 0.2296 twice
array([0., 0.])
>>> stats.truncnorm.var(-1, 1, loc=0)
0.29112509477279314
>>> stats.truncnorm.var(-1, 1, loc=[0])
array([0.29112509])
>>> stats.truncnorm.var(-1, 1, loc=[0, 0]) # expect to get 0.2911 twice
array([1., 1.])

truncnorm.moment doesn’t seem to support array-likes at all, either:

>>> stats.truncnorm.moment(1, -1, 1, loc=[0])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\ProgramData\Miniconda3\lib\site-packages\scipy\stats\_distn_infrastructure.py", line 1266, in moment
    fac = float(scale) / float(loc)
TypeError: float() argument must be a string or a number, not 'list'

This is on Scipy version 1.6.0. This problem was even worse on version 1.4.1, where truncnorm.var() could produce negative numbers:

>>> stats.truncnorm.var(-1, 1, loc=0)
0.29112509477279314
>>> stats.truncnorm.var(-1, 1, loc=[0, 0])
array([-0.41774981, -0.41774981])

I don’t think I’m using these functions incorrectly, as the docstrings for mean(), var(), and moment() all say that loc and scale can be array-like.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
mdhabercommented, Feb 18, 2021

One problem is line 7917:

        pA, pB = self._pdf(np.array([a, b]), a, b)

fix for mean is:

        pA, pB = self.pdf(np.array([a, b]), a, b)

But that didn’t fix var so I’ll just use np.vectorize. I’ll open a PR.

0reactions
czgdp1807commented, Jul 2, 2021

Ah! I missed it. No problems. Thanks for highlighting your work on this issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Truncated normal distribution - Wikipedia
In probability and statistics, the truncated normal distribution is the probability distribution derived from that of a normally distributed random variable ...
Read more >
Chapter 4 Truncated Distributions
truncated random variable YT (a, b) have the cdf FT (a,b). The following lemma illustrates the relationship between the means and variances of...
Read more >
Using the truncated normal distribution
The term truncated normal distribution may sound highly technical but it is actually fairly simple and has many practical applications.
Read more >
How to sample random numbers of particular variance from ...
1 Answer 1 · works great, but for some values the corrector gives negative std. e.g. for the following inputs: 0, 0.08838834764831845, - ......
Read more >
Truncated Distribution / Truncated Normal Distribution
The truncated normal distribution is defined in the same way as the normal distribution: by the mean(μ) and standard deviation(σ).
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