[R] Beginner - simple simulation

From: Jacob van Wyk <jlvw_at_na.rau.ac.za>
Date: Tue 01 Mar 2005 - 19:25:37 EST

Hallo to everybody. I am new to the list and would appreciate some help in a basic "first demo" of how to use R for simulating a simple game; I would like my students to use R and this may stimulate their interest.

The problem is simply:

Two players (A and B) play the following game. Each player rolls a die
(fair, 6-sided) and they write down the result: say A rolls nA and B
rolls nB. If nA is even, A pays B $nB: if nA is odd, B pays A $nA (we think of A paying B a negative amount). The amount that A pays B is a random variable X. Find the expectation of X.

Theoretically it is 1/4 - B is ahead on average with 25c.

Would anybody be prepared to help a little. I want to avoid loops and vectorize the computations, simulate the value of X for various sample sizes and preparea basic plot to show that the average value of X "converges" to 1/4.

I would start with, say,
for one simulated roll of the two dice. I want to repeat this n times, where n is, say, 10:2000 in steps of 10. Put the results in a matrix and work columnwise - choosing when the first roll is even, selecting the corresponding value of the second roll, and computing the payoff as described, etc. But I need help to put this together.

In Matlab I would, for example, do the following to display the average payouts of A and B:

for s=samplesizes



Then do the plotting, etc. (One could also take differences, and so on.)

I would really appreciate if anybody would be kind enough to help. I thought it might be a nice example to introduce students (in general, perhaps - because it is a kind of interesting game) to simulation in R.

Thank you !
(PS Any credit would be respected, i.e. my students will know who
helped me with this introduction.)

Jacob L van Wyk
Department of Mathematics and Statistics University of Johannesburg APK
P O Box 524
Auckland Park 2006
South Africa
Tel: +27-11-489-3080
Fax: +27-11-489-2832

R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Tue Mar 01 19:33:16 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:30:38 EST