%This script loads infetion data (Confirmed, Deaths and Recovered in matrix form (country x time), see, e.g., https://github.com/CSSEGISandData/COVID-19) and fits
%an SIR model to the inital exponential period of the considered infection.

load InfectionData.mat 
N=(7.8-1.3)*10^9;
beta=1/8;

numdatapoints=size(Confirmed,2);
%data begins at 21-Jan-2020
dates=datetime(2020,1,21)+caldays(1:800); 

%Beginning date for fit: 21-Feb-2020
startfitindex=find(dates=='21-Feb-2020');

%Consider only cases outside China: In sample data: from row IndexOther onwards
ConfirmedOther=sum(Confirmed(IndexOther:end,:));
DeathsOther=sum(Deaths(IndexOther:end,:));
RecoveredOther=sum(Recovered(IndexOther:end,:));

ExistingOther=ConfirmedOther-DeathsOther-RecoveredOther;

xvalues=0:1:length(ExistingOther(startfitindex:end))-1;
expeqn='1139*exp(a*x)';
fitobject = fit(xvalues',ExistingOther(startfitindex:end)',expeqn,'Start',1)
if (fitobject(0)~=ExistingOther(startfitindex))
    error('Error: Prefactor in exponential fit function and starting data do not match!')
end

%SIR model:
I0=ExistingOther(startfitindex)/N;
S0=1-I0;

expparam=coeffvalues(fitobject);
alpha=beta+expparam;
[x,v]=ode45(@SIR,[0,120],[S0,I0],[],alpha,beta);

