A Random Variable is a function on a probability space. A probability space is a set of possible outcomes (like the numbers 1,2,3,4,5,6 for the outcome of a die) and a way to measure probabilities of events (like the probability of getting an even number is 1/2).

Fundamental to these ideas is the idea of a set. In the above case we mentioned two sets {1,2,3,4,5,6} and {2,4,6}. We would like to be able to do basic operations on these objects – list members, test for membership, compute unions, intersections, complements, etc…. We would also like to describe the sample space of two dice, the product of {1,2,3,4,5,6} x {1,2,3,4,5,6}, easily.

SymPy already has an abstract notion of a Set as well as an implementation of real intervals (like (0,1] ) and an implementation of Unions of Intervals (like (0,1] U [2,3) ). This week I’ve added an implementation of Finite Sets (like the dice example above) and an implementation of Cartesian Product Sets.

FiniteSets are implemented as a python frozen set. All the standard operations you’d like work well on them. They can store arbitrary objects like 1,2,3, or ‘Eggs’ or int. If they contain only real numbers then they will play nicely with real Intervals.

A ProductSet is a Cartesian product of combination of a list of sets. Elements of these sets are tuples with elements in the constituent sets. Product Sets allow the easy creation of relatively complex sets out of simple pieces. Some examples are below.

There has also been some ground-laying work on Random Variables. There is a standard interface and a rudimentary (and broken) implementation for finite sample spaces. Goal for next week. Games with dice.

As always, code is available on github. At this point I’d suggest looking at the branch cartesian_product_set

https://github.com/mrocklin/sympy

>>> Interval(0,2) - FiniteSet(1)
[0,1) U (1,2]
>>> square = Interval(0,1) * Interval(0,1)
>>> (.5, .5) in square
True
>>> cube = square * Interval(0,1)
>>> two_dice = FiniteSet(1,2,3,4,5,6) * FiniteSet(1,2,3,4,5,6)
>>> both_even = FiniteSet(2,4,6) * FiniteSet(2,4,6)
>>> two_dice.subset(both_even)
True
>>> two_coins = FiniteSet('H', 'T') ** 2
>>> for element in two_coins:
... print element
(H, H)
(H, T)
(T, H)
(T, T)

### Like this:

Like Loading...

*Related*