Day 4 (Part 2) — 10 days of Statistics (HackerRank)

Celestial
3 min readJun 23, 2023

--

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.
Negative Distribution

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).
Geometric Distribution

💡 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 -

  1. p, q, n = 1/3, 2/3, 5: This line assigns the values to the variables p, q, and n. The value 1/3 is assigned to p, 2/3 is assigned to q, and 5 is assigned to n.
  2. q**(n-1)*p: This expression calculates the value of q raised to the power of (n-1) and then multiplies it by p. The * operator represents exponentiation.
  3. 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.
  4. print(...): Finally, the print 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 -

  1. p, q, n = 1/3, 2/3, 5: This line assigns the values to the variables p, q, and n. The value 1/3 is assigned to p, 2/3 is assigned to q, and 5 is assigned to n.
  2. map(lambda x:q**(x-1)*p, range(1,6)): This part of the code uses the map function to apply a lambda function to each element of the range 1 to 6 (exclusive). The lambda function takes a parameter x and calculates q**(x-1)*p.
  3. sum(...): The sum function is used to calculate the sum of the values generated by the map function.
  4. 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.
  5. print(...): Finally, the print 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.

--

--

Celestial
Celestial

Written by Celestial

Uncovering Patterns , Empowering Strategies.

No responses yet