%Main - This file estimates parameters for the SIWR model (Fixed parameters: mu=0, gamma=0.25)
clear;

%% Load Angola Data

CholeraData;
 

%% Starting Values for Parameter Estimates

params = zeros(4,1);
params(1) = 0.75;                   % betaI
params(2) = 0.75;                   % betaW  
params(3) = 0.01;                   % xi
params(4) = 1/(0.00573*15566000);   % k


%% Initial Values & Simulation Time (days)

%Initial conds (variables go in order: s, i, w, r)
x0 = zeros(4,1);
x0(2) = params(4)*data(1);
x0(1) = 1-x0(2);
x0(3) = 0;
x0(4) = 0;

 
%% Full Model Parameter Estimation (Poisson ML + Nelder Mead)
paramests = fminsearch(@(minip) siwrML(times,x0,minip,data),params,optimset('Display','iter'));

%re-simulate the model with the final parameter estimates
[test,xest] = ode45(@siwrODE,times,x0,[],paramests);
yest = xest(:,2)/paramests(4);


%% Compare fits to data

figure(1)
    set(gca,'LineWidth',1,'FontSize',16,'FontName','Arial')
    hold on
    plot(test,yest,'k','LineWidth',2);
    plot(times,data,'o');
%    legend('Infecteds'); 
    ylabel('Infected Population');  
    xlabel('Time (days)');
    
 figure(2)
    hold on
    plot(test,(yest-data),'o');
    ylabel('Residuals');  
    xlabel('Time (days)'); 
    
% figure(3)
%     hist(yest-data)
    
    
