function out=hedgeDistResample(data)
%hedgeDistResample takes an nx24 matrix of phototaxis choices, calculates
%the mean and samples random binomial re-populations of the original data 
%matrix with said mean. Used to calculate the mean expected distribution
%and confidence intervals in Figure 1C of Kain et al. Also saves the
%resampled MADs of the distributions for use in p-value calculation.
%
%Usage:
%
%out=hedgeDistResample(data)
%
%out=kx24 array of resampled histograms, where k=# resamplings, 10,000 by
%default
%
%data=nx24 matrix of phototaxis choices, with NaNs allowed.

%number of reps and allocation of output in memory
k=10000;
out.resamples=zeros(k,25);
out.MADs=zeros(k,1);

%calculate global mean
mu=mean(nanmean(data));

%for all reps
for i=1:k
    
    %make random new data to match size of original data
    temp=rand(size(data));
    temp(isnan(data))=NaN;
    
    %make it binary
    temp=temp<mu;
    
    %output histogram
    out.resamples(i,:)=hist(nansum(temp),0:24);
    
    %output mad
    out.MADs(i)=mad(nansum(temp));
    
end