# Possible bug: FindFit NonlinearModelFit constraint vs data

The first group of answers is correct. The second group of answers is wrong, in my opinion. In all cases, removing the condition, True, yields a correct answer. Can someone explain? I haven't checked the minimization and maximization functions for this problem yet.

iMax=4;(*or greater*)
FindFit[1+0 Range@iMax,{5.179098401833815*^-7*MBB,True},{MBB},{ds}]
FindFit[Table[{i,1},{i,iMax}],{5.179098401833815*^-7*MBB,True},{MBB},{ds}]
NonlinearModelFit[1+0 Range@iMax,{5.179098401833815*^-7*MBB,True},{MBB},{ds}]["BestFitParameters"]
NonlinearModelFit[Table[{i,1},{i,iMax}],{5.179098401833815*^-7*MBB,True},{MBB},{ds}]["BestFitParameters"]
(*{MBB->1.91815*^6}*)
(*{MBB->1.91815*^6}*)
(*{MBB->1.91815*^6}*)
(*{MBB->1.91815*^6}*)

iMax=3;(*also works with 1 or 2*)
FindFit[1+0 Range@iMax,{5.179098401833815*^-7*MBB,True},{MBB},{ds}]
FindFit[Table[{i,1},{i,iMax}],{5.179098401833815*^-7*MBB,True},{MBB},{ds}]
NonlinearModelFit[1+0 Range@iMax,{5.179098401833815*^-7*MBB,True},{MBB},{ds}]["BestFitParameters"]
NonlinearModelFit[Table[{i,1},{i,iMax}],{5.179098401833815*^-7*MBB,True},{MBB},{ds}]["BestFitParameters"]
(*{MBB->1.}*)
(*{MBB->1.}*)
(*{MBB->1.}*)
(*{MBB->1.}*)

• I can reproduce the problem in 11.0, didn't try other versions. The problem must affect only one specific method, but I do not know how to figure out which method it is using by default. Try setting the Method option to various values and see the messages it issues. The constraint seems to matter because some methods cannot handle constraints at all. Thus adding even a trivial constraint affects automatic method selection. – Szabolcs Oct 17 '17 at 13:51
• Would you give some context as to how this situation occurs? Without out that context this question (and your previous answer mathematica.stackexchange.com/questions/157504/…) seem pathological. You're adding in parameters that are not needed, fitting data with no variability, and unnecessarily scaling the predictor variable (i.e., multiplying by 5.179098401833815*^-7). If you select a multiplier that gets you numbers closer to the dependent variable, everything works fine. (Except for the poor fit.) – JimB Oct 17 '17 at 17:07
• Rescaling in the real world is certainly necessary. But rescaling aside, attempting to fit a regression with 3 (or 2 or 1) points is not a reasonable objective in the real world. For iMax=3 adding WorkingPrecision -> 30 or using {5179098401833815/10^7*MBB, True} gets one the "correct" answer. This is not a bug. For more issues (and solutions) to fitting functions to data see mathematica.stackexchange.com/questions/139038/…. – JimB Oct 17 '17 at 18:45
• Adding the equivalent NMinimize call would help. – JimB Oct 17 '17 at 19:54
• @JimB norm@iMax_=Sqrt[iMax]*Abs[5.179098401833815*^-7*MBB-1];NMinimize[{norm@3,True},MBB]  as requested – Chris Chiasson Oct 18 '17 at 13:10

I think this issue is mainly (but not completely) associated with numeric precision issues and is not a software bug. Here's why...

Consider the data with just 3 observations to fit the model in the question. (And fitting any model with just 3 observations let alone a model that is essentially a mean where there is no variability is more than a bit unrealistic.)

iMax = 3;
data = Table[{i, 1}, {i, iMax}]
(* {{1, 1}, {2, 1}, {3, 1}} *)


Here are some scenarios starting with the original fit (with and without the additional condition) and three ways to deal with the precision issue:

(* Original fit *)
NonlinearModelFit[
data, {5.179098401833815*10^(-7)*MBB, True}, {MBB}, {ds}]["BestFitParameters"]
(* {MBB -> 1.0000077686435793} *)

(* Original commands but with the unused condition removed *)
NonlinearModelFit[data,
5.179098401833815*10^(-7)*MBB, {MBB}, {ds}]["BestFitParameters"]
(* {MBB -> 1.9308380000000007*^6} *)

(* Rationalize the multiplicative coefficient and increase WorkingPrecision *)
NonlinearModelFit[data, {(5179098401833815/10^22)*MBB, True}, {MBB}, {ds},
WorkingPrecision -> 30]["BestFitParameters"]
(* {MBB -> 1.93083729107428342103958129882812530.*^6} *)

(* Scale the multiplicative coefficient even just slightly *)
10*MBB /. NonlinearModelFit[
data, {5.179098401833815*10^(-6) MBB, True}, {MBB}, {ds}]["BestFitParameters"]
(* 1.9304817970723812*^6 *)

(* Remove the unnecessary multiplicative coefficient for the fitting
and then include it after fitting *)
(MBB /. NonlinearModelFit[
data, {MBB, True}, {MBB}, {ds}]["BestFitParameters"])/(5.179098401833815*10^(-7))
(* 1.9308380000000002*^6 *)

• For a linear function (which the example is) using LinearModelFit does give the appropriate answer: c = Table[5.179098401833815*10^(-7), {i, iMax}]; y = {1, 1, 1}; LinearModelFit[y, c, x, IncludeConstantBasis -> False]["BestFitParameters"]` (although it doesn't have the capability to include constraints). – JimB Oct 18 '17 at 21:46