
% Code adapted from:
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Demonstration code for Bayes Rule: A Tutorial Introduction to Bayesian Analysis, JV Stone.
% Copyright: 2012, JV Stone, Psychology Department, Sheffield University, Sheffield, England.
% The MatLab code below is version 7.5. This code can be downloaded from http://jim-stone.staff.shef.ac.uk/BayesBook/Matlab.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%
% Matlab code for plotting probability of head/tail outcomes 
% as a function of bias b.
clear all;
% set random number seeds.
s=999; rand('seed',s); randn('seed',s);
pthresh=0.05; % critical p value for rejecting null hypothesis
% set coin bias
theta0=0.75;
%theta0=0.6;

% Make vector of possible bias values
inc = 0.001; % set resolution of distributions.
bmin = inc;
bmax = 1-inc;
b = bmin:inc:bmax;	% range of bias values p(head).
a = 1-b;          		% range of p(tails) values.

% make prior either uniform or binomial.
% set switch for uniform prior.
uprior=0;
if uprior     % Make uniform prior. 
    ht=1./(bmax-bmin);
    prior = ones(size(b)).*ht;
else  % Make prior=binomial
    C = nchoosek(4,2); % not really needed here.
    p = C.* b.^2 .* a.^2;
    prior = p/max(p);
end
%ni=6;
ni=6;
nmid=round(ni/2);
NN = 2^ni; % max number of coin flips in loop below is NN.
pvalsig(1:ni)=pthresh;
%
% loop through number of realizations 
nrealize=100;
%nrealize=1;
nsites=5;
%nsites=100;
for iter=1:nrealize
%
%
% loop through number of distinct 'sites'
%
%
for j=1:nsites
% find likelihood and posterior for different numbers N of events.
oldbias=0.5;
flips = rand(NN,1); % get NN random numbers between zero and one.
x0 = flips<theta0; % get number of "heads" for a coin with bias 0.6.
%x0 = flips<0.5; % get number of "heads" for a fair coin (bias=0.5).
%
iflag(j)=0;
oldbias=0.5;
oldheads=0;
oldheadspredictnull=0;
oldheadspredictnullrej=0;
oldheadspredictbayes=0;
for i=1:ni % step up number of coin flips in powers of 2.
    N=2^i; % get number of coin flips.
    iend=N;
    nflips(i)=N;
    nflipsnew(i)=2^(i-1);
    x = x0(1:N); % get data for first N coin flips.
    k = sum(x); % get number of heads.
    % C = nchoosek(N,k); % binmial coefficient, not needed here.
    C = 1; % set binomial coefficient to one.
    nh = k; % nh = number of heads.
    nt = N-k; % nt = number of tails.
    % likelihood function = probability of nh heads and nt tails.
    lik = C.* b.^nh .* a.^nt; 
    lik = lik/max(lik); % scale likelihood to have max value of one
    p = lik.*prior;           % find posterior = likelihood * prior
    maxp = max(p);             % find max value of p
    ind = find(maxp==p);     % find index of max value of p
    p = p/maxp;                   % make max value of p be one.
    best = b(ind);                 % find estimated bias, 
    % same as (number of heads)/(number of coin flips)
    heads(i,j)=k;
    tails(i,j)=N-k;
    bias(i,j)=best;
    biasnull(i,j)=0.5;
    biasnullrej(i,j)=biasnull(i,j);
    headspredictnull(i,j)=N/2;
    headspredictnullrej(i,j)=N/2;     
    headspredictbayes(i,j)=oldbias*N;
    if (iflag(j)==1)
        headspredictnullrej(i,j)=headspredictbayes(i,j);
        biasnullrej(i,j)=bias(i,j);
    end
    newheads(i,j)=heads(i,j)-oldheads;
    newheadspredictnull(i,j)=headspredictnull(i,j)-oldheadspredictnull;
    newheadspredictnullrej(i,j)=headspredictnullrej(i,j)-oldheadspredictnullrej;
    newheadspredictbayes(i,j)=headspredictbayes(i,j)-oldheadspredictbayes;
    errornull(i,j)=abs(newheads(i,j)-newheadspredictnull(i,j));
    errornullrej(i,j)=abs(newheads(i,j)-newheadspredictnullrej(i,j));
    errorbayes(i,j)=abs(newheads(i,j)-newheadspredictbayes(i,j));
    pval(i,j)=1.0-sum(p(b>0.5))/sum(p);
    if pval(i,j)<=pthresh
        iflag(j)=1;
    end
    oldbias=bias(i,j);
    oldheads=heads(i,j);
    oldheadspredictnull=headspredictnull(i,j);
    oldheadspredictnullrej=headspredictnullrej(i,j);
    oldheadspredictbayes=headspredictbayes(i,j);
    if (i==nmid)
      posteriormid(:,j)=p;
    end
end
posteriorfull(:,j)=p;
end
%medianbias=median(bias');
%meanbiasnull=mean(biasnull');
%meanbiasnullrej=mean(biasnullrej');
biasprime=bias';
[bval,ival]=sort(biasprime);
if (nsites>6) 
ilow=round(1+nsites/4);
imedian=round(nsites/2);
ihigh=round(3*nsites/4);
else
    ilow=1;
    imedian=round(nsites/2);
    ihigh=nsites;
end
%
imid=nmid;
ifull=ni;
%
jlowmid=ival(ilow,imid);
jmedmid=ival(imedian,imid);
jhighmid=ival(ihigh,imid);
%
biaslowmid=biasprime(jlowmid,imid);
biasmedmid=biasprime(jmedmid,imid);
biashighmid=biasprime(jhighmid,imid);
%
pmidlow(:,iter)=posteriormid(:,jlowmid);
pmidmed(:,iter)=posteriormid(:,jmedmid);
pmidhigh(:,iter)=posteriormid(:,jhighmid);
%
%
jlowfull=ival(ilow,ifull);
jmedfull=ival(imedian,ifull);
jhighfull=ival(ihigh,ifull);
%
pfulllow(:,iter)=posteriorfull(:,jlowfull);
pfullmed(:,iter)=posteriorfull(:,jmedfull);
pfullhigh(:,iter)=posteriorfull(:,jhighfull);
%
%
medianbias(iter,:)=median(bias');
meanbias(iter,:)=mean(bias');
medianbiasnull(iter,:)=median(biasnull');
medianbiasnullrej(iter,:)=median(biasnullrej');
meanbiasnullrej(iter,:)=mean(biasnullrej');
biaslower(:,iter)=prctile(bias',25);
biasupper(:,iter)=prctile(bias',75);
biasnullrejlower(:,iter)=prctile(biasnullrej',25);
biasnullrejupper(:,iter)=prctile(biasnullrej',75);
pvalmean(:,iter)=mean(pval');
pvalmedian(:,iter)=median(pval');
pvallower(:,iter)=prctile(pval',25);
pvalupper(:,iter)=prctile(pval',75);
%bias
%pval
%headspredictbayes
%headspredictnullrej
%msenull
%msebayes
%
% determine mean and spead of scenarios
%
newheadsmean(:,iter)=mean(newheads')';
newheadslower(:,iter)=prctile(newheads',25);
newheadsupper(:,iter)=prctile(newheads',75);
newheadsbayesmean(:,iter)=mean(newheadspredictbayes')';
newheadsbayeslower(:,iter)=prctile(newheadspredictbayes',25);
newheadsbayesupper(:,iter)=prctile(newheadspredictbayes',75);
newheadsnullmeanrej(:,iter)=mean(newheadspredictnullrej')';
newheadsnullrejlower(:,iter)=prctile(newheadspredictnullrej',25);
newheadsnullrejupper(:,iter)=prctile(newheadspredictnullrej',75);
newheadsnullmean(:,iter)=mean(newheadspredictnull')';
%
% determine RMSE for different estimates
%
diffbayes=newheadsmean(:,iter)'-newheadsbayesmean(:,iter)';
diffnull=newheadsmean(:,iter)'-newheadsnullmean(:,iter)';
diffnullrej=newheadsmean(:,iter)'-newheadsnullmeanrej(:,iter)';
rmsebayes(:,iter)=sqrt(diffbayes.*diffbayes);
rmsenull(:,iter)=sqrt(diffnull.*diffnull);
rmsenullrej(:,iter)=sqrt(diffnullrej.*diffnullrej);
%
end
%
%
if (nrealize>1) 
pfullmedm=mean(pfullmed');
pfulllowm=mean(pfulllow');
pfullhighm=mean(pfullhigh');
pmidmedm=mean(pmidmed');
pmidlowm=mean(pmidlow');
pmidhighm=mean(pmidhigh');
%
newheadsmeanm=mean(newheadsmean');
newheadslowerm=mean(newheadslower');
newheadsupperm=mean(newheadsupper');
newheadsbayesmeanm=mean(newheadsbayesmean');
newheadsbayeslowerm=mean(newheadsbayeslower');
newheadsbayesupperm=mean(newheadsbayesupper');
newheadsnullmeanm=mean(newheadsnullmean');
newheadsnullmeanrejm=mean(newheadsnullmeanrej');
%
pvalmeanm=mean(pvalmean');
pvalmedianm=mean(pvalmedian');
pvallowerm=mean(pvallower');
pvalupperm=mean(pvalupper');
%
rmsebayesm=mean(rmsebayes');
rmsenullm=mean(rmsenull');
rmsenullrejm=mean(rmsenullrej');
rmsebayeslower=prctile(rmsebayes',25);
rmsebayesupper=prctile(rmsebayes',75);
rmsenulllower=prctile(rmsenull',25);
rmsenullupper=prctile(rmsenull',75);
rmsenullrejlower=prctile(rmsenullrej',25);
rmsenullrejupper=prctile(rmsenullrej',75);
%
biasupperm=mean(biasupper');
biaslowerm=mean(biaslower');
biasnullrejupperm=mean(biasnullrejupper');
biasnullrejlowerm=mean(biasnullrejlower');
medianbiasm=mean(medianbias);
meanbiasm=mean(meanbias);
medianbiasnullm=mean(medianbiasnull);
medianbiasnullrejm=mean(medianbiasnullrej);
meanbiasnullrejm=mean(meanbiasnullrej);
else
pfullmedm=pfullmed(:,1);
pfulllowm=pfulllow(:,1);
pfullhighm=pfullhigh(:,1);
pmidmedm=pmidmed(:,1);
pmidlowm=pmidlow(:,1);
pmidhighm=pmidhigh(:,1);
newheadsmeanm=newheadsmean(:,1);
newheadslowerm=newheadslower(:,1);
newheadsupperm=newheadsupper(:,1);
newheadsbayesmeanm=newheadsbayesmean(:,1);
newheadsbayeslowerm=newheadsbayeslower(:,1);
newheadsbayesupperm=newheadsbayesupper(:,1);
newheadsnullmeanm=newheadsnullmean(:,1);
newheadsnullmeanrejm=newheadsnullmeanrej(:,1);
pvalmeanm=pvalmean(:,1);
pvalmedianm=pvalmedian(:,1);
pvallowerm=pvallower(:,1);
pvalupperm=pvalupper(:,1);
rmsebayesm=rmsebayes(:,1);
rmsenullm=rmsenull(:,1);
rmsenullrejm=rmsenullrej(:,1);
rmsebayeslower=rmsebayesm;
rmsebayesupper=rmsebayesm;
rmsenulllower=rmsenullm;
rmsenullupper=rmsenullm;
rmsenullrejlower=rmsenullrejm;
rmsenullrejupper=rmsenullrejm;
biasupperm=biasupper;
biaslowerm=biaslower;
medianbiasnullm=medianbiasnull(1,:);
medianbiasnullrejm=medianbiasnullrej(1,:);
biasnullrejlowerm=biasnullrejlower;
biasnullrejupperm=biasnullrejupper;
medianbiasm=medianbias(1,:);
end
%
% plot prior along with the posterior for largest sample for mid and last iteration 
figure(1)
hold
set(gca,'FontSize',14);
plot(b,pfullmedm,'k','Linewidth',2); 
plot(b,pfulllowm,'k--','Linewidth',1); 
plot(b,pfullhighm,'k--','Linewidth',1); 
plot(b,pmidmedm,'g','Linewidth',2); 
plot(b,pmidlowm,'g--','Linewidth',1); 
plot(b,pmidhighm,'g--','Linewidth',1); 
plot(b,prior,'r','Linewidth',2);
xlabel('bias toward active state, \theta'); ylabel('Probability Distribution, \it{P}(\theta)');
set(gca,'YLim',[0 1.1],'FontName','Courier'); grid on;
%
% plot actual vs. predicted # of heads 
figure(2);
hold
set(gca,'FontSize',14);
plot(nflipsnew,newheadsmeanm,'k','Linewidth',2);
plot(nflipsnew,newheadslowerm,'k--');
plot(nflipsnew,newheadsupperm,'k--');
plot(nflipsnew,newheadsbayesmeanm,'b','Linewidth',2);
plot(nflipsnew,newheadsbayeslowerm,'b--');
plot(nflipsnew,newheadsbayesupperm,'b--');
plot(nflipsnew,newheadsnullmeanm,'r','Linewidth',2);
plot(nflipsnew,newheadsnullmeanrejm,'m','Linewidth',2);
xlabel('number of years, {\itN} '); ylabel('number of active years,{\itN+}');
set(gca,'YLim',[0 23],'FontName','Courier'); grid on;
%
% plot RMSE values vs # of flips 
%
figure(3);
hold
set(gca,'FontSize',14);
plot(nflipsnew,rmsebayesm,'b','Linewidth',2);
plot(nflipsnew,rmsebayesupper,'b--','Linewidth',2);
plot(nflipsnew,rmsebayeslower,'b--','Linewidth',2);
plot(nflipsnew,rmsenullm,'r','Linewidth',2);
plot(nflipsnew,rmsenulllower,'r--','Linewidth',2);
plot(nflipsnew,rmsenullupper,'r--','Linewidth',2);
plot(nflipsnew,rmsenullrejm,'m','Linewidth',2);
plot(nflipsnew,rmsenullrejlower,'m--','Linewidth',2);
plot(nflipsnew,rmsenullrejupper,'m--','Linewidth',2);
xlabel('number of years, {\itN}'); ylabel('absolute error \epsilon');
set(gca,'FontName','Ariel'); grid on;
set(gca,'YLim',[0 3.5],'FontName','Courier'); grid on;
%
% plot p values vs # of flips 
%
figure(4);
hold
set(gca,'FontSize',14);
plot(nflips,pvalmedianm,'b','Linewidth',2);
plot(nflips,pvallowerm,'b--','Linewidth',1);
plot(nflips,pvalupperm,'b--','Linewidth',1);
plot(nflips,pvalsig,'k--','Linewidth',1.5);
xlabel('number of years, {\itN_{tot}}'); ylabel('{\itp} value for rejecting null');
set(gca,'YLim',[0 0.8],'FontName','Courier'); grid on;
set(gca,'XLim',[0 64],'FontName','Courier'); grid on;
%
% plot convergence of bias estimates
%
figure(5);
hold
set(gca,'FontSize',14);
plot(nflips,medianbiasnullm,'r','Linewidth',2);
plot(nflips,medianbiasnullrejm,'m','Linewidth',2);
plot(nflips,medianbiasm,'k','Linewidth',2);
plot(nflips,biaslowerm,'k--','Linewidth',1.5);
plot(nflips,biasupperm,'k--','Linewidth',1.5);
plot(nflips,biasnullrejlowerm,'m--','Linewidth',1.5);
plot(nflips,biasnullrejupperm,'m--','Linewidth',1.5);
xlabel('number of years, {\itN_{tot}}'); ylabel('estimated bias, \theta');
set(gca,'YLim',[0.4 0.7],'FontName','Courier'); grid on;
set(gca,'XLim',[0 64],'FontName','Courier'); grid on;

%%%%%%%%%%%%%%%%%%%%%%