matlab - Generate and plot the empirical joint pdf and CDF -


given pair of 2 variables (x,y), how can generate , plot empirical joint pdf , cdf in vanilla matlab (no toolboxes)?

original answer (matlab r2015a or lower)

the data are:

  • the random variables x, y: defined vectors of samples x, y.
  • the bin edges @ x, y axes: defined vectors x_axis, y_axis. edges must increasing, need not uniformly spaced.

the resulting pdf , cdf defined @ centers of rectangles determined x , y edges.

to plot results in 3d, use surf(...) instead of imagesc(...).

clear  %// data (example): x = randn(1,1e5); %// random variables. y = randn(1,1e5);  x_axis = -3:.2:3; %// define edges of bins x axis. column vector y_axis = -3:.2:3; %// same y axis  %// compute corners of 2d-bins: [x_mesh_upper,y_mesh_upper] = meshgrid(x_axis(2:end),y_axis(2:end)); [x_mesh_lower,y_mesh_lower] = meshgrid(x_axis(1:end-1),y_axis(1:end-1));  %// compute centers of 1d-bins: x_centers = (x_axis(2:end)+x_axis(1:end-1))/2; y_centers = (y_axis(2:end)+y_axis(1:end-1))/2;  %// compute pdf: pdf = mean( bsxfun(@le, x(:), x_mesh_upper(:).') ...     & bsxfun(@gt, x(:), x_mesh_lower(:).') ...     & bsxfun(@le, y(:), y_mesh_upper(:).') ...     & bsxfun(@gt, y(:), y_mesh_lower(:).') ); pdf = reshape(pdf,length(x_axis)-1,length(y_axis)-1); %// pdf values @ %// grid points defined x_centers, y_centers pdf = pdf ./ (y_mesh_upper-y_mesh_lower) ./ (x_mesh_upper-x_mesh_lower); %// normalize pdf unit integral  %// compute cdf: cdf = mean( bsxfun(@le, x(:), x_mesh_upper(:).') ...     & bsxfun(@le, y(:), y_mesh_upper(:).') ); cdf = reshape(cdf,length(x_axis)-1,length(y_axis)-1);  %// plot pdf figure imagesc(x_centers,y_centers,pdf) axis xy axis equal colorbar title 'pdf'  %// plot cdf figure imagesc(x_centers,y_centers,cdf) axis xy axis equal colorbar title 'cdf' 

pdf cdf

edited answer (matlab r2015b or higher)

matlab r2015b includes histogram2 function work. automatically normalization obtain pdf (given appropriate input flag), or cdf.

using same example above,

clear  %// data (example): x = randn(1,1e5); % random variables. y = randn(1,1e5);  x_axis = -3:.2:3; % define edges of bins x axis. column vector y_axis = -3:.2:3; % same y axis  %// compute , plot pdf figure histogram2(x, y, x_axis, y_axis, 'normalization', 'pdf')  %// compute , plot cdf figure histogram2(x, y, x_axis, y_axis, 'normalization', 'cdf') 

pdf, r2010b cdf, r2015b


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

IIS->Tomcat Redirect: multiple worker with default -