Home > 02-Probability > fig_2_11_demo_scatter_histogram.m

fig_2_11_demo_scatter_histogram

PURPOSE ^

% Fig. 2.11 page 56

SYNOPSIS ^

This is a script file.

DESCRIPTION ^

% Fig. 2.11 page 56
 example random number generation, scatterplot and histogram

 Wolfgang Förstner 2015
 last changes: Susanne Wenzel 09/16
 wfoerstn@uni-bonn.de, wenzel@igg.uni-bonn.de

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 %% Fig. 2.11 page 56
0002 % example random number generation, scatterplot and histogram
0003 %
0004 % Wolfgang Förstner 2015
0005 % last changes: Susanne Wenzel 09/16
0006 % wfoerstn@uni-bonn.de, wenzel@igg.uni-bonn.de
0007 
0008 close all
0009 
0010 addpath(genpath('../General-Functions/'));
0011 
0012 % get current screensize, for proper positioning of figures and set default
0013 % plot settings
0014 ss = plot_init;
0015 
0016 %% initialize random number generation by fixed seed
0017 %  (0 would use a random initialization)
0018 init_rand_seed(2);
0019 
0020 %% 1D random variable following a univariate Gaussian distribution
0021 
0022 % parameters
0023 N = 225;         % number of samples
0024 mu_y = 2;        % mean
0025 sigma_y = 0.25;   % standarddeviation
0026 
0027 %% a) generate sample
0028 
0029 % generate standard normal samples ~N(0,1)
0030 x = randn(N,1);
0031 % transform to desired Gaussian N(mu,sigma)
0032 y = mu_y*ones(N,1)+sigma_y*x;
0033 
0034 % plot samples
0035 figure('name','Sample','color','w','Position',[0.1*ss(1),0.55*ss(2),0.35*ss(1),0.35*ss(2)]); hold on
0036 for n=1:N
0037     plot([y(n),y(n)],[0,0.5],'-k','LineWidth',1)
0038 end
0039 plot([mu_y-4*sigma_y,mu_y+4*sigma_y],[0,0],'-k','LineWidth',1);
0040 plot([0,0],[0,1],'-k','LineWidth',1);
0041 xlim([mu_y-5*sigma_y,mu_y+5*sigma_y]); ylim([-0.1,1.1]);
0042 xlabel('$y$');
0043 % title(['Fig. 2.11a A sample of $N = 255$ normally distributed random variables $y\sim N(2,0.25)$'])
0044 title(['Fig. 2.11a A sample of $N = ',num2str(N),'$ normally distributed random variables $y\sim N('...
0045     ,num2str(mu_y),',',num2str(sigma_y),')$'])
0046 
0047 %% b) plot histogram (number of bins = sqrt(N))
0048 figure('name','Histogram','color','w','Position',[0.55*ss(1),0.55*ss(2),0.35*ss(1),0.35*ss(2)]); hold on;
0049 
0050 N_bin = floor(sqrt(N));       % number of bins
0051 [NN,r]=hist(y,N_bin);         % calculate histogram
0052 bar(r,NN)                     % plot histogram
0053 
0054 % plot expected Gaussian / expected number of samples
0055 range = abs(r(N_bin)-r(1))*N_bin/(N_bin-1);     % range of histogram
0056 plot(r,N_bin*range*normpdf(r,mu_y,sigma_y),'-r','LineWidth',4);     
0057 xlim([mu_y-5*sigma_y,mu_y+5*sigma_y]);ylim([0,max(NN)*1.1]);
0058 xlabel('$y$');ylabel('$\sim p_y(y)$')
0059 title(['Fig. 2.11b Histogram of the sample with $',num2str(N_bin),'$ bins, overlayed with its probability density.'])
0060 
0061 %% 2d random vector with mean [0,0]
0062 
0063 % parameters
0064 sigma_x = 4.9;   % std in x
0065 sigma_y = 3.2;   % std in y
0066 rho = 0.7;       % correlation
0067 N = 500;         % number of samples
0068 
0069 % covariance matrix
0070 C = [sigma_x^2 sigma_x*sigma_y*rho; sigma_x*sigma_y*rho sigma_y^2];
0071 % generate sample
0072 x = rand_gauss([0,0]',C,N)';
0073 
0074 % plot samples with standard ellipse
0075 figure('name','2D Sample','color','w','Position',[0.25*ss(1),0.1*ss(2),0.35*ss(1),0.35*ss(2)]); hold on
0076 % standard ellipse
0077 plot_ellipse(struct('mean',[0;0],'cov',C),'-k');
0078 % threefold standard ellipse
0079 plot_ellipse(struct('mean',[0;0],'cov',9*C),'-g');  
0080 % x-axis
0081 plot([-4*sigma_x,+4*sigma_x],[0,0],'-k')
0082 % y-axis
0083 plot([0,0],[-4*sigma_y,+4*sigma_y],'-k')
0084 % samples
0085 plot(x(:,1),x(:,2),'.k')
0086 
0087 xlim([-5*sigma_x,+5*sigma_x]);ylim([-5*sigma_y,+5*sigma_y])
0088 axis equal
0089 title({'Fig. 2.11c Sample of 2d random vector','standard ellipse (black), threefold standard ellipse (green)'})

Generated on Sat 21-Jul-2018 20:56:10 by m2html © 2005