-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPlotScaling.m
96 lines (73 loc) · 2.52 KB
/
PlotScaling.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
% Test relative Entropy scaling for two dirichlet distributions with
% respect to the flat distribution
%
% [email protected], 2019
clear; close all;
d_vals = [1:12];
N_MC = 5000;
% KL-divergence (relative entropy) base-2
KL_pq = @(p,q) sum(p.*log2(p./q));
% Alpha-values
alpha_vals = [0.1 1];
% Values
klvals = zeros(length(alpha_vals), length(d_vals), N_MC);
klvals2 = zeros(length(alpha_vals), length(d_vals), N_MC);
for i = 1:length(d_vals) % Over dimensions
d = d_vals(i);
fprintf('Simulation %d/%d (d = %d) \n', i, length(d_vals), d);
% Flat distribution
p = ones(2^d-1,1)/(2^d-1);
% Loop MC runs
for k = 1:N_MC
for a = 1:length(alpha_vals)
% Random distribution from Dirichlet
alpha = ones(1,2^d-1) * alpha_vals(a);
q = dirnd(alpha,1)';
kl = KL_pq(p, q); kl(isnan(kl) | isinf(kl)) = 0;
klvals(a,i,k) = kl;
% Reverse p and q
kl = KL_pq(q, p); kl(isnan(kl) | isinf(kl)) = 0;
klvals2(a,i,k) = kl;
end
end
end
%%
figure;
for i = 1:length(alpha_vals)
subplot(1,2,i);
for z = 1:1
if (z == 1)
values = squeeze(klvals(i,:,:));
marker = '-';
else
values = squeeze(klvals2(i,:,:));
marker = '--';
end
errorbar(d_vals, median(values,2), ...
median(values,2)' - prctile(values', 95), ...
median(values,2)' - prctile(values', 5), ['r' marker]);
hold on;
errorbar(d_vals, median(values,2), ...
median(values,2)' - prctile(values', 84), ...
median(values,2)' - prctile(values', 16), ['ks' marker]);
axis square; axis tight;
hold on;
xlabel('$N$','interpreter','latex');
if ( z==1)
ylabel('KL$(p|q)$','interpreter','latex');
else
ylabel('KL$(q|p)$','interpreter','latex');
end
title(sprintf('$p_c = 1/n, q \\sim D(\\alpha = %0.1f)$', ...
alpha_vals(i)),'interpreter','latex');
end
% Calculate entropies of the flat distribution
% H = zeros(length(d_vals),1);
% for j = 1:length(d_vals)
% p = ones(2^d_vals(j)-1,1); p = p / sum(p);
% H(j) = -sum(p.*log2(p));
% end
% plot(d_vals, H, 'r--');
end
%pdfcrop(0.9, 0.6);
%eval(sprintf('print -dpdf ../figs/KL_test.pdf'));