# Three questions on user-defined derivatives in TMinuit

Dear all,

I plan to use the user-defined first-order derivatives in TMinuit, but still calculate the hessian matrix with finite differences. There are three questions on how to implement it.

1. Does “user-defined first-order derivatives” mean that we need to give analytical expression to ‘gin’ vector?

2. For those fixed parameters, do we need to specify that their values in the “gin” vector are equal to 0 or TMinuit will automatically set their first-order derivatives equal to 0?

3. In the mnderi() function, it is said that the first derivatives of FCN will be calculated by using user-defined functions or by finite differences, according to whether ISW(3) is zero or one. I am not sure how to set the value of ISW(3). Also it seems that the function makes judgement based on the value of ISW(2), rather than ISW(3).

Please let me know your suggestions on these questions. Thanks a lot!

No It means that you can provide analitically calculated derivative values in a given point to ‘gin’ vector. But only in case ‘iflag == 2’. Minuit compares your analitically calculated derivatives with numerically ones.

Actually you don’t have to bother about it AFAIK.

use TMinuit::Command() method to set this internal flag. Something like:
gMinuit->Command(“set grad 0”); or if you want to force using ‘user-defined’ derivatives, then gMinuit->Command(“set grad 1”);

see:
root.cern.ch/root/htmldoc/TMinui … uit:mnderi

Rene

No It means that you can provide analitically calculated derivative values in a given point to ‘gin’ vector. But only in case ‘iflag == 2’. Minuit compares your analitically calculated derivatives with numerically ones.
[/quote]

Thanks for your reply. Since I am a newbie to TMinuit, I am not clear about how to set these internal flags. Where to set ‘iflag==2’?

Also below you mention to use TMinuit::Command() to set ISW(). Can we use the same way to set for “iflag”?

BTW, is there a good reference material or example to show how to correctly set up the TMinuit?

There are two new questions when I play around with the user-defined derivatives.

First, the program takes much longer time to get convergence when I use the defined derivatives in ‘FCN’ function than not using them while keep other things unchanged. It is strange since theoretically the user-defined paramaters should be faster.

Second, when I add the command “gMinuit->Command(“set grad 1”)” into the program, TMinuit reports the following warning message:

then gives me very strange output:

ERR MATRIX APPROXIMATE
FCN=90877.1 FROM MIGRAD STATUS=NOT POSDEF 148 CALLS 149 TOTAL
EDM= unknown STRATEGY= 1 ERROR MATRIX UNCERTAINTY 100.0 per cent

The first-order derivatives for all parameters (except fixed) are very big.

The analytical expressions for first-order derivatives are correct since it works for other programs.

Some programs related to TMinuit are listed below:
TMinuit * gMinuit = new TMinuit(65);
gMinuit->SetFCN(fcn);

``````Double_t arglist;
Int_t ierflg = 0;

arglist = 0.5;  // 1.0 for chisquare type, 0.5 for negative log likelihood
gMinuit->mnexcm("SET ERR", arglist, 1, ierflg);
``````

Please let me know how to solve these problems. Thanks again.

Hi Larry,

1. For those fixed parameters, do we need to specify that their values in >the “gin” vector are equal to 0 or TMinuit will automatically set their >first-order derivatives equal to 0?

I have a suspicion that you do not assign the gradients correctly.
Make sure in your objective function you use fNexofi to get the right

Double_t *par,Int_t flag)
{
.
.
if (flag == 2)
{
for (Int_t ipar = 0; ipar < nrPar; ipar++)
}
.
}

This way you are skipping the fixed parameters

Eddy

I am still unclear how to set the value of ‘iflag’. The fcn() function seems reading the value of ‘iflag’ from outside as a normal integer (not a pointer). But I didn’t see anywhere in the main() function we define and set the ‘iflag’.

Thank you!

I assume that iflag is the last argument in the parameter list of the
objective function.
It is set by the minimizer Minuit and you should expect iflag
to have a value >=1 and <= 4:

1. initialize your function; this is optional
2. calculate the gradient, also optional
3. what to do after fit is finished , also optional
4. calculate function value

Did you ever have a look at the "Long write-up D506 of Minuit
by Fred James. Msot of it still applies

Eddy,

Thanks for your help. Just a quick question on using “fNexofi”. In the function of mnderi(), it seems that, for derivatives calculated by fcn, the ‘iext’ does the same job as ‘gMinuit->fNexofi[ipar]-1’ in your suggestion. Do I still need to make some changes in FCN function?

Larry

Yes,

L100:
for (iint = 1; iint <= fNpar; ++iint) {
iext = fNexofi[iint-1];
if (fNvarl[iext-1] <= 1) {
fGrd[iint-1] = fGin[iext-1];
} else {
dd = (fBlim[iext-1] - fAlim[iext-1]).5TMath::Cos(fX[iint-1]);
fGrd[iint-1] = fGin[iext-1]*dd;
}
}

FNpar is the number of free(!) variables, iext is an index to your complete
variable table. So you better fill it that way in your FCN.
The first argument to FCN is again the # of free variables, sio if
you loop across that you should transform it there again with
fNexofi.

Eddy