-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFindEntropy.m
27 lines (23 loc) · 1023 Bytes
/
FindEntropy.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
function [S,dS] = FindEntropy(data,sigma,x)
% finds the entropy and its gradient
% data - matrix with data. each row corresponds to one data point.
% sigma - scalar, the parameter that appears in the Parzen function.
% x - matrix with points where the entropy will be evaluated. Each row is a point. If x is empty, then x = data.
% S - the entropy. It is a column vector with size(x,1) elements.
% dS - the gradient of the entropy at the points x. It has the same size as x.
if isempty(x)
x = data;
end
S = zeros(size(x,1),1);
dS = zeros(size(x));
for ii=1:size(x,1)
difference = (repmat(x(ii,:),size(data,1),1) - data);
squaredDifference = sum(difference.^2,2);
gaussian = exp(-(1/(2*sigma^2))*squaredDifference);
laplacian = sum(gaussian.*squaredDifference);
parzen = sum(gaussian);
V = (1/(2*sigma^2))*laplacian/parzen;
S(ii) = V + log(abs(parzen));
dS(ii,:) = (1/parzen)*sum(difference.*repmat(gaussian,1,size(data,2)).*(2*sigma^2*V-repmat(squaredDifference,1,size(data,2))));
end
end