# [SciPy-dev] arithmetic on distributions

Perry, Alexander (GE Infrastructure) Alex.Perry at ge.com
Wed Aug 10 18:35:13 CDT 2005

```> Perry, Alexander (GE Infrastructure) wrote:
> > First suggestion.  When calling an object to freeze it,
> > if the loc is already a distribution, we should merge
> > its variance into the variance of the newly frozen
> > distribution [...] Currently, an error is triggered.

From Robert Kern:
> I think most people would simply conclude that
> passing in a frozen distribution doesn't work
> (especially since it is certainly not documented to do so).

I _know_ it doesn't work and is nowhere documented as working.
That's why it is a suggestion for improvement (not a bug report).

> > Because of the difficulty of merging the standard deviation
> > of the old value into the new desired standard deviation,

You didn't comment on this assertion, but then disagreed below.
The code inside the wrapper is not particularly clean or obvious,
since norm() expects a stdev, yet stats() returns the variance.

#! /usr/bin/python
from scipy.stats.distributions import norm
from math import sqrt
# Proposal, written as a wrapper
def normal(loc,scale,**args):
try:
loc,locvar = loc.stats()
scale = sqrt ( scale*scale+locvar )
except:
pass
return norm(loc,scale,**args)
# Two different early constants
n1 = 10
n2 = norm(n1,3)
# Lots of code later
print normal(n1,4) .stats()
print normal(n2,4) .stats()

> > I suspect that most people who encounter the error will take the
> > lazy way out and simply discard the variance attached to the value:
> > try: value=value.stats()[0]
> > except: pass # the above is wrong!

> I can't imagine why.

It's wrong because I quietly discarded the distribution.
It was presumably present on that variable for a reason,
and I only did it to make the subsequent setup call work.

> > Third suggestion.  The __cmp__ method should exist [...]
> If the rich comparisons (__lt__ and __eq__, at least) are defined,
>  then __cmp__ will never be called.

Yes, that's how my python version works, but I saw it documented the
other way in some mailing list discussion archive.  I'll look into it.

```