Advertisement
Guest User

Untitled

a guest
Aug 19th, 2013
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 0.98 KB | None | 0 0
  1. %% clear workspace
  2. close all
  3. clear all
  4. clc
  5.  
  6. %% parameters
  7. n_boot = 1e3;
  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 and draw bootstrap samples#
  22. p_est = zeros(2, n_boot);
  23. for i = 1:n_boot
  24.     p_est(:, i) = polyfit( t, x, 1); % estimated parameters
  25.     x_best_fit = polyval( p_est(:, 1), t ); % fitted line
  26.     x_residuals = x_best_fit - x; % residuals of i-th fit
  27.     x_res_boot = x_residuals( randi(n_data, n_data, 1) ); % resample residuals with replacement
  28.     x = x_best_fit + x_res_boot; % add resampled residuals to i-th fitted line
  29. end
  30.  
  31. %% plot
  32. figure;
  33. subplot(2,2,1:2)
  34. hold on
  35. for i = 1:n_boot
  36.     plot( t, polyval( p_est(:, i), t ), 'r-')
  37. end
  38. plot(t, x, 'k.')
  39. hold off
  40. title('original noisy data and bootstrapped fit')
  41. subplot(2,2,3)
  42. hist( p_est(2, :) )
  43. title('p0')
  44. subplot(2,2,4)
  45. hist( p_est(1, :) )
  46. title('p1')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement