I am trying to fitThe code first produces a plot of the model to our flu outbreak datadataset, by findingthen simulates the SIR model using the parameter values $\beta$ and $\gamma$ that maximizewe found in the likelihoodmanual calibration to the full dataset of the total number infected .
(*Model*Set equations*parameter values*)
ClearAll[\[Beta]{\[Beta], \[Gamma]]\[Gamma]} = {1.7, 0.45};
(*Define the SIR model equations*)
sireqns = {s'[t] == -\[Beta]*s[t]*i[t]/n,
i'[t] == \[Beta]*s[t]*i[t]/n - \[Gamma]*i[t],
r'[t] == \[Gamma]*i[t]};
(*Initial conditions*)
initialConditions = {s[0] == n-1, i[0] == 1, r[0] == 0};
(* Solving*Solve the differential equations *equations*)
sol = ParametricNDSolve[NDSolve[{sireqns, initialConditions}, {s, i, r}, {t,
0, tmax}, {\[Beta], \[Gamma]}];
predicted(*Extract the infected values from the solution*)
infectedValues = i /. sol;
sol[[1]];
(*LikelihoodfitPlot function*)=
likelihood[\[Beta]_?NumericQ Plot[infectedValues[t], \[Gamma]_?NumericQ]{t, :=1, 14}, PlotStyle -> Red,
Exp[Total[(numberinfectedPlotStyle -> predicted)^2]];
(*MaximizeRed, thePlotRange likelihood*)
result-> =All,
NMaximize[PlotLegends -> {Log[likelihood[\[Beta]"Simulated infected"}];
Show[pointsPlot, \[Gamma]]]fitPlot, 0PlotRange <=-> \[Beta]All]
Now, we want to calculate the likelihood of the model with these specific parameter values, i . e . the probability of observing these numbers of reported cases given our simulated numbers of infected people .
We are building up to calibrating the SIR model to our flu outbreak data from previous exercises using likelihood as a measure of the divergence between the model projections and the data. This time, even though we are looking at the same outbreak, the dataset only shows the reported cases, and we know that 60 % of flu cases are reported.
(*Adjust <=infected 2,values for reporting rate*)adjustedInfectedValues =
infectedValues*0.6
(*Simulated reported cases*)
simulatedReportedCases = 0Round[adjustedInfectedValues]
(*Likelihood <=calculation*)
likelihood \[Gamma]= <=
1} Product[Exp[-\[Lambda]] \[Lambda]^k/k!, {\[Beta]\[Lambda], \[Gamma]
simulatedReportedCases}];
optimalParameters =(*Print {\[Beta],the \[Gamma]}likelihood /.value*)
Print["Likelihood:", Last[result]likelihood]
I am looking for this solution
I tried to write some code similar to Writing a sum-of-squares function or SIR model fits but towards the end, I can't find my error. Any suggestions?