<jupyter_start><jupyter_text>Solution to: [Day 4: Binomial Distribution I](https://www.hackerrank.com/challenges/s10-binomial-distribution-1/problem) Table of Contents- Table of Contents- Notes    - Random Variable    - Binomial Experiment    - Bernoulli Random Variable and Distribution    - Binomial Distribution    - Cumulative Probability    - Example Problem- Solution    - Imports    - Input    - Get Probability    - Main<jupyter_code>%%javascript
$.getScript('https://kmahelona.github.io/ipython_notebook_goodies/ipython_notebook_toc.js')<jupyter_output><empty_output><jupyter_text>Notes  Random VariableA random variable, 	**X**, is the real-valued function X: S -> R in which there is an event for each interval I where I <=R. You can think of it as the set of probabilities for the possible outcomes of a sample space. For example, if you consider the random variable X as possible sums for the values rolled by 2 four-sided dice:	X = {2, 3, 4, 5, 6, 7, 8}    	P(X = 2) = P({ (1, 1)}) = 1/16	P(X = 3) = P({ (1, 2), (2, 1)}) = 2/16	P(X = 4) = P({ (1, 3), (2, 2), (3, 1)}) = 3/16	P(X = 5) = P({ (1, 4), (2, 3), (3, 2), (4, 1)}) = 4/16	P(X = 6) = P({ (2, 4), (3, 3), (4, 2)}) = 3/16	P(X = 7) = P({ (3, 4), (4, 3)}) = 2/16	P(X = 8) = P({ (4, 4)}) = 1/16  Binomial ExperimentA binomial experiment (or Bernoulli trial) is a statistical experiment that has the following properties:	- The experiment consists of n repeated trials.	- The trials are independent.	- The outcome of each trial is either success (s) or failure (f).  Bernoulli Random Variable and DistributionThe sample space of a binomial experiment only contains two points: **s** and **f**.We define a Bernoulli random variable to be the random variable defined by:\begin{equation}\large\begin{cases}X(s) = 1 \\X(f) = 0\end{cases}If we consider the probability of success to be **p** and the probability of failure to be **q** (where q = 1-p), then the **probability mass function (PMF)** of X is:\begin{equation}\large\mathrm{PMF} = \begin{cases}1-p = q, \text{ if x = 0} \\p, \text{ if x = 1} \\0, \text{otherwise}\end{cases}\end{equation}This is also expressed as:\begin{equation}\largef(x) = p^{x}(1-p)^{1-x}\end{equation}  Binomial DistributionWe define a binomial process to be a binomial experiment meeting the following conditions:- The number of successes is x.- The total number of trials is n.- The probability of success of 1 trial is p.- The probability of failure of 1 trial q, where q = 1 - p.- b(x, n, p) is the binomial probability, meaning the probability of having exactly x successes out of n trials.The binomial random variable is the number of successes, x, out of n trials.The binomial distribution is the probability distribution for the binomial random variable, given by the following **probability mass function**:\begin{equation}\largeb(x, n, p) = {n \choose x} * p^{x} * p^{n-x}\end{equation}Note: Recall that ${n \choose x} is notation for combinations, and solved:\begin{equation}\largenCr = \frac{nPr}{!r} = \frac{n!}{r! * (n - r)!}\end{equation}  Cumulative ProbabilityWe consider the distribution function for some real-valued random variable, **X**, to be Fx(X) = P(X <= x). Because this is a non-decreasing function that accumulates all the probabilities for the values of x up to (and including) X, we call it the **cumulative distribution function (CDF)** of X. As the CDF expresses a cumulative range of values, we can use the following formula to find the cumulative probabilities for all x [a, b]:\begin{equation}\largeP(a < X \leq b) = F_{x}(b) - F_{x}(a)\end{equation}This formula takes a summation of the binomial probability mass function  Example ProblemA fair coin is tossed n times. Find the following probabilities:- Getting 5 heads.- Getting at least 5 heads.- Getting at most 5 heads.* Note: Problem 2 & 3 are solved in Python Solution using CDF function.To calculate the probability distribution of a binomial distribution, use the binominal probability mass function:\begin{equation}\large\text{b(x, n, p)} = {n \choose x} * p^{x} * q^{n-x}\end{equation}\begin{equation}\large\text{5, 10, 0.5} = {10 \choose 5} ** 0.5^{5} * 0.5^{0.5} = 0.24609\end{equation}  Solution The 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?Write a program to compute the answer using the above parameters. Then print your result, rounded to a scale of 3 decimal places (i.e., 1.234 format).Input Format:A single line containing the following values:1.09 1  Imports<jupyter_code>from typing import Tuple<jupyter_output><empty_output><jupyter_text>Input<jupyter_code>def get_input() -> Tuple[float, int]:
	"""Returns ipnut for binomial distribution problem i

	Returns:
		Tuple[float, int]: Represents respective probabilities of girl and boy babies.
	"""
	girl_prob, boy_prob = [x for x in input().split()]
	return (float(girl_prob), int(boy_prob))<jupyter_output><empty_output><jupyter_text>Get Probability<jupyter_code>def factorial(num: int) -> float:
	"""Returns factorial for num

	Args:
		num (int): Number to get factorial

	Returns:
		float: Factorial of num
	"""
	if num == 0:
		return 1

	output = num
	for i in range(num-1, 0, -1):
		output *= i
	return output
##### Get p
def get_probability_p(girl_prob: float, boy_prob: int, desired_sex: str) -> float:
    """Returns probability of successful trial.

    Args:
        girl_prob (float): P(girl)
        boy_prob (int): P(boy)
        desired_sex (str): calc p for sex

    Returns:
        float: [description]
    """
    if desired_sex == 'm':
        return boy_prob / (girl_prob + boy_prob)
    return girl_prob / (girl_prob + boy_prob)

def get_bin_mass_dist(x, n, p) -> float:
	"""Returns binomial mass distribution of p(x, n, p).

	Args:
		x ([type]): Success trials
		n ([type]): Total Trials
		p ([type]): P(X)

	Returns:
		float: probability mass distribution
	"""

	combinations = factorial(n) / (factorial(x) * factorial(n - x))
	bin_mass = combinations * p ** x * (1 - p) ** (n - x)
	return bin_mass
def get_cum_prob_bin_dist(r: int, x: int, n: int, p: float) -> float:
	"""Returns cumulative prob of binary distribution with inclusive limits [r:x].

	Args:
		r (int): lower limit of dist
		x (int): upper limit of dist
		n (int): total trial
		p (float): probability of x

	Returns:
		float: cumulative probability of binary distribution
	"""
	cum_prob = 0
	for i in range(r, x + 1):
		bin_mass = get_bin_mass_dist(i, n, p)
		cum_prob += bin_mass
	return cum_prob
def solution() -> None:
	"""Prints solution to Day 4: Binomial Distribution i.
	"""
	## Constants
	N = 6
	R = 3
	X = 6
	DESIRED_SEX = 'b'

	## funcs
	girl_prob, boy_prob = get_input()
	p = get_probability_p(girl_prob, boy_prob, DESIRED_SEX)
	cum_prob = get_cum_prob_bin_dist(R, X, N, p)
	print("{0:.3f}".format(cum_prob))<jupyter_output><empty_output><jupyter_text>Main<jupyter_code>def main():
	"""Prints solutions to:
		- Example problems 1, 2, & 3.
		- Day 4: Binomial Distribution i problem.
	"""
	print("\nExample 2")
	r, x, n, p = 0, 5, 10, 0.5
	print( get_cum_prob_bin_dist(r, x, n, p))

	print("\nExample 3")
	r, x, n, p = 5, 10, 10, 0.5
	print( get_cum_prob_bin_dist(r, x, n, p))

	print("\nSolution Problem")
	solution()
if __name__ == "__main__":
	main()<jupyter_output>Example 2
0.623046875

Example 3
0.623046875

Solution Problem
1.09 1
0.696
{'fraction_non_alphanumeric': 0.1269670477471419, 'fraction_numerical': 0.025420309347679894, 'mean_word_length': 4.103637611530543, 'pattern_counts': {'":': 1, '<': 23, '<?xml version=': 0, '>': 27, 'https://': 2, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '77909', 'license': 'Apache-2.0', 'n_tokens_mistral': 2665, 'n_tokens_neox': 2400, 'n_words': 952, 'path': 'statistics/10_days/12_day4binomialdistributioni.ipynb', 'repo_name': 'jaimiles23/hacker_rank', 'size': '7435'}