Error using barrier. Objective function is undefined at initial point. Fmincon cannot continue.
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Imane Zemmouri
el 24 de Oct. de 2023
Error using barrier. Objective function is undefined at initial point. Fmincon cannot continue.
Anyone can give me an idea how to solve it? Running code as attach. Thank you very much.
fit_S_powerAGARCH_N
%=========================================================================
% Estimating the standard AGARCH-X Models by QMLE
%=========================================================================
function fit_S_powerAGARCH_N( )
clear all
clc
addpath D:\work\Econometrics\econ
%ss = RandStream('mt19937ar','Seed',1);
%RandStream.setGlobalStream(ss);
rand('seed',00);
%data=xlsread('taux1.xlsx');
data=xlsread('empirics.xlsx');
y1=data(:,7);
%n=size(y1)
x1=data(:,8);
x2=data(:,9);
%n=size(x2)
x3=data(:,10);
%nn=size(x3)
delta=1.0;
%r1=100*price2ret(y1); r1=r1-mean(r1);
%r2=100*price2ret(y2);r2=r2-mean(r2);
%print -deps D:\Recherches\Log_PGarch\Gmail\plot9.eps
r=y1;
%options=optimset('MaxFunEvals',500,'Maxiter',500,'Display','iter');
ops = optimset( 'LargeScale','off','Display','off');
start=rand(1,7);%.25*[0.019 0.0450 0.115 0.872 2.943 0.051 0.028];%20.9*
[theta,lf1,~,~,~,hess]=fminunc(@(b) neglog(b,r,x1,x2,x3),start,ops)
a=real(theta)
h(1)=r(1);%2;%*sqrt(var(r));
%mu=mean(abs(r));
nobs=length(r);
for j = 2:nobs
%h(j)=exp(0.5*a(1))*(abs(r(j-1)))^a(2)*h(j-1)^a(3);
h(j) = a(1)+a(2)*(max(r(j-1),0)).^delta+a(3)*(max(-r(j-1),0)).^delta+a(4)*h(j-1)+a(5)*x1(j-1)+a(6)*x2(j-1)+a(7)*x3(j-1);
%h(j) = a(1)+(a(2)+a(3))*abs(r(j-1)).^delta+a(4)*h(j-1)+a(5)*x1(j-1)+a(6)*x2(j-1)+a(7)*x3(j-1);
end
plot(h)
hold on
plot(0.11*r.^2)
%print -deps D:\Recherches\Log_PGarch\Gmail\plot10.eps
%pause
%save('egarch100_d1.mat','theta1')
end
%-------------------------------------------------------------------------
% Likelihood function for a logGARCH(1,1) model
%-------------------------------------------------------------------------
function logt = neglog(b,y,m1,m2,m3)
%b = abs(b);
nobs = length(y);
%yy=yy';
delta=1.0;
v = y;
u1=m1;
u2=m2;
u3=m3;
h = abs(v)+log(2*abs(v)+1)+exp(2.5)*ones(nobs,1);
%h = 4*ones(nobs,1);
for j = 2:nobs
%h(j)=exp(0.5*b(1))*(abs(v(j-1)))^b(2)*h(j-1)^b(3);
h(j) = b(1)+b(2)*(max(v(j-1),0)).^delta+b(3)*(max(-v(j-1),0)).^delta+b(4)*h(j-1)+b(5)*m1(j-1)+b(6)*m2(j-1)+b(7)*m3(j-1);
%h(j) = b(1)+0.5*(b(2)+b(3))*abs(v(j-1)).^delta+b(4)*h(j-1)+b(5)*m1(j-1)+b(6)*m2(j-1)+b(7)*m3(j-1);
end
%logl= - 0.5*log(2*pi) - 0.5*log(h.^2) - 0.5*(v./h).^2;
logl = - 0.5.*log(2.*pi) -(1./delta).*log(h) - 0.5.*(v./(h).^(1./delta)).^2;
logt = -mean(logl);
end
0 comentarios
Respuesta aceptada
Walter Roberson
el 24 de Oct. de 2023
Your first returns in the xlsx file is empty. empty is read in as NaN.
The returns column gets passed into the objective function as x1. Because of the empty entry, x1 starts with NaN.
for j = 2:nobs
h(j) = a(1)+a(2)*(max(r(j-1),0)).^delta+a(3)*(max(-r(j-1),0)).^delta+a(4)*h(j-1)+a(5)*x1(j-1)+a(6)*x2(j-1)+a(7)*x3(j-1);
You use x1(j-1) to calculate h(j) . When j = 2 (first iteration of the loop) that is a reference to x1(1) but x1(1) is NaN.
This "poisons" the rest of your calculations.
You also use x3(j-1) in your calculation. x3 comes from the "relative volume" column. Your first 20 rows of "relative volume" are empty in your xlsx file and so are read in as NaN.
To prevent this poisoning of the data, right after you read the data you can insert
data(isnan(data)) = 0;
This will prevent the code from crashing.
Whether the code will produce "correct" answers is a different question. As it calculates values up to about 1e+18 I suspect the calculations are either incorrect or else the work-around of inserting 0 is not appropriate.
I also experimented with removing any data row that had a NaN. The output does not appear to be significantly different than if I change the NaN to 0.
0 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!