Challenge - 5 Problems
Uniform Distribution Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of uniform random sample generation
What is the output of this code snippet that generates 3 random numbers from a uniform distribution between 0 and 1?
SciPy
from scipy.stats import uniform sample = uniform.rvs(size=3, random_state=42) print([round(x, 2) for x in sample])
Attempts:
2 left
💡 Hint
Use the random_state to get reproducible results and round the output to 2 decimals.
✗ Incorrect
The uniform.rvs function with random_state=42 produces the same 3 values every time. Rounded to 2 decimals, they are [0.37, 0.95, 0.73].
❓ data_output
intermediate2:00remaining
Mean and variance of uniform distribution
What are the mean and variance of a uniform distribution between 2 and 5?
SciPy
from scipy.stats import uniform rv = uniform(loc=2, scale=3) mean = rv.mean() var = rv.var() print(round(mean, 2), round(var, 2))
Attempts:
2 left
💡 Hint
Mean is midpoint; variance is (scale^2)/12.
✗ Incorrect
Mean = (2 + 5)/2 = 3.5; Variance = (3^2)/12 = 9/12 = 0.75.
❓ visualization
advanced3:00remaining
Plotting PDF of uniform distribution
Which option produces the correct plot of the probability density function (PDF) of a uniform distribution from 0 to 4?
SciPy
import numpy as np import matplotlib.pyplot as plt from scipy.stats import uniform x = np.linspace(-1, 5, 500) rv = uniform(loc=0, scale=4) plt.plot(x, rv.pdf(x)) plt.title('Uniform PDF from 0 to 4') plt.show()
Attempts:
2 left
💡 Hint
Uniform PDF is constant between loc and loc+scale.
✗ Incorrect
The PDF of uniform(0,4) is 1/4=0.25 between 0 and 4, zero outside. So the plot is a flat line at 0.25 in that range.
🧠 Conceptual
advanced1:30remaining
Effect of scale parameter on uniform distribution
If you increase the scale parameter of a uniform distribution while keeping the location fixed, what happens to the variance?
Attempts:
2 left
💡 Hint
Variance formula involves square of scale.
✗ Incorrect
Variance of uniform(loc, scale) is (scale^2)/12, so it grows with the square of scale.
🔧 Debug
expert2:00remaining
Identify the error in uniform distribution sampling code
What error does this code raise?
from scipy.stats import uniform
sample = uniform.rvs(loc=1, scale=-2, size=5)
print(sample)
SciPy
from scipy.stats import uniform sample = uniform.rvs(loc=1, scale=-2, size=5) print(sample)
Attempts:
2 left
💡 Hint
Scale parameter cannot be negative for uniform distribution.
✗ Incorrect
The scale parameter must be positive; negative scale raises ValueError.