Skip to main content

fitting - 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.


Thank you for your help!


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!



Answer




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


Comments

Popular posts from this blog

plotting - Filling between two spheres in SphericalPlot3D

Manipulate[ SphericalPlot3D[{1, 2 - n}, {θ, 0, Pi}, {ϕ, 0, 1.5 Pi}, Mesh -> None, PlotPoints -> 15, PlotRange -> {-2.2, 2.2}], {n, 0, 1}] I cant' seem to be able to make a filling between two spheres. I've already tried the obvious Filling -> {1 -> {2}} but Mathematica doesn't seem to like that option. Is there any easy way around this or ... Answer There is no built-in filling in SphericalPlot3D . One option is to use ParametricPlot3D to draw the surfaces between the two shells: Manipulate[ Show[SphericalPlot3D[{1, 2 - n}, {θ, 0, Pi}, {ϕ, 0, 1.5 Pi}, PlotPoints -> 15, PlotRange -> {-2.2, 2.2}], ParametricPlot3D[{ r {Sin[t] Cos[1.5 Pi], Sin[t] Sin[1.5 Pi], Cos[t]}, r {Sin[t] Cos[0 Pi], Sin[t] Sin[0 Pi], Cos[t]}}, {r, 1, 2 - n}, {t, 0, Pi}, PlotStyle -> Yellow, Mesh -> {2, 15}]], {n, 0, 1}]

plotting - Plot 4D data with color as 4th dimension

I have a list of 4D data (x position, y position, amplitude, wavelength). I want to plot x, y, and amplitude on a 3D plot and have the color of the points correspond to the wavelength. I have seen many examples using functions to define color but my wavelength cannot be expressed by an analytic function. Is there a simple way to do this? Answer Here a another possible way to visualize 4D data: data = Flatten[Table[{x, y, x^2 + y^2, Sin[x - y]}, {x, -Pi, Pi,Pi/10}, {y,-Pi,Pi, Pi/10}], 1]; You can use the function Point along with VertexColors . Now the points are places using the first three elements and the color is determined by the fourth. In this case I used Hue, but you can use whatever you prefer. Graphics3D[ Point[data[[All, 1 ;; 3]], VertexColors -> Hue /@ data[[All, 4]]], Axes -> True, BoxRatios -> {1, 1, 1/GoldenRatio}]

plotting - Mathematica: 3D plot based on combined 2D graphs

I have several sigmoidal fits to 3 different datasets, with mean fit predictions plus the 95% confidence limits (not symmetrical around the mean) and the actual data. I would now like to show these different 2D plots projected in 3D as in but then using proper perspective. In the link here they give some solutions to combine the plots using isometric perspective, but I would like to use proper 3 point perspective. Any thoughts? Also any way to show the mean points per time point for each series plus or minus the standard error on the mean would be cool too, either using points+vertical bars, or using spheres plus tubes. Below are some test data and the fit function I am using. Note that I am working on a logit(proportion) scale and that the final vertical scale is Log10(percentage). (* some test data *) data = Table[Null, {i, 4}]; data[[1]] = {{1, -5.8}, {2, -5.4}, {3, -0.8}, {4, -0.2}, {5, 4.6}, {1, -6.4}, {2, -5.6}, {3, -0.7}, {4, 0.04}, {5, 1.0}, {1, -6.8}, {2, -4.7}, {3, -1.