⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cml.src

📁 GAUSS软件的CML模块
💻 SRC
📖 第 1 页 / 共 3 页
字号:
**   _cml_GradProc - scalar, pointer to a procedure that computes the
**              gradient of the function with respect to the parameters.
**              For example, the statement:
**
**                     _cml_GradProc = &gradproc;
**
**              tells CML that a gradient procedure exists as well
**              where to find it.  The user-provided procedure has
**              two input arguments, a Kx1 vector of parameter values and
**              an NxP matrix of data.  The procedure returns a single
**              output argument, an NxK matrix of gradients of the log-
**              likelihood function with respect to the parameters evaluated
**              at the vector of parameter values.
**
**              Default = 0, i.e., no gradient procedure has been provided.
**
**   _cml_UserNumGrad - scalar, pointer to user provided numerical gradient
**              procedure.  The instruction
**
**                     _cml_UserNumGrad = &usernumgrad;
**
**              tells CML that a procedure for computing the
**              numerical gradients exists. The user-provided procedure
**              has three input arguments, a pointer to a function that
**              computes the log-likelihood function, a Kx1 vector of
**              parameter values, and an NxP matrix of data.  The procedure
**              returns a single output argument, an NxK matrix of
**              gradients of each row of the input data matrix with
**              respect to each parameter.
**
**   _cml_HessProc - scalar, pointer to a procedure that computes the
**              hessian, i.e., the matrix of second order partial derivatives
**              of the function with respect to the parameters.  For example,
**              the instruction:
**
**                    _cml_HessProc = &hessproc;
**
**              will tell OPTMUM that a procedure has been provided for the
**              computation of the hessian and where to find it.  The
**              procedure that is provided by the user must have two
**              input arguments, a Px1 vector of parameter values and an
**              NxK data matrix.  The procedure returns a single
**              output argument, the PxP symmetric matrix of second order
**              derivatives of the function evaluated at the parameter
**              values.
**
**   _cml_UserNumHess - scalar, pointer to user provided numerical Hessian
**              procedure.  The instruction
**
**                     _cml_UserNumHess = &usernumhess;
**
**              will tell CML that a procedure for computing the
**              numerical Hessian exists. The user-provided procedure
**              three input arguments, a pointer to a function that
**              computes the log-likelihood function, a Px1 vector of
**              parameter values, and an NxK matrix of data. The procedure
**              returns a single output argument, a PxP Hessian matrix of
**              the function with respect to the parameters.
**
**   _cml_GradStep  - scalar, or 1x2, or Kx1, or Kx2, increment size for
**              computing numerical gradient and/or Hessian.  If scalar,
**              stepsize will be value times parameter estimates for the
**              numerical gradient.  If 1x2, the first element is
**              multiplied times parameter value for gradient and second
**              element the same for the Hessian.  If Kx1, the step size
**              for the gradient will be the elements of the vector, i.e.,
**              it will not be multiplied times the parameters, and if
**              Kx2, the second column sets the step sizes for the Hessian.
**
**   _cml_GradCheckTol - scalar, if nonzero and if proc's exist for
**              computing the gradient or Hessian, their calculations
**              will be compared with numerical gradients and Hessians
**              in order to determine their correctness.
**
**  ----- Convergence Criteria -----
**
**   _cml_DirTol  - scalar, convergence tolerance for gradient of estimated
**              coefficients.  Default = 1e-5.  When this criterion has been
**              satisifed OPTMUM will exit the iterations.
**
**   _cml_MaxIters - scalar, maximum number of iterations.
**
**   _cml_MaxTime - scalar, maximum time in iterations in minutes.
**              Default = 1e+5, about 10 weeks.
**
**  ----- Data -----
**
**   _cml_Active  - vector, defines fixed/active coefficients.
**                     This global allows you to fix a parameter to its
**                     starting value.  This is useful, for example, when
**                     you wish to try different models with different
**                     sets of parameters without having to re-edit
**                     the function.  When it is to be used, it must
**                     be a vector of the same length as the starting
**                     vector.  Set elements of _cml_Active to 1 for
**                     an active parameter, and to zero for a fixed one.
**
**
**   __weight - vector, frequency of observations.  By default all
**              observations have a frequency of 1.  zero frequencies
**              are allowed.  It is assumed that the elements of __weight
**              sum to the number of observations.
**
**   _cml_Lag   - scalar, if the function includes lagged values of the
**              variables _cml_Lag may be set to the number of lags.  When
**              _cml_Lag is set to a nonzero value then __row is set to 1
**              (that is, the function must evaluated one observation at a
**              time), and CML will pass a matrix to the user-provided
**              function and gradient procedures.  The first row in this
**              matrix will be (i - _cml_Lag)-th observation and the last
**              row will be the i-th observation.  The read loop will begin
**              with the (_cml_Lag+1)-th observation.  Default = 0.
**
**   _cml_NumObs  - scalar, number of cases in the data set that was analyzed.
**
**   _cml_ParNames - Kx1 character vector, parameter labels.
**
**   __row    - determines the number of rows in the data set to be passed
**              to the user-provided procedures. Default = 0.
**
**   __rowfac - If CML fails due to insufficient memory while attempting
**              to read a GAUSS data set, then __rowfac may be set to some
**              value between 0 and 1 to read a proportion of the original
**              number of rows of the GAUSS data set.
**
**  ----- Miscellaneous -----
**
**   __title  -  title of run
**
**   _cml_IterData - 3x1 vector, contains information about the iterations.
**              The first element contains the elapsed time in minutes of the
**              iterations, the second element contains the # of iterations,
**              and the third element contains a character variable indicating
**              the type of covariance matrix of the parameters.
**
**   _cml_Diagnostic - scalar. If 1, current estimates ("coeffs"),
**                        gradient ("gradient"), direction ("direct"),
**                        function value ("function"), Hessian ("Hessian"),
**                        and step length computed in the line search ("step")
**                        are printed to the screen.  If 2, they are
**                        stored in _cml_Diagnostic using VPUT.  Use
**                        VREAD to extract.  If 3, both 1 and 2 occur.
**
**-------------------**------------------**-------------------**-----------**
**-------------------**------------------**-------------------**-----------**
**
*****************************************************************************
**            Procedure For Computing Likelihood Function                  **
*****************************************************************************
**
**          The user must provide a procedure for computing the
**      log-likelihood for either one observation, or for a matrix of
**      observations.  The procedure must have two input arguments,
**      first, a vector of parameter values, and second, one or more rows
**      of the data matrix.  The output argument is the log-likelihood
**      for the observation or observations in the second argument
**      evaluated at the parameters values in the first argument.
**          Suppose that the function procedure has been named fct,
**      the following considerations apply:
**
**      FORMAT
**              logl=fct(x,y)
**
**      INPUT
**              x - vector of parameters of model
**
**              y - one or more rows of the data set (if the data set has
**                  been transformed, or if vars /= 0, i.e., there is
**                  selection, then y is a selected observation)
**                  if __row == 1,  one row of the data set
**                  if __row >= 2,  if data set is stored in memory then
**                                  all of the data set will be passed to FCT;
**                                  if data set is stored in GAUSS data file
**                                  then __row will be passed to passed to
**                                  FCT.
**                  if __row <= 0,  For data set is stored in memory same as
**                                  __row >= 2,
**                                  for GAUSS data file the maximum number of
**                                  rows that will fit in memory will be
**                                  computed by CML.
**                 if _cml_Lag >= 1,  a matrix of observations, the first is
**                                 the i-_cml_Lag row, and the final row is
**                                 the i-th row.
**
**     OUTPUT
**              logl - the log-likelihood
**                     if __row == 1 or _cml_Lag >= 1,  a scalar value for
**                     a given observation, otherwise a vector of
**                     log-likelihoods.
**
**
**      REMARKS
**
**          If you have written the procedure such that it must compute
**       the log-likelihood of one observation at a time then you
**       must set __row = 1.  But if you are able to write the procedure
**       so that a vector of log-likelihoods may be returned then set
**       __row=0; If you are getting "insufficient memory" messages when the
**       data are being read from a GAUSS data file then either set __row ==1
**       or to some positive value.
**          Also, if the data set is stored in a GAUSS data set and the
**       selected data set will fit into memory, then CML will read
**       it in and store it before beginning the iterations.  In this
**       case the setting of __row will follow the rules of a data set
**       stored in memory.
**          Significant reduction in computation time may be achieved
**       when the data set can be stored in memory and procedure is
**       written to compute vectors of log-probabilities.
**
**-------------------**------------------**-------------------**-----------**
**-------------------**------------------**-------------------**-----------**
**
*****************************************************************************
**                            Source Code                                  **
*****************************************************************************
*/

