9
$\begingroup$

I am trying to fit my data to a combined function model. Say I have two functions with some common parameter(s)

f[x_]:= a*x^2 + b*x + c
g[x_]:= d*x^2 + e*x + c

Also, I have separate data and errors for these two functions.

Performing the fit is easy: I create the chi-squared function manually and minimize via NMinimize. In my work the code is rather lengthy, but looks similar to

Chi2[data_, errors_, dataprime_, errorsprime] := 
Sum[((data[[i, 2]] - f[data[[i, 1]]])/errors[[i]])^2,{i, 1, Length[data]}] + 
Sum[((data[[i, 2]] - g[data[[i, 1]]])/errors[[i]])^2,{i, 1, Length[data]}];

where data and dataprime are the different datasets for the functions f[x] and g[x] respectively (data[[i,1]] represents, say, the i-th x value while data[[i,2]] represents what f[data[[i,1]]] should be, say, y).

However, I would like to perform, using the values obtained from NMinimize, a NonlinearModelFit.

This would give me error estimation for parameters (which are hard to get with my current technique) and also provides confidence band levels and other features which are quite useful to me.

However, I haven't been able to find the way to implement a NonlinearModelFit ... any suggestion?

(I already did a search and found some links related to calculating the errrors through the Hessian. However, this is not convenien for me due to errors in calculating the inverse of the Hessian.)

Thanks in advance!

$\endgroup$
3
  • $\begingroup$ Maybe this will help. $\endgroup$ Dec 7, 2012 at 10:13
  • $\begingroup$ I will give it a try, I was wondering doing a similar thing using a piecewisefunction, however, probably this is more convenient. I will let you know @b.gatessucks as soon as I am done ;) $\endgroup$
    – pablo
    Dec 7, 2012 at 10:26
  • $\begingroup$ @b.gatessucks I was trying with a simple function, then I defined myfunc[y_, x_] := (a*x)*KroneckerDelta[1 - y]; And then performed the NonlinearModelFit eo = NonlinearModelFit[{{1, 1, 2.6}, {1, 2, 6.1}, {1, 3, 9.3}, {1, 4,11.9}, {1, 5, 15.4}}, myfunc[y, x], {a}, {y, x}] Howevere I get some error: "Experimental`NumericalFunction::dimsl: "{x} given in {y,x} should be a list of dimensions for a particular argument" Though fit seems ok. I don't get the problem, do you wonder what may be? THanks in advance! $\endgroup$
    – pablo
    Dec 7, 2012 at 11:14

1 Answer 1

14
$\begingroup$

That's one way, without the errors; you can add them as an exercise.

(* Sample data *)
SeedRandom[1];
dataf = Table[{i + RandomReal[{-0.4, 0.4}], i^1.5}, {i, 10}];
datag = Table[{i + RandomReal[{-0.4, 0.4}], i^2 - RandomReal[{-5, 5}]}, {i, 10}];

(* Prepend 1 to data for f, 2 to data for g *)
allData =  Join[{1, Sequence @@ #} & /@ dataf, {2, Sequence @@ #} & /@ datag];
f[x_]:= a*x^2 + b*x + c
g[x_]:= d*x^2 + e*x + c
myF[index_, x_] :=  KroneckerDelta[index - 1] f[x] + KroneckerDelta[index - 2] g[x]

nlm = NonlinearModelFit[allData, myF[index, x], {a, b, c, d, e}, {index, x}];

Show[ListPlot[{dataf, datag}],  Plot[{nlm[1, x], nlm[2, x]}, {x, 1, 10}]]

plot

nlm["ParameterConfidenceIntervalTable"]

table

Adding relative weighting is easily achieved via e.g.

nlm = NonlinearModelFit[allData, 
 myF[index, x], {a, b, c, d, e}, {index, x}, 
 Weights -> Join[Array[1 &, 10], Array[200 &, 10]]]

so that the new fits obeys

 nlm["ParameterConfidenceIntervalTable"]

Mathematica graphics

$\endgroup$
4
  • $\begingroup$ Yes, that's perfect, thank you a lot! Now it's working for me thanks to you $\endgroup$
    – pablo
    Dec 7, 2012 at 12:04
  • $\begingroup$ THanks for the corrections! ;) @m_goldberg $\endgroup$
    – pablo
    Dec 7, 2012 at 16:44
  • $\begingroup$ I am puzzled by the error message : Experimental`NumericalFunction::dimsl: {x} given in {index,x} should be a list of dimensions for a particular argument. I ran the elegant solution with KroneckerDelta on my Laptop using Mathematica 10.1, however the error message persists. I am wondering what caused the error message and would appreciate an explanation. Best wishes Dieter Haidt $\endgroup$
    – user47621
    Mar 25, 2017 at 6:11
  • $\begingroup$ I hope you don't mind the edit. If not please erase. $\endgroup$
    – chris
    Apr 18, 2017 at 17:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.