Day 4 of this challenge series comprises four parts/challenges that focus on the concepts of Binomial Distribution and Geometric Distribution. In this blog, we will cover the Geometric distribution.
To ensure a solid foundation, let’s begin by clarifying the fundamental concept before delving into the challenges.
Negative Binomial Distribution(GD)
- The negative binomial distribution models the number of independent and identical Bernoulli trials needed to obtain a fixed number of successes.
- The binomial distribution focuses on the number of successes in a fixed number of trials, while the negative binomial distribution focuses on the number of trials needed to achieve a fixed number of successes.
Geometric Distribution
- The geometric distribution is a special case of the negative binomial distribution that deals with the number of Bernoulli trials required to get a success (i.e., counting the number of failures before the first success)
- GD is used to calculate the probability of needing a specific number of trials (k) until the first success occurs, where each trial has a constant probability of success (p).
- Negative Binomial Distribution is used to calculate the probability of needing a specific number of trials (k) to achieve a fixed number of successes (r), where each trial has a constant probability of success (p).
💡 In summary, the geometric distribution focuses = on the number of trials needed until the first success, the negative binomial distribution focuses = on the number of trials needed to achieve a fixed number of successes, and the binomial distribution focuses = on the number of successes in a fixed number of trials.
Challenge #1 — Geometric Distribution I
Question —
Answer -
p, q, n = 1/3, 2/3, 5
print(f'{q**(n-1)*p:.3f}')
Explanation -
p, q, n = 1/3, 2/3, 5
: This line assigns the values to the variablesp
,q
, andn
. The value1/3
is assigned top
,2/3
is assigned toq
, and5
is assigned ton
.q**(n-1)*p
: This expression calculates the value ofq
raised to the power of(n-1)
and then multiplies it byp
. The*
operator represents exponentiation.f'{q**(n-1)*p:.3f}'
: This line uses f-string formatting to create a string representation of the calculated value from the previous step. The:.3f
specifies that the value should be formatted as a floating-point number with three decimal places.print(...)
: Finally, theprint
function is used to display the calculated value as a string.
Challenge #2 — Geometric Distribution I
Question —
Answer -
p, q, n = 1/3, 2/3, 5
print(f'{sum(map(lambda x:q**(x-1)*p, range(1,6))):.3f}')
Explanation -
p, q, n = 1/3, 2/3, 5
: This line assigns the values to the variablesp
,q
, andn
. The value1/3
is assigned top
,2/3
is assigned toq
, and5
is assigned ton
.map(lambda x:q**(x-1)*p, range(1,6))
: This part of the code uses themap
function to apply a lambda function to each element of the range1
to6
(exclusive). The lambda function takes a parameterx
and calculatesq**(x-1)*p
.sum(...)
: Thesum
function is used to calculate the sum of the values generated by themap
function.f'{sum(...):.3f}'
: This line uses f-string formatting to create a string representation of the sum calculated in the previous step. The:.3f
specifies that the sum should be formatted as a floating-point number with three decimal places.print(...)
: Finally, theprint
function is used to display the calculated sum as a string.
Stay tuned for future blog posts! Follow me to stay updated with all the upcoming content.