Badges: 4
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
Report Thread starter 1 month ago
Hi so I have this question. “ Your company practices "acceptance sampling" on stock you receive from
vendors. For a lot size of 200 units, you destructively test 25 randomly selected
units. If more than 2 units do not conform to specifications, the entire lot of 200
units is rejected. What is the probability of rejecting the lot if the actual number
of non-conforming units, in the lot is given by the variable "bad"? Assume the
non-conforming units are randomly distributed within the lot, and that your
destructive tests are definitive. Hints: You can use np.zeros(200) to create an
array of zeros with 200 elements. Using 10,000 replications will give you the
necessary precision in a stochastic estimation of the probability.” could anyone point me in the right direction of the code I would have to write in python computer language
Badges: 9
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
Report 1 month ago
My numpy knowledge is horrendous and my statistics knowledge is even worse. I don't even understand the question. I've coded up something to simulate the scenario though.

import numpy as np

def is_lot_acceptable(*, bad, units, sample_size):
    # Create the lot of units and assign 1 to the first "bad" number of units
    lot = np.zeros(units, dtype=int)
    lot[:bad] = 1

    # Generate some random indexes
    indexes = np.random.choice(lot.shape[0], size=sample_size, replace=False)

    # Get the unit values of the indexes
    sample = lot[indexes]

    # Count the number of 1s in the sample
    bad_unit_count = np.count_nonzero(sample)

    # Return True if there are 2 or less bad units
    return bad_unit_count <= 2

rejections = 0
for _ in range(10_000):
    # I've just set "bad" to some arbitrary number, in this case 15
    rejections += int(is_lot_acceptable(bad=15, units=200, sample_size=25))

Last edited by Composure; 1 month ago
Badges: 9
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
Report 1 month ago
Sorry, I also can't help with "stochastic estimation", lol! But, this problem is cute (reminds me a little of the birthday problem and, if one squints, Project Euler problem 307.

I needed some Python practice, so I thought I'd have a go at this. The following program should solve the problem exactly, no trials or stochastic estimation. Hopefully the comments are instructive, and there is no logic or Python error (let me know if you find one!) The basic idea is to fix the 25 elements we'll choose, and then randomly distribute the "bad" units amongst the total. We count the probability that we pick 0, 1 or 2 (or an arbitrary number) of the chosen units when distributing. (For fun: there is an almost symmetric, and probably more natural, way to picture it: fix the "bad" units, then vary the chosen ones. Flip two of the inputs in n_bad_chosen and you're doing that version instead!)

Hope it can be of some value checking your "estimates" are reasonable.

from math import comb
from math import factorial

# Idea: We fix the "chosen" units, and randomly place "bad" attribute.
# This function returns the probability exactly n bad attributes are placed in
# the "chosen" elements.
def n_bad_chosen(n, total, chosen, bad):
    if (n>chosen) or (bad>total) or (chosen>total) or (chosen+bad-n>total) or (n>bad):
        return 0
    ret = 1.0
    # Placing  the "bad" elements not chosen, we have "total-chosen" options for
    # the first, then "total-chosen-1", then eventually "total-chosen-bad-n+1". 
    ret *= factorial(total-chosen)/factorial(total-chosen-(bad-n))
    # Placing the "bad" elements chosen, we have "chosen" options, then
    # "chosen-1", until "chosen-n+1".
    ret *= factorial(chosen)/factorial(chosen-n)
    # In each choice, we had total, total-1... total-bad+1 total options.
    ret /= (factorial(total)/factorial(total-bad))
    # We have choose(bad,n) ways to place the n "chosen" bad choices in the
    # total bad choices.
    ret *= comb(bad,n)
    return ret

def solve(total,chosen,bad,max_bad):
    ret = 1.0
    # If we want <= max_bad chosen bad choices, we sum the probability of
    # choosing 0, 1, ... max_bad. (Actually, we subtract from 1, as we count
    # failure, not success.)
    for n in range(max_bad+1):
        ret -= n_bad_chosen(n,total,chosen,bad)
    return ret

def main():
    for bad in range(101):
        print (bad, f'\t{solve(200,25,bad,2):.10f}';)

Last edited by gcseeeman; 1 month ago

Quick Reply

Attached files
Write a reply...
new posts
to top
My Feed

See more of what you like on
The Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.


When did you submit your Ucas application if you applied to go to university this year?

September 2021 (11)
October 2021 (81)
November 2021 (17)
December 2021 (27)
January 2021 (14)
I still haven't submitted it yet! (16)
Something else (3)

Watched Threads

View All