using fminsearch on several data sets simultaneously

6 views (last 30 days)
hello everyone,
I'm a beginner with matlab, so please explain everything in detail...
I have several data sets (testI followed by a number) that have the same equation with three parameters. the first two are different for each data set, the third is the same for all sets. I am trying to find the parameters that best fit my data.
I don't have any problems with fittting each data set individually using
[p1, c2]=fminsearch(@(p)chi2S(p,testConc,testI, testE), p0)
p1 consists of three values, of a, b, and K. When I use the function on each data set, I get different 'best' values for a, b, K.
What I want is to evaluate all sets simultaneously to get a single value for K that is best for all sets. a and b may vary for each set.
I hope I am more or less clear, if you need more info please ask.
Let me now if you have any ideas, Thanks, Yamel

Accepted Answer

Sargondjani
Sargondjani on 23 Jun 2012
As an alternative you could make an optimization routine for K only. That way you would not need fminunc.
It would look very similar to what I did above, but now your objective function would only depend on K. Inside this objective function you optimize a and b for every dataset (for given K). Then you sum the errors and minimize that for K. This way matlab would not have to try to solve all parameters simultaneously.

More Answers (4)

Walter Roberson
Walter Roberson on 18 Jun 2012
minimize the sum of the squares of the fits for all three.
[p1, c2]=fminsearch(@(p) (chi2S(p,testConc,testI1, testE).^2 + chi2S(p,testConc,testI2, testE).^2 + chi2S(p,testConc,testI3, testE).^2), p0)
  2 Comments
mark wentink
mark wentink on 18 Jun 2012
will this not however assume that a and b need to be the same in all equations as well?
Walter Roberson
Walter Roberson on 18 Jun 2012
Good point. I will need to think more about this.

Sign in to comment.


Peter Perkins
Peter Perkins on 22 Jun 2012
The usual way to do this is by using what are called "dummy variables". I can't tell what you mean by, "I have several data sets (testI followed by a number)", so I'm going to take a guess at how to use a dummy variable in your case, you'll have to adapt it as appropriate.
I'll guess that you have two predictor variables, testC and testI, and one response, testE. Let's say you have two sets of those, with lengths n1 and n2. Create a new predictor variable
dummy = [repmat(1,n1,1); repmat(2,n2,1)]
then concatenate the two testC's together, testI's together, testE's together. Now you have one big (n1+n2)x4 set of data: the three original but concatenated variables, and dummy. Your model is chi2S(p,testConc,testI, testE), I'll guess that inside of that you compute something in the form of
sum((testE - f(p,testC,testI)).^2)
To get "stratified" estimates of a and b, and a "pooled" estimate of K, you need is to minimize
sum((testE(dummy==1) - f(p([1 3 5]),testC(dummy==1),testI(dummy==1))).^2) + sum((testE(dummy==2) - f(p([2 4 5]),testC(dummy==2),testI(dummy==1))).^2)
where p is now [a1 a2 b1 b2 K]. Pick starting values, pass this to fminsearch, and there you go. If your model really is this kind of response = f(parameters,predictors) form, I would strongly recommend that you use nlinfit, if you have access to the Statistics Toolbox, or lsqcurvefit, if you have access to the Optimization Toolbox.
Hope this helps.
  1 Comment
Sargondjani
Sargondjani on 23 Jun 2012
i think "several data sets (testI followed by a number)" refers just to their names

Sign in to comment.


Sargondjani
Sargondjani on 23 Jun 2012
To answer the original post: if you have N data sets, you just have to combine the objective functions for the N data sets, so you you simply add the errors (i assume chi2S calculates the error). The inputs should then be: N times a, b and 1 time K. All have to be put in one vector (dim: 2xN+1,1), say:
X0=[K0;a0(1:N,1);b0(1:N,1)] %i put 1:N to emphasize the dimensions
The objective would look like:
function chi2S_tot(K,a,b,testConc,testI, testE) %dim. a & b = (N,1);
error=zeros(N,1);
error = ..... %jusst calculate error for each individual set, as you did before
err_tot = sum(error);
Now the call for fminsearch would be:
[X,c2]=fminsearch(@(X)chi2S_tot(X(1,1),X(2:N+1,1),X(N+2:end,1),......), X0);
K=X(1,1)
a=X(2:N+1,1)
b=X(N+2:end,1)
I dont know how many data sets you have, but the problem is that fminsearch is not well suited for optimizing for more than a couple of variables, so you problably need the optimization toolbox (fminunc) to solve this in a reasonable amount of time... or you can write your own procedure (easier than it seems )

mark wentink
mark wentink on 25 Jun 2012
Just to clarify: I have one predictor value testC, one result testI and an associated error testE. my function chi2S is
function y = chi2S(p, conc, intensity, sigma) y = 0.25*sum(((calcI(p(1), p(2), p(3), conc) - intensity).^2)./(sigma.^2));
where calcI is the function my data is supposed to fit: function I = calcI(alpha, beta, K, T)
I = .125*(4*beta*T + K*(beta-2*alpha)+ (2*alpha+beta)*(sqrt(K*(K+8*T)))) ./ T;
between data sets, I have same testC and testE, but different testI. I have about a hundred testI sets.
  5 Comments
mark wentink
mark wentink on 25 Jun 2012
Hey Sargondjani,
Thanks a lot for your help.
Sadly, during my internship, what this is all part of, I have run into a more immediate problem. The error bars on the data I'm using (I didn't get the data myself), are way to big. Hence one set of parameters is just as likely to be correct as another.
Before all the above becomes of any use, I would have to find a way to get better data, but it is more likely we will turn to another method of finding what we want.
Sorry for the slightly anti-climax ending, and thanks a lot for your help. I might just continue this study to learn a bit more about Matlab.
Thank you for your quick response and clear answers.
Sargondjani
Sargondjani on 25 Jun 2012
you're welcome m8. sorry to hear about your data problem... that's just terrible!!

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!