File: numhessian.cc

package info (click to toggle)
octave-optim 1.5.3-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,368 kB
  • sloc: cpp: 1,437; makefile: 185; perl: 169; xml: 29; sh: 3
file content (253 lines) | stat: -rw-r--r-- 7,744 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// Copyright (C) 2004, 2006 Michael Creel <michael.creel@uab.es>
//
// This program is 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.
//
// This program 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.
//
// You should have received a copy of the GNU General Public License along with
// this program; if not, see <http://www.gnu.org/licenses/>.

// numhessian: numeric second derivative

#include <oct.h>
#include <octave/parse.h>
#include <octave/lo-mappers.h>
#include <octave/Cell.h>
#include <float.h>

#include "error-helpers.h"

// argument checks
static bool
any_bad_argument(const octave_value_list& args)
{
  if (!args(0).is_string())
    {
      _p_error("numhessian: first argument must be string holding objective function name");
      return true;
    }

  if (!args(1).OV_ISCELL ())
    {
      _p_error("numhessian: second argument must cell array of function arguments");
      return true;
    }

  // minarg, if provided
  if (args.length() == 3)
    {
      int tmp;
      bool err;
      SET_ERR (tmp = args(2).int_value(), err);
      if (err)
        {
          _p_error("numhessian: 3rd argument, if supplied,  must an integer\n\
that specifies the argument wrt which differentiation is done");
          return true;
        }
      if ((tmp > args(1).length ()) || (tmp < 1))
        {
          _p_error("numhessian: 3rd argument must be a positive integer that indicates \n\
which of the elements of the second argument is the\n\
one to differentiate with respect to");
          return true;
        }
    }
  return false;
}



DEFUN_DLD(numhessian, args, ,
          "numhessian(f, {args}, minarg)\n\
\n\
Numeric second derivative of f with respect\n\
to argument \"minarg\".\n\
* first argument: function name (string)\n\
* second argument: all arguments of the function (cell array)\n\
* third argument: (optional) the argument to differentiate w.r.t.\n\
        (scalar, default=1)\n\
\n\
If the argument\n\
is a k-vector, the Hessian will be a kxk matrix\n\
\n\
function a = f(x, y)\n\
        a = x'*x + log(y);\n\
endfunction\n\
\n\
numhessian(\"f\", {ones(2,1), 1})\n\
ans =\n\
\n\
    2.0000e+00   -7.4507e-09\n\
   -7.4507e-09    2.0000e+00\n\
\n\
Now, w.r.t. second argument:\n\
numhessian(\"f\", {ones(2,1), 1}, 2)\n\
ans = -1.0000\n\
")
{
  int nargin = args.length();
  if (!((nargin == 2)|| (nargin == 3)))
    {
      error("numhessian: you must supply 2 or 3 arguments");
      return octave_value_list();
    }

  // check the arguments
  if (any_bad_argument (args))
    {
      error ("error in numhessian");
      return octave_value_list();
    }

  std::string f (args(0).string_value());
  Cell f_args_cell (args(1).cell_value());
  octave_value_list f_args, f_return;
  int i, j, k, minarg;
  bool test;
  double di, hi, pi, dj, hj, pj, hia, hja, fpp, fmm, fmp, fpm, obj_value, SQRT_EPS, diff;

  // Default values for controls
  minarg = 1; // by default, first arg is one over which we minimize

  // copy cell contents over to octave_value_list to use feval()
  k = f_args_cell.numel ();
  f_args.resize (k); // resize only once
  for (i = 0; i<k; i++) f_args(i) = f_args_cell(i);

  // check which arg w.r.t which we need to differentiate
  if (args.length() == 3) minarg = args(2).int_value();
  Matrix parameter = f_args(minarg - 1).matrix_value();
  k = parameter.rows();
  Matrix derivative(k, k);

  f_return = OCTAVE__FEVAL (f, f_args);
  if (f_return.length () > 0 && f_return(0).is_double_type ())
    obj_value = f_return(0).double_value();
  else
    {
      error ("numhessian: function must return a scalar of class 'double'");
      return octave_value_list ();
    }

  diff = exp(log(DBL_EPSILON)/4);
  SQRT_EPS = sqrt(DBL_EPSILON);


  for (i = 0; i<k;i++)
    {
      // approximate 2nd deriv. by central difference
      pi = parameter(i);
      test = (fabs(pi) + SQRT_EPS) * SQRT_EPS > diff;
      if (test) hi = (fabs(pi) + SQRT_EPS) * SQRT_EPS;
      else hi = diff;


      for (j = 0; j < i; j++)
        { // off-diagonal elements
          pj = parameter(j);
          test = (fabs(pj) + SQRT_EPS) * SQRT_EPS > diff;
          if (test) hj = (fabs(pj) + SQRT_EPS) * SQRT_EPS;
          else hj = diff;

          // +1 +1
          parameter(i) = di = pi + hi;
          parameter(j) = dj = pj + hj;
          hia = di - pi;
          hja = dj - pj;
          f_args(minarg - 1) = parameter;
          f_return = OCTAVE__FEVAL (f, f_args);
          if (f_return.length () > 0 && f_return (0).is_double_type ())
            fpp = f_return(0).double_value();
          else
            {
              error ("numhessian: function must return a scalar of class 'double'");
              return octave_value_list ();
            }

          // -1 -1
          parameter(i) = di = pi - hi;
          parameter(j) = dj = pj - hj;
          hia = hia + pi - di;
          hja = hja + pj - dj;
          f_args(minarg - 1) = parameter;
          f_return = OCTAVE__FEVAL (f, f_args);
          if (f_return.length () > 0 && f_return (0).is_double_type ())
            fmm = f_return(0).double_value();
          else
            {
              error ("numhessian: function must return a scalar of class 'double'");
              return octave_value_list ();
            }

          // +1 -1
          parameter(i) = pi + hi;
          parameter(j) = pj - hj;
          f_args(minarg - 1) = parameter;
          f_return = OCTAVE__FEVAL (f, f_args);
          if (f_return.length () > 0 && f_return (0).is_double_type ())
            fpm = f_return(0).double_value();
          else
            {
              error ("numhessian: function must return a scalar of class 'double'");
              return octave_value_list ();
            }

          // -1 +1
          parameter(i) = pi - hi;
          parameter(j) = pj + hj;
          f_args(minarg - 1) = parameter;
          f_return = OCTAVE__FEVAL (f, f_args);
          if (f_return.length () > 0 && f_return (0).is_double_type ())
            fmp = f_return(0).double_value();
          else
            {
              error ("numhessian: function must return a scalar of class 'double'");
              return octave_value_list ();
            }

          derivative(j,i) = ((fpp - fpm) + (fmm - fmp)) / (hia * hja);
          derivative(i,j) = derivative(j,i);
          parameter(j) = pj;
        }

      // diagonal elements

      // +1 +1
      parameter(i) = di = pi + 2 * hi;
      f_args(minarg - 1) = parameter;
      f_return = OCTAVE__FEVAL (f, f_args);
      if (f_return.length () > 0 && f_return (0).is_double_type ())
        fpp = f_return(0).double_value();
      else
        {
          error ("numhessian: function must return a scalar of class 'double'");
          return octave_value_list ();
        }
      hia = (di - pi) / 2;

      // -1 -1
      parameter(i) = di = pi - 2 * hi;
      f_args(minarg - 1) = parameter;
      f_return = OCTAVE__FEVAL (f, f_args);
      if (f_return.length () > 0 && f_return (0).is_double_type ())
        fmm = f_return(0).double_value();
      else
        {
          error ("numhessian: function must return a scalar of class 'double'");
          return octave_value_list ();
        }
      hia = hia + (pi - di) / 2;

      derivative(i,i) = ((fpp - obj_value) + (fmm - obj_value)) / (hia * hia);
      parameter(i) = pi;
    }

  return octave_value(derivative);
}