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

I have a set of measurement data and I try to fit there a function (a Exp[b x] + c Exp[d x] - o). I have two problems:

• The fit function does not converge nor after $500$ iterations.
• The fit function always has to be equal or less than the data points.

fitdata = {{-0.826333, 9.72503}, {-0.796121, 9.61975}, {-0.765909, 8.25744},
{-0.735697, 7.40448}, {-0.705485, 5.38543}, {-0.675273, 5.15899},
{-0.645061, 4.9356}, {-0.614848, 4.67804}, {-0.584636, 4.28955},
{-0.554424, 4.73144}, {-0.524212, 4.93957}, {-0.494, 4.80621},
{-0.463788, 5.77301}, {-0.433576, 3.39416}, {-0.403364, 1.73309},
{-0.373152, 1.4862}, {-0.342939, 1.4212}, {-0.312727, 1.49505},
{-0.282515, 1.35071}, {-0.252303, 1.28845}, {-0.222091, 1.25183},
{-0.191879, 1.43158}, {-0.161667, 1.39557}, {-0.131454, 1.40259},
{-0.101242, 1.36108}, {-0.0710303, 1.2265}, {-0.0408181, 1.23474},
{-0.0106061, 1.24481}, {0.0196061, 1.26526}, {0.0498182, 1.32446},
{0.0800303, 1.31866}, {0.110242, 1.35345}, {0.140455, 1.45935},
{0.170667, 1.58142}, {0.200879, 1.64947}, {0.231091, 1.72851},
{0.261303, 1.79931}, {0.291515,1.53534}, {0.321727, 1.76849},
{0.351939, 2.56439}, {0.382152, 3.65875}, {0.412364, 1.30584},
{0.442576, 2.4179}, {0.472788, 3.02307}, {0.503, 1.58539},
{0.533212, 1.45324}, {0.563424, 1.4743}, {0.593636, 1.42791},
{0.623849, 1.44165}, {0.654061, 1.56433}, {0.684273, 1.68152},
{0.714485, 2.1933}, {0.744697, 2.30194}, {0.774909, 2.29156},
{0.805121, 2.62207}, {0.835333, 3.09906}, {0.865546, 3.17169},
{0.895758, 4.08508}, {0.92597, 7.48046}, {0.956182, 4.48303},
{0.986394, 4.11621}, {1.01661, 4.18457}, {1.04682, 4.72107},
{1.07703, 5.77667}, {1.10724, 6.35589}, {1.13745, 6.41082},
{1.16767, 7.43164}, {1.19788, 9.28222}};


@ All - The bigger the choice, the harder it is to choose. Thank you for incredible nice and quick answers!

• Welcome! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour and check the faqs! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign!
– user9660
May 11 '16 at 13:26
• @Lukas - I think it is that the fitted function always has to be less than the data, so in fact the function has an upper bound given by the data. May 11 '16 at 13:57
• @JasonB Even though my comment disappeared for some reason, this makes sense. So I will delete my answer - I have overlooked this bit of information in the question. May 11 '16 at 14:45
• Note that fac1*Exp[e1*(x - off1)] is equivalent to (fac1 Exp[-e1 off1]) Exp[e1 x] . The term in parenthesis can be treated as a single parameter and re-written as a Exp[b x]. In other words, the original equation from the OP should work fine, given a reasonable starting point. May 11 '16 at 15:05
• @JackLaVigne You're absolutely right - it must be too late here ~.~ However, I deleted my answer since I overlooked the restriction that the fit function must be less or equal than the data for any value of x. May 11 '16 at 15:07

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]]


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)]


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]]


## 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]]


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]]


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]]


## 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]]


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