%%
clear
N0 = [1e5 1e5];
p = [5e-2 1e-2];
NiterMax = 100;

Ntestes = 100;
for qt = 1:Ntestes

    t = (1:NiterMax)' - 1;
    
    Y = zeros( NiterMax, 1 );
    N = N0;
    for i = 1 : NiterMax
        for j=1:length(N0)
            dN(j) = sum( rand(N(j),1)<p(j) );
        end
        N = N - dN;
        Y(i) = sum(dN);
    end
%     Y0 = N0*exp(-p*t);
%     Y = round( Y0 + sqrt(Y0).*randn(size(Y0)) );
    
%     YL = log( Y );
%     sYL = sqrt( Y + 1*(Y==0) ) ./ ( Y+ 1*(Y==0) );
%     XL = [ones(size(t)) t];
%     [AL(:,qt), sAL(:,qt), Q2L(qt), NGL, nada, FL ] = mmq_lin( XL, YL, sYL );
    
%     sYL2 = sqrt( FL  ) ./ FL;
%     [AL2(:,qt), sAL2(:,qt), Q2L2(qt), NGL, nada, FL2 ] = mmq_lin( XL, YL, sYL2 );
%     
    YnL = Y;
    sYnL = sqrt( Y + 1*(Y==0) );
    FUNCAO = @(A)(A(1)*exp(A(3)*t) + A(2)*exp(A(4)*t));
    [AnL(:,qt), sAnL(:,qt), Q2nL(qt), NGL, nada, FnL ] = mmq_naolin( FUNCAO, YnL, sYnL, [N0(:).*p(:); -p(:)] );
%     
%     sYnL2 = sqrt( FnL + 1*(FnL==0) );
%     [AnL2(:,qt), sAnL2(:,qt), Q2nL2(qt), NGL, nada, FnL2 ] = mmq_naolin( FUNCAO, YnL, sYnL2, AnL(:,qt) );
end