truncnorm gives incorrect means and variances
See original GitHub issuetruncnorm.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:
- Created 3 years ago
- Comments:7 (7 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
One problem is line 7917:
fix for mean is:
But that didn’t fix
var
so I’ll just usenp.vectorize
. I’ll open a PR.Ah! I missed it. No problems. Thanks for highlighting your work on this issue.