Sampling with replacement in Matlab

  • Context: MATLAB 
  • Thread starter Thread starter Dustinsfl
  • Start date Start date
  • Tags Tags
    Matlab Sampling
Click For Summary
SUMMARY

This discussion focuses on simulating the probability of rejecting a good batch using the Binomial Probability Law in MATLAB. The two cases analyzed are the probabilities of having 95 or more defective items and 98 or more defective items in a batch of 100, with a failure rate of \(p = 0.94\). The MATLAB code provided utilizes functions such as nchoosek and rand to compute and plot the probabilities, revealing that approximately 72% of batches will have 5 or more defects. The conversation also clarifies the definition of a "good batch" and the implications of sampling with replacement.

PREREQUISITES
  • Understanding of Binomial Probability Law
  • Familiarity with MATLAB programming, specifically functions like nchoosek and rand
  • Basic knowledge of statistical sampling techniques
  • Ability to interpret probability distributions and simulation results
NEXT STEPS
  • Learn about MATLAB's nchoosek function for combinatorial calculations
  • Explore statistical simulation techniques in MATLAB
  • Study the implications of sampling with replacement in statistical analysis
  • Investigate the differences between Binomial and Normal distributions in quality control
USEFUL FOR

Statisticians, quality control analysts, and MATLAB users interested in simulating probabilities related to batch testing and defect analysis.

Dustinsfl
Messages
2,217
Reaction score
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
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
-->
 
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?
 
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:
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.
 
dwsmith said:
the question is what is the probability of rejecting a good batch with that criteria.

What does "good batch" mean?

.
 
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.
 
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.

.
 
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:

Similar threads

  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 4 ·
Replies
4
Views
3K
Replies
2
Views
2K
  • · Replies 12 ·
Replies
12
Views
4K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K