Created
February 26, 2022 14:21
-
-
Save QuantVI/79a1c164f3017c6a7a2d860e55cf5d5b to your computer and use it in GitHub Desktop.
Simulate Sampling without replacement from an Urn, to get a probability
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import copy | |
| import random | |
| # Consider using the modules imported above. | |
| class Hat: | |
| def __init__(self, **kwargs): | |
| self.d = kwargs | |
| self.contents = [ | |
| key for key, val in kwargs.items() for num in range(val) | |
| ] | |
| def draw(self, num: int) -> list: | |
| if num >= len(self.contents): | |
| return self.contents | |
| else: | |
| indices = random.sample(range(len(self.contents)), num) | |
| chosen = [self.contents[idx] for idx in indices] | |
| #new_contents = [ v for i, v in enumerate(self.contents) if i not in indices] | |
| new_contents = [pair[1] for pair in enumerate(self.contents) | |
| if pair[0] not in indices] | |
| self.contents = new_contents | |
| return chosen | |
| def __repr__(self): return str(self.contents) | |
| def experiment(hat, expected_balls, num_balls_drawn, num_experiments): | |
| trials =[] | |
| for n in range(num_experiments): | |
| copyn = copy.deepcopy(hat) | |
| result = copyn.draw(num_balls_drawn) | |
| trials.append(result) | |
| #trials = [ copy.deepcopy(hat).draw(num_balls_drawn) for n in range(num_experiments) ] | |
| expected_contents = [key for key, val in expected_balls.items() for num in range(val)] | |
| temp_eval = [[o for o in expected_contents if o in trial] for trial in trials] | |
| temp_compare = [ evaled == expected_contents for evaled in temp_eval] | |
| return expected_contents,temp_eval,temp_compare, trials | |
| #evaluations = [ all(x in trial for x in expected_contents) for trial in trials ] | |
| #if evaluations: prob = sum(evaluations)/len(evaluations) | |
| #else: prob = 0 | |
| #return prob, expected_contents | |
| #hat3 = Hat(red=5, orange=4, black=1, blue=0, pink=2, striped=9) | |
| #hat4 = Hat(red=1, orange=2, black=3, blue=2) | |
| hat1 = Hat(blue=3,red=2,green=6) | |
| a1,a2,a3,a4 = experiment(hat=hat1, expected_balls={"blue":2,"green":1}, num_balls_drawn=4, num_experiments=1000) | |
| #actual = probability | |
| #expected = 0.272 | |
| #self.assertAlmostEqual(actual, expected, delta = 0.01, msg = 'Expected experiment method to return a different probability.') | |
| hat2 = Hat(yellow=5,red=1,green=3,blue=9,test=1) | |
| b1,b2,b3,b4 = experiment(hat=hat2, expected_balls={"yellow":2,"blue":3,"test":1}, num_balls_drawn=20, num_experiments=100) | |
| #actual = probability | |
| #expected = 1.0 | |
| #self.assertAlmostEqual(actual, expected, delta = 0.01, msg = 'Expected experiment method to return a different probability.') |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Trying to debug why hat1 would return a prob over 60% when the answer should be 27.2%