View difference between Paste ID: wrFkecuN and HZn8U5Am
SHOW: | | - or go back to the newest paste.
1
%% clear workspace
2
close all
3
clear all
4
clc
5
6
%% parameters
7
n_boot = 1e2;
8
n_data = 20;
9
sigma = 0.5;
10
p0 = 5;
11
p1 = 2;
12
13
%% generate data
14
t = (1:n_data)';
15
x_true = p0 + p1 * t;
16
17
%% generate noise
18
x_noise =  sigma * randn(n_data,1);
19
x = x_true + x_noise;
20
21
%% fit original data
22
p_est = zeros(2, n_boot);
23
p_est(:, 1) = polyfit( t, x, 1);
24
x_residuals = polyval( p_est(:, 1), t ) - x;
25
26
%% draw bootstrap samples
27-
x_noise_boot = x_noise( randi( n_data, n_data, n_boot ) );
27+
x_res_boot = x_residuals( randi( n_data, n_data, n_boot ) );
28-
x_boot = bsxfun( @plus, x_noise_boot, x_true );
28+
x_boot = bsxfun( @plus, x_res_boot, x_true );
29
30
%% fit bootstrap samples
31
for i = 2:n_boot
32
    p_est(:, i) = polyfit( t, x_boot(:, i), 1 );
33
end
34
35
%% plot
36
figure;
37
subplot(2,2,1:2)
38
hold on
39
for i = 1:n_boot
40
    plot( t, polyval( p_est(:, i), t ), 'r-')
41
end
42
plot(t, x, 'k.')
43
hold off
44
title('original noisy data and bootstrapped fit')
45
subplot(2,2,3)
46
hist( p_est(2, :) )
47
title('p0')
48
subplot(2,2,4)
49
hist( p_est(1, :) )
50
title('p1')