<jupyter_start><jupyter_text>Day 4  Binomial Distribution IThe ratio of boys to girls for babies born in Russia is 1:09:1. If there is 1 child born per birth, what proportion of Russian families with exactly 6 children will have at least 3 boys?  Input<jupyter_code>from io import StringIO
example_bdst = lambda: StringIO('1.09 1')<jupyter_output><empty_output><jupyter_text>Solution<jupyter_code>import math
comb = lambda n, r: math.factorial(n) / (math.factorial(r) * math.factorial(n - r))
def b(x, n, p):
    return comb(n, x) * (p ** x) * ((1 - p) ** (n - x))<jupyter_output><empty_output><jupyter_text>Output<jupyter_code>def solve_bdst(stream):
    ratio_boy, ratio_girl = map(float, stream.readline().split())
    n = 6
    p_boy = ratio_boy / (ratio_boy + ratio_girl)
    print('{:.3f}'.format(b(3, n, p_boy) + b(4, n, p_boy) + b(5, n, p_boy) + b(6, n, p_boy)))
    
solve_bdst(example_bdst())<jupyter_output>0.696<jupyter_text>Binomial Distribution IIA manufacturer of metal pistons finds that, on average, 12% of the pistons they manufacture are rejected because they are incorrectly sized. What is the probability that a batch of 10 pistons will contain:    No more than 2 rejects?    At least 2 rejects?  Input<jupyter_code>from io import StringIO
example_bdst_2 = lambda: StringIO('12 10')<jupyter_output><empty_output><jupyter_text>Solution<jupyter_code>comb = lambda n, r: math.factorial(n) / (math.factorial(r) * math.factorial(n - r))
def b(x, n, p):
    return comb(n, x) * (p ** x) * ((1 - p) ** (n - x)) 

def max_2_rejects(n, p_reject):
    return b(0, n, p_reject) + b(1, n, p_reject) + b(2, n, p_reject)
    
def at_least_2_rejects(n, p_reject):
    return sum([b(x, n, p_reject) for x in range(2, 11)])<jupyter_output><empty_output><jupyter_text>Output<jupyter_code>def solve_bdst_2(stream):
   p_reject, n = [int(x) for x in stream.readline().split()]
   print('{:.3f}'.format(
       max_2_rejects(n, p_reject / 100)
   ))
   print('{:.3f}'.format(
       at_least_2_rejects(n, p_reject / 100)
   ))
solve_bdst_2(example_bdst_2())<jupyter_output>0.891
0.342<jupyter_text>Geometric Distribution I & IIThe probability that a machine produces a defective product is 1/3 . What is the probability that the 1st defect is found during the 5th inspection?The first line contains the respective space-separated numerator and denominator for the probability of a defect, and the second line contains the inspection we want the probability of being the first defect for: Geometric Distribution IIThe probability that a machine produces a defective product is 1/3. What is the probability that the defect is found during the first 5 inspections?  Input<jupyter_code>from io import StringIO
example_geom = lambda: StringIO('''1 3
5''')<jupyter_output><empty_output><jupyter_text>Solution<jupyter_code>comb = lambda n, r: math.factorial(n) / (math.factorial(r) * math.factorial(n - r))

def b_star(x, n, p):
    return comb(n-1, x-1) * (p ** x) * (1 - p) ** (n - x)

def defect_on_nth_inspection(p_defect, nth_inspection):
    return b_star(1, nth_inspection, p_defect)

def defect_during_first_n_inspections(p_defect, n_inspections):
    return sum([b_star(1, x, p_defect) for x in range(1, n_inspections + 1)])
### Output
def get_params(stream):
    p_defect_num, p_defect_denom = [int(x) for x in stream.readline().split()]
    p_defect = p_defect_num / p_defect_denom
    nth_inspection = int(stream.readline())
    return p_defect, nth_inspection<jupyter_output><empty_output><jupyter_text>Output for Geometric Distribution I<jupyter_code>def solve_geom_1(stream):
    p_defect, nth_inspection = get_params(stream) 
    print('{:.3f}'.format(
        defect_on_nth_inspection(p_defect, nth_inspection)
    ))
solve_geom_1(example_geom())<jupyter_output>0.066<jupyter_text>Output for Geometric Distribution II<jupyter_code>def solve_geom_2(stream):
    p_defect, nth_inspection = get_params(stream) 
    print('{:.3f}'.format(
        defect_during_first_n_inspections(p_defect, nth_inspection)
    ))
solve_geom_2(example_geom())<jupyter_output>0.868
{'fraction_non_alphanumeric': 0.11894814450724994, 'fraction_numerical': 0.02334725976898501, 'mean_word_length': 5.1760242792109254, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '396990', 'license': 'Apache-2.0', 'n_tokens_mistral': 1570, 'n_tokens_neox': 1499, 'n_words': 386, 'path': 'ten_days_of_statistics/4_binomial.ipynb', 'repo_name': 'talkdirty/knarrekcah', 'size': '4069'}