-
Notifications
You must be signed in to change notification settings - Fork 1
/
hellingerDist.m
30 lines (25 loc) · 906 Bytes
/
hellingerDist.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
% Function to calculate Hellinger distance based on histograms
function distance = hellingerDist(x, y)
% x = x(~isnan(x));
% y = y(~isnan(y));
% Return maximum distance if inputs are empty or invalid
minV = min([x(:); y(:)]);
maxV = max([x(:); y(:)]);
if isempty(x) || isempty(y) || minV >= maxV
distance = 1;
return;
end
% Define number of bins and edges
numBins = 10;
edges = linspace(minV, maxV, numBins + 1);
% Compute and normalize histograms
histX = histcounts(x, edges);
histX = histX./sum(histX);
histY = histcounts(y, edges);
histY = histY./sum(histY);
% histX = histcounts(x, edges, 'Normalization','probability');
% histY = histcounts(y, edges, 'Normalization', 'probability');
% Compute Hellinger distance
sqrtDiff = sqrt(histX) - sqrt(histY);
distance = sqrt(sum(sqrtDiff.^2)) / sqrt(2);
end