Entropy of a RV:Entropy of a binary random variable
% ********************************************************************* % ({'Entropy of a binary random variable','\bf(C) [email protected]'}) % *********************************************************************
clear all variables; clc;
p = linspace(0+1e-3,1-1e-3,20)'; yi = - p.*log2 (p); byi = - (1-p).*log2 (1-p); logt= -log2 (p);
Hx = -p.*log2 (p) - (1-p).*log2(1-p);
togglefig('Entropy');clf
hold on
plot(p,logt,'k-*','LineWidth',2,'MarkerSize',10)
plot(p,yi, 'b-.','LineWidth',3,'MarkerSize',10)
plot(p,byi, 'r-.','LineWidth',3,'MarkerSize',10)
plot(p,Hx, 'm-o','LineWidth',2,'MarkerSize',10)
% text(0.5,0.6,'\bft.*log(t)','fontsize',12);
xlabel('\bfp','fontsize',12)
%text(0.5,0.9,['Hx = ',num2str(sum(Hx),'%.4f')]);
hold off
legend({'-log(p)','-p*log(p)','-(1-p)*log(1-p)','H(x)=-plog(p)-(1-p)log(1-p)'},'fontsize',12)
set(gca,'ylim',[0,1.5],'xlim',[0,1.5])
title({'Entropy of a binary random variable','\bf(C)
if 1 print('-depsc2','eps\entropy_tlogt') print('-djpeg','eps\entropy_tlogt') end