You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 KiB
42 KiB
None
<html lang="en">
<head>
</head>
</html>
Lab Example 1 — Sampling variability and CLT
Objective: show that individual measurements vary, but sample means are more stable.
Students should:
- Generate a non-normal population.
- Take many random samples.
- Compute the mean of each sample.
- Plot the distribution of the sample means.
In [1]:
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(10)
population = np.random.exponential(scale=2.0, size=100000)
sample_size = 30
n_samples = 1000
sample_means = []
for i in range(n_samples):
sample = np.random.choice(population, size=sample_size, replace=True)
sample_means.append(sample.mean())
plt.hist(population, bins=40)
plt.title("Original population")
plt.xlabel("Value")
plt.ylabel("Frequency")
plt.show()
plt.hist(sample_means, bins=40)
plt.title("Distribution of sample means")
plt.xlabel("Sample mean")
plt.ylabel("Frequency")
plt.show()
Even when the original data are not normally distributed, the distribution of sample means tends to become approximately normal when the sample size increases.
Lab Example 2 — Confidence interval for a mean¶
Objective: estimate a parameter with uncertainty.
Use the repeated measurement example:
In [2]:
import numpy as np
from scipy import stats
measurements = np.array([9.91, 10.03, 9.98, 10.05, 10.01, 9.96, 10.08, 9.94,
10.02, 9.99, 10.04, 9.97, 10.06, 9.95, 10.00])
n = len(measurements)
xbar = measurements.mean()
s = measurements.std(ddof=1)
confidence = 0.95
alpha = 1 - confidence
t_critical = stats.t.ppf(1 - alpha/2, df=n-1)
margin_error = t_critical * s / np.sqrt(n)
ci_lower = xbar - margin_error
ci_upper = xbar + margin_error
print("Mean:", xbar)
print("Standard deviation:", s)
print("95% confidence interval:", ci_lower, ci_upper)
Interpretation:
We do not report only the mean. We report the mean with an interval that reflects uncertainty.
Suggested question:
Does the confidence interval include 10,\Omega?
Lab Example 3 — Hypothesis test for a nominal value¶
Objective: test whether the measured component is statistically consistent with a reference value.
For the resistor example:
H_0: \mu = 10
H_a: \mu \neq 10
Python:
In [3]:
from scipy import stats
mu0 = 10.0
t_statistic, p_value = stats.ttest_1samp(measurements, popmean=mu0)
print("t statistic:", t_statistic)
print("p-value:", p_value)