1

I have data points — denoted by Fk— covering quite a large range $(1,\,10^{1010})$. I plotted them with ListLogPlot. Then I tried to fit the data points using NonlinearModelFit, and now I have two problems:

  1. Fitting the data points

    fit = NonlinearModelFit[Fk[300], a*k^(B*k), {a, B}, k] 
    

    gives 1 k^1 for the fitted model.

    However, fitting the data points with:

    fit2 = NonlinearModelFit[Fk[300], a*k^(b*c*k), {a, b, c}, k] 
    

    gives the fitted model 140.714 k^1.16997 k which I completely do not unterstand. I mean why should the output change upon inserting the variable $c$, which could also be combined with $b$ such that say $b\,c= B$ and fit should be equal to fit2.

  2. If I now plot the data Fk vs. k together with the fitted model, the fitted curve ends at some value of $k \approx 120$, and I do not unterstand why. My code for this is

    Show[{ListLogPlot[Fk[300], PlotStyle -> Red], LogPlot[fit2[k], {k, 0, 300}]}]
    

plot

Red dots = data points; blue line = fitted curve

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
user46908
  • 11
  • 2
  • Thanks, is it now ok? – user46908 Feb 26 '17 at 13:03
  • Yes, I will delete my comment. – m_goldberg Feb 26 '17 at 13:05
  • Can you provide us with your points? (Btw it makes no sense to fit b*c since they're directly correlated into each other.) – Julien Kluge Feb 26 '17 at 14:13
  • For the fact that the plot stops at a value around k=120, that may be because your function is hitting the upper limit of machine-precision numbers. See (105774). Try increasing the WorkingPrecision for your Plot function. – MarcoB Feb 26 '17 at 15:49
  • Non-linear fitting rarely plug-and-play. Two things I would recommend: (i) Find a good initial guess for the parameters. E.g., a=Fk[0]. (ii) If the fit is 1% off at k=300 the absolute error is $10^{800}$ times larger than a 1000% error at k=50. That is, you probably want to specify weights to be some function of Fk. Finally, plotting works with Exclusions -> None, see this bug. – Felix Feb 26 '17 at 16:20
  • You almost certainly want to fit the log of Fk[300]: NonlinearModelFit[Log[Fk[300]], Log[a] + B k Log[k], {a, B}, k]. Then you want to check the residuals to see if the assumption of a constant variance makes sense. – JimB Feb 26 '17 at 16:50
  • The data points are given by: Fk[n_] = LinearSolve[ Table[PadRight[Table[StirlingS2[k, m], {m, k}], n], {k, 1, n}], Table[BellB[k]^2, {k, 1, n}]] – user46908 Feb 26 '17 at 17:47
  • The problem however still is that if I just use NonlinearModelFit to fit these data with the model: fit= NonlinearModelFit[Fk[300], ax^(bx), {a, b}, x] the Output is: FittedModel[1.k^(1.k)] which is obviously wrong. – user46908 Feb 26 '17 at 17:51
  • What do you mean with "still ... wrong"? Did you apply any of the suggestions above? The result is simply indicative for a failure to find a fit, so the initial parameters (1,1) are returned. – Felix Feb 26 '17 at 19:15
  • Yes, I tried to Increase the WorkingPrecision and finding out how to use an initial guess for the Parameters. Actually I really just started using mathematica. – user46908 Feb 27 '17 at 06:55

1 Answers1

4

You want to fit the log of Fk[300] and add in some constraints for the parameters.

Fk[n_] := LinearSolve[
   Table[PadRight[Table[StirlingS2[k, m], {m, k}], n], {k, 1, n}], 
   Table[BellB[k]^2, {k, 1, n}]];
data = Transpose[{Range[1, 300], Log[Fk[300]]}];

nlm = NonlinearModelFit[data, {Log[a] + b k Log[k], a > 0 && b > 0}, {a, b}, k];
nlm["BestFitParameters"]
(* {a -> 74145.49180618675`,b -> 1.1879871492270795`} *)

But this doesn't provide a great fit. (Or rather if you add in a few more terms, you can predict a whole lot better. But that's not a Mathematica issue.)

ListLogPlot[{Fk[300], Exp[nlm["PredictedResponse"]]}]

Fit using logs

Here is a "close-up" of the lack of fit showing the residual associated with the values of k: Residuals vs k

JimB
  • 41,653
  • 3
  • 48
  • 106
  • Hi Jim. Thanks for your answer. I now used your NonlinearModelFit however I got a different a and b: {a -> 1.70003*10^-6, b -> 1.20435} – user46908 Feb 27 '17 at 07:03
  • And yes, you are right it is not the best fit. I will try to make it better, but what I am looking for is just an asymptotic behaviour of the Fk, thus from the fit I already kind of see what to expect. – user46908 Feb 27 '17 at 07:13
  • Using loga as the parameter rather than a in Log[a] gives a number much closer to yours and (maybe more importantly) gives a smaller error variance. However, if your objective is to estimate asymptotic behaviour and you're not so interested in small values of k, then that probably doesn't help much. – JimB Feb 27 '17 at 17:06
  • Yeah I realized that even for getting just an asymptotic behaviour my fit was far too bad, thus I changed the model to ak^(bk^c). Now the fit is much better and the residuals pretty small. Thanks again for your answer. – user46908 Feb 28 '17 at 07:04