Sampling with replacement in Matlab

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

Discussion Overview

The discussion revolves around simulating the probability of rejecting a good batch using the Binomial Probability Law in Matlab. Participants explore different cases based on the number of defective items allowed in a batch and the implications of their simulations.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • Some participants describe their approach to simulating the probability of rejecting a good batch, using specific mathematical formulations and Matlab code.
  • Others question the interpretation of the results, particularly how the calculated probabilities relate to the acceptance or rejection of batches based on defect counts.
  • A participant seeks clarification on the Matlab command used in the simulation, specifically the transpose operation denoted by the apostrophe.
  • There are discussions about the definition of a "good batch" and the criteria for rejection, with some participants suggesting that a good batch is one with 5 or fewer defects.
  • Some participants express confusion regarding the original question's wording and context, leading to requests for clarification and the exact phrasing of the problem.
  • One participant provides an alternative simulation approach using SciLab, comparing results with those obtained in Matlab.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the interpretation of the results or the original question's intent. Multiple competing views on the definition of a good batch and the implications of the simulation results remain unresolved.

Contextual Notes

There are ambiguities in the original question regarding the population size and the criteria for rejecting a batch, which affect the clarity of the discussion. Participants also express uncertainty about the implications of their findings in relation to good and defective batches.

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