#include gauss.ext
#include cml.ext

external proc indices;

proc (5) = cml(dataset,var,lfct,start);
    local x,f,g,h,retcode;
    local Lmlcpvcp, Lmlhsvcp, Lmlfhess, Lmlitdta, LLoutput;
    local Lmllagr;

#ifUNIX
    if __output == 2;
        LLoutput = 1;
    else;
        LLoutput = __output;
    endif;
#else
    LLoutput = __output;
#endif

    if _cml_GradMethod == 0;
        _cml_eps2 = __macheps^(1/3);
    else;
        _cml_eps2 = __macheps^(1/2);
    endif;
    _cml_eps3 = __macheps^(1/3);

    { x,f,g,h,retcode,Lmlfhess,Lmlitdta,Lmlcpvcp,Lmlhsvcp,_cml_NumObs,
        Lmllagr } = _cml(dataset,var,lfct,start, _cml_Algorithm,
        _cml_CovPar, _cml_Delta, _cml_Extrap, _cml_GradMethod,
        _cml_GradProc, _cml_DirTol, _cml_HessProc, _cml_Interp, _cml_Key,
        _cml_Lag, _cml_MaxIters, _cml_MaxTime, _cml_MaxTry, _cml_NumObs,
        _cml_ParNames, _cml_LineSearch, _cml_Options,
        _cml_UserSearch, _cml_UserNumGrad, _cml_UserNumHess, _cml_Active,
        _cml_GradStep, _cml_GradCheckTol, __altnam, LLoutput,
         __row, __title,__weight );

    _cml_Lagrange = Lmllagr;
    _cml_FinalHess = Lmlfhess;
    _cml_IterData = Lmlitdta;
    _cml_HessCov = Lmlhsvcp;
    _cml_XprodCov = Lmlcpvcp;
    retp(x,f,g,h,retcode);
