gradMTm#

Purpose#

Computes numerical gradient with mask.

Format#

g = gradMTm(&fct, par1, data1, mask)#
Parameters:
  • &fct (scalar) – pointer to procedure returning either Nx1 vector or 1x1 scalar.

  • par1 (struct) – an instance of structure of type PV containing parameter vector at which gradient is to be evaluated

  • data1 (struct) – structure of type DS containing any data needed by fct

  • mask (Kx1 matrix) – elements in g corresponding to elements of mask set to zero are not computed, otherwise they are computed.

Returns:

g (NxK or 1xK) – Jacobian or gradient.

Examples#

// Declare PV structure to store parameters
struct PV p1;
p1 = pvCreate;
p1 = pvPack(p1, 0.1|0.2, "P");

// Declare DS structure to store data
struct DS d0;
d0 = dsCreate;
d0.dataMatrix = seqa(1, 1, 15);

// Write function
proc fct(struct PV p0, struct DS d0);

   local p,y;
   p = pvUnpack(p0, "P");
   y = p[1] * exp(-p[2] * d0.dataMatrix);

   retp(y);
endp;

mask = { 0, 1 };

// Find gradient
g = gradMTm(&fct, p1, d0, mask);

Remarks#

par1 must be created using the pvPack() procedures.

Source#

gradmt.src