9
$\begingroup$

I am trying to find a nonlinear model from the data.

enter image description here

My code is below:

data = {{0.0, 0.0}, {0.05, 0.87}, {0.1, 0.99}, {0.15, 0.98}, {0.2, 
   0.91}, {0.25, 0.81}, {0.3, 0.71}, {0.35, 0.62}, {0.4, 0.51}, {0.45,
    0.31}, {0.5, 0.31}, {0.55, 0.23}, {0.6, 0.18}, {0.65, 0.14}, {0.7,
    0.08}, {0.75, 0.05}, {0.8, 0.03}, {0.85, 0.02}, {0.9, 
   0.01}, {0.95, 0.002}, {1, 0}};

model=((1 - x)/(1 - a))^((0.5 (1 - a))/
          a) (x/a)^0.5;

(* fit model*)

    NonlinearModelFit[data, model, a, x]

NonlinearModelFit doesn't work for this model, i.e.

enter image description here

Are there any other ways to solve this problem?

Thanks in advance!

Update:

If I try:

NonlinearModelFit[data, {model, {a > 0.000001}}, a, x]

Errors:

enter image description here

$\endgroup$
3
  • 1
    $\begingroup$ What about constrains for a - is it $0<a<1 $? $\endgroup$ Commented Jun 19, 2022 at 16:12
  • $\begingroup$ @AlexTrounev i try NonlinearModelFit[data, {model, {a > 0.000001}}, a, x] but it does not work... $\endgroup$
    – ABCDEMMM
    Commented Jun 19, 2022 at 16:26
  • 3
    $\begingroup$ Just curious: Are the response variables relative frequencies and are the first and last responses (both 0) from observations? Also should the 4th observation be {0.15, 0.98} rather than {0.2, 0.98}? $\endgroup$
    – JimB
    Commented Jun 19, 2022 at 18:59

3 Answers 3

7
$\begingroup$

We can use NMimimize to solve this problem as follows

data = {{0.0, 0.0}, {0.05, 0.87}, {0.1, 0.99}, {0.2, 
    0.98}, {0.2, 0.91}, {0.25, 0.81}, {0.3, 0.71}, {0.35, 0.62}, {0.4,
     0.51}, {0.45, 0.31}, {0.5, 0.31}, {0.55, 0.23}, {0.6, 
    0.18}, {0.65, 0.14}, {0.7, 0.08}, {0.75, 0.05}, {0.8, 
    0.03}, {0.85, 0.02}, {0.9, 0.01}, {0.95, 0.002}, {1, 0}};

f[a_, x_] := ((1 - x)/(1 - a))^((0.5 (1 - a))/a) (x/a)^0.5;


vec[a_] = Table[data[[i, 2]] - f[a, data[[i, 1]]], {i, Length[data]}];
sol = NMinimize[{vec[a] . vec[a], 0 < a < 1}, {a}]

(*Out[]= {0.0152585, {a -> 0.127671}}*)

Visualization

Show[Plot[f[a, x] /. sol[[2]], {x, 0, 1}], 
 ListPlot[data, PlotStyle -> Red]]

Figure 1

$\endgroup$
2
  • $\begingroup$ Nice answer! can we still use fit function for this task? $\endgroup$
    – ABCDEMMM
    Commented Jun 19, 2022 at 16:27
  • 1
    $\begingroup$ We can use NonlinearModelFit with option Method -> NMinimize as it shown by xzczd. $\endgroup$ Commented Jun 19, 2022 at 16:54
11
$\begingroup$
nlm = NonlinearModelFit[data, {model, 0 < a < 1}, a, x, Method -> NMinimize]
Plot[nlm[x], {x, 0, 1}]~Show~ListPlot@data

enter image description here

$\endgroup$
5
  • $\begingroup$ Very nice answer(+1). How you know about option Method -> NMinimize? :) $\endgroup$ Commented Jun 19, 2022 at 16:47
  • 2
    $\begingroup$ @alex This is mentioned in Details and Options section of NonlinearModelFit: "Possible settings for Method include "ConjugateGradient", "Gradient", "LevenbergMarquardt", "Newton", "NMinimize", and "QuasiNewton", with the default being Automatic. " Also, an example setting Method->NMinimize can be found in Options section of document of FindFit (Yeah, no example in document of NonlinearModelFit, at least for now). But honestly speaking, I learned this when reading certain post in this site, IIRC. :) $\endgroup$
    – xzczd
    Commented Jun 19, 2022 at 16:51
  • $\begingroup$ Thank you very much. $\endgroup$ Commented Jun 19, 2022 at 17:03
  • 4
    $\begingroup$ I think it's worth mentioning that NMinimize also takes options. For example Method->{NMinimize, Method->”RandomSearch”} $\endgroup$ Commented Jun 19, 2022 at 23:57
  • $\begingroup$ @DavidKeith thanks for your suggestions! $\endgroup$
    – ABCDEMMM
    Commented Jun 20, 2022 at 10:55
9
$\begingroup$

The problem seems to stem from the first and last data points, with $x = 0$ and $x = 1$. My guess is that it has to do with $\partial f/\partial x$ being singular at these points. In addition, $\partial f/\partial a$ is singular when $a = 0$ or $a = 1$.

If you remove the offending data points, and give Mathematica an initial guess for $a$ that is away from the trouble spots, NonlinearModelFit runs without complaints & yields parameter a -> 0.127671.

newdata = Most[Rest[data]]
fit = NonlinearModelFit[newdata, model, {{a, 0.5}}, x]
Show[ListPlot[newdata, PlotStyle -> Orange], Plot[fit[x], {x, 0, 1}]]

enter image description here

Note that since the model automatically goes through the omitted data points for $0 < a < 1$, omitting them shouldn't affect the quality of the fit.

$\endgroup$
1
  • 3
    $\begingroup$ For whatever it's worth, that's why I suspected that first and last points aren't really data but "theoretical anchors" (for lack of a better term). Also, the curve "shape" is that of a beta distribution and the x values are equally spaced which suggests some sort of binning of the observations (i.e., maybe fitting a probability distribution rather than a regression). $\endgroup$
    – JimB
    Commented Jun 20, 2022 at 15:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.