endp;

proc(0) = CMLset;

    gausset;
    _cml_Algorithm = 3;     /* optimization algorithm */
    _cml_CovPar = 1;        /* selects covariance matrix of parameters  */
    _cml_ParNames = 0;      /* parameter names */
    _cml_LineSearch = 2;    /* selects type of step length */
    _cml_DirTol = 1e-5;     /* convergence tolerance for gradient */
    _cml_HessProc = 0;      /* procedure to compute hessian */
    _cml_GradProc = 0;      /* procedure to compute gradient */
    _cml_GradMethod = 0;    /* numerical gradient method */
    _cml_MaxIters = 1e+5;           /* maximum number of iterations  */
    _cml_IterData = { .,.,. };      /* elapsed time, # of iters, cov method */
    _cml_UserSearch = 0;    /* interactive line search */
    _cml_UserNumGrad = 0;
    _cml_UserNumHess = 0;
    _cml_Extrap = 2.0;      /* extrapolation constant */
    _cml_Interp = 0.25;     /* interpolation constant */
    _cml_Delta = 0;
    _cml_MaxTry = 100;
    _cml_Options = 0;
    _cml_Lag = 0;           /* number of lags in model */
    _cml_FinalHess = 0;     /* final Hessian */
    _cml_NumObs = 0;        /* rows in transformed data matrix */
    _cml_HessCov = 0;       /* info matrix v-c matrix of par's */
    _cml_XprodCov = 0;      /* cross proc matrix v-c matrix of par's  */
    _cml_key = 1;
    _cml_MaxTime = 1e5;     /* maximum time for bootstrapping */
    _cml_Active = 1;
    _cml_GradStep = { . };  /* size of increment for computing gradient  */
    _cml_Diagnostic = { . };
    _cml_Alpha = .05;
    _cml_HessPD = { . };

    _cml_Increment = 0;     /* if nonzero, histogram increments */
    _cml_Center = 0;        /* if nonzero, center points for histogram  */
    _cml_Width = 2;         /* width of histogram = _cml_Width * sd's  */
    _cml_NumSample = 50;    /* bootstrap sample size */
    _cml_NumCat = 16;       /* # of cat's for bootstrapped histogram  */
    _cml_Select = { . };
    _cml_BootFname = "";
    _cml_GradCheckTol = 0;
    _cml_PriorProc = {.};
    _cml_BayesAlpha = 1.4;
    _cml_Switch = 0;
    _cml_DFTol = { . };
    _cml_FeasibleTest = 1;

    _cml_Smoothing = 0;
    _cml_EndPoints = { 0 0 };
    _cml_NumPoints = 100;
    _cml_Truncate = { 0 0 };

    _cml_A = { . };
    _cml_B = { . };
    _cml_C = { . };
    _cml_D = { . };
    _cml_EqProc = { . };
    _cml_IneqProc = { . };
    _cml_Bounds = { . };
    _cml_Lagrange = 0;
    _cml_EqJacobian = { . };
    _cml_IneqJacobian = { . };

    _cml_dsn = "";

endp;

/*

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -