Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Simulation scripts for a Stats question.

  1. Aug 3, 2011 #1
    Just wondering how I can manipulate this script to give an estimate for (b). For reading purposes, here's the whole question.

    3. Suppose there are 50 memory chips in a box, of which 40 are "good" and 10 are "bad."
    We withdraw four of the 50 chips at random to upgrade a computer.

    (a) What is the probability that all four chips are good?
    (b) What is the probability that at least one chip is good?

    Next we use R software and the sample function to approximate these probabilities
    by simulation. This function takes a random sample from a nite population. For our
    simulation, we represent the population as the vector 1:50, which has the numbers 1
    through 50 as its elements. (Of these, we regard the ten chips numbered 41 through 50
    to be the bad ones.)

    Accordingly, we use the statement sample(1:50, 4) to sample four dierent chips at
    random from the box of 50. Because this statement is a random one, it is likely to give a
    dierent result each time it is used. For example, repeat three times:
    sample(1:50, 4)

    To solve problem (a), we must generate many samples of four chips, and we need an
    automated way to count the good chips in each sample. We can do this by determining
    how many chips have numbers 40 or smaller.

    We simulate m = 1000 samples of four chips from the box. In the following R program,
    we loop through these m samples, counting the number of good items found in each. The
    proportion of the m samples consisting entirely of good chips approximates the probability
    that all four chips are good. We can calculate the proportion using the mean function.

    m <- 1000 # number of samples to simulate

    n = 1:m # vector: n = 1, 2, ..., m; simulation number
    good <- numeric(m) # initialize for use in loop
    emp_probab <- numeric(m) # empirical probability calculated using m simulations

    for (i in 1:m)
    pick <- sample(1:50, 4) # vector of 4 items from ith box
    good <- sum(pick <= 40) # number Good in ith box
    emp_probab<-mean(good[1:i] == 4)}

    mean(good == 4) # approximates P{All Good}

    plot (emp_probab,ylim=c(0,0.5))

    I already know (b) is approximately 1, but I'm just wondering what I have to do to this script. I've changed certain lines to suit it (I think, it's a stats class, never done comp sci) but my lecturer said it was wrong.

    Changed lines:
    emp_probab<-mean(good[1:i] == 4)} TO emp_probab<-mean(good[1:i] >= 1)}

    mean(good == 4) TO mean(good >= 1)

    plot (emp_probab,ylim=c(0,0.5)) TO plot (emp_probab,ylim=c(0.9,1))

    Obviously the last one is just to take graph of the plot being graphed. Any suggestions?
  2. jcsd
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Can you offer guidance or do you also need help?