楼主: Lisrelchen
1871 11

[专题汇总]模式识别 [推广有奖]

11
Lisrelchen 发表于 2016-3-23 08:39:02
Parzen Window Density Estimation
  1. close all; clc; clear;

  2. rand('seed',0);
  3. randn('seed',0);

  4. x_truth = linspace( -1, 3, 1000 );
  5. indsGT  = x_truth>0.0;
  6. indsLT  = x_truth<2.0;
  7. inds    = find( indsGT & indsLT );
  8. p_truth = zeros( 1, 1000 );
  9. p_truth(inds) = 1/2;

  10. % Generate some data from the true distribution:
  11. %


  12. N_values = [ 32, 256, 5000 ];
  13. h_values = [ 0.05, 0.2 ];

  14. n_hs = length(h_values);
  15. colors = 'rb';
  16. for ni = 1:length(N_values)
  17.   N = N_values(ni);
  18.   X = unifrnd( 0, 2, N, 1 );

  19.   figure;
  20.   pt = plot( x_truth, p_truth, '-g' ); hold on;
  21.   ph = zeros(n_hs,1); % handles for each Parzen density estimation
  22.   
  23.   for hi = 1:length(h_values)
  24.     h = h_values(hi);     

  25.     % For each possible h plot the Parzen approximation on top of the truth:
  26.     %   
  27.     x_grid = linspace(-1,3,100); % where we will sample our density \hat{p}

  28.     p_hat = [];
  29.     for xi = 1:length(x_grid)
  30.       x = x_grid(xi);
  31.       p = mean( normpdf( ( repmat( x, N, 1 ) - X ) / h ) )/h;

  32.       p_hat = [ p_hat, p ];  
  33.     end

  34.     ph(hi) = plot( x_grid, p_hat, ['-',colors(hi)] );
  35.    
  36.   end
  37.   
  38.   legend( [pt,ph(1),ph(2)], {'truth',['N=',num2str(N),'; h=0.05'],['N=',num2str(N), '; h=0.2']} );
  39.   fn = ['chap_2_prob_31_N_',num2str(N),'.eps'];
  40.   saveas(gcf,['../../WriteUp/Graphics/Chapter2/',fn],'epsc');
  41.   
  42. end
  43. http://waxworksmath.com/Authors/N_Z/Theodoridis/WWW/chapter_2.html
复制代码

12
Lisrelchen 发表于 2016-3-23 08:40:46
K Nearest Neighbor Density Estimation using Matlab
  1. close all; clc; clear;

  2. rand('seed',0);
  3. randn('seed',0);

  4. x_truth = linspace( -1, 3, 1000 );
  5. indsGT  = x_truth>0.0;
  6. indsLT  = x_truth<2.0;
  7. inds    = find( indsGT & indsLT );
  8. p_truth = zeros( 1, 1000 );
  9. p_truth(inds) = 1/2;

  10. % Generate some data from the true distribution:
  11. %
  12. N = 5000;
  13. X = unifrnd( 0, 2, N, 1 );

  14. k_values = [ 32, 64, 256 ];

  15. n_ks = length(k_values);
  16. colors = 'rbk';

  17. figure;
  18. pt = plot( x_truth, p_truth, '-g' ); hold on;
  19. ph = zeros(n_ks,1); % handles for each Parzen density estimation

  20. for ki = 1:length(k_values)
  21.   k = k_values(ki);     
  22.   
  23.   % For each possible h plot the K-NN density approximation on top of the truth:
  24.   %   
  25.   x_grid = linspace(0,2,100); % where we will sample our density \hat{p}
  26.   
  27.   p_hat = [];
  28.   for xi = 1:length(x_grid)
  29.     x = x_grid(xi);
  30.    
  31.     % find the K-NN of x and the volume around them:
  32.     %
  33.     [dum,inds] = sort(abs(X - x));
  34.     nn     = inds(1:k);
  35.     xnn    = X(nn);
  36.     V_of_x = max(xnn) - min(xnn);
  37.    
  38.     p = k/(N*V_of_x);
  39.    
  40.     p_hat = [ p_hat, p ];  
  41.   end
  42.   
  43.   ph(ki) = plot( x_grid, p_hat, ['-',colors(ki)] );
  44.   
  45. end

  46. legend( [pt,ph(1),ph(2),ph(3)], {'truth','k=32','k=64','k=256'} );
  47. fn = ['chap_2_prob_32.eps'];
  48. saveas(gcf,['../../WriteUp/Graphics/Chapter2/',fn],'epsc');
  49. http://waxworksmath.com/Authors/N_Z/Theodoridis/WWW/chapter_2.html
复制代码

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2026-3-7 17:57