Untitled
unknown
plain_text
10 months ago
13 kB
22
Indexable
Experiment 3: Bootstrap Sampling and Bootstrap
Errors
Aim
To understand and implement Bootstrap sampling techniques for estimating population
parameters, and to analyze the Bootstrap errors for evaluating the accuracy and stability
of the estimates.
Theory
Bootstrap is a resampling technique used to estimate the distribution of a statistic by
repeatedly sampling with replacement from the observed dataset.
Bootstrap Sampling: Randomly sample (with replacement) from the dataset to
create multiple “bootstrap samples.”
Bootstrap Estimate: For each sample, compute a statistic (e.g., mean, median,
variance, regression coefficient).
Bootstrap Distribution: Repeating the process many times creates an empirical
distribution of the statistic.
Bootstrap Error / Confidence Interval: The standard deviation (or percentile
based interval) of this bootstrap distribution serves as the bootstrap error
(standard error).
This technique is widely used in machine learning and data analysis when the theoretical
distribution of a statistic is unknown or complex.
Outline of Experiments
We will conduct the following tasks:
1. Basic Bootstrap Sampling – Resample data and compute mean/variance
distribution.
2. Bootstrap Confidence Intervals – Estimate 95% CI for population mean and
median.
3. Bootstrap Standard Error – Compare bootstrap SE vs. theoretical SE.
4. Bootstrap in Regression – Apply bootstrap to regression coefficients.
5. Visualizing Bootstrap Distributions – Histogram/Boxplot of resampled
statistics.
6. Bias Estimation – Estimate bias of mean/median using bootstrap.
7. Comparison with Normal Approximation – See when bootstrap provides better
results.
Procedure (Flow)
1. Input dataset (small sample).
2. Draw B bootstrap samples (B = 1000 or more).
3. Compute statistic of interest (mean, median, variance, regression coefficient).
4. Record all values to create the bootstrap distribution.
5. Estimate:
o Bootstrap mean/median
o Bootstrap standard error (SE)
o Confidence intervals (CI)
o Bias
6. Visualize results (histogram, density, boxplot).
7. Interpret bootstrap errors vs. actual errors.
Expected Output
A set of bootstrap resampled statistics (mean, median, variance, etc.).
Graphical representation of bootstrap distributions (histograms, boxplots).
Estimated bootstrap error and confidence intervals.
Insights into the accuracy, stability, and bias of the estimators.
Learning Outcomes
By completing this experiment, students will be able to:
✅ Understand the concept of resampling with replacement.
✅ Implement bootstrap sampling in MATLAB.
✅ Compute bootstrap estimates of mean, variance, regression coefficients.
✅ Estimate bootstrap errors and confidence intervals.
✅ Compare bootstrap-based inference with traditional parametric methods.
✅ Appreciate why bootstrap is powerful when the theoretical distribution is unknown.
Experiment 3: Bootstrap Sampling and Bootstrap Errors
Problem 1: Estimating the Mean and Confidence
Interval using Bootstrap
Aim:
To apply bootstrap sampling on a small dataset and estimate the mean, standard error,
and confidence interval (CI) using MATLAB, along with visualization of the bootstrap
distribution.
Problem Statement:
A teacher records the exam scores of 10 students as:
Student Score
1 55
2 60
3 65
4 70
5 75
6 80
7 85
8 90
9 95
10 100
Since the dataset is small, the true variability of the mean is uncertain.
Your task is to:
1. Apply bootstrap sampling (B = 1000 resamples) with replacement.
2. Compute the bootstrap mean, standard error, and 95% confidence interval of
the sample mean.
3. Plot the histogram of the bootstrap means to visualize the distribution.
Expected Output:
Bootstrap Mean ≈ 77.5 (close to original sample mean).
Bootstrap Standard Error (SE) ≈ 4–5.
95% Confidence Interval (CI) ≈ [72, 83].
A histogram of bootstrap means showing an approximately bell-shaped
distribution.
MATLAB Code:
% Problem 1: Bootstrap estimation of mean and CI
% Original data (exam scores of 10 students)
X = [55, 60, 65, 70, 75, 80, 85, 90, 95, 100];
n = length(X);
% Number of bootstrap resamples
B = 1000;
boot_means = zeros(B,1);
% Generate bootstrap samples and compute means
for b = 1:B
sample = X(randi(n, n, 1)); % resample with replacement
boot_means(b) = mean(sample); % compute mean
end
% Bootstrap estimates
boot_mean = mean(boot_means);
boot_se = std(boot_means);
CI = prctile(boot_means, [2.5 97.5]);
% Display results
disp(['Bootstrap Mean = ', num2str(boot_mean)]);
disp(['Bootstrap Standard Error = ', num2str(boot_se)]);
disp(['95% Confidence Interval = [', num2str(CI(1)), ', ',
num2str(CI(2)), ']']);
% Plot histogram of bootstrap means
histogram(boot_means, 20);
xlabel('Bootstrap Means');
ylabel('Frequency');
title('Bootstrap Distribution of the Mean');
Learning Outcome:
By solving this problem, students will learn:
✅ The concept of resampling with replacement.
✅ How bootstrap helps in quantifying uncertainty in small samples.
✅ How to calculate bootstrap mean, SE, and CI.
✅ How to visualize the bootstrap distribution in MATLAB.
Problem 2: Bootstrap Estimation of Median and
Confidence Interval
Aim:
To estimate the median and its 95% confidence interval using bootstrap resampling in
MATLAB.
Problem Statement:
Consider the monthly incomes (in ₹1000) of 12 employees:
Employee Income
1 22
2 25
3 27
4 28
5 30
6 32
7 35
8 38
9 40
10 42
11 45
12 50
The dataset is small and not normally distributed. Use bootstrap sampling (B=1000) to:
1. Estimate the bootstrap median.
2. Compute the 95% CI of the median.
3. Plot the histogram of bootstrap medians.
Expected Output:
Bootstrap Median ≈ 33–35.
95% CI ≈ [28, 40].
Histogram of bootstrap medians with central clustering.
MATLAB Code:
% Problem 2: Bootstrap estimation of median
X = [22,25,27,28,30,32,35,38,40,42,45,50];
n = length(X);
B = 1000;
boot_medians = zeros(B,1);
for b = 1:B
sample = X(randi(n, n, 1));
boot_medians(b) = median(sample);
end
boot_median = mean(boot_medians);
CI = prctile(boot_medians,[2.5 97.5]);
disp(['Bootstrap Median = ', num2str(boot_median)]);
disp(['95% Confidence Interval = [', num2str(CI(1)), ', ',
num2str(CI(2)), ']']);
histogram(boot_medians,20);
xlabel('Bootstrap Medians');
ylabel('Frequency');
title('Bootstrap Distribution of the Median');
Learning Outcome:
✅ Understand how to estimate median variability using bootstrap.
✅ Learn that median requires non-parametric confidence intervals.
Problem 3: Bootstrap Estimation of Variance and
Standard Deviation
Aim:
To estimate the variance and standard deviation of a dataset using bootstrap sampling.
Problem Statement:
The weights (in kg) of 8 packages in a warehouse are:
[15, 16, 14, 18, 17, 20, 22, 19]
Use bootstrap resampling (B = 1000) to:
1. Estimate the variance and standard deviation.
2. Compute the 95% confidence interval for the variance.
3. Plot the histogram of bootstrap variances.
Expected Output:
Bootstrap Variance ≈ 6–8.
Bootstrap Standard Deviation ≈ 2.5–3.0.
95% CI ≈ [4, 10].
MATLAB Code:
% Problem 3: Bootstrap variance and standard deviation
X = [15,16,14,18,17,20,22,19];
n = length(X);
B = 1000;
boot_vars = zeros(B,1);
for b = 1:B
sample = X(randi(n,n,1));
boot_vars(b) = var(sample,1); % variance
end
boot_variance = mean(boot_vars);
boot_sd = sqrt(boot_variance);
CI = prctile(boot_vars,[2.5 97.5]);
disp(['Bootstrap Variance = ', num2str(boot_variance)]);
disp(['Bootstrap SD = ', num2str(boot_sd)]);
disp(['95% CI of Variance = [', num2str(CI(1)), ', ', num2str(CI(2)),
']']);
histogram(boot_vars,20);
xlabel('Bootstrap Variance');
ylabel('Frequency');
title('Bootstrap Distribution of Variance');
Learning Outcome:
✅ Learn how bootstrap estimates variance & SD when sample size is small.
✅ Recognize the variability in spread estimates.
Problem 4: Bootstrap Confidence Interval for
Correlation
Aim:
To estimate the confidence interval of correlation coefficient between two variables
using bootstrap.
Problem Statement:
The heights (in cm) and weights (in kg) of 10 students are:
Height 150 152 154 155 160 162 165 168 170 172
Weight 50 52 53 54 60 62 65 66 68 70
Use bootstrap (B=1000) to:
1. Estimate the correlation coefficient (r).
2. Find the 95% CI for correlation.
3. Visualize bootstrap correlation values in a histogram.
Expected Output:
Correlation (r) ≈ 0.95.
95% CI ≈ [0.90, 0.99].
MATLAB Code:
% Problem 4: Bootstrap CI for correlation
X = [150 152 154 155 160 162 165 168 170 172];
Y = [50 52 53 54 60 62 65 66 68 70];
n = length(X);
B = 1000;
boot_corr = zeros(B,1);
for b = 1:B
idx = randi(n,n,1);
sampleX = X(idx);
sampleY = Y(idx);
boot_corr(b) = corr(sampleX',sampleY');
end
r_boot = mean(boot_corr);
CI = prctile(boot_corr,[2.5 97.5]);
disp(['Bootstrap Correlation = ', num2str(r_boot)]);
disp(['95% CI = [', num2str(CI(1)), ', ', num2str(CI(2)), ']']);
histogram(boot_corr,20);
xlabel('Bootstrap Correlation');
ylabel('Frequency');
title('Bootstrap Distribution of Correlation');
Learning Outcome:
✅ Learn how to apply bootstrap to relationship strength (correlation).
✅ See how sampling variability affects correlation estimates.
Problem 5: Bootstrap Confidence Interval for
Regression Coefficient
Aim:
To estimate the slope coefficient in simple linear regression using bootstrap sampling.
Problem Statement:
The dataset relates advertising cost (X, in ₹1000) and sales revenue (Y, in ₹10000):
Cost (X)
Sales (Y)
1 2 3 4 5 6
Using bootstrap (B=1000):
2 4 5 4 6 8
1. Fit the regression model Y = β0 + β1*X.
2. Estimate the slope coefficient β1 using bootstrap.
3. Compute the 95% CI of β1.
4. Visualize bootstrap slopes.
Expected Output:
Slope β1 ≈ 1.1–1.3.
95% CI ≈ [0.8, 1.5].
MATLAB Code:
% Problem 5: Bootstrap CI for regression slope
X = [1 2 3 4 5 6]';
Y = [2 4 5 4 6 8]';
n = length(X);
B = 1000;
boot_slopes = zeros(B,1);
for b = 1:B
idx = randi(n,n,1);
sampleX = X(idx);
sampleY = Y(idx);
coeffs = polyfit(sampleX,sampleY,1);
boot_slopes(b) = coeffs(1); % slope
end
boot_slope = mean(boot_slopes);
CI = prctile(boot_slopes,[2.5 97.5]);
disp(['Bootstrap Slope = ', num2str(boot_slope)]);
disp(['95% CI = [', num2str(CI(1)), ', ', num2str(CI(2)), ']']);
histogram(boot_slopes,20);
xlabel('Bootstrap Slopes');
ylabel('Frequency');
title('Bootstrap Distribution of Regression Slope');
Learning Outcome:
✅ Learn how to apply bootstrap in regression analysis.
✅ Understand how to compute uncertainty in slope estimates.
Problem 6: Bootstrap Confidence Interval for
Difference in Means (practice problem)
Problem Statement:
Two groups of students (Group A and Group B) take different teaching methods. We
want to estimate the difference in their average scores with a bootstrap confidence
interval.
Group A Group B
72 68
75 70
78 74
80 77
Group A Group B
74 72
Aim: Estimate a 95% bootstrap confidence interval for the difference in mean scores
between Group A and Group B.
Expected Output:
Approximate difference in means (A – B).
95% CI bounds using bootstrap resampling.
Histogram of bootstrap distribution of differences.
MATLAB Code:
A = [72, 75, 78, 80, 74];
B = [68, 70, 74, 77, 72];
diff_mean = mean(A) - mean(B);
num_boot = 1000;
boot_diffs = zeros(num_boot,1);
for i = 1:num_boot
boot_A = datasample(A, length(A));
boot_B = datasample(B, length(B));
boot_diffs(i) = mean(boot_A) - mean(boot_B);
end
CI = prctile(boot_diffs,[2.5 97.5]);
disp(['Observed Difference: ', num2str(diff_mean)]);
disp(['95% CI: ', num2str(CI(1)), ' to ', num2str(CI(2))]);
histogram(boot_diffs);
xlabel('Bootstrap Difference in Means'); ylabel('Frequency');
title('Bootstrap CI for Difference in Means');
Problem 7: Bootstrap for Skewness Estimation
Problem Statement:
We want to estimate the skewness of exam marks using bootstrap.
Marks
45
50
55
60
65
Marks
70
75
Aim: Estimate bootstrap distribution of skewness and construct a confidence interval.
Expected Output:
Observed skewness.
Bootstrap distribution of skewness.
Approximate 95% CI for skewness.
MATLAB Code:
data = [45,50,55,60,65,70,75];
obs_skew = skewness(data);
num_boot = 1000;
boot_skew = zeros(num_boot,1);
for i = 1:num_boot
boot_sample = datasample(data,length(data));
boot_skew(i) = skewness(boot_sample);
end
CI = prctile(boot_skew,[2.5 97.5]);
disp(['Observed Skewness: ', num2str(obs_skew)]);
disp(['95% CI: ', num2str(CI(1)), ' to ', num2str(CI(2))]);
histogram(boot_skew);
xlabel('Bootstrap Skewness'); ylabel('Frequency');
title('Bootstrap CI for Skewness');Editor is loading...
Leave a Comment