Author Allen Downey
Recipients Allen Downey, DavidMertz, josh.r, mark.dickinson, pitrou, rhettinger, serhiy.storchaka, steven.daprano, veky, wolma
Date 2018-05-17.19:06:00
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1526583960.68.0.682650639539.issue25478@psf.upfronthosting.co.za>
In-reply-to
Content
I'd like to second Raymond's suggestion.  With just a few additional methods, you could support a useful set of operations.  One possible API:

def scaled(self, factor)
"""Returns a new Counter with all values multiplied by factor."""

def normalized(self, total=1)
"""Returns a new Counter with values normalized so their sum is total."""

def total(self)
"""Returns the sum of the values in the Counter."""

These operations would make it easier to use a Counter as a PMF without subclassing.

I understand two arguments against this proposal

1) If you modify the Counter after normalizing, the result is probably nonsense.

That's true, but it is already the case that some Counter methods don't make sense for some use cases, depending on how you are using the Counter (as a bag, multiset, etc)

So the new features would come with caveats, but I don't think that's fatal.

2) PMF operations are not general enough for core Python; they should be in a stats module.

I think PMFs are used (or would be used) for lots of quick computations that don't require full-fledged stats.

Also, stats libraries tend to focus on analytic distributions; they don't really provide this kind of light-weight empirical PMF.

I think the proposed features have a high ratio of usefulness to implementation effort, without expanding the API unacceptably.


Two thoughts for alternatives/extensions:

1) It might be good to make scaled() available as __mul__, as Peter Norvig suggests.

2) If the argument of scaled() is a mapping type, it might be good to support elementwise scaling.  That would provide an elegant implementation of Raymond's chi-squared example and my inspection paradox example (http://greenteapress.com/thinkstats2/html/thinkstats2004.html#sec33)

Thank you!
Allen
History
Date User Action Args
2018-05-17 19:06:00Allen Downeysetrecipients: + Allen Downey, rhettinger, mark.dickinson, pitrou, steven.daprano, serhiy.storchaka, wolma, josh.r, veky, DavidMertz
2018-05-17 19:06:00Allen Downeysetmessageid: <1526583960.68.0.682650639539.issue25478@psf.upfronthosting.co.za>
2018-05-17 19:06:00Allen Downeylinkissue25478 messages
2018-05-17 19:06:00Allen Downeycreate