/*
* Copyright (C) 1996 Christopher Sims
* Copyright (C) 2003 Karibzhanov, Waggoner, and Zha
*
* This free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* It is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* If you did not received a copy of the GNU General Public License
* with this software, see .
*/
//======= Revisions by T. Zha.
//======= Fixing bugs: convering all if-else loop. 02/24/05
/*=========================================================
* csminwel.c
*
* Unconstrained minimization. Uses a quasi-Newton method with BFGS update of
* the estimated inverse hessian. It is robust against certain pathologies
* common on likelihood functions. It attempts to be robust against "cliffs",
* i.e. hyperplane discontinuities, though it is not really clear whether what
* it does in such cases succeeds reliably.
*
* function [fhat,xhat,ghat,Hhat,itct,fcount,retcodehat] = csminwelmex(fcn,x0,H0,grad,crit,nit,varargin)
* fcn: string naming the objective function to be minimized
* x0: initial value of the parameter vector
* H0: initial value for the inverse Hessian. Must be positive definite.
* grad: Either a string naming a function that calculates the gradient, or the null matrix.
* If it's null, the program calculates a numerical gradient. In this case fcn must
* be written so that it can take a matrix argument and produce a row vector of values.
* crit: Convergence criterion. Iteration will cease when it proves impossible to improve the
* function value by more than crit.
* nit: Maximum number of iterations.
* varargin: A list of optional length of additional parameters that get handed off to fcn each
* time it is called.
* Note that if the program ends abnormally, it is possible to retrieve the current x,
* f, and H from the files g1.mat and H.mat that are written at each iteration and at each
* hessian update, respectively. (When the routine hits certain kinds of difficulty, it
* write g2.mat and g3.mat as well. If all were written at about the same time, any of them
* may be a decent starting point. One can also start from the one with best function value.)
*
* retcodes: 0, normal step (converged). 1, zero gradient (converged).
* 4,2, back and forth adjustment of stepsize didn't finish.
* 3, smallest stepsize still improves too slow. 5, largest step still improves too fast.
* 6, no improvement found.
*---------------------
* Fixed 7/17/93 to use inverse-hessian instead of hessian itself in bfgs update.
* Fixed 7/19/93 to flip eigenvalues of H to get better performance when it's not psd.
*
* Note: to set the level of display output, change preprocessor definitions at the beginning of this file.
* to display all output, uncomment both VERBOSE_WARNINGS and VERBOSE_DETOUTPUT
* to display only warnings without output, uncomment VERBOSE_WARNINGS
* to display no ouput, comment both VERBOSE_DETOUTPUT and VERBOSE_WARNINGS
*
* MATLAB algorithm by Christopher Sims
* C implementation by Iskander Karibzhanov
* Modified by Dan Waggoner and Tao Zha
*
* Copyright(c) 1996 Christopher Sims
* Copyright(c) 2003 Karibzhanov, Waggoner, and Zha
*=======================================================
* Revision history by T. Zha:
*
* 10/3/2002 - 1. corrected problem with memory corruption in C-MEX-file (csminwelmex.c)
* (needed to switch fcnRhs[0] back to x[0] before destroying it.
* If we don't do this, we will later clear previously destroyed array
* (one of x[1], x[2] or x[3]) which causes memory fault.
* The reason why fcnRhs[0] pointed to array other than x[0] is
* because we use mxSetPr in feval and gfeval.
* This was not a problem in C-file (csminwel.c).
*
* 10/11/2002 - 1. changed csminit function to avoid using fPeak without first initializing it
* 2. added two switches in csminit function to assign retcode to 7 for lambda>=4
* 3. added one more verbose level to display only warnings or all output *
*
* 07/13/2005 - Change #define GRADSTPS_CSMINWEL to double GRADSTPS_CSMINWEL in the .h file so the user can change the value.
* INDXNUMGRAD_CSMINWEL (gradient method) is now defined in optpackage.h.
*
* 03/10/2006 - Iskander's use of randmax=1/RAND_MAX is incorrect. Changed to randmax=1.0/RAND_MAX. Note rand() is in stdlib.h and time() is in time.h.
* - Fatal BUG by Iskander to have eye(n) instead of eye(nn). Corrected by TZ.
*
* 02/07/2011 - Modified code and .h so not to use Zha's matrix and optimization packages.
========================================================*/
#include "dw_csminwel.h"
#include "dw_error.h"
#include "dw_std.h"
#include "dw_rand.h"
#include
#include
#include
#include
#include
#define VERBOSE_WARNINGS //Display warnings.
#define VERBOSE_DETOUTPUT //Display detailed output.
#define STRLEN 192
double GRADSTPS_CSMINWEL = 1.0e-04; //Default value. Will be overwritten by the data in the input file if it exists.
//1.0e-04 (for monthly TBVAR)
//Step size for numerical gradient only when the value of x is less than 1.0 in absolute value.
//If abs(x)>1.0, the step size is GRADSTPS_CSMINWEL*x.
// Added by DW to eliminate the need for tzmatlab.
#define TZ_TRUE 1
#define TZ_FALSE 0
#define tzMalloc(n,type) dw_malloc(n*sizeof(type))
#define tzDestroy(buffer) {if (buffer) { dw_free(buffer); (buffer) = NULL; }}
#define tzFclose(file_ptr) {if (file_ptr) { fclose(file_ptr); (file_ptr) = (FILE *)NULL; }}
#define NEARINFINITY 1.0E+300
static double GLB_sclForHess;
static int numgrad(double *g, double *x, int n,
double (*fcn)(double *x, int n, double **args, int *dims),
double **args, int *dims);
static void csminit(double *fhat, double *xhat, int *fcount, int *retcode,
double *x0, double f0, double *g, int badg, double *H0, int n,
double (*fcn)(double *x, int n, double **args, int *dims),
double **args, int *dims);
static void bfgsi(double *H, double *dg, double *dx, int n, int nn);
static int peakwall(double *g, int retcode, double *x, int n,
int (*gfcn)(double *x, int n, double *g, double **args, int *dims),
double (*fcn)(double *x, int n, double **args, int *dims),
double **args, int *dims);
static double times(double *x, double *y, int n);
static double *mtimes(double *x, double *y, int n, int nn);
static double *mminus(double *x, double *y, int n);
static FILE *fptr_interesults = (FILE *)NULL; //Printing intermediate results to a file.
static char filename_sp2vecs[STRLEN]; //Two vectors. 1st row: numerical gradient; 2nd row: vectorized parameters.
#define MAX_NUM_POSSIBLE_BADCASES 25 //After this number, reset inverse hessian (especially dealing with the case for "Correct for low angle").
#define EPS (1.0e-10) //Small number to rectify special case of converging to exactly zero function value.
#define TERMINATEVALUE (1.0e+300) //If the value of the objective function at the intial value is greater than this, terminates the program.
void csminwel(double (*fcn)(double *x, int n, double **args, int *dims),
double *xh, int n, double *H, double *gh,
int (*gfcn)(double *x, int n, double *g, double **args, int *dims),
double *fh, double crit, int *itct, int nit,
int *fcount, int *retcodeh, double **args, int *dims)
{
//If gfcn is passed as NULL, numerical gradient is automatically computed.
int done=0, badg[4] = { 0, 0, 0, 0 }, badgh, nogh=1, stuck=0;
double *x[4], *g[4], f[4], *dg, *dx;
int retcode[3], fc=0, ih, nn, i;
int i_dw, verbose_err, terminal_err; // needed by DW changes
char *err_msg, *err_fmt; // needed by DW changes
int cnt_n_badcases = 0; //Must set to 0. Counts the number of bad cases before restarting with the initial diagonal (inverse of) Hessian. Added by TZ.
// TSdmatrix *H_dm = tzMalloc(1, TSdmatrix); //H_dm wil point to the same location as H. Removed by DW - only used to reset to diagonal matrix.
#ifdef VERBOSE_DETOUTPUT
time_t begtime, currentime;
#endif
GLB_sclForHess = H[0]; //The scale factor for the initial (inverse of) Hessian, which was supposed to be **diagonal**. Added by TZ.
nn = n*n; /* n: dimension size of x or xh */
*itct = -1; /* itct: number of actual iterations */
*fcount = -1; /* fcount: number of evaluations of the function */
for (i=0; i<4; i++)
x[i] = tzMalloc(n, double); //x[i] = calloc(n, sizeof(double)); Commented out by TZ.
memcpy(x[0],xh,n*sizeof(double));
for (i=0; i<4; i++)
g[i] = tzMalloc(n, double); //calloc(n, sizeof(double)); Commented out by TZ.
f[0] = fcn(x[0],n,args,dims);
if (f[0] > TERMINATEVALUE) {
printf("Bad initial parameter. Minimization is terminated without any returned value!\n");
return;
}
if (gfcn)
/* if grad is a string, compute it */
badg[0] = gfcn(x[0],n,g[0],args,dims);
else
/* if grad is not string, compute it */
badg[0] = numgrad(g[0],x[0],n,fcn,args,dims);
retcode[2] = 101;
/* iterate until done is false */
while (!done) {
#ifdef OCTAVE_MEX_FILE
OCTAVE_QUIT;
#elif defined(MATLAB_MEX_FILE)
if (utIsInterruptPending())
{
mexPrintf("\n\n** Ctrl-C detected, quitting...\n");
dw_exit(0);
}
#endif
#ifdef VERBOSE_DETOUTPUT
time(&begtime);
#endif
for (i=0; i0) printf("\n");
// #endif
(*itct)++;
csminit(&f[1],x[1],&fc,&retcode[0],x[0],f[0],g[0],badg[0],H,n,fcn,args,dims);
*fcount += fc;
/* if retcode1=1 gradient is zero and you are at the peak */
if (retcode[0]!=1) {
badg[1] = peakwall(g[1],retcode[0],x[1],n,gfcn,fcn,args,dims);
/* Bad gradient or back and forth on step length.
Possibly at cliff edge. Try perturbing search direction. */
if (badg[1]) {
double *Hcliff = tzMalloc(nn, double); //calloc(nn,sizeof(double)); Commented out by TZ.
// double randmax=1.0/(double)RAND_MAX; //03/10/2006, changed from 1/ to 1.0/ to make randmax a legal double.
/* if stuck, give it another try by perturbing Hessian */
memcpy(Hcliff,H,nn*sizeof(double));
for (i=0; i= 0; i--) eye[i]=0.0; // We need eye to be the identity matrix and so have to zero it. DW - 06/20/2011
for (i=0; i0. TZ, 9/03.
//stuck = (2.0*fabs(*fh-f[0]) <= crit*(fabs(*fh)+fabs(f[0])+EPS)); //Used if fh<0. Added by TZ, 9/03.
/* if nothing REALLY worked, too bad, you're stuck */
if (!badg[0] && !badgh && !stuck) {
/* if you are not stuck, update H0 matrix */
dg = mminus(gh,g[0],n);
dx = mminus(xh,x[0],n);
bfgsi(H,dg,dx,n,nn);
tzDestroy(dx);
tzDestroy(dg);
}
#ifdef VERBOSE_DETOUTPUT
//=== Prints out intermediate results.
printf("========================================\n");
printf(" (1) New value of the obj. func. on iteration %d: %.9f\n (2) Old value: %.9f\n (3) Downhill improvement: %.9f\n",
(int)*itct, *fh, f[0], f[0]-(*fh));
time(¤time);
//=== Times the iterative progress.
printf(" (4) Seconds to complete one iteration: %0.4f\n (5) Current time of day: %s\n\n", difftime(currentime, begtime), ctime(¤time));
fflush(stdout); // Flush the buffer to get out this message without delay.
#endif
//--------- Prints outputs to a file. ---------
if ( !(fptr_interesults = fopen(filename_sp2vecs,"wt")) ) {
printf("\n\nUnable to create the starting point data file %s in csminwel.c!\n", filename_sp2vecs);
dw_exit(EXIT_FAILURE);
}
fprintf(fptr_interesults, "========= Only one block at a time if more-than-one blocks are used. ========== \n");
fprintf(fptr_interesults, "--------Numerical gradient---------\n");
for (i=0; i nit) {
#ifdef VERBOSE_WARNINGS
// Changed by DW to avoid tzGetDebugFile() call
terminal_err=dw_SetTerminalErrors(0);
verbose_err=dw_SetVerboseErrors(USER_ERR);
err_fmt="Warning: Termination as the maximum number of iterations %d is reached with return code %d.";
sprintf(err_msg=dw_malloc(strlen(err_fmt)+21),err_fmt,nit,*retcodeh); // max of 24 digits total in nit and *retcodeh
dw_UserError(err_msg);
dw_free(err_msg);
dw_SetTerminalErrors(terminal_err);
dw_SetVerboseErrors(verbose_err);
/* printf("\nWarning: Termination as the maximum number of iterations %d is reached with return code %d.\n", nit, *retcodeh); */
/* fprintf(tzGetDebugFile(), "\nWarning: Termination as the maximum number of iterations %d is reached with return code %d.\n", nit, *retcodeh); */
#endif
done = 1;
}
else if (stuck) //stuck = 1 means fabs(*fh-f[0]) < crit, which means converged. why do we use the word "stuck" is beyond me.
{
#ifdef VERBOSE_DETOUTPUT
// Changed by DW to avoid tzGetDebugFile() call
terminal_err=dw_SetTerminalErrors(0);
verbose_err=dw_SetVerboseErrors(USER_ERR);
err_fmt="Convergence (improvement < crit %.4e) with return code %d.";
sprintf(err_msg=dw_malloc(strlen(err_fmt)+21),err_fmt,crit,*retcodeh); // max of 24 characters total in crit and *retcodeh
dw_UserError(err_msg);
dw_free(err_msg);
dw_SetTerminalErrors(terminal_err);
dw_SetVerboseErrors(verbose_err);
//printf("\nConvergence (improvement < crit %.4e) with return code %d.\n", crit, *retcodeh);
//fprintf(tzGetDebugFile(), "\nConvergence (improvement < crit %.4e) with return code %d.\n", crit, *retcodeh);
#endif
done = 1;
}
#ifdef VERBOSE_WARNINGS
switch (*retcodeh) {
case 1:
printf("\nConverged: Zero gradient.\n"); break;
case 2:
printf("\nWarning: Back adjustment of stepsize didn't finish.\n"); break;
case 3:
printf("\nWarning: Smallest stepsize still improving too slow.\n"); break;
case 4:
printf("\nWarning: Forth adjustment of stepsize didn't finish.\n"); break;
case 6:
printf("\nWarning: Smallest step still improving too slow, reversed gradient.\n"); break;
case 5:
printf("\nWarning: Largest stepsize still improving too fast.\n"); break;
case 7:
printf("\nWarning: Possible inaccuracy in Hessian matrix.\n"); break;
}
#endif
//=== Restarts from the initial (inverse of) Hessian when stuck for a while in bad cases. This can happen even *retcodeh=0 while stuck=0.
//if (*retcodeh && *retcodeh != 1)
if (!done)
if (cnt_n_badcases++ >= MAX_NUM_POSSIBLE_BADCASES)
{
// changed by DW to initialize H to diagonal matrix
for (i_dw=nn-1; i_dw >= 0; i_dw--) H[i_dw]=0.0;
for (i_dw=nn-1; i_dw >= 0; i_dw-=n+1) H[i_dw]=GLB_sclForHess;
/* H_dm->M = H; */
/* H_dm->nrows = H_dm->ncols = n; */
/* InitializeDiagonalMatrix_lf(H_dm, GLB_sclForHess); */
/* //H_dm->flag = M_GE | M_SU | M_SL; //Hessian is symmetric. */
cnt_n_badcases = 0; //Reset after we restart wtih the initial Hessian.
#ifdef VERBOSE_WARNINGS
// Changed by DW to avoid tzGetDebugFile() call
terminal_err=dw_SetTerminalErrors(0);
verbose_err=dw_SetVerboseErrors(USER_ERR);
err_fmt="Hessian is reset to the initial value because the maximum number of possibly bad cases, %d, is reached with return code %d!";
// max of 24 digits total in MAX_NUM_POSSIBLE_BADCASES and *retcodeh
sprintf(err_msg=dw_malloc(strlen(err_fmt)+21),err_fmt,MAX_NUM_POSSIBLE_BADCASES,*retcodeh);
dw_UserError(err_msg);
dw_free(err_msg);
dw_SetTerminalErrors(terminal_err);
dw_SetVerboseErrors(verbose_err);
/* printf("Hessian is reset to the initial value because the maximum number of possibly bad cases, %d, is reached with return code %d!\n", MAX_NUM_POSSIBLE_BADCASES, *retcodeh); */
/* fprintf(tzGetDebugFile(), "Hessian is reset to the initial value because the maximum number of possibly bad cases, %d, is reached with return code %d!\n", MAX_NUM_POSSIBLE_BADCASES, *retcodeh); */
#endif
}
f[0] = *fh;
memcpy(x[0],xh,n*sizeof(double));
memcpy(g[0],gh,n*sizeof(double));
badg[0] = badgh;
}
//--------- Prints outputs to a file. ---------
if ( !(fptr_interesults = fopen(filename_sp2vecs,"wt")) ) {
printf("\n\nUnable to create the starting point data file %s in csminwel.c!\n", filename_sp2vecs);
dw_exit(EXIT_FAILURE);
}
fprintf(fptr_interesults, "========= Only a block at a time if more-than-one blocks are used. ========== \n");
fprintf(fptr_interesults, "--------Numerical gradient---------\n");
for (i=0; i= NEARINFINITY) {
*xp = tmp; //Puts back to the original place. TZ, 9/03.
f0 = fcn(x,n,args,dims);
*g = (f0-fm)/dh;
}
else if (fm >= NEARINFINITY) {
//Switches to the other side of the boundary.
*xp = tmp; //Puts back to the original place. TZ, 9/03.
f0 = fcn(x,n,args,dims);
*g = (fp-f0)/dh;
}
else {
*g = (fp-fm)/(2.0*dh);
*xp = tmp; //Puts back to the original place. TZ, 9/03.
}
if (fabs(*g)>1.0e+15) {
#ifdef VERBOSE_WARNINGS
printf("Bad gradient.\n");
#endif
*g = 0.0;
badg = 1;
}
}
return badg;
}
#endif
////#undef INDXNUMGRAD_CSMINWEL
////#undef GRADSTPS_CSMINWEL
#define ANGLE 0.01 //When output of this variable becomes negative, we have a wrong analytical graident.
//.005 works for identified VARs and OLS.
//.005 implies 89.71 degrees (acrcos(ANGLE)*180/pi).
//.01 implies 89.43 degrees (acrcos(ANGLE)*180/pi).
//.05 implies 87.13 degrees (acrcos(ANGLE)*180/pi).
//.1 implies 84.26 degrees (acrcos(ANGLE)*180/pi).
#define THETA .4 //(0 1e12) {
#ifdef VERBOSE_WARNINGS
printf("Near-singular H problem.\n");
#endif
for (i=0; i -THETA*dfhat*lambda (see Berndt et al.)
If that's not the case and grad is good, OR
if grad is bad and f is not going down, shrinkSignal = 1 */
shrinkSignal = ( !badg && (f0-f <= (tmp>0?tmp:0)) ) ||
( badg && (f0-f < 0 ) );
/* the optimal lambda should also be such that f0-f<-(1-THETA)*dfhat*lambda
If that's not the case with lambda>0, AND grad is good, growthSignal = 1 */
growSignal = !badg && ( (lambda > 0) && (f0-f >= -(1-THETA)*dfhat*lambda) );
/* If shrinkSignal=1 AND ( lambda>lambdaPeak or lambda negative )
(note when lambdaPeak=0 the second part only excludes lambda=0)
try shrinking lambda */
if ( shrinkSignal && ( (lambda>lambdaPeak) || (lambda<0) ) ) {
/* if shrink=0 OR lambda/factor is already smaller than lambdaPeak, increase factor */
if ( (lambda>0) && ((!shrink) || (lambda/factor <= lambdaPeak)) ) {
shrink = 1;
factor = pow(factor,.6);
while (lambda/factor <= lambdaPeak)
factor = pow(factor,.6);
if (fabs(factor-1)lambdaPeak))
lambdaMax=lambda;
/* shrink lambda */
lambda /= factor;
/* if lambda has already been shrunk as much as possible */
if (fabs(lambda) < MINLAMB)
{
/* if lambda is positive AND you have not made any improvement
try going against gradient, which may be inaccurate */
if ((lambda > 0) && (f0 <= *fhat))
lambda = -lambda*pow(factor,6);
else {
/* if lambda is negative: let it be known and quit trying */
if (lambda < 0)
*retcode = 6;
/* if you have not made any imporvement:
let it be known and quit trying */
else
*retcode = 3;
done = 1;
}
}
}
/* If growSignal=1 and lambda positive OR ( lambda>lambdaPeak or lambda negative )
(note when lambdaPeak=0 the second part only excludes lambda=0)
try increase lambda */
else
if ( (growSignal && (lambda > 0) ) ||
( shrinkSignal && (lambda <= lambdaPeak) && (lambda > 0) ) ) {
if (shrink) {
shrink = 0;
factor = pow(factor,.6);
if (fabs(factor-1) < MINDFAC) {
if (fabs(lambda) < 4)
*retcode = 4;
else
*retcode = 7;
done = 1;
}
}
if ( (f0) ) {
fPeak = f;
lambdaPeak = lambda;
if (lambdaMax <= lambdaPeak)
lambdaMax = lambdaPeak*factor*factor;
}
/* increase lambda (up to 1e20) */
lambda *= factor;
/* if lambda has been increased up to the limit and
you have not made any imporvement:
let it be known and quit trying */
if (fabs(lambda) > 1e20) {
*retcode = 5;
done = 1;
}
}
/* If growthSignal=shrinkSignal=0 you found a good lambda, you are done */
else {
done = 1;
*retcode = factor<1.2 ? 7 : 0;
}
}
tzDestroy(dxtest);
tzDestroy(dx);
}
#ifdef VERBOSE_DETOUTPUT
printf("Norm of dx %10.5g\n", dxnorm);
#endif
}
#undef ANGLE
#undef THETA
#undef FCHANGE
#undef MINLAMB
#undef MINDFAC
static double times(double *x, double *y, int n) {
double z = 0;
int i;
for (i=0; i= 0; i--) H[i]=0.0;
for (i=nn-1; i >= 0; i-=n+1) H[i]=GLB_sclForHess;
/* H_dm = tzMalloc(1, TSdmatrix); //H_dm wil point to the same location as H. */
/* H_dm->M = H; */
/* H_dm->nrows = H_dm->ncols = n; */
/* InitializeDiagonalMatrix_lf(H_dm, GLB_sclForHess); */
/* //H_dm->flag = M_GE | M_SU | M_SL; //Hessian is symmetric. */
/* tzDestroy(H_dm); */
#ifdef VERBOSE_WARNINGS
printf("BFGS update failed.\n");
printf("|dg| = %f |dx| = %f\n",sqrt(times(dg,dg,n)),sqrt(times(dx,dx,n)));
printf("dg\'*dx = %f\n",dgdx);
printf("|H*dg| = %f\n",sqrt(times(Hdg,Hdg,n)));
#endif
}
tzDestroy(Hdg);
}
static double *mtimes(double *x, double *y, int n, int nn) {
double *x0;
double *z;
int i, j;
z = tzMalloc(nn, double); //calloc(nn, sizeof(double)); Commented out by TZ.
for (i=0, x0=x; i