View difference between Paste ID: mbapsz4c and 6748SLib
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
%% draw bootstrap samples
22
x_noise_boot = x_noise( randi( n_data, n_data, n_boot ) );
23
x_boot = bsxfun( @plus, x_noise_boot, x_true );
24
25
%% fit bootstrap samples
26
x_all = [x, x_boot];
27
p_est = zeros(2, n_boot);
28
for i = 1:n_boot
29-
    p_est(:, i) = polyfit( t, x_all(:, i), 1 );
29+
    p_est(:, i) = polyfit( t, x_all(:, i), 1 ); % the actual objective function would be minimizing the
30
						% sum of squares with x_true, but this is hard to incorporate
31
						% in this example since my actual application isn't a fit per se,
32
						% but a model that sort of de-noises x (or x_boot).
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')