1
$\begingroup$

I am trying to fit an upside down Cauchy distribution to very few data points which are seen in the picture below:

enter image description here

I know that the minimum should be near x = 4, and I tried NonLinearModelFit, but the result was very bad.

Is there a way to get better fit results with few data points?

$\endgroup$

closed as off-topic by corey979, C. E., Alex Trounev, MarcoB, Michael E2 Oct 14 at 18:02

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question cannot be answered without additional information. Questions on problems in code must describe the specific problem and include valid code to reproduce it. Any data used for programming examples should be embedded in the question or code to generate the (fake) data must be included." – corey979, C. E., Alex Trounev, MarcoB, Michael E2
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    $\begingroup$ Please show the code you've tried. $\endgroup$ – JimB Oct 7 at 19:31
5
$\begingroup$

One can't expect much with estimating 5 coefficients (a, b, c, d, and error variance) with just 7 points. That also makes it difficult for getting convergence to the maximum likelihood or least-squares estimates as good starting values become more critical. (The default starting values are all 1.0 and most of the final estimates are far away from 1.0.)

And finally, one's brain (at least mine) doesn't necessarily think in terms of least squares: a curve that one might superimpose upon the data in one's head is not necessarily what a least squares solution looks like.

Here is a fit with mean and single prediction bands:

data = {{1, 2637}, {2, 2722}, {3, 2608}, {4, 2471}, {5, 2685}, {6, 2630}, {7, 2784}};
nlm = NonlinearModelFit[data, a - b/(d \[Pi] (1 + (-c + x)^2/d^2)),
  {{a, 2800}, {b, 1900}, {c, 4}, {d, 0.1}}, x, MaxIterations -> 10000];
nlm["BestFitParameters"]
(* {a -> 2700.41, b -> 670336., c -> 3.61325, d -> 0.000160892} *)

f = Join[{nlm[x]}, nlm["MeanPredictionBands"], nlm["SinglePredictionBands"]];
g = Plot[f, {x, 0, 8},
  PlotStyle -> {{Black, Solid}, {Green, Dotted}, {Green, Dotted}, {Red, Dotted}, {Red, Dotted}}];
Show[ListPlot[data, PlotRange -> {{0, 8}, {2300, 3000}}], g]

Data and fit with prediction bands

$\endgroup$
  • $\begingroup$ Thank you for your answer! Is there a way to "force" Mathematica to place the minimum at x=4? $\endgroup$ – lukask Oct 9 at 10:42
  • $\begingroup$ Certainly. You just set c to be 4. But you lose all ability to estimate how good that estimate might be. $\endgroup$ – JimB Oct 9 at 13:33
  • $\begingroup$ Just one last thing. If I remove the "MeanPredictionBands" and the "SinglePredictionBands" Mathematica doesn't plot the black line entirely on the given range. How can I force it to do so? $\endgroup$ – lukask Oct 11 at 15:17
  • $\begingroup$ Add in PlotRange -> {All, {2300, 3000}} to the Plot command. $\endgroup$ – JimB Oct 11 at 15:30

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