Command Reference : User-Defined Optimization : Defining the Objective and Controls
  
Defining the Objective and Controls
To use optimize, you must first construct an EViews subroutine with arguments to define an output objective which depends on input controls.
Recall that a subroutine with arguments is simply a set of commands in a program that can be called one or more times within the program ( “Subroutine with Arguments”). The arguments of the subroutine will correspond to the objective and to inputs that are required to calculate the objective. Each time the subroutine is called, the objective will be computed using the current values of the input controls.
The objective, which must be associated with an argument of the subroutine, may be a scalar, or may consist of many values stored in an EViews object such as a vector, matrix, or series.
The controls, which may be thought of as input parameters, are passed into the subroutine as an argument. As with the objective, the controls may be a scalar value, or a multi-valued object such as a vector, matrix, or series.
Note that when series objects are employed as either the objective or control, only the corresponding elements in the current workfile sample will be used.
optimize will determine the values of the controls that optimize the objective. If the objective is many-valued, EViews will optimize the sum or sum-of-squares of the values, with respect to the control elements.
Since the objective is defined using an EViews subroutine, you may optimize almost anything that may be computed using EViews commands. Notable, you may use optimize to optimize general functions as well as likelihoods involving matrix computations (neither of which may be optimized using the Logl object).
Consider, for example, the simple quadratic function defined as an EViews subroutine:
subroutine f(scalar !y, scalar !x)
!y = 5*!x^2 - 3*!x - 2
endsub
This subroutine has one output and one input, the program variable scalars !X and !Y, respectively. For a given control value for !X, the subroutine computes the value of the scalar objective !Y.
In its simplest form, a subroutine designed to work with optimize requires only two arguments—an objective and control parameters. However you may include additional arguments, some of which may be used by optimize, while others are ignored. For example, the subroutine,
subroutine SqDev(series out, scalar in, series y)
out = (y - in)^2
endsub
computes the squared deviations of the argument series Y from the control scalar, and places the element results in the output objective series OUT. The subroutine argument for the series Y will not be used by optimize, but allows optimization to be performed on arbitrary series without re-coding the subroutine.
By default, optimize will assume that the first subroutine argument corresponds to the objective and the second argument corresponds to the controls. As we will see, the default associations may be changed through the use of options in the optimize command ( “The Optimize Command”).
Typically, multiple control values are passed into the subroutine in the form of a vector or matrix, as in
subroutine local loglike(series logl, vector beta, series dep, group regs)
!pi = @acos(-1)
series r = dep - beta(1) - beta(2)*regs(1) - beta(3)*regs(2) -    beta(4)*regs(3)
logl = @log((1/beta(5)*@dnorm(r/beta(5))
endsub
where the control vector BETA and the auxiliary arguments for the dependent variable series DEP and the regressors group REGS are used as inputs for the computation of the normal log-likelihood contributions in the objective series LOGL. Note that the first four elements of the vector BETA correspond to the mean regression coefficients, and the last element is the parameter for the standard deviation of the error distribution.
Lastly, when designing your subroutine, you should always define the objective to return NA values for bad control values, since returning an arbitrary value may make numeric derivatives unreliable at points close to the invalid region.