-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathpdfCdfRank.m
executable file
·75 lines (60 loc) · 1.96 KB
/
pdfCdfRank.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
% Compute the pdf, cdf and rank distributions for a sequence of numbers.
%
% INPUTS: sequence of values: x, (1xn), 'plt' - 'on' or 'off'
% bin - bin size, default is [], then it gets selected
% automatically
% OUTPUTS: pdf, cdf and rank distribution values, plot is optional
%
% Note: pdf = frequency distribution, cdf = cumulative frequency,
% rank = log-log scale of the sorted sequence
% Last updated: November 24 2012
function [xpdf, ypdf, xcdf, ycdf, logk, logx] = pdfCdfRank(x, plt='off', bin = [])
xx = unique(x);
if length(bin) == 0;
bin = length(xx) / 5;
end
for ii = 1:numel(xx)
xcdf(ii) = xx(ii);
ycdf(ii) = length(find(x <= xx(ii))) / numel(x);
% how many x's fall in the interval [xx(ii)-0.5*numel(xx)/bin,xx(ii)+0.5*numel(xx)/bin]
xpdf(ii) = xx(ii);
ypdf(ii) = length(find(abs(xx(ii) - x) <= 0.5 * numel(xx) / bin)) / numel(x);
end
x=-sort(-x);
logk = log(1:length(x));
logx = log(x);
if strcmp(plt, 'on')
set(gcf, 'color', [1, 1, 1])
subplot(2, 2, 1)
hist(x, bin, 'facecolor', [0.5 0.5 0.5])
title('histogram')
subplot(2, 2, 3)
plot(xpdf, ypdf, 'k.')
title('pdf')
axis('tight')
subplot(2, 2, 2)
plot(xcdf, ycdf, 'k.')
title('cdf')
axis('tight')
subplot(2, 2, 4)
plot(logk, logx, 'k.')
title('rank')
axis('tight')
end
%!test
%!shared adj
%! adj = randomGraph(randi(30)+30,0.2);
%! [xp,yp,xc,yc,lk,lx] = pdfCdfRank(degrees(adj),'off');
%! assert(length(xp),length(xc))
%! assert(length(xp),length(yp))
%! assert(length(yp),length(yc))
%! assert(length(lk),length(lx))
%!test
%! [xp,yp,xc,yc,lk,lx] = pdfCdfRank(degrees(adj),'off', bin=5);
%! assert(length(xp),length(xc))
%! assert(length(xp),length(yp))
%! assert(length(yp),length(yc))
%! assert(length(lk),length(lx))
%!demo
%! adj = randomGraph(1000, 0.2);
%! pdfCdfRank(degrees(adj),'on');