added section on cumulative densities

This commit is contained in:
2017-11-25 18:46:03 +01:00
parent 12a417d6bc
commit 6c95ec7256
13 changed files with 381 additions and 76 deletions

View File

@@ -0,0 +1,18 @@
x = randn(200, 1); % generate some data
xs = sort(x); % sort the data
cdf = [1:length(x)]/length(x); % cumulative
plot(xs, cdf);
hold on;
dx = 0.01;
xx = [-4:dx:4]; % x-values for Gaussian pdf
gauss = exp(-0.5*xx.^2)/sqrt(2.0*pi); % Gaussian pdf
gausscdf = cumsum(gauss)*dx;
plot(xx, gausscdf);
hold off;
printf('data : probability of x<-1: %.2f\n', cdf(xs<-1.0)(end))
printf('gauss: probability of x<-1: %.2f\n', gausscdf(xx<-1.0)(end))
printf('\n')
printf('data : 5%% percentile at %.2f\n', xs(cdf<0.05)(end))
printf('gauss: 5%% percentile at %.2f\n', xx(gausscdf<0.05)(end))

View File

@@ -1,11 +1,11 @@
x = randn(100, 1); % generate some data
db1 = 2; % large bin width
db2 = 0.5; % small bin width
bins1 = -4:db1:4; % large bins
bins2 = -4:db2:4; % small bins
[h1,b1] = hist(x, bins1);
[h2,b2] = hist(x, bins2);
bins1 = -4:2:4; % large bins
bins2 = -4:0.5:4; % small bins
[h1,b1] = hist(x,bins1);
[h2,b2] = hist(x,bins2);
subplot( 1, 2, 1 );
bar(b1, h1)
hold on
bar(b2, h2, 'facecolor', 'r' )

View File

@@ -1,9 +1,9 @@
hn1 = h1/sum(h1)/db1;
hn2 = h2/sum(h2)/db2;
subplot( 1, 2, 2 )
bar(b1,hn1)
bar(b1, hn1)
hold on
bar(b2,hn2, 'facecolor', 'r' )
bar(b2, hn2, 'facecolor', 'r' )
xlabel('x')
ylabel('Probability density')
hold off

View File

@@ -0,0 +1,42 @@
data = randn(100, 1); % generate some data
sigma = 0.2; % standard deviation of Gaussian kernel
xmin = -4.0; % minimum x value for kernel density
xmax = 4.0; % maximum x value for kernel density
dx = 0.05*sigma; % step size for kernel density
xg = [-4.0*sigma:dx:4.0*sigma]; % x-axis for single Gaussian kernel
% single Gaussian kernel:
kernel = exp(-0.5*(xg/sigma).^2)/sqrt(2.0*pi)/sigma;
ng = (length(kernel)-1)/2; % half the length of the Gaussian
x = [xmin:dx:xmax+0.5*dx]; % x-axis for kernel density
kd = zeros(1, length(x)); % vector for kernel density
for i = 1:length(data) % for every data value ...
xd = data(i);
% index of data value in kernel density vector:
inx = round((xd-xmin)/dx)+1;
% start index for Gaussian in kernel density vector:
k0 = inx-ng;
% end index for Gaussian in kernel density vector:
k1 = inx+ng;
g0 = 1; % start index in Gaussian
g1 = length(kernel); % end index in Gaussian
% check whether left side of Gaussian extends below xmin:
if inx < ng+1
% adjust start indices accordingly:
k0 = 1;
g0 = ng-inx+1;
end
% check whether right side of Gaussian extends above xmax:
if inx > length(kd)-ng
% adjust end indices accordingly:
k1 = length(kd);
g1 = length(kernel)-(inx+ng-length(kd));
end
% add Gaussian on kernel density:
kd(k0:k1) = kd(k0:k1) + kernel(g0:g1);
end
kd /= length(data); % normalize by number of data points
% plot kernel density:
plot(x, kd)
xlabel('x')
ylabel('Probability density')