# How to use the DiscreteRandomVariable class?

The reference manual talks about DiscreteRandomVariable class. But, I do not understand how it should be initialized? There are variables "X" and "f" but the docs do not explain what they are, nor there is any example...

Additionally: is it possible to calculate functions on DiscreteRandomVariables? E.g, if X and Y are random variables, can I write "Z = X * Y" and have a new random variable Z?

edit retag close merge delete

## Comments

This class seems to be in pretty bad shape. I guess that we would better get rid of it. Moreover, for defining two random variables you need a common probability space. And the law of X*Y highly depends on the correlations between X and Y. Were you thinking about the independent case?

( 2015-12-03 14:38:39 -0500 )edit

@vdelecroix Yes, I was thinking about the independent case, For example, suppose X is a discrete random variable with the following distribution: {10: 0.3, 20: 0.7} and Y is discrete random variable with the following distribution: {30: 0.4, 40: 0.6}. Then, Z=X*Y is a discrete random variable with the following distribution: {300: 0.12, 600: 0.28, 400: 0.18, 800: 0.42}. It seems quite straightforward to implement.

( 2015-12-15 06:18:09 -0500 )edit

Sure you can do

sage: X = {10: 0.3, 20: 0.7}
sage: Y = {30: 0.4, 40: 0.6}
sage: Z = defaultdict(lambda:0.0)
sage: for x,px in X.iteritems():
....:    for y,py in Y.iteritems():
....:         Z[x*y] += px*py
sage: Z
defaultdict(<function <lambda> at 0x7f3deebbb140>,
{400: 0.180000000000000,
800: 0.420000000000000,
300: 0.120000000000000,
600: 0.280000000000000})

( 2015-12-16 13:18:00 -0500 )edit