comparison m-toolbox/classes/+utils/@math/ecdf.m @ 0:f0afece42f48

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