Simulation scripts for a Stats question.

In summary, to estimate the probability for (b), you can change certain lines in the script to calculate the proportion of samples where at least one chip is good and plot the results accordingly.
  • #1
Slats18
47
0
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?
 
Physics news on Phys.org
  • #2

Thank you for your question. To manipulate the script to estimate the probability for (b), you can follow these steps:

1. Change the line "emp_probab<-mean(good[1:i] == 4)}" to "emp_probab<-mean(good[1:i] >= 1)}". This will calculate the proportion of samples where at least one chip is good.

2. Change the line "mean(good == 4)" to "mean(good >= 1)". This will calculate the overall proportion of samples where at least one chip is good.

3. Change the line "plot (emp_probab,ylim=c(0,0.5))" to "plot (emp_probab,ylim=c(0.9,1))". This will change the y-axis limits to better display the estimated probability for (b).

I hope this helps. If you have any further questions, please don't hesitate to ask.
 

FAQ: Simulation scripts for a Stats question.

1. What is a simulation script?

A simulation script is a computer program or code that is used to simulate or model a real-world situation or process. In the context of statistics, it is used to generate data that can be used to test hypotheses or make predictions.

2. Why are simulation scripts important in statistics?

Simulation scripts are important in statistics because they allow researchers to test their hypotheses or predictions in a controlled environment. They also provide a way to estimate the probability of certain outcomes and understand the behavior of complex statistical models.

3. How do I create a simulation script for a stats question?

To create a simulation script for a stats question, you will first need to define the problem you want to solve or the hypothesis you want to test. Then, you will need to choose the appropriate statistical model and determine the parameters and variables that will be used in the simulation. Finally, you will need to write the code using a programming language such as R, Python, or MATLAB.

4. What are some common pitfalls when using simulation scripts in statistics?

Some common pitfalls when using simulation scripts in statistics include using incorrect or unrealistic assumptions, not generating enough data to accurately represent the real-world scenario, and not properly validating the results. It is important to carefully design and test the simulation script to avoid these pitfalls.

5. Can simulation scripts be used for any type of statistical analysis?

Simulation scripts can be used for many types of statistical analysis, but they are most commonly used for complex or computationally intensive problems that cannot be solved analytically. They are also useful for testing the assumptions and behavior of statistical models, as well as for teaching and learning statistical concepts.

Similar threads

Replies
1
Views
1K
Replies
18
Views
2K
Replies
10
Views
260
Replies
1
Views
2K
Replies
27
Views
2K
Replies
1
Views
1K
Back
Top