# Fitting Hill function to a given dataset

I have the following data:

data = {{15, 1}, {18, 1470.7/1476.9333333333334}, {21, 1326.2333333333333/1476.9333333333334}, {24, 316.8/1476.9333333333334}};


Based on the data and knowledge of the underlying dynamics, Hill function, i.e., $$f (x) = a^n / (a^n + x^n)$$, can be a good candidate for fitting; and, $$a$$ and $$n$$ to be determined from the data. So, we have:

nlm = NonlinearModelFit[data, a^n/(a^n + x^n), {a, n}, x]


which gives error:

The function value {-1475.9327805109833+0.0002583292146366989I,-1469.699783452097+0.0001011495715564914I,-1325.2332352782923+0.00004579492074835434 I,-44.140176596973895+0.00002305423442489074 I} is not a list of \real numbers withdimensions {4} at {a,n} = {-3.5525189040549865,-5.13906385562555}.


How can this be fixed?

EDIT

Now, by adding the constraint $$a > 0$$ and $$n > 0$$, the error is fixed and Mathematica returns an answer:

nlm = NonlinearModelFit[data, {a^n/(a^n + x^n), a > 0, n > 0}, {a, n}, x]

***2.17245*10^17/(2.17245*10^17 + x^6.18796)***


But, it doesn't seem it fits the data:

Show[ListPlot[data], Plot[nlm[x], {x, 15, 24}], Frame -> True]


• You can add constraints on the parameters, and/or provide an initial value for them. There are examples in the documentation. Commented Nov 20, 2023 at 14:45
• You can add constraints wo your model as in {a^n/(a^n + x^n), a > 0, n > 0}; please have a look at the documentation. Commented Nov 20, 2023 at 15:39
• @BobHanlon 's answer (+1) is a good example of the advantage of having good starting values. But I would argue that by dividing by the largest response variable the model actually being fit is c a^n/(a^n + x^n) so there are 3 parameters with just 4 data points. That suggests overfitting and fudging to me. Some tempering of the feeling of a good fit can be achieved by using the SinglePredictionBands option.
– JimB
Commented Nov 20, 2023 at 21:26

Include estimates of parameters in NonlinearModelFit

\$Version

(* "13.3.1 for Mac OS X ARM (64-bit) (July 24, 2023)" *)

Clear["Global*"]

data = {{15, 1}, {18, 1470.7/1476.9333333333334}, {21,
1326.2333333333333/1476.9333333333334}, {24,
316.8/1476.9333333333334}} // Rationalize;

f[a_, n_, x_] := a^n/(a^n + x^n)


Develop estimates for parameters

eqns = (f[a, n, #[[1]]] - #[[2]] == 0 & /@ data[[{1, -1}]])

(* {-1 + a^n/(15^n + a^n) == 0, -(216/1007) + a^n/(24^n + a^n) == 0} *)

est = NArgMin[{Total[eqns[[All, 1]]^2], a > 0, n > 0}, {a, n}]

(* {22.8901, 27.4141} *)

(nlm = NonlinearModelFit[data, f[a, n, x],
Transpose[{{a, n}, est}], x])["BestFitParameters"]

(* {a -> 22.8313, n -> 25.9977} *)

{xmin, xmax} = MinMax[data[[All, 1]]]

(* {15, 24} *)

Plot[nlm[x], {x, xmin, xmax},
Epilog -> {Red, AbsolutePointSize[4],
Point[data]}]
`

• I don't know why the fit is as good as it is. Commented Nov 20, 2023 at 20:19

This is just an extended comment. In short, that model can't provide a reasonable fit to your data.

The model can be rewritten as the following:

$$\frac{1}{\left(\frac{x}{a}\right)^n+1}$$

Given that all of the responses are greater than 1 (and real), that means that

$$\left(\frac{x}{a}\right)^n+1<1$$ or $$\left(\frac{x}{a}\right)^n<0$$. That can only potentially happen if $$a<0$$ and $$n$$ is an odd integer.

There does not appear to be a combination of $$a$$ and $$n$$ satisfying those restrictions that can match more than a single data point.

• Your adjustment essentially is adding an additional parameter and with 3 parameters and just 4 data points, that doesn't lend to having much confidence in the fit. (And I do really understand that maybe even those 4 data points were very expensive to obtain.)
– JimB
Commented Nov 20, 2023 at 21:33
• That is a very interesting problem but my advice is to get more data. You have 4 parameters (actually at least 5 counting the error structure which has currently been ignored) and only 8 data points. (Biological systems as opposed to Physics systems require many more data points than one would like. And I do understand that getting more data is many times impossible.)
– JimB
Commented Nov 25, 2023 at 16:38