SIR parameters fitting with Maximum likelihood estimation
Show older comments
Dear community,
I am trying to fitting data in SIR model, I want to get a parameter (beta,gamma) based on MLE, but a have the following error:
Supplied objective function must return a scalar value.
The SIR model>

I am using differte functions like:
(System ODE)
function dudt=sirpar(~,u,theta)
%Asignar compartimenteos
S=u(1);
I=u(2);
R=u(3);
%Escritura del sistema de ecuaciones diferenciales
%ds/dt%
dudt(1,1)=-theta(1)*S*I;
dudt(2,1)=theta(1)*S*I-theta(2)*I;
dudt(3,1)=theta(2)*I;
end
Resolution
function [y] = odefit(t_obs,theta)
S0=1; I0=1/5072000; R0=0;
[t,y] = ode45(@(t,u)sirpar(t,u,theta),t_obs,[S0 I0 R0]);
end
Log_like function
function val=log_lik(theta,datos_obs)
t_obs=1:length(datos_obs(:,2));
[y] = odefit(t_obs,theta);
I_mod=y(:,2);
val=-sum(log(normpdf(datos_obs,mean(I_mod),1)));
and the principal script
function [y,val,theta]= SIR_MLE_2
tol=1e-10
while nomr((I_obs-I_mod).^2)<tol
% Datos experimentales
datos_obs=xlsread('datos_sir');
S_obs=datos_obs(:,1);
I_obs=datos_obs(:,2);
R_obs=datos_obs(:,3);
% Sisitema de ecuaciones SIR
%Condiciones iniciales
theta=[0.341 0.141];
%Tiempo
t_obs=1:length(I_obs);
%Parámetros
%Resolución del sistema en función del parámetro
[y] = odefit(t_obs,theta);
%Control Gráfico
%plot(t_obs,y)
%plot(t_obs,err)
%Datos del modelo
S_mod=y(:,1);
I_mod=y(:,2);
R_mod=y(:,3);
%plot(t_obs,S_mod,'b')
hold on
plot(t_obs,S_obs,'r')
%MLE SUm
val=log_lik(theta,datos_obs)
%Optimizacion
options=optimset('Display','off','MaxIter',10000,'TolX',10^-30,'TolFun',10^-30);
theta0=[0.341 0.141];
[theta,fval,exitflag,output,grad,hessian]=fminunc('log_lik',theta0,options,datos_obs);
theta0=theta;
end
end
The data attachmet here!
I hope anyone can help me!
Thanks :)
Answers (0)
Categories
Find more on Linear and Nonlinear Regression in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!