Tried to optimize the function but getting output as infinite
4 views (last 30 days)
Show older comments
Need to minimize below equation:
F=1/ 2 x^T (Ks +)^−1 x − 1/ 2 log( Ks + Kn) − M/ 2 log(2π).
where Ks = s^2 exp ((x-x')*(x-x')'/(2*l^2))
have to find parameters s,l.
Used Gradient Descent algorithm:
load PCG_1.mat
syms x1 x2 positive
N=5; % Length of the window
n=5; % Length of the signal
%a = mecg(1:n+N+2)';
a = env(1:n+N+2);
a0=[10 1]; % Initial point
for i=1:n
a1= a(i:i+N-1);
for j=1:n
a2=a(1+j:1+j+N-1);
k1(i,j) = (x1^2)* exp(-((a1-a2)*(a1-a2)')*0.5/(x2^2)); % Equation (5) from [1] % Squares exponential covariance function
end
end
a= a(1:n)';
x0=a0;
K=k1;
tol= 1e-6; % tolerence
maxiter=1000; % maximum iterations
dxmin=1e-6; % differences
alpha=0.1; % step size
gnorm =inf ; x=x0' ; niter =0 ; dx= inf;
syms x1 x2 positive
f= 0.5*a'*inv(K)*a...
+ 0.5 * log(det(K))...
+ 1000/2 *log(2*pi);
g1= jacobian (f, [x1 , x2]);
while and(gnorm>=tol , and(niter <=maxiter , dx >=dxmin))
g= (eval(subs(g1,{x1,x2},x')))';
g
gnorm =norm(g);
xnew = x - alpha *g;
if ~isfinite(xnew)
error('x is inf or NaN')
end
niter = niter +1;
dx= norm (xnew - x);
x=xnew;
end
1 Comment
Walter Roberson
on 23 Nov 2016
Please recheck your equation F. It appears to be missing symbols as you have (Ks +) with nothing between the + and the close bracket.
Is Kn a constant or related to Ks? A description of what each name is and its size would help
Answers (2)
John D'Errico
on 18 Nov 2016
So what is your problem? No optimization is guaranteed to converge to the local solution that you may desire. We don't know if your data is the problem or if you have mis-implemented this algorithm or mis-wrote the objective function.
Instead, you will be far better off using a good optimization code, written by professionals. At least there the question will not be if there is a problem in the code.
Don't write your own optimizers. There are better codes already written. Use them. Start with those in the optimization toolbox, or other optimization tools like the genetic algorithms TB. Others exist too.
1 Comment
Walter Roberson
on 23 Nov 2016
You should be pre-allocating your K.
But even if you do that: setting the values in your K matrix is quite slow. Slow enough that you should probably write it as a completely different phase that creates K and f and saves it to a file and then creates f, and saves it to a file, so that you would then have f available to try different minimization algorithms. You should also be using matlabFunction to generate a function handle to send to fminsearch as fminsearch does not expect symbolic expressions.
I put in a waitbar() to monitor how far into i had been done; it is taking a few minutes each i. You should consider using parfor
11 Comments
Walter Roberson
on 28 Nov 2016
Your K becomes all 0 if x1 goes to 0, and that is not invertible. You need a bounded search but fmincon is always unbounded. Or you could fake it with max(eps, x1.^2)
There might be other circumstances in which your K has bad conditioning or low rank.
See Also
Categories
Find more on Nonlinear Optimization in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!