The uniform distribution helps us understand situations where all outcomes are equally likely. It is useful to model random events with no bias.
Uniform distribution in 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.
from scipy.stats import uniform rv = uniform(loc=0, scale=1) # Uniform between 0 and 1 print(rv.pdf(0.5))
rv = uniform(loc=10, scale=5) # Uniform between 10 and 15 print(rv.cdf(12))
samples = uniform(loc=5, scale=3).rvs(size=4) print(samples)
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.
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()
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.
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
loc parameter represent in the scipy.stats.uniform distribution?Solution
Step 1: Understand the parameters of uniform distribution
The uniform distribution in scipy useslocas the starting point of the range andscaleas the length of the range.Step 2: Identify the role of
Theloclocparameter sets the minimum value where the uniform distribution starts.Final Answer:
The starting point (minimum value) of the uniform distribution range -> Option AQuick Check:
loc= start value [OK]
- Confusing loc with scale
- Thinking loc is the mean
- Mixing loc with variance
Solution
Step 1: Calculate scale from range
The range is from 5 to 15, so the length is 15 - 5 = 10. This is the scale.Step 2: Set loc and scale correctly
Setloc=5(start) andscale=10(length) to cover 5 to 15.Final Answer:
uniform(loc=5, scale=10) -> Option CQuick Check:
loc=5, scale=15-5=10 [OK]
- Swapping loc and scale values
- Using scale as the end value
- Setting loc to zero incorrectly
from scipy.stats import uniform rv = uniform(loc=2, scale=3) print(round(rv.pdf(3), 2))
Solution
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.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.Final Answer:
0.33 -> Option AQuick Check:
PDF = 1/scale = 1/3 = 0.33 [OK]
- Calculating PDF as zero inside range
- Using loc instead of scale for PDF
- Rounding errors without checking range
from scipy.stats import uniform rv = uniform(loc=1, scale=4) prob = rv.cdf(6) print(prob)
Solution
Step 1: Check parameters and range
loc=1 and scale=4 means range is from 1 to 5 (1+4=5).Step 2: Evaluate cdf at 6
6 is outside the range (greater than 5), so cdf(6) should return 1 (full probability).Step 3: Confirm method existence and output
Thecdfmethod exists for uniform and returns valid output even outside range.Final Answer:
No error; code runs correctly -> Option BQuick Check:
cdf exists and returns 1 for x > loc+scale [OK]
- Thinking scale must cover queried x
- Assuming cdf method is missing
- Believing loc must be zero
Solution
Step 1: Set correct loc and scale for temperature range
Range is 10 to 20, so loc=10 and scale=20-10=10.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.Final Answer:
rv = uniform(loc=10, scale=10) prob = rv.cdf(15) -> Option DQuick Check:
prob = (15-10)/10 = 0.5 [OK]
- Using wrong scale length
- Setting loc to zero incorrectly
- Calculating CDF outside range without adjustment
Ready to prove it?
Pass the test and this pattern moves unknown
