Tried to optimize the function but getting output as infinite

4 views (last 30 days)
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
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

Sign in to comment.

Answers (2)

John D'Errico
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
Bharathi Surisetti
Bharathi Surisetti on 23 Nov 2016
Edited: Walter Roberson on 23 Nov 2016
Hi , I tried to use the optimization toolbox. As I compile this code , the system gets stuck . I don't know what's wrong.
clc
close all
load PCG_1.mat
syms x1 x2 positive
N=1500; % Length of the window
n=1500; % Length of the signal
a = env(1:n+N+2);
a0=[1 1]; % Initial point
tic
for i=1:n
a1= a(i:i+N-1);
for j=1:n
a2=a(1+j:1+j+N-1);
K(i,j) = (x1^2)* exp(-((a1-a2)*(a1-a2)')*0.5/(x2^2)); % Equation (5) from [1] % Squares exponential covariance function
end
end
t=toc;
f= 0.5*a'*inv(K)*a...
+ 0.5 * log(det(K))...
+ n/2 *log(2*pi);
x = fminsearch(f,a0)

Sign in to comment.


Walter Roberson
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
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.
Bharathi Surisetti
Bharathi Surisetti on 1 Dec 2016
I think its because of the covariance matrix . It should be positive definite which is not in my case.
The way I implemented the covariance matrix is it right? Here's the link for the paper.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!