fitting curve with constrained parameter estimation

27 views
Skip to first unread message

Hany Ferdinando

unread,
Dec 4, 2018, 8:23:25 AM12/4/18
to OPTI Toolbox Forum
Hi,

I have xdata-ydata pair and would like to estimate parameters of my own function to fit it to xdata-ydata pair. Using examples in https://www.inverseproblem.co.nz/OPTI/index.php/Probs/NLS as a guidance, I set up linear constraints to A and b, assigned value for upper and lower bounds. 

fun = @(x,t)((((x(1)*lognpdf(t,log(x(2)),x(3))+x(4)*lognpdf(t,log(x(5)),x(6))+x(7)*lognpdf(t,log(x(8)),x(9))+x(10)*lognpdf(t,log(x(11)),x(12))+x(13)*lognpdf(t,log(x(14)),x(15))))));

Opt = opti('fun', fun, 'data', xdata, ydata, 'ineq', A, B, 'bounds', lb, ub, 'x0', x0, 'solver', 'levmar');

It converged in about 1 s. Is there any way to speed up this computation? Did I use the correct method?

Thanks a lot

Best
Hany

Jonathan Currie

unread,
Dec 5, 2018, 2:48:26 AM12/5/18
to OPTI Toolbox Forum
Hi,

For a 15 variable constrained NLS fitting problem, especially one calling statistical functions, 1s sounds pretty fast to me. However if you want to add the gradient to OPTI (rather than finite difference), it might help. Below is an example of how to do this using the symbolic toolbox:

% Core of lognpdf.m:
lognpdf = @(x,mu,sigma) exp(-0.5 * ((log(x) - mu)./sigma).^2) ./ (x .* sqrt(2*pi) .* sigma);

% Your objective
fun = @(x,t)((((x(1)*lognpdf(t,log(x(2)),x(3))+x(4)*lognpdf(t,log(x(5)),x(6))+x(7)*lognpdf(t,log(x(8)),x(9))+x(10)*lognpdf(t,log(x(11)),x(12))+x(13)*lognpdf(t,log(x(14)),x(15))))));

% Create symbolic version
x = sym('x',[15,1]);
t = sym('t');
fSym = fun(x,t)

% Calculate analytic gradient
gradSym = jacobian(fSym,x)

% And convert to anonymous function
gradVec = matlabFunction(gradSym, 'vars', {x,t})
% Reshape to matrix
grad = @(x,xdata) reshape(gradVec(x,xdata), length(xdata), length(x0));

% Solve it
opts = optiset('display','iter','solver','levmar');
Opt = opti('fun', fun, 'grad', grad, 'data', xdata, ydata, 'ineq', A, B, 'bounds', lb, ub, 'x0', x0, 'opts', opts)
[x,f,e,i] = solve(Opt)

% And plot solution
plot(Opt)

Jonathan

--
You received this message because you are subscribed to the Google Groups "OPTI Toolbox Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to opti-toolbox-fo...@googlegroups.com.
To post to this group, send email to opti-tool...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages