Bird
Raised Fist0
SciPydata~5 mins

Uniform distribution in SciPy

Choose your learning style10 modes available
Introduction

The uniform distribution helps us understand situations where all outcomes are equally likely. It is useful to model random events with no bias.

When simulating rolling a fair die where each face has the same chance.
When picking a random number between two limits with equal chance.
When modeling equal probability of events in a simple game.
When generating random samples for testing algorithms.
When estimating probabilities in simple random experiments.
Syntax
SciPy
from scipy.stats import uniform

# Create a uniform distribution object
rv = uniform(loc=lower_bound, scale=upper_bound - lower_bound)

# Get probability density function (PDF) at x
pdf_value = rv.pdf(x)

# Get cumulative distribution function (CDF) at x
cdf_value = rv.cdf(x)

# Generate random samples
samples = rv.rvs(size=number_of_samples)

loc sets the start (minimum) of the distribution.

scale sets the width (max - min) of the distribution.

Examples
Creates a uniform distribution from 0 to 1 and finds the PDF at 0.5.
SciPy
from scipy.stats import uniform

rv = uniform(loc=0, scale=1)  # Uniform between 0 and 1
print(rv.pdf(0.5))
Creates a uniform distribution from 10 to 15 and finds the CDF at 12.
SciPy
rv = uniform(loc=10, scale=5)  # Uniform between 10 and 15
print(rv.cdf(12))
Generates 4 random samples between 5 and 8.
SciPy
samples = uniform(loc=5, scale=3).rvs(size=4)
print(samples)
Sample Program

This program creates a uniform distribution between 2 and 7. It calculates and prints the PDF and CDF at 3, generates 5 random samples, and plots the PDF and CDF curves.

SciPy
from scipy.stats import uniform
import numpy as np
import matplotlib.pyplot as plt

# Define uniform distribution from 2 to 7
start = 2
end = 7
rv = uniform(loc=start, scale=end - start)

# Calculate PDF values for points between 0 and 10
x = np.linspace(0, 10, 100)
pdf_values = rv.pdf(x)

# Calculate CDF values
cdf_values = rv.cdf(x)

# Generate 5 random samples
samples = rv.rvs(size=5)

# Print results
print(f"PDF at 3: {rv.pdf(3):.3f}")
print(f"CDF at 3: {rv.cdf(3):.3f}")
print(f"Random samples: {samples}")

# Plot PDF and CDF
plt.plot(x, pdf_values, label='PDF')
plt.plot(x, cdf_values, label='CDF')
plt.title('Uniform Distribution from 2 to 7')
plt.xlabel('x')
plt.ylabel('Probability')
plt.legend()
plt.grid(True)
plt.show()
OutputSuccess
Important Notes

The PDF value for uniform distribution is constant between loc and loc+scale.

Values outside the range have PDF = 0 and CDF = 0 or 1 depending on side.

Random samples follow the uniform distribution within the specified range.

Summary

Uniform distribution models equal chance outcomes between two limits.

Use scipy.stats.uniform with loc and scale to set range.

PDF is constant inside the range; CDF increases linearly.

Practice

(1/5)
1. What does the loc parameter represent in the scipy.stats.uniform distribution?
easy
A. The starting point (minimum value) of the uniform distribution range
B. The width (length) of the uniform distribution range
C. The mean of the uniform distribution
D. The variance of the uniform distribution

Solution

  1. Step 1: Understand the parameters of uniform distribution

    The uniform distribution in scipy uses loc as the starting point of the range and scale as the length of the range.
  2. Step 2: Identify the role of loc

    The loc parameter sets the minimum value where the uniform distribution starts.
  3. Final Answer:

    The starting point (minimum value) of the uniform distribution range -> Option A
  4. Quick Check:

    loc = start value [OK]
Hint: Remember: loc = start, scale = length [OK]
Common Mistakes:
  • Confusing loc with scale
  • Thinking loc is the mean
  • Mixing loc with variance
2. Which of the following is the correct way to create a uniform distribution from 5 to 15 using scipy?
easy
A. uniform(loc=10, scale=5)
B. uniform(loc=5, scale=15)
C. uniform(loc=5, scale=10)
D. uniform(loc=0, scale=10)

Solution

  1. Step 1: Calculate scale from range

    The range is from 5 to 15, so the length is 15 - 5 = 10. This is the scale.
  2. Step 2: Set loc and scale correctly

    Set loc=5 (start) and scale=10 (length) to cover 5 to 15.
  3. Final Answer:

    uniform(loc=5, scale=10) -> Option C
  4. Quick Check:

    loc=5, scale=15-5=10 [OK]
Hint: Scale = end - start, loc = start [OK]
Common Mistakes:
  • Swapping loc and scale values
  • Using scale as the end value
  • Setting loc to zero incorrectly
3. What is the output of this code?
from scipy.stats import uniform
rv = uniform(loc=2, scale=3)
print(round(rv.pdf(3), 2))
medium
A. 0.33
B. 0.20
C. 0.50
D. 0.00

Solution

  1. Step 1: Understand PDF of uniform distribution

    PDF is constant inside the range and equals 1/scale. Here, scale=3, so PDF = 1/3 ≈ 0.3333.
  2. Step 2: Check if x=3 is inside the range

    Range is from loc=2 to 2+3=5. Since 3 is inside, PDF at 3 is 1/3 ≈ 0.33.
  3. Final Answer:

    0.33 -> Option A
  4. Quick Check:

    PDF = 1/scale = 1/3 = 0.33 [OK]
Hint: PDF inside range = 1/scale [OK]
Common Mistakes:
  • Calculating PDF as zero inside range
  • Using loc instead of scale for PDF
  • Rounding errors without checking range
4. Identify the error in this code snippet:
from scipy.stats import uniform
rv = uniform(loc=1, scale=4)
prob = rv.cdf(6)
print(prob)
medium
A. The scale should be 5 to cover up to 6
B. No error; code runs correctly
C. The loc parameter must be zero
D. The cdf method does not exist for uniform

Solution

  1. Step 1: Check parameters and range

    loc=1 and scale=4 means range is from 1 to 5 (1+4=5).
  2. Step 2: Evaluate cdf at 6

    6 is outside the range (greater than 5), so cdf(6) should return 1 (full probability).
  3. Step 3: Confirm method existence and output

    The cdf method exists for uniform and returns valid output even outside range.
  4. Final Answer:

    No error; code runs correctly -> Option B
  5. Quick Check:

    cdf exists and returns 1 for x > loc+scale [OK]
Hint: CDF returns 1 for x beyond range max [OK]
Common Mistakes:
  • Thinking scale must cover queried x
  • Assuming cdf method is missing
  • Believing loc must be zero
5. You want to model a uniform distribution for daily temperatures between 10°C and 20°C. Which code correctly calculates the probability that temperature is less than 15°C?
hard
A. rv = uniform(loc=10, scale=5) prob = rv.cdf(15)
B. rv = uniform(loc=10, scale=20) prob = rv.cdf(15)
C. rv = uniform(loc=0, scale=10) prob = rv.cdf(15)
D. rv = uniform(loc=10, scale=10) prob = rv.cdf(15)

Solution

  1. Step 1: Set correct loc and scale for temperature range

    Range is 10 to 20, so loc=10 and scale=20-10=10.
  2. Step 2: Calculate CDF at 15

    CDF at 15 is (15 - loc)/scale = (15-10)/10 = 0.5, which is the probability temperature is less than 15.
  3. Final Answer:

    rv = uniform(loc=10, scale=10) prob = rv.cdf(15) -> Option D
  4. Quick Check:

    prob = (15-10)/10 = 0.5 [OK]
Hint: CDF = (x - loc) / scale inside range [OK]
Common Mistakes:
  • Using wrong scale length
  • Setting loc to zero incorrectly
  • Calculating CDF outside range without adjustment
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard