0
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
1 % ECDF Compute empirical cumulative distribution function
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
3 % Compute empirical cumulative distribution function for a series of data
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
4 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
5 % CALL
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
6 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
7 % [F,X] = ecdf(Y);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
8 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
9 % INPUT
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
10 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
11 % - Y, a data series
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
12 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
13 % L Ferraioli 09-03-2011
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
14 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
15 % $Id: ecdf.m,v 1.2 2011/03/09 11:40:27 luigi Exp $
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
16 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
17 function [F,X] = ecdf(Y)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
18
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
19 % sort input data
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
20 X = sort(Y);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
21
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
22 % define variable for cumulative sum
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
23 sw = ones(size(X));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
24 n = numel(sw);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
25
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
26 jj = 2;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
27 idd = false(size(sw));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
28 % Fi is calculated practically as Fi = ni/n where ni is the position of
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
29 % the ith element of the sorted input array. If two or more elements are
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
30 % equal then only the ni corresponding to the last one is considered and
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
31 % the other elements are removed. ni is obtained by a cumulative sum of
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
32 % an arrays of ones. If k elements are equal they are removed and
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
33 % substituted by a single k.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
34 while jj < n+1
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
35 if X(jj) ~= X(jj-1); % if successive elements are different we can go on
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
36 jj = jj+1;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
37 else % if some elements are equal than we have to count the equal elements
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
38 jt = jj+1;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
39 while jt<=n+1 && X(jt-1)==X(jj-1) % counting equal elements
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
40 jt = jt + 1;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
41 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
42
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
43 for ji=jj:jt-2
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
44 idd(ji-1) = true; % make the assignement for successive removal of elements
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
45 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
46 t = jt-jj;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
47 % set the value corresponding to the number of equal elements in
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
48 % correspondence to the last element of the series of eqials
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
49 sw(jt-2) = t;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
50
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
51 jj = jt;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
52 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
53 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
54
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
55 % remove unwanted elements corresponding to duplicates
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
56 sw(idd) = [];
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
57
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
58 % calculate cumulative distribution by cumulative sum
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
59 F = cumsum(sw)./n;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
60
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
61 % remove elements corresponding to replicas
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
62 X(idd) = [];
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
63
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
64 end |