I have a question: Objective function is undefined at initial point.
이전 댓글 표시
Hello,
I am trying to run a code and it always gave me the same error message:
Error using fminusub (line 16) Objective function is undefined at initial point. Fminunc cannot continue.
I really need some help with my initial starting values or objective function.
Any suggestions are welcomed!
Thanks.
채택된 답변
추가 답변 (3개)
Matt J
2021년 12월 7일
0 개 추천
It looks like you didn't test your objective function to make sure that it runs error-free.
댓글 수: 3
Imane Zemmouri
2021년 12월 7일
Matt J
2021년 12월 7일
I mean you should run your objective function (separate from fminunc) with your initial point as input. If you get an error message or bad output, you'll know you haven't finished debugging your objective function code.
Imane Zemmouri
2021년 12월 8일
Imane Zemmouri
2021년 12월 8일
0 개 추천
댓글 수: 3
Walter Roberson
2021년 12월 8일
It turns out your code ignores h and hh after calculating them, so it doesn't matter what they are!
Instead your code calls Ms_logGARCH_Lik and inside that you have
for j=1:k %Getting full matrices for each state
%
%log((X_t)^2)=log((h_t)^2)+log((e_t)^2)
%
ARplus(:,j)=phi(:,j)+theta(:,j);
ARnega(:,j)=psi(:,j)+theta(:,j);
MA1(:,j)=theta(:,j)*log(e(:,j).^2);
if x>0
AR1(:,j)=ARplus(:,j)*log(x(:,j).^2);
Cond_mean(first_idx:end,j) = C(1,j)+AR1(first_idx-1:end-1,j)-MA1(first_idx-1:end-1,j);% Conditional mean for each state
elseif x<0
AR2(:,j)=ARnega(:,j)*log(x(:,j).^2);
Cond_mean(first_idx:end,j) = C(1,j)+AR2(first_idx-1:end-1,j)-MA1(first_idx-1:end-1,j);% Conditional mean for each state
end
e(:,j)=sqrt(exp(log(x(:,1).^2)-Cond_mean(:,j))); % Error for each state
end
That code is questionable because x is a vector of length 1000, so x>0 is testing all(x<0) and x>0 is testing all(x>0) -- neither of which is true. The code is not calculating according to the condition for each x value: it is ending up skipping those statements.
Now, in this function, m has not been pre-defined. So a few lines later:
case 'Normal'
m(:,j)=(e(:,j)./(sqrt(exp(Cond_mean(:,j)))*sqrt(2*pi))).*exp(-(e(:,j)).^2/2); % Normal prob density at each state
this is after the for j loop, so j is at its last value from the loop -- j = k, and in particular j = 2. So you are assigning into column 2 of m without having assigned anything into column 1 of m. So column 1 of m is 0.
After that,
f=Mmax(n,k);%ones(n,k);
k is 2, n is 1000. Mmax assigns 1:1000 as column 1 of f, and assigns [2;2:1000] as column 2 of f . This creates f and gives it an initial value, none of the entries of which are 0.
for i=first_idx:n
f(i,1)=ones(k,1)'*(p*E(i-1,:)'.*m(i,:)'); % MS Filter equation
E(i,:)=((p*E(i-1,:)'.*m(i,:)')/f(i,1)); % MS Filter equation for probabilities
end
Remember that m(:,1) is 0, so m(i,:) starts with a 0. In the first iteration, E(i-1,:) has been pre-defined (as a pair of integers), and f(2,1) ends up non-zero in the assignment to f(i,1) . But then in the assignment to E, because of the first entry of the row of m being 0 and the .* being used, the first entry of E(i,:) will be 0.
Then on the next iteration when you go to use that E, with the first element being 0 and with the 0 leading m... f(i,1) will become 0. Then on the next line you divide by that f(i,1) that is 0, so you get NaN and infinities...
Maybe the switch needs to be inside the for j loop.
Imane Zemmouri
2021년 12월 8일
Walter Roberson
2021년 12월 13일
We would need your corrected source code to investigate further.
Imane Zemmouri
2021년 12월 8일
0 개 추천
댓글 수: 1
John D'Errico
2021년 12월 8일
편집: John D'Errico
2021년 12월 8일
Please stop writing a new answer every time you want to make a comment. Learn to use a comment instead.
카테고리
도움말 센터 및 File Exchange에서 Conditional Variance Models에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!