How to use the DiscreteRandomVariable class?

asked 2015-11-29 16:52:11 +0200

Erel Segal-Halevi gravatar image

updated 2015-11-29 17:06:22 +0200

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 flag offensive 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?

vdelecroix gravatar imagevdelecroix ( 2015-12-03 21:38:39 +0200 )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.

Erel Segal-Halevi gravatar imageErel Segal-Halevi ( 2015-12-15 13:18:09 +0200 )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})
vdelecroix gravatar imagevdelecroix ( 2015-12-16 20:18:00 +0200 )edit