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; |
| 24 | + | x_best_fit = polyval( p_est(:, 1), t ); |
| 25 | x_residuals = x_best_fit - x; | |
| 26 | ||
| 27 | %% draw bootstrap samples | |
| 28 | - | x_boot = bsxfun( @plus, x_res_boot, x_true ); |
| 28 | + | |
| 29 | x_boot = bsxfun( @plus, x_res_boot, x_best_fit ); | |
| 30 | ||
| 31 | %% fit bootstrap samples | |
| 32 | for i = 2:n_boot | |
| 33 | p_est(:, i) = polyfit( t, x_boot(:, i), 1 ); | |
| 34 | end | |
| 35 | ||
| 36 | %% plot | |
| 37 | figure; | |
| 38 | subplot(2,2,1:2) | |
| 39 | hold on | |
| 40 | for i = 1:n_boot | |
| 41 | plot( t, polyval( p_est(:, i), t ), 'r-') | |
| 42 | end | |
| 43 | plot(t, x, 'k.') | |
| 44 | hold off | |
| 45 | title('original noisy data and bootstrapped fit')
| |
| 46 | subplot(2,2,3) | |
| 47 | hist( p_est(2, :) ) | |
| 48 | title('p0')
| |
| 49 | subplot(2,2,4) | |
| 50 | hist( p_est(1, :) ) | |
| 51 | title('p1') |