You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Inconsistent behaviour of statsmodels.stats.weightstats.DescrStatsW #4797
There is no specific ndim/shape handling, squeeze or similar. The return shape is a consequence of the numpy behavior, specifically np.dot([1.], 1.) returns an array instead of scalar, at least in this version of numpy.
N=1 is a special case, because variance cannot be computed.
This needs unit test for N=1. I don't know what the behavior for statistic that involve var or std is. (zero division, nan, inf ?). Currently the behavior is just inherited as consequence of the standard computation.
also behavior for different dimension of weights, if squeeze is removed.
@rhalbersma what result statistics or methods are you using if N=1?
The squeeze is needed if we want to allow scalar 1. to allow for arbitrary shape weights.
np.dot allows scalar multiplication, but checks shape if ndim > 0
@rhalbersma what result statistics or methods are you using if N=1?
I just wanted sum and mean for N=1 and use them inside a print() statement. When applying a formatter, I get errors because the value is wrapped in an array. I'd expect just to get a scalar like for N > 1.
Note that I'm not asking that all stats work for N=1, just that a well-behaved stat is returned as a scalar rather as an array of length 1.
Edit: Just checked my code. I use DescrStatsW for a blackjack results tracker. I store the raw win/loss counts in a defaultdict and use data=list(dict.keys()) and weights=list(dict.values()). It's not just that I have to skip the first hand to get nobs > 1 in order to print out a running mean, I have to wait until I have both a win and a loss in order to get more than one different outcome.
I don't know what the behavior for statistic that involve var or std is. (zero division, nan, inf ?). Currently the behavior is just inherited as consequence of the standard computation.
Sure, if there is a 1 / (nobs - 1) somewhere, users just receive an exception and can deal with that as they please.
The squeeze is needed if we want to allow scalar 1. to allow for arbitrary shape weights.
But the documentation clearly says that weights is either None or a 1-D ndarray, so scalar or other arbitrary shape weights are not currently supported. Removing the squeeze in __init__ gives the correct scalar outcome from the np.dot for sum and mean even if nobs == 1.
Fixed by PR #7230, which only squeezes weights when they are genuinely multi-dimensional (statsmodels/stats/weightstats.py), fixing single-observation .mean returning an array instead of a scalar. A regression test (test_weightstats_len_1) was added for exactly this case. Closing as fixed.
Activity
josef-pkt commentedon Jul 14, 2018
There is no specific ndim/shape handling, squeeze or similar. The return shape is a consequence of the numpy behavior, specifically
np.dot([1.], 1.)returns an array instead of scalar, at least in this version of numpy.josef-pkt commentedon Jul 14, 2018
however, the
__init__removes extra dimsI don't remember
why squeeze. Most likely the squeeze should be removed.rhalbersma commentedon Aug 20, 2020
Is there a fix for this? It's kind of annoying to special case
N=1in user-code.josef-pkt commentedon Aug 20, 2020
N=1 is a special case, because variance cannot be computed.
This needs unit test for N=1. I don't know what the behavior for statistic that involve var or std is. (zero division, nan, inf ?). Currently the behavior is just inherited as consequence of the standard computation.
also behavior for different dimension of weights, if squeeze is removed.
@rhalbersma what result statistics or methods are you using if N=1?
josef-pkt commentedon Aug 20, 2020
The squeeze is needed if we want to allow scalar 1. to allow for arbitrary shape weights.
np.dot allows scalar multiplication, but checks shape if ndim > 0
However, this is not the intended behavior, we need an inner product and not scalar multiplication.
needs more checking and unit tests. (I don't remember the implementation details here)
josef-pkt commentedon Aug 20, 2020
aside naming inconsistency that I just saw:
usevar='pooled'use_var="equal"in onewayrhalbersma commentedon Aug 20, 2020
I just wanted
sumandmeanforN=1and use them inside aprint()statement. When applying a formatter, I get errors because the value is wrapped in an array. I'd expect just to get a scalar like forN > 1.Note that I'm not asking that all stats work for
N=1, just that a well-behaved stat is returned as a scalar rather as an array of length 1.Edit: Just checked my code. I use
DescrStatsWfor a blackjack results tracker. I store the raw win/loss counts in adefaultdictand usedata=list(dict.keys())andweights=list(dict.values()). It's not just that I have to skip the first hand to getnobs > 1in order to print out a running mean, I have to wait until I have both a win and a loss in order to get more than one different outcome.rhalbersma commentedon Aug 20, 2020
Sure, if there is a
1 / (nobs - 1)somewhere, users just receive an exception and can deal with that as they please.rhalbersma commentedon Aug 21, 2020
But the documentation clearly says that weights is either
Noneor a 1-Dndarray, so scalar or other arbitrary shape weights are not currently supported. Removing thesqueezein__init__gives the correct scalar outcome from thenp.dotforsumandmeaneven ifnobs == 1.bashtage commentedon Jul 30, 2026
Fixed by PR #7230, which only squeezes weights when they are genuinely multi-dimensional (statsmodels/stats/weightstats.py), fixing single-observation .mean returning an array instead of a scalar. A regression test (test_weightstats_len_1) was added for exactly this case. Closing as fixed.