Econstudent26
Badges: 4
Rep:
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#1
Report Thread starter 1 month ago
#1
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
0
reply
Composure
Badges: 9
Rep:
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#2
Report 1 month ago
#2
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.

Code:
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))

print(rejections)
Last edited by Composure; 1 month ago
0
reply
gcseeeman
Badges: 9
Rep:
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#3
Report 1 month ago
#3
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.


Code:
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}';)

main()
Last edited by gcseeeman; 1 month ago
1
reply
X

Quick Reply

Attached files
Write a reply...
Reply
new posts
Back
to top
Latest
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.

Personalise

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

September 2021 (11)
6.51%
October 2021 (81)
47.93%
November 2021 (17)
10.06%
December 2021 (27)
15.98%
January 2021 (14)
8.28%
I still haven't submitted it yet! (16)
9.47%
Something else (3)
1.78%

Watched Threads

View All