2

For my AP statistic class, we had to design an experiment (usually a survey of what brand of soda people prefer), so me being an overachiever decided to study how boys and girls distribute themselves among other genders. For instance, girls will sit near each other normally.

My way of doing this would be to find the average percentage of people guys sit around that are the same gender for both genders independently. I'd expect to see these numbers be $50$% if they sat randomly, but I'm willing to bet they're around $75$%-$85$%.

How can I produce random simulations of this test? It's a requirement for the assignment.

Jacob Claassen
  • 868
  • 1
  • 8
  • 19
  • Are you looking for a computer simulation or a mathematical model? Are the students arranged in rows and columns such as in a classroom? Are there an even number of boys and girls? – Samadin May 13 '17 at 07:10
  • Computer simulation. Rows & columns like a regular classroom. Every student is randomly a boy or girl independently. – Jacob Claassen May 13 '17 at 07:14
  • Depending on the class size, rather than random simulations (Monte Carlo) you could exhaustively probe the entire sample space. For example, a class of 12 boys and 12 girls, sitting in a set of 24 open seats, can be arranged in 2.7 million different ways, which is not unreasonable to do on a computer. – David K May 13 '17 at 11:41

2 Answers2

3

For a successful simulation you need three things. First, a well-defined measure of what is important, here 'sitting close'. Second, a way to way to model what you mean by random behavior, here 'unbiased seating, not influenced by gender'. Third, an automated way of computing the measure, so that you can run many iterations on a computer without having to do the evaluation by hand.

To give a simple illustration, suppose we consider positions of boys and girls along a row of a dozen seats. One measure of closeness or 'clumpiness' of seating might be runs.

Runs as a measure: If there are 6 boys and 6 girls, then the smallest number of runs is two: BBBBBBGGGGGG or GGGGGGBBBBBB, for maximum clumpiness. And the largest number of runs is 12 BGBGBGBGBGBG or GBGBGBGBGBGB for minimum clumpiness.

Random scrambling: One possibility would be to have equal numbers of boys and girls, as above. On a computer it is easy to scramble 6 boys and 6 girls at random. In order to use numbers in stead of letters, use 1's for girls and 2's for boys. In R statistical software the sample function will easily make random arrangements or permutations: We start with a vector kids containing six 1's and six 2's. Then we repeatedly permute them using sample.

kids = rep(1:2, each=6);  kids
## 1 1 1 1 1 1 2 2 2 2 2 2
perm = sample(kids, 12); perm
## 1 1 2 2 2 1 2 1 2 1 2 1          # 1st random permutation     
perm = sample(kids, 12); perm
## 1 2 2 2 2 1 2 1 1 1 1 2          # 2nd random permutation
perm = sample(kids, 12); perm
## 1 1 2 1 2 2 1 1 2 2 2 1          # 3rd
perm = sample(kids, 12); perm
## 1 2 2 1 1 2 2 1 1 2 2 1          # etc.
perm = sample(kids, 12); perm
## 1 2 1 2 2 2 1 2 2 1 1 1

Automatic counting of runs: In R the function rle (for Run Length Encoding) will find the runs in a permutation and count the length of each run. Here is an example for one permutation.

perm = sample(kids, 12); perm
## 1 1 1 2 2 1 1 2 1 2 2 2
rle(perm)
## Run Length Encoding
## lengths: int [1:6] 3 2 2 1 1 3
## values : int [1:6] 1 2 1 2 1 2

The translation is that we have six runs in this permutation: The first run consists of three 1's, the second run consists of two 2's, the third of two 1's, and so on.

We can capture the lengths and values as follows:

info = rle(perm)
info$lengths
## 3 2 2 1 1 3
length(info$lengths)
## 6                     # number of runs
max(info$lengths)
## 3                     # length of longest run
mean(info$lengths)
## 2                     # average run length

A simulation: We can put this altogether to find the 'typical behavior' of runs of six boys and six girls sitting at random in a row of 12 seats. At the beginning we make vectors nr and mx to receive the results of 10,000 scramblings of 12 kids. The actual numbers are put into these vectors one at a time as the program goes through the 'for' loop.

m = 10000;  nr = mx = numeric(m);  kids = rep(1:2, each=6)
for (i in 1:m)  {
   perm = sample(kids, 12)
   info = rle(perm)
   nr[i] = length(info$lengths)
   mx[i] = max(info$lengths)    }
mean(nr);  mean(mx)
## 7.0126
## 3.1963

So when six boys and six girls sit at random in a row of 12 seats without regard to gender, there will be about 7 runs, and the longest run will be about 3.

We can make histograms of the simulated numbers and maximum lengths. It is probably easiest to interpret run lengths. It seems that if there are fewer than 3 or 4 runs when real groups of 6 girls and 6 boys sit in a row, then there may be clumping together of genders. And if there are more than 10 or 11 runs, then boys and girls may be tending to 'pair up'.

enter image description here

Histogram code, in case you want it:

par(mfrow=c(1,2))
 hist(nr, prob=T, br=(min(nr):(max(nr)+1))-.5, col="skyblue2", main="Dist'n of Numbers of Runs")
 hist(mx, prob=T, br=(min(mx):(max(mx)+1))-.5, col="skyblue2", main="Dist'n of Longest Runs")
par(mfrow=c(1,1))

Notes: (1) There is a lot of theoretical information about the distributions of numbers of runs and run lengths in intermediate-level probability and mathematical statistics books. You might get some worthwhile information from Wikipedia and university sites.

(2) A larger number m of iterations will give more precise results. The program runs fast enough on today's laptops that more extensive simulations are feasible.

(3) R is free software available at www.r-project.org.

BruceET
  • 51,500
2

There are many approaches you can take, I'll describe a straightforward one here.

First, it's important to note that your simulation results will be affected by (1) the geometry of the classroom and (2) the number of boys vs. girls. So if you are eventually going to compare it to real data, you want to match the simulation accordingly.

Let's define some parameters:

  • $R$ = number of rows
  • $C$ = number of columns
  • $B$ = number of boys in class
  • $G$ = number of girls in class
  • $N$ = total number of students

Hence $RC >= N$ (some seats may be empty), and $B+G = N$.

Next, you want to decide what you mean "sit around". Specifically, do you want to look at just students who sit next to each other, in front or behind, who sit diagonally, etc?

In the diagram below I only consider up'down'left'right as neighbors for $R=4$, $C=6$, $B=15$, $G=9$, $N=24$.

For the simulation, you want to first assign a boy or a girl randomly to each desk. After that's complete, you then want to loop over each student, counting the number of neighbors having the same gender. Taking an average over all students (being sure to take into account that students can have either 2,3, or 4 neighbors) will give you the percentage you want. You then want to run the simulation many times to get an accurate distribution.

Hope this helps, I'm happy to continue if some of the steps are still confusing.

enter image description here

Samadin
  • 466
  • Of course, this is closer to what was asked than my answer (+1), but I was trying for something easier to implement. – BruceET May 13 '17 at 09:11
  • @BruceET Yea, usually I'd start with your approach and slowly build up in complexity. Also, I was thinking of looking at clustering rather than percentages, but I thought that may be harder to interpret on a high school level course. (+1 returned!) – Samadin May 13 '17 at 09:30
  • I'm currently teaching a simulation course with 15 - 20 students. Almost all the men sit to my left, women to my right. May be OK to split room into quadrants and use chi-sq GOF test to see if 1/4 of men sit in each. – BruceET May 13 '17 at 09:43
  • Interesting, this now makes me think of how to differentiate between a gender bias vs. location bias. In my experience, if the air condition is on the left, the women tend to sit to the right! – Samadin May 13 '17 at 10:18