What is FindFormula returning?

I was playing around with FindFormula and am experiencing some odd behavior.

I am running MMA 12.0.0.0 on a Windows 10, x86 architecture - if that matters.

Example 1: All is well

data = Table[{x, Sin[2 x] + Cos[x]}, {x, RandomReal[{-10, 10}, 1000]}];

fits = FindFormula[data, x, 5, All]


This example returns fives formulas consistently and has no issues.

Example 2: Peculiar behaviors

data = {{0, 3}, {2, 5}, {10, 4}, {6, 2}};

ClearAll[fits];

fits = FindFormula[data, x, 5, All]


The behavior is quite peculiar.

1. Sometimes it returns 1 correct formula followed by 4 numbers. What are those 4 numbers supposed to represent?
1. Sometimes it returns 3 or 4 correct formulas followed by 2 or 1 numbers.
1. Sometimes it returns five correct formulas (most infrequent).

To see this behavior, just keep running the last commands again and do it multiple times.

Here is an image to show some of these behaviors.

Can someone explain what is going on?

• If you are getting a single number as formula, it represents y == constant. I believe that FindFormula uses stochastic heuristics, which accounts for the different results over multiple calls. – m_goldberg May 16 at 12:33
• @m_goldberg: Does that mean that those single number results should just be ignored because they aren't even close to any reasonable formula? – Moo May 16 at 12:41
• @AntonAntonov: Image has been added for two runs. Thanks – Moo May 16 at 12:52
• If there is much of any measurement error and just 4 data points, then FindFormula should be renamed WishfulThinking. One tends to get a constant (such a 3.5 which is the mean of the response variable) when there are very few data points and not much of a relationship. Getting a constant as the predictor does not automatically mean there is a poor fit. There just might not be much of a relationship to fit. – JimB May 16 at 15:14

Options[FindFormula]

(* {Method -> Automatic, TargetFunctions -> All, TimeConstraint -> Automatic,
SpecificityGoal -> 0.8, RandomSeeding -> 1234, "Monitor" -> False,
PerformanceGoal -> Automatic} *)


The option RandomSeeding specifies what seeding of pseudorandom generators should be done inside the operation of FindFormula. With the default Automatic seeding, a different seed is used for each instance. This may cause the results to change when repeatedly called. With an explicit random seeding, the result will always be the same. The other options can also affect the results. When a constant is returned, a good fit was not found within the TimeConstraint and the result is just a number approximating the data. Specifying a TimeConstraint can produce better results.

ClearAll[fits];

data = {{0, 3}, {2, 5}, {10, 4}, {6, 2}};

fits = FindFormula[data, x, 5, RandomSeeding -> 0]

(* {3. + 2.12083 x - 0.65 x^2 + 0.0447917 x^3, 3.5, 3.4433, 3.56611, 3.68451} *)

fits2 = FindFormula[data, x, 5, RandomSeeding -> 0]

(* {3. + 2.12083 x - 0.65 x^2 + 0.0447917 x^3, 3.5, 3.4433, 3.56611, 3.68451} *)


With the same RandomSeeding the results are identical:

fits === fits2

(* True *)


Allowing more time to find a fit,

fits3 = FindFormula[data, x, 5, RandomSeeding -> 0,
TimeConstraint -> 10]

(* {4.55207 - 0.180593 x - 1.55207 Cos[x] - 0.0747033 Tan[x],
4.55207 - 0.180593 Abs[x] - 1.55207 Cos[x] - 0.0747033 Tan[x],
2.87535 + 1.00482 E^-x - 0.880171 Sec[x] - 0.138999 Sin[x],
2.87535 + 1.00482 E^-x - 0.880171 Sec[x] - 0.138999 Sin[Abs[x]],
3. + 2.12083 x - 0.65 x^2 + 0.0447917 x^3} *)

• From a usability point of view, I would think that the return should state something to the effect of "Some results are not usable, recommend increasing TimeConstraint" to inform users rather then generating results that are not valid. My 2 cents. Thanks. – Moo May 16 at 14:46
• @Moo On the other hand, I would not want to have some messages like this to appear in repeated executions of my functions. (Which are, say, within a package.) I mean that your "two cents" come from the perspective that "Mathematica is a fancy calculator." Also, I would like to point out that there is a PerformanceGoal option: setting that option to "Quality" might be more inline with your expectations. – Anton Antonov May 16 at 20:40
• @AntonAntonov: I truly understand your point. Having said that, why not eliminate the result altogether and give no result at all. As a user, I see that and am now spending my cycles trying to decode why it happened. Why show results that make no sense? So, I can accept that you don't want to flood users with repeated error messages, but the premise is that you shouldn't display incorrect results and if you do, you should somehow warn the user and make a recommendation to tell them the results are suspect with a helper message to increase TimeConstraint and only show this once in any session. – Moo May 16 at 21:46
• @Moo Please consider making a Community (community.wolfram.com) discussion with your last comment and original question. That might produce interesting/fruitful responses. (I will answer that comment of yours later...) – Anton Antonov May 16 at 22:30