Sampling with replacement in Matlab

In summary, the conversation discusses the use of the Binomial Probability Law to simulate the probability of rejecting a good batch with a probability of 0.94. The two cases considered are when 95 or more and 98 or more of the 100 samples are good. The conversation also includes a code for plotting the curves from p=0.9 to p=0.99. Further, there is a discussion about the interpretation of the results, with the question of what the probability of rejecting a good batch with the given criteria is. The original question asks for the use of a computer simulation to determine this probability, assuming sampling with replacement. The definition of a good batch is when the probability of obtaining a good chip is p=0
  • #1
Dustinsfl
2,281
5
I am trying to simulate the probability of rejecting a good batch for a probability of \(0.94\) using the Binomial Probability Law.

My two cases are
\[
P[k\geq 95] = \sum_{k = 95}^{100}\binom{100}{k}p^k(1 - p)^{100 - k}
\]
and
\[
P[k\geq 98] = \sum_{k = 98}^{100}\binom{100}{k}p^k(1 - p)^{100 - k}
\]

I am not sure with what the question wants me to do so I plotted the both curves from \(p = 0.9\) to \(p = 0.99\).

Code:
clear all
close all

p = (0.9:0.001:0.99);                 % probability range
P = zeros(length(p), 1);              % pre-allocating P
PP = zeros(length(p), 1);            % pre-allocating PP
T = zeros(length(p), 1);              % pre-allocating T
TT = zeros(length(p), 1);            % pre-allocating TT

% only allowing 5 defective
for i = 1:length(p)
    for n = 95:100
        T(n, 1) = nchoosek(100, n)*p(i).^n.*(1 - p(i)).^(100 - n);
        P(i, 1) = sum(T); 
    end
end

figure(1)
plot(p, P)
grid on

% only allowing 2 defective
for j = 1:length(p)
    for n = 98:100
        TT(n, 1) = nchoosek(100, n)*p(j).^n.*(1 - p(j)).^(100 - n);
        PP(j, 1) = sum(TT); 
    end
end

figure(2)
plot(p, PP)
grid on
 
Physics news on Phys.org
  • #2
dwsmith said:
I am trying to simulate the probability of rejecting a good batch for a probability of \(0.94\) using the Binomial Probability Law.

My two cases are
\[
P[k\geq 95] = \sum_{k = 95}^{100}\binom{100}{k}p^k(1 - p)^{100 - k}
\]
and
\[
P[k\geq 98] = \sum_{k = 98}^{100}\binom{100}{k}p^k(1 - p)^{100 - k}
\]

I am not sure with what the question wants me to do so I plotted the both curves from \(p = 0.9\) to \(p = 0.99\).

Code:
clear all
close all

p = (0.9:0.001:0.99);                 % probability range
P = zeros(length(p), 1);              % pre-allocating P
PP = zeros(length(p), 1);            % pre-allocating PP
T = zeros(length(p), 1);              % pre-allocating T
TT = zeros(length(p), 1);            % pre-allocating TT

% only allowing 5 defective
for i = 1:length(p)
    for n = 95:100
        T(n, 1) = nchoosek(100, n)*p(i).^n.*(1 - p(i)).^(100 - n);
        P(i, 1) = sum(T); 
    end
end

figure(1)
plot(p, P)
grid on

% only allowing 2 defective
for j = 1:length(p)
    for n = 98:100
        TT(n, 1) = nchoosek(100, n)*p(j).^n.*(1 - p(j)).^(100 - n);
        PP(j, 1) = sum(TT); 
    end
end

figure(2)
plot(p, PP)
grid on

Code:
--> p=0.94;  %failure rate
--> N=10000; %number of simulated batches to test
--> ss=rand(N,100); %N samples of batches of size 100
--> sm=sum(ss'>p);  %number of failures in each batch
--> Pest=sum(sm>=5)/N %proportion of batches with 5 or more fails
Pest =
    0.7222
-->
 
  • #3
I have two questions then.
  1. Can you explain sum(ss'>5)? What does the ' do?
  2. This tells us 72% of batches will have 5 or more defects. How does that relate to good batches that are rejected? From my code, we know that 45% of time we accept a a defective batch. So 65% we have good batches. Is the answer then 72 - 65 = 7% of good batches are rejected?
 
  • #4
dwsmith said:
I have two questions then.
  1. Can you explain sum(ss'>5)? What does the ' do?
  2. This tells us 72% of batches will have 5 or more defects. How does that relate to good batches that are rejected? From my code, we know that 45% of time we accept a a defective batch. So 65% we have good batches. Is the answer then 72 - 65 = 7% of good batches are rejected?

Your question is ambiguous, I read it as the population has 94% good and 6% bad, what is the probability of rejecting a batch of 100 when the rejection criterion is that it have 5 or more defective items.

For questions about what Matlab commands do use the help system of try them out at the console (with small arrays).

.
 
Last edited:
  • #5
zzephod said:
Your question is ambiguous, I read it as the population has 94% good and 6% bad, what is the probability of rejecting a batch of 100 when the rejection criterion is that it have 5 or more defective items.

For questions about what Matlab commands do use the help system of try them out at the console (with small arrays).

.

the question is what is the probability of rejecting a good batch with that criteria.
 
  • #6
dwsmith said:
the question is what is the probability of rejecting a good batch with that criteria.

What does "good batch" mean?

.
 
  • #7
zzephod said:
What does "good batch" mean?

.

A batch with 5 or less defects. I am trying to find the probability that we reject a good batch of thousand when we only test a 100.
 
  • #8
dwsmith said:
A batch with 5 or less defects. I am trying to find the probability that we reject a good batch of thousand when we only test a 100.

Please post the original question with the exact wording as asked.

.
 
  • #9
zzephod said:
Please post the original question with the exact wording as asked.

.

Use a computer to simulation to determine the probability of rejecting a good batch. To simplify your code, assume sampling with replacement. A good batch is defined as one with a probability of obtaining a good chip p=0.95. The two strategies are to accept the batch if 95 or more of the 100 samples are good and if 98 or more are good.
 
  • #10
dwsmith said:
Use a computer to simulation to determine the probability of rejecting a good batch. To simplify your code, assume sampling with replacement. A good batch is defined as one with a probability of obtaining a good chip p=0.95. The two strategies are to accept the batch if 95 or more of the 100 samples are good and if 98 or more are good.

I see no mention of a population or batch of size of thousand, where did that come from?

That is exactly what the original code I posted does (except I was using p=0.94 which is what you posted in your oringinal post).

Doing exactly the same but with p=0.95, and in SciLab rather than Matlab:
Code:
-->p=0.95;
-->N=10000;
-->ss=rand(N,100);
-->sm=sum(ss>p,2);size(sm)
 ans  =
    10000.    1.  
 
-->Pest=sum(sm>=5)/N
 Pest  =

    0.5642

I would strongly suggest you practice posting questions as they are asked, and include any side information in the section of the book/lecture pertaining to the question. Otherwise you are just wasting the time of anyone foolish enough to try helping you (but then I knew that before replying didn't I).As I have just installed it, here it is in Octave:

Code:
octave:8> p=0.95;N=10000;
octave:9> ss=rand(N,100);
octave:10> sm=sum(ss'>p);
octave:11> Pest=sum(sm>=5)/N
Pest =  0.56080
 
Last edited:

FAQ: Sampling with replacement in Matlab

1. What is sampling with replacement in Matlab?

Sampling with replacement in Matlab refers to the process of randomly selecting elements from a dataset, while allowing duplicates to be selected multiple times. This is in contrast to sampling without replacement, where each element can only be selected once.

2. How is sampling with replacement performed in Matlab?

Sampling with replacement can be performed in Matlab using the randsample function. This function takes in a vector or array of elements, and a number representing the sample size. It then randomly selects elements from the input with replacement, and returns a vector or array containing the selected elements.

3. What is the difference between sampling with replacement and sampling without replacement?

The main difference between sampling with replacement and sampling without replacement is that with replacement, duplicates are allowed to be selected multiple times, while without replacement, each element can only be selected once. This can affect the resulting sample size and the distribution of the selected elements.

4. Can sampling with replacement be used for imbalanced datasets in Matlab?

Yes, sampling with replacement can be used for imbalanced datasets in Matlab. This is because it allows for a more balanced representation of the different classes or categories in the dataset, as duplicate elements can be selected multiple times. However, it is important to carefully consider the implications of using sampling with replacement for imbalanced datasets, as it can also introduce bias in the resulting sample.

5. Are there any alternatives to sampling with replacement in Matlab?

Yes, there are alternatives to sampling with replacement in Matlab. One alternative is sampling without replacement, which can be achieved using the randsample function by specifying the 'Replace' parameter as false. Another alternative is stratified sampling, which involves dividing the dataset into subgroups and sampling from each subgroup separately. This can help to ensure a more representative sample of the overall dataset.

Similar threads

Replies
2
Views
3K
Replies
8
Views
1K
Replies
12
Views
3K
Replies
8
Views
649
Replies
2
Views
1K
Replies
2
Views
2K
Back
Top