Friday, August 12, 2016

Monty R and Monty n

As part of my ongoing attempt to learn the R language, I decided to try to generate a data set to analyze in R using R itself. I wrote a  little R script which generates simulated data for a thousand trials of the "Monty Hall" problem. The script is here, along with other R examples.

If you source the script, a data frame MontyHall will be populated. The data is re-generated each time you source the script, so if you want to capture a single run, you should export the data and work from that (a saved example run is here, and contains the data  I used in this post).
In the classical Monty Hall problem, a game show host, named Monty Hall, presents a contestant with three (3) doors. Behind one of the doors is a valuable prize, traditionally a car, while behind the others are dud prizes, traditionally goats. A game proceeds with the contestant selecting a door and keeping the prize that lies behind it (hoping for the actual prize, not a dud). However, instead of opening the selected door, Monty opens one of the other two doors, revealing a goat, and asks if the contestant would like to switch and select the remaining door instead. The problem asks which choice has the higher probability of revealing the actual prize, or whether both options have the same probability of achieving the hoped-for outcome.
In this simulation, we have the contestant choosing to switch half of the time, hopefully generating enough cases so that the experimental probabilities (from the simulation) match up with the theoretical probabilities that we can come up with by analyzing the problem. (You can increase the number of trials by modifying the script).

The simulation gives us a data set that looks like this.

Our simulation has the probability of switching set to 1/2, as if we were basing our decision on the flip of a fair coin.

We can see that our simulation is pretty close:

We would expect that the probability of the contestant selecting the winning door right away would be 1/3, and this is what we see in the simulation.

All that this tells us is that R is generating random numbers in a reasonable enough way for our purposes. What makes the Monty Hall problem interesting are the probabilities of winning when the contestant switches doors, compared to when they don't switch.

If the contestant does not switch, their chance of winning is the same as their chance of selecting the prize right away, which is just 1/3.

This is not the result that some people expect (some argue that the chance of  winning when staying with the original selection should be 1/2), but the simulation agrees:

When the contestant makes their initial selection , they have either won or lost. Switching amounts to trading a win for a loss: if the contestant has picked the winner, they have now switched away from it; if they have picked a looser (one of the goats), when Monty opens the other door (revealing the other goat), switching will cause them to win. So, switching amounts to trading a win for a loss or a loss for a win. Because the probability of losing on the initial door selection is 2/3, it is a good idea to switch: the first choice was likely a goat.
And the simulation agrees:

What about if we pursue the strategy of the imaginary contestant in our simulation, and flip a coin to decide whether we switch doors or stay with the original choice? The law of total probability tells us that we have a probability of winning equal to 1/2.

And in the simulation we come pretty close:

Monty n

While writing up the R simulation, I wondered what would happen if the Monty Hall problem was generalized to n doors so that Monty could keep on offering you more chances to switch. There are likely many ways of doing this, so I picked what I think is a simple generalization:

Suppose that the contestant has n doors to choose from where n > 2. There is still only one (1) car (and n-1 goats). After a door is selected, Monty will always open a remaining door and provide the contestant with a chance to switch doors, as long  as there remain unopened doors that have not been previously selected by the contestant. Monty will not open doors that were previously chosen, nor will he give the contestant the opportunity to switch back to a previously chosen door.

I've put a short write up of this on ShareLatex here. At least in the way that I've framed the problem, there is a nice formula for the probability of winning on n doors if you switch k times:

Or if you prefer a direct formula instead of recursive:

If you work out some values using your preferred version of the formula, you should  get results that agree with these:

From this, or from the formulas themselves, you can see that the probability of winning keeps increasing as long as you keep switching doors as long as Monty gives you the option to.

I did not simulate the n > 3 case using R, but I did write a general simulation using Java (a listing for this is included in the write up here), and found that the simulation results agreed the the theoretical probabilities pretty closely (I have not carried out a detailed analysis of the results).

Thursday, August 4, 2016

Chladni on Google Books

While doing some light research a short while back (for this post), I hit a minor impasse: the Google Books scans of some figures in an early edition of  Ernst Chladni's book on sound were sadly obscured by folding.

Although I was able to find other digital images of similar scans, I sent in a support request to Google Books, and was surprised when within less than two weeks they had re-scanned the images, properly folding out the pages.

These and other nicely restored images can be found towards the end of the book.

Sunday, July 17, 2016

a first slice

Here is my first simple attempt at a geometric design using sliceformstudio - I'm looking forward to trying out many more.

I have played around a bit with sliceforms before, using instructions from John Sharp, who has written quite a bit about using them for creating models of conic sections and surfaces, and has a nice short blog post here about their history. I learned about sliceformstudio a short while ago from mathmunch.

This design was based on a simple pattern of pentagons around a decagon, to which the wondrous sliceformstudio applied some interweaving that I tweaked only slightly using the very simple interface.

After printing off the generated strip file onto cardstock, 10 long strips and 10 short strips are cut, folded, and slid together to create the final model. I've copied the generated strip file to a pdf here.

Friday, June 24, 2016

more Chladni figures in R

Following on from an earlier post, the Chladni images here are made using a slightly modified version of the same R script (source here), which uses cosines instead of sines. If you imagine the square of the vibrating surface to be fixed at the center (as depicted below), using cosines seems the natural choice. When modeling standing waves, cosines are used to model open-ended pipes, while sines are used to model fixed-end strings.

Playing around with cosine-based formulas led to some images that seemed very close to Chadni's own diagrams, which can be found in an appendix to his book on Google books.

For a few of these I've uploaded the scripts that produced them.

Chladni image 40 b
Source here.

 Chladni image 41
Source here.

Chladni image 53
Source here.

Chladni image 58
Source here.

Chladni image 63
Source here.

And of course, many more are possible. Try a few yourself :)

Rascal Triangle

A few early iterations of something produced by a short script in R. What is it? You may see it more clearly in some of its later stages of development.

Yes, it is Pascal's Triangle modulo 2 - I knew you would recognize it :). The R source for generating these images is here. Like other recent posts, this is another example of using R in some simple programming exercises, pretty much completely unrelated to its intended purpose as a language for statistical computing. A wile back, there was a post about using TinkerPlots, a data management software tool for young folk, to do something similar (more detailed instructions on drawing a general Pascal Triangle in TinkerPlots can be found in this article),