This post originated from an RSS feed registered with Ruby Buzz
by Eigen Class.
Original Post: Warm fuzzy things for random simulations
Feed Title: Eigenclass
Feed URL: http://feeds.feedburner.com/eigenclass
Feed Description: Ruby stuff --- trying to stay away from triviality.
Let's talk about random experiments. The simplest one is tossing a coin, with
outcomes "heads" and "tails". It's so elementary that we fully understand it
intuitively once we're told that the coin is not loaded. What's "everything",
though? There are three basic problems of interest:
How to simulate the experiment with a computer and obtain results undistinguishable from the real thing?
If we're given an outcome, can we compute its probability?
Can we enumerate all possible outcomes and their probabilities?
In our first experiment, the answers are "rand 2" (if take e.g. "heads" = 0,
"tails" = 1), "1/2" and "heads, tails, both with probability 1/2". Easy
indeed. Now on to something harder: rolling a fair die. The answers are now "1 +
rand(6)", "1/6", and "1..6, all with probability 1/6". Still trivial. Let's
aim for the stars! What about rolling three dice? The outcomes are clearly
3..18, but what are the probabilities?
Intuition doesn't help much here --- we "can see" the possible outcomes, but
most people cannot give the precise probabilities intuitively (I certainly can't; I
can at most draw an approximate graph, but that's because I am used to
convolutions). The first problem was how to simulate the experiment; this is
by far the easiest one. This is what our dice rolling simulation looks like in
Ruby:
We can now simulate the experiment easily, but this doesn't help us with the
harder problems: giving the probability of an outcome and doing so for all
possible outcomes. Clearly, the above snippet has got all the information we
need about this random simulation; it's indeed a precise definition. Wouldn't
it be nice to have the computer solve the three problems of interest
(simulation, distribution and expectation) for us if we give it the
specification of the simulation? It turns out this can be done with a little
code massaging:
What's really nice is that Simulation, Expectation and Distribution can be
reused for other random experiments. Say you get an extra roll if any of the
first 3 dice gave a 6: