# Python

Watch
Announcements
#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
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

# Create the lot of units and assign 1 to the first "bad" number of units
lot = np.zeros(units, dtype=int)

# 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

# Return True if there are 2 or less bad units

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

print(rejections)```
Last edited by Composure; 1 month ago
0
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.
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".
# Placing the "bad" elements chosen, we have "chosen" options, then
# "chosen-1", until "chosen-n+1".
ret *= factorial(chosen)/factorial(chosen-n)
# We have choose(bad,n) ways to place the n "chosen" bad choices in the
return ret

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.)
return ret

def main():

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

new posts
Back
to top
Latest
My Feed

### Oops, nobody has postedin the last few hours.

Why not re-start the conversation?

see more

### See more of what you like onThe Student Room

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

### Poll

Join the discussion

#### 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%