-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcdf.m
42 lines (37 loc) · 833 Bytes
/
cdf.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
% evaluate cumulative distribution function inferred from D for values q
function P = cdf(D,q)
D = sort(D);
nd = length(D);
nq = length(q);
L = zeros(nq, 1);
R = zeros(nq, 1);
% for each query
for k = 1:nq
% binary search for query's "neighbours"
l = 1;
r = nd;
while r - l > 1
m = floor((l+r)/2);
if D(m) > q(k) && D(m) ~= D(l)
r = m;
else
l = m;
end
end
while D(r) == D(l)
l = l - 1;
end
% reach last occurrence of right neighbour
while r < nd && D(r) == D(r+1)
r = r + 1;
end
L(k) = l;
R(k) = r;
end
% vectorized interpolation
d = (q-D(L))./(D(R)-D(L));
P = (L + (R - L).*d)/nd;
% trim out of range interpolations
P = P .* (P > 0); % if p<0 then p=0
P = P - (P - 1).*(P > 1); %if p>1 then p=1
end