Fit a function to data so that fit is always equal or less than the data

Let's first solve it without constraints.

Using fitdata from your question one needs to set reasonable starting values. You may have to experiment (I like to use Manipulate) to get in the ball park.

nlm = NonlinearModelFit[fitdata, a*Exp[b*x] + c*Exp[d*x] + o,
      {{a, 0.1}, {b, 3}, {c, 0.3}, {d, -4}, {o, 1}}, x]

yields

nlm["BestFitParameters"]
(* {a -> 0.127629, b -> 3.38374, c -> 0.310767, d -> -4.07292, 
 o -> 1.02299} *)

Now plot it

Show[ListPlot[fitdata, PlotStyle -> Black], 
 Plot[nlm[x], {x, -1, 1.5}, PlotStyle -> Red]]

Mathematica graphics

Constrained Solution

In order to implement the constraints we will create a list (one for each point) using Map.

Map[a*Exp[b*#[[1]]] + c*Exp[d*#[[1]]] + o <= #[[2]] &, fitdata]

I won't print the result here but you can see it in your notebook.

Now we want to include those constraints in the optimization. One form is to wrap the model in a list and add the constraints separated by commas. This can be done if we Apply the function Sequence to the list.

nlmConstrained = NonlinearModelFit[fitdata, {a*Exp[b*x] + c*Exp[d*x] + o,
   Sequence @@Map[a*Exp[b*#[[1]]] + c*Exp[d*#[[1]]] + o <= #[[2]] &, 
     fitdata]}, {{a, 0.05}, {b, 4}, {c, 0.1}, {d, -5}, {o, 0.7}}, x]

the result is

nlmConstrained["BestFitParameters"]
(* {a -> 0.0579711, b -> 4.02179, c -> 0.0963365, d -> -5.49028,
  o -> 0.725906} *)

and plotting it shows that the constraints are satisfied.

Show[ListPlot[fitdata, PlotStyle -> Black], 
 Plot[nlmConstrained[x], {x, -1, 1.5}, PlotStyle -> Red]]

Mathematica graphics

I was pleasantly suprised to find that solver was fast despite having 68 constraints imposed.


This answer provides two solutions both use Quantile regression.

  1. The first uses sort of brute force fitting with a family of curves.

  2. The second is similar to the one by Jack LaVigne, but uses QuantileRegressionFit on the second step, which in this case seems to be more straightforward.

This command loads the package QuantileRegression.m used below:

Import["https://raw.githubusercontent.com/antononcube/MathematicaForPrediction/master/QuantileRegression.m"]

1. QuantileRegressionFit only

Generate a family of functions:

funcs = Table[Exp[k*x], {k, -6, 6, 0.002}];
Length[funcs]

(* 6001 *)

Do Quantile regression fit with the family of functions:

qfunc = First@QuantileRegressionFit[fitdata, funcs, x, {0.01}]

(* 0. + 0.0125232 E^(-5.398 x) + 0.0930724 E^(-5.392 x) + 
 0.232695 E^(0.2 x) + 0.511794 E^(0.214 x) + 0.039419 E^(4.336 x) *)

and visualize:

Show[
 ListPlot[fitdata, PlotStyle -> Black, PlotRange -> All],
 Plot[qfunc, {x, -1, 1.5}, PlotStyle -> Red, PlotRange -> All]]

enter image description here

The found fitted function satisfies the condition to be less or equal of the data points, but it is made of too many Exp terms. So the next step is to reduce the number of Exp terms to two as required in the question.

We can visually evaluate the contribution of each of the terms in the found fit:

Plot[Evaluate[(List @@ qfunc)/qfunc], {x, -1, 1.5}, PlotRange -> All, 
 PlotLegends -> (List @@ qfunc)]

enter image description here

We pick two of the terms (plus the intercept) and call QuantileRegressionFit again:

qfunc2 = 
 First@QuantileRegressionFit[fitdata, 
   Prepend[(List @@ qfunc)[[{3, -1}]], 1], x, {0}]

(* 0.658637 + 0.105279 E^(-5.392 x) + 0.0414842 E^(4.336 x) *)

Show[ListPlot[fitdata, PlotStyle -> Black, PlotRange -> All],
 Plot[qfunc2, {x, -1, 1.5}, PlotStyle -> Red, PlotRange -> All]]

enter image description here

2. NonlinearModelFit model followed by QuantileRegressionFit

First we find a model with the required functions:

nlm = NonlinearModelFit[fitdata, 
  a*Exp[b*x] + c*Exp[d*x] + o, {{a, 0.1}, {b, 3}, {c, 0.3}, {d, -4}, {o, 1}}, x]

nlm["BestFitParameters"]

(* {a -> 0.127629, b -> 3.38374, c -> 0.310767, d -> -4.07292, 
 o -> 1.02299} *)

Show[ListPlot[fitdata, PlotStyle -> Black], 
 Plot[nlm[x], {x, -1, 1.5}, PlotStyle -> Red]]

enter image description here

Using the model functions:

{Exp[b*x], Exp[d*x]} /. 
 Cases[nlm["BestFitParameters"], HoldPattern[(b | d) -> _]]

(* {E^(3.38374 x), E^(-4.07292 x)} *)

find the quantile regression curve that is below the data points (0. quantile):

qfunc = 
 First@QuantileRegressionFit[
   fitdata, {1, Exp[b*x], Exp[d*x]} /. 
    Cases[nlm["BestFitParameters"], HoldPattern[(b | d) -> _]], 
   x, {0.0}]

(* 0. + 0.303621 E^(-4.07292 x) + 0.13403 E^(3.38374 x) *)

Plot the result:

Show[
 ListPlot[fitdata, PlotStyle -> Black, PlotRange -> All], 
 Plot[qfunc, {x, -1, 1.5}, PlotStyle -> Red, PlotRange -> All]]

enter image description here

Tags:

Fitting