I know sympy has random variables, and so does sage, but they cannot
be manipulated (+-*/ exp, integrate...) and so their uses are limited;
 and I couldn't find anything like that in the googlable literature.

Let's put it this way: A student may want to check his answers in his
statistics exam preparation.  The textbook is an old-fashioned one and
it doesn't come with colourful answers.  E could write a program to
cruch out the numbers but very soon that could become laborious.  I
want to have a systematic way of doing that.

Yes, I don't need to do it abstractly (like a convolution), nor do I
want to.  I want to do it concretely, "experimental mathematics"
fashion.  Or else there isn't much value in checking your
"theoretical" answer against another "theoretical" answer from the
computer.

My codes is appended below.  Note that I would like to replace "class
Radd(Symbol)"  by "class Radd(Add)" at some point - and I don't know
enough to do that yet.

Thank you.
Y.



#randomvar.py
#to construct a class of random variables which can be
#algebraically manipulated

from random import *
from sympy import *

#class randomvar(Symbol):
#    pass

class Normal(Symbol):

    def __new__(cls, name, mean, variance,**assumptions):
        return super(Normal, cls).__new__(cls,name)

    def __init__(self,name,mean,variance,**assumptions):
        self.name=name
        self.mean=mean
        self.variance=variance
        self.sd=sqrt(variance)

    def __call__(self):
        return normalvariate(self.mean,self.sd)

    def __add__(self,y):
        return Normal(self.name+ '+'+y.name, 0.5*(self.mean + y.mean),
self.variance+y.variance)

class Rsymbol(Symbol):
    """ random variable object,
    input: random number generator, e.g. numpy.random.normal, random.expovariate
    arguments = unkeyworded arguments for the random number generator,
e.g. 0,1 for mean, variance"""

    def __new__(cls, name,random_number_generator,*arguments, **assumptions):
        #print 'mro:',  cls.__mro__
        return super(Rsymbol, cls).__new__(cls,name)

    def __init__(self,name,random_number_generator,*arguments,**assumptions):
        self.generator=random_number_generator
        self.arguments=arguments
    def __call__(self):
        return self.generator(*self.arguments)

    def __add__(self,y):
        return Radd(self,y)


class Radd(Symbol):
    """"adding random numbers - temporary, until i work out sympy.core.add
    only x+y allowed at the moment"""

    def __new__(cls, x, y, *arguments, **assumptions):
        name=x.name+"_plus_"+y.name
        return super(Radd, cls).__new__(cls,name)

    def __init__(self,x, y,*arguments,**assumptions):
        self.first=x
        self.second=y
    def __call__(self):
        return 
self.first.generator(*self.first.arguments)+self.second.generator(*self.second.arguments)



if __name__=="__main__":
    x=Normal('x',0,1.0)
    y=Normal('y',0,4.0)
    print x.name, x.mean, x.sd
    print y.name,y.mean,y.sd
    print x.mean**2+y.mean**2
    z=x+y
    print z.name, z.mean, z.variance
    for i in range(10):
        print z()

    a=Rsymbol('a',normalvariate,50,1)
    b=Rsymbol('b',normalvariate,1000,4)
    c=a+b
    print a.name,a.generator,a.arguments
    for p in (a,b,c):
        print '\n------\n',p.name
        for i in range(10):
            print p()

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To post to this group, send email to sympy@googlegroups.com
To unsubscribe from this group, send email to sympy+unsubscr...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/sympy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to