Friday, February 15, 2008

The Usage of fmincon in matlab

Try to understand what used in DCC_GARCH model

options=optimset('fmincon');
options = optimset(options , 'Display' , 'iter');
options = optimset(options , 'Diagnostics' , 'on');
options = optimset(options , 'LevenbergMarquardt' , 'on');
options = optimset(options , 'LargeScale' , 'off');


dccstarting=[ones(1,dccP)*.01/dccP ones(1,dccQ)*.97/dccQ];
fprintf(1,'\n\nEstimating the DCC model\n')

[dccparameters,dccllf,EXITFLAG,OUTPUT,LAMBDA,GRAD]=fmincon('dcc_mvgarch_likelihood',dccstarting,ones(size(dccstarting)),[1-2*options.TolCon],[],[],zeros(size(dccstarting))+2*options.TolCon,[],[],options,stdresid,dccP,dccQ);

fmincon('dcc_mvgarch_likelihood',dccstarting,ones(size(dccstarting)),[1-2*options.TolCon],[],[],zeros(size(dccstarting))+2*options.TolCon,[],[],options,stdresid,dccP,dccQ);
<=> (fun, x0, A,b,[],[],lb,[],[],options,stdresid,dccP,dccQ);
Notice options.TolCon is the precision (so here the constraint consider the precision)

1. Syntax

x = fmincon(fun,x0,A,b) starts at x0 and attempts to find a minimum x to the function described in fun subject to the linear inequalities A*x <= b. x0 can be a scalar, vector, or matrix.

x = fmincon(fun,x0,A,b,Aeq,beq) minimizes fun subject to the linear equalities Aeq*x = beq as well as A*x <= b. Set A=[] and b=[] if no inequalities exist.

x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub) defines a set of lower and upper bounds on the design variables in x, so that the solution is always in the range lb <= x <= ub. Set Aeq=[] and beq=[] if no equalities exist. Set lb(i)=-Inf if x(i) is unbounded below, and set ub(i)=Inf if x(i) is unbounded above.

x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon) subjects the minimization to the nonlinear inequalities c(x) or equalities ceq(x) defined in nonlcon. fmincon optimizes such that c(x) <= 0 and ceq(x) = 0. Set lb=[] and/or ub=[] if no bounds exist.
eg: function [c,ceq] = mycon(x)
c = ... % Compute nonlinear inequalities at x.
ceq = ... % Compute nonlinear equalities at x.
One can use ceq = []; if no constraints....
If use Heissian method:
function [c,ceq,GC,GCeq] = mycon(x)
c = ... % Nonlinear inequalities at x
ceq = ... % Nonlinear equalities at x
if nargout > 2 % nonlcon called with 4 outputs
GC = ... % Gradients of the inequalities
GCeq = ... % Gradients of the equalities
end
fmincon(@objfun,x0,[],[],[],[],[],[],@confun,options)



x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,options) minimizes with the optimization options specified in the structure options. Use optimset to set these options. Set nonlcon = [] if there are no nonlinear inequality or equality constraints.

[x,fval] = fmincon(...) returns the value of the objective function fun at the solution x.

[x,fval,exitflag] = fmincon(...) returns a value exitflag that describes the exit condition of fmincon.

[x,fval,exitflag,output] = fmincon(...) returns a structure output with information about the optimization.

[x,fval,exitflag,output,lambda] = fmincon(...) returns a structure lambda whose fields contain the Lagrange multipliers at the solution x.

[x,fval,exitflag,output,lambda,grad] = fmincon(...) returns the value of the gradient of fun at the solution x.

[x,fval,exitflag,output,lambda,grad,hessian] = fmincon(...) returns the value of the Hessian at the solution x. See Hessian.

2. use NARGOUT to determine the parameters
function [f,g] = myfun(x)
f = ... % Compute the function value at x
if nargout > 1 % fun called with two output arguments
g = ... % Compute the gradient evaluated at x
end

3. If have Global parameters used in the object function:

a) Parameterizing Your Function as a Nested Function

As an alternative to writing your function as an anonymous function, you can write a single M-file that

Accepts the additional parameters to your function as inputs.

Invokes the optimization function.

Contains your function as a nested function.

The following example illustrates how to write an M-file to find zeros of the x3 + bx+ c, for different values of the coefficients b and c.

function y = findzero(b, c, x0)

options = optimset('Display', 'off'); % Turn off Display
y = fsolve(@poly, x0, options);

function y = poly(x) % Compute the polynomial.
y = x^3 + b*x + c;
end
end
b) As an example, suppose you want to find the zeros of the function ellipj using fsolve. fsolve expects the objective function to take one input argument, but the ellipj function takes two, u and m. You can see this function by typing

type ellipj

You are solving for the variable u, whiled m is simply a second parameter to specify which Jacobi elliptic function. To look for a zero near u0 = 3 for m = 0.5, you can create a function handle to an anonymous function that captures the current value of m from the workspace. Then, when the solver fsolve calls this function handle, the parameter m exists and ellipj will be called with two arguments. You pass this function handle to fsolve with the following commands:

u0 = 3; m = 0.5;
options = optimset('Display','off'); % Turn off Display
x = fsolve(@(u) ellipj(u,m), u0, options)