The Beta distribution

The four parameter beta distribution is a two-parameter beta distribution over (0,1) transplated to the interval (l,r). This driver will show the density of this distribution in a few different cases.

Contents

Setup the experiment

This experiment should be run from the rapr/experiments/datastats directory

cwd = pwd;
dirtail = 'experiments/density';
if strcmp(cwd(end-length(dirtail)+1:end),dirtail) == 0
    warning('rapr:dir','%s should be executed from rapr/%s\n',mfilename,dirtail);
end
addpath('../../matlab'); % ensure we have the RAPr codes available
params = [
      0,   0, 0.6, 0.9   % uniform [0,6,0.9]
      2,  16,   0,   1   % skewed right
      1,   1, 0.1, 0.9   % equicentric
   -0.5,-0.5, 0.2, 0.7   % bi-modal
];

Plot beta distributions

The following code plots 5 beta distributions.

s={'-.','-','--',':'}; c={[1 0.5 0.5],[0.5 0.5 1],[0 0.75 0.5],[1 0 0.25]};
figure(1); clf; hold on; lgn={}; hs=[];to={'FontSize',14};
set(gcf,'Color',[1 1 1]); lws={'LineWidth',1};
for i=1:size(params,1)
  a=params(i,1);b=params(i,2); l=params(i,3); r=params(i,4);
  f = @(x) (1./(r-l))*betapdf((x-l)/(r-l),b+1,a+1);
  ls={'LineStyle',s{i},'Color',c{i}};[x,y]=fplot(f,[l,r]);h=plot(x,y,ls{:});
  line([l l],[0,f(l)],ls{:},lws{:}); line([r r],[0,f(r)],ls{:});
  lgn{end+1}=sprintf('Beta(%g,%g,%g,%g)',a,b,l,r); hs(end+1)=h;
end
xlim([0 1]); ylim([0,7]);axis off; pbaspect([2,1,1]); line([0 1],[0 0],'Color',[0 0 0]);
legend(hs,lgn,'Location','NorthWest'); text(0,-0.3,'0',to{:});text(1,-0.3,'1',to{:});
print(gcf,'beta-density.eps','-depsc2');

Plot pdfs for these distributions

This code is based on the driver_pagerank_pdfs.m file which contains more explicit comments on what each step does. See that file for more information.

load('../../data/example-small.mat');
n=size(P,1);

N=18; pdata=[]; f=[]; xi=[];

for di=1:size(params,1)
    a=params(di,1);b=params(di,2); l=params(di,3); r=params(di,4);
    d=alphadist('beta',a,b,l,r); eA=d.mf(1); eA=eA(end);
    [ex stdx xw X] = gqrapr(P,N,d,'direct'); xeA = inoutpr(P,eA);
    s=rand('state');rand('state',10); % ensure consistent results
    nsamples = 10000; betasamples = d.rnd(nsamples);rand('state',s);
    for k=1:length(ex)
        % interpolate and evaluate at betasamples
        prsamples = barylag([xw(:,1) X(k,:)'], betasamples);
        [f(:,k),xi(:,k)] = ksdensity(prsamples); % estimate the density
    end
    pdata(di).f=f; pdata(di).xi=xi; pdata(di).xeA=xeA; pdata(di).ex=ex;
end
xlims=[0 0.5]; ylims=[0 max(max([pdata.f]))+1];
for di=1:size(params,1);
    figure(di+1); clf; tp={'fontsize',16};
    f=pdata(di).f; xi=pdata(di).xi;xeA=pdata(di).xeA;ex=pdata(di).ex;
    for i=1:length(ex),
        subplot(6,1,i); plot(xi(:,i),f(:,i),'k-',lws{:});
        xlim(xlims); ylim(ylims); axis off;
        line([xeA(i),xeA(i)],[-1 ylims(2)],'Marker','o','Color',[0.5 0.5 1],lws{:});
        line([ex(i),ex(i)],[-1 ylims(2)],'Marker','*','Color',[1 0.5 0.5],lws{:});
        line(xlims, [0 0],'Color',[0 0 0]); text(-0.01,mean(ylims),sprintf('x_%i',i),tp{:});
        if i==6, h=text(0,-8,'0',tp{:});h=text(0.5,-8,'0.5',tp{:});end,
        pbaspect([8 1 1]);
    end
    set(gcf,'Color',[1 1 1]); print(gcf,sprintf('pagerank-pdfs-%i.eps',di),'-depsc2');
end
solved pagerank(a=0.7500) in    83 multiplies to 9.621054e-13 tolerance
solved pagerank(a=0.8500) in   133 multiplies to 9.358972e-13 tolerance
solved pagerank(a=0.5000) in    36 multiplies to 9.967582e-13 tolerance
solved pagerank(a=0.4500) in    32 multiplies to 4.928419e-13 tolerance