2
$\begingroup$

Question: is it possible to increase the accuracy of the BestFit property for LinearModelFit?

I have code that uses LinearModelFit and the BestFit property to return the best fitting slope of a list of data. But, a peculiar result I've ran into is very slightly negative slope as a result for data where the slope is 0.

Example:

{{Log[10], Log[2]}, {Log[20], Log[2]}, {Log[30], Log[2]},{Log[40], Log[2]}, {Log[50], Log[2]}}

produces

7.411988753*10^-21

for the slope, even though we can see that the exact slope is 0.

Here is the context:

For[j = 1, j <= Length[pointsList], j++,
  measureData = Map[{Log[1/#], Log[BoxCount[pointsList[[j]], #]]} &, scaleList];
  line = LinearModelFit[measureData, x, x];
  AppendTo[dimensionList, Coefficient[line["BestFit"], x, 1]]];

pointsList is just a list of lists of points; scaleList = {1/10, 1/20, 1/30, 1/40, 1/50}. The example I shared was one list from pointsList and its corresponding element in dimensionList.

So, is there a way to increase the accuracy of linear model fit? Alternatively, I could emplace an If expression to solve the best fitting slopes I can solve algebraicly, but I was hoping this wasn't necessary.

$\endgroup$
8
  • 1
    $\begingroup$ add the option ...,WorkingPrecision->100] to LinearModelFit[...] to have a 100 digits working precision. $\endgroup$
    – andre314
    Jul 8, 2017 at 16:37
  • 1
    $\begingroup$ try also ...WorkingPrecision->Infinity] to have exact arithmetic $\endgroup$
    – andre314
    Jul 8, 2017 at 16:38
  • $\begingroup$ This didn't seem to make the guess for the slope more correct, just closer to the intended guess. Using infinity left me with a large expression saturated with logs instead of a slope of 0. $\endgroup$
    – Anthony
    Jul 8, 2017 at 19:43
  • $\begingroup$ What is BoxCount? $\endgroup$
    – m_goldberg
    Jul 8, 2017 at 20:48
  • $\begingroup$ Try Coefficient[line["BestFit"] // Chop $\endgroup$
    – m_goldberg
    Jul 8, 2017 at 20:51

1 Answer 1

1
$\begingroup$

The concern raised in the OP makes no effective difference. The range of the independent variable is Log[50] - Log[10], so the difference in the predicted value of the dependent variable is at most

(7.411988753*10^-21 (Log[50] - Log[10])

When added to Log[2] it makes no difference (literally zero) at machine precision, due to the way floating-point numbers work:

(7.411988753*10^-21 (Log[50] - Log[10]) + Log[2]) - Log[2]
(*  0.  *)

The nonzero value of the slope is most likely due to rounding or truncation error in the numerical procedure used to estimate the "best" fit. But this error is negligible in this case.

Note: This is merely an explanation of the numerics of the problem, which seemed to be the basis for the issue raised. More important issues related to how to model -- some raised in the comments -- are nearly impossible to address, since the data provided seemed manufactured simply to illustrate the error in the slope.

$\endgroup$

Your Answer

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

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