简体   繁体   中英

Matlab: fmincon throws error

I am implementing the expression given in the image which is the log-likelihood for AR(p) model. 对数似然

In this case, p=2 . I am using fmincon as the optimization tool. I checked the documentation and other examples over internet regarding the syntax of this command. Still, I am unable to mitigate the problem. Can somebody please help in eliminating the problem?

The following is the error

Warning: Options LargeScale = 'off' and Algorithm = 'trust-region-reflective' conflict.
Ignoring Algorithm and running active-set algorithm. To run trust-region-reflective, set
LargeScale = 'on'. To run active-set without this warning, use Algorithm = 'active-set'. 
> In fmincon at 456
  In MLE_AR2 at 20 
Error using ll_AR2 (line 6)
Not enough input arguments.

Error in fmincon (line 601)
      initVals.f = feval(funfcn{3},X,varargin{:});

Error in MLE_AR2 (line 20)
   [theta_hat,likelihood] =
   fmincon(@ll_AR2,theta0,[],[],[],[],low_theta,up_theta,[],opts);

Caused by:
    Failure in initial user-supplied objective function evaluation. FMINCON cannot
    continue.

The vector of unknown parameters,

theta_hat = [c, theta0, theta1, theta2] where c = intercept in the original model which is zero ; theta0 = phi1 = 0.195 ; theta1 = -0.95; theta2 = variance of the noise sigma2_epsilon.

The CODE:

     clc
    clear all
    global ERS
    var_eps = 1;
    epsilon = sqrt(var_eps)*randn(5000,1); % Gaussian signal exciting the AR model
    theta0 = ones(4,1);  %Initial values of the parameters
    low_theta = zeros(4,1); %Lower bound of the parameters
    up_theta = 100*ones(4,1); %upper bound of the parameters
    opts=optimset('DerivativeCheck','off','Display','off','TolX',1e-6,'TolFun',1e-6,...
   'Diagnostics','off','MaxIter', 200, 'LargeScale','off');
    ERS(1) = 0.0;
    ERS(2) = 0.0;
    for t= 3:5000
    ERS(t)=  0.1950*ERS(t-1) -0.9500*ERS(t-2)+ epsilon(t); %AR(2) model y
    end


   [theta_hat,likelihood,exit1] = fmincon(@ll_AR2,theta0,[],[],[],[],low_theta,up_theta,[],opts);

exit(1,1)=exit1;

format long;disp(num2str([theta_hat],5))


function L = ll_AR2(theta,Y)
rho0 = theta(1); %c
rho1 = theta(2); %phi1
rho2 = theta(3); %phi2
sigma2_epsilon = theta(4);
T= size(Y,1);
p=2;
mu_p = rho0./(1-rho1-rho2); %mean of Y for the first p samples
%changed sign of the log likelihood expression    
cov_p = xcov(Y);
L1 = (Y(3:end) - rho0 - rho1.*Y(1:end-1) - rho2.*Y(1:end-2)).^2;
 L = (p/2).*(log(2*pi)) + (p/2).*log(sigma2_epsilon) - 0.5*log(det(inv(cov_p))) + 0.5*(sigma2_epsilon^-1).*(Y(p) - mu_p)'.*inv(cov_p).*(Y(p) - mu_p)+...
     (T-p).*0.5*log(2*pi) + 0.5*(T-p).*log(sigma2_epsilon) + 0.5*(sigma2_epsilon^-1).*L1;
L = sum(L);
end

You are trying to pass constant parameters to the objective function ( Y ) in addition to the optimization variables ( theta ).
The right way of doing so is using anonymous function:

 Y = ...; %// define your parameter here
 fmincon( @(theta) ll_AR2(theta, Y), theta0, [],[],[],[],low_theta,up_theta,[],opts);

Now the objective function, as far as fmincon concerns, depends only on theta .

For more information you can read about anonymous functions and passing const parameters .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM