Message Boards Message Boards

Fit data to a ODE system

Posted 4 years ago

I have a three equation ODE system. I have two parameters $\beta$ and $\gamma$ for which I try to find numerical values such that the model fits the data.

Here is the code I use

pop = 65800000;
sol = ParametricNDSolveValue[{s'[t] == -β x[t] s[t] , s[0] == pop, x'[t] == β x[t] s[t] -γ  x[t], x[0] == 1, r'[t] == γ  x[t], r[0] == 0}, {s, x, r}, {t, 0, 61}, {β, γ}];

The data I have

infe = {7, 7, 6, 4, 4, 4, 4, 1, 1, 0, 2, 5, 25, 44, 86, 116, 176, 196,269, 404, 632, 921, 1178, 1370, 1739, 2221, 2803, 3570, 4396, 5284, 6473, 6953, 8268, 9328, 10575, 12310, 13144, 16796, 17923, 20002, 22511, 25269, 29561, 30366, 33599, 39161, 42022, 41290, 61650, 66955, 68578, 71849, 79404, 80827, 82333, 86740, 89431, 91012, 94094, 98769, 99741};

reco = {4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 602, 1290, 1590, 1590, 2200, 2200, 3280, 3900, 4950, 5700, 5700, 7200, 7930, 9440, 10900, 12400, 14000, 15400, 16200, 17300, 19300, 21300, 23200, 24900, 26400, 27200, 27700, 28800, 31000};

popu = {pop, pop, pop, pop, pop, pop, pop, pop, pop, pop, pop, pop, pop, pop, pop, pop, pop, pop, pop, pop, pop, pop, pop, pop, pop, pop, pop, pop, pop, pop, pop, pop, pop, pop, pop, pop, pop, pop, pop, pop, pop, pop, pop, pop, pop, pop, pop, pop, pop, pop, pop, pop, pop, pop, pop, pop, pop, pop, pop, pop, pop};

(I know the last one seems creepy.)

susce = popu - Total[{infe, reco}];
data1 = {susce, infe, reco};

and

ω = 61;
transformedData1 = {ConstantArray[Range[3], ω] // Transpose, ConstantArray[Range[ω], 3], data1}~Flatten~{{2, 3}, {1}};

I write

model[β_, γ_][i_, t_] := Through[sol[β, γ][t], List][[i]] /; And @@ NumericQ /@ {β, γ, i, t}

and

fit = NonlinearModelFit[transformedData1, model[β, γ][i, t], {β, γ}, {i, t}]

This yields

FittedModel[model[-2.85412.x 10^6,0.0196833]]

Since it is the well known SIR model, the parameter $\beta$ should be positive but I do not know why it gives a negative number.

How can I fit the data to the ODE system and check if the differential system fits the data?

It was more beneficial to also provide a notebook with the code. This way we wouldn't have to copy so many cells, one by one, into Mathematica.

Anyway, I looked at your code (after copy and paste...) and the instruction

fit = NonlinearModelFit[transformedData1, 
  model[\[Beta], \[Gamma]][i, t], {\[Beta], \[Gamma]}, {i, t}]

Provided the following error messages, a fact you didn't;t mention at all. The result is meaningless unless you move the issue that generates the errors...

ParametricNDSolveValue::nderr: Error test failure at t == 5.618515451992051`; unable to continue.

InterpolatingFunction::dmval: Input value {6.} lies outside the range of data in the interpolating function. Extrapolation will be used.

InterpolatingFunction::dmval: Input value {6.} lies outside the range of data in the interpolating function. Extrapolation will be used.

InterpolatingFunction::dmval: Input value {6.} lies outside the range of data in the interpolating function. Extrapolation will be used.

General::stop: Further output of InterpolatingFunction::dmval will be suppressed during this calculation.

ParametricNDSolveValue::ndsz: At t == 36.00272702425443`, step size is effectively zero; singularity or stiff system suspected.
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract