%% fit distribution of levels with binomial
% one need to provide the probaibly distrbution (n value, see below), which is th
sp = [1 0.];
options = optimset('Display','off');
[estimate6,Error6] = fminsearch(@fun,sp,options);

function [Error] = fun(var)

% [n,~]=histcounts([N1 N2],'Normalization','probability');
n=[0.588590000000000	0.323580000000000	0.0770700000000000	0.0107700000000000	0	0];  % the probaiblity distribution
Fit=var(1)*binopdf(1:6,4,var(2)); % 1:6 is the probabily over the number of states, in this case from 1-6. 4 is the assumed model 4-mer. This can be changed accordingly.
Error = sum( (n-Fit).^2 );

end

% %% bootstrap to calculate fit error
% for i=1:1000
%     data=datasample([N1 N2],104);
%     
%     [n,x]=histcounts(data,'Normalization','probability');
%     if length(n)<4
%         n=[n 0];
%     end
% 
% sp = [1 0.];
% options = optimset('Display','off');
% [estimates(:,i)] = funfun(n);
% end
% 
% function estimate = funfun(n)
% 
% sp = [1 0.];
% options = optimset('Display','off');
% estimate = fminsearch(@fun,sp,options);
% 
% function [Error] = fun(var)
% 
% Fit=var(1)*binopdf(1:4,4,var(2));
% Error = sum( (n-Fit).^2 );
% 
% end
% end
