# Gradient calculation in GSLMultiFit

**URL:** <https://root-forum.cern.ch/t/gradient-calculation-in-gslmultifit/11483>\
**Category:** ROOT\
**Created:** [December 5, 2010, 6:36pm UTC](https://root-forum.cern.ch/t/gradient-calculation-in-gslmultifit/11483 "2010-12-05T18:36:03Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![LeWhoo](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/e8c25b/32.png) [@LeWhoo](https://root-forum.cern.ch/u/LeWhoo)\
**Post date:** [December 5, 2010, 6:36pm UTC](https://root-forum.cern.ch/t/gradient-calculation-in-gslmultifit/11483/1 "2010-12-05T18:36:03Z")

</div>

I think I mentioned it a long time ago (although I am not sure), but perhaps now it could be fixed.

It seems that GSLMultiFit during the gradient calculation procedure before starting first iteration of the fit, calculates gradient for all values - even those fixed. No such thing happens for Minuit. I think this could mean a large efficiency loss when performing many small, quick separate fits for functions with lot’s of fixed parameters.

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [December 6, 2010, 9:42am UTC](https://root-forum.cern.ch/t/gradient-calculation-in-gslmultifit/11483/2 "2010-12-06T09:42:15Z")

</div>

Hi,

are you sure this is happening with the current trunk revision or the latest release, 5.27.06 ?  
Can you reproduce it with a small example ?

Lorenzo

---

<div class="post-metadata">

**Author:** ![LeWhoo](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/e8c25b/32.png) [@LeWhoo](https://root-forum.cern.ch/u/LeWhoo)\
**Post date:** [December 7, 2010, 11:11am UTC](https://root-forum.cern.ch/t/gradient-calculation-in-gslmultifit/11483/3 "2010-12-07T11:11:53Z")

</div>

My ROOT displays: 5.27/07 🙂

I’ll try to make an example.

---

<div class="post-metadata">

**Author:** ![LeWhoo](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/e8c25b/32.png) [@LeWhoo](https://root-forum.cern.ch/u/LeWhoo)\
**Post date:** [December 7, 2010, 3:12pm UTC](https://root-forum.cern.ch/t/gradient-calculation-in-gslmultifit/11483/4 "2010-12-07T15:12:58Z")

</div>

Seems that the problem exists only with fitting via ROOT::Math etc. interfaces, not via TH::Fit(). I modified the script for the simulatnous fitting of two functions. It displays the changes of the fixed parameter before the first fit iteration.  
[combinedFit2.C](https://root-forum.cern.ch/uploads/default/original/2X/7/75facc90dc472344f9377efd3d3a968204d6e9f3.C) (5.69 KB)

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [December 9, 2010, 8:42am UTC](https://root-forum.cern.ch/t/gradient-calculation-in-gslmultifit/11483/5 "2010-12-09T08:42:05Z")

</div>

HI,  
the problem was in the macro that I have attached in the post

> [@Locate like command](https://root-forum.cern.ch/t/locate-like-command/65/1):
>
> Hi everybody, I’m just learning how to use root interface. There is a way to have a graphical integral of an histogram (like the PAW command “locate -i”) ?? cheers Paolo Zuccon.

The global chi2 function needs to be modified to not force the gradient calculation of all parameters. See the changes in the new macro which I attach to this post

Best Regards

Lorenzo  
[combinedFit2.C](https://root-forum.cern.ch/uploads/default/original/2X/f/fe26103f71809737cf93f972b871854514b1aa32.C) (5.99 KB)

---

<div class="post-metadata">

**Author:** ![LeWhoo](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/e8c25b/32.png) [@LeWhoo](https://root-forum.cern.ch/u/LeWhoo)\
**Post date:** [December 9, 2010, 11:26am UTC](https://root-forum.cern.ch/t/gradient-calculation-in-gslmultifit/11483/6 "2010-12-09T11:26:48Z")

</div>

Is this example in general is easily changeable to a 2D case? I thought yes, but it still counts gradient for me in the 2D case after modification. Perhaps a bug in my copy&paste or perhaps something more general.

To change it into a 2D case, I simply changed the wrapper functions to 2D:

```auto
		ROOT::Math::WrappedMultiTF1 wfRed(*psf_fin_red,2);
		ROOT::Math::WrappedMultiTF1 wfBlue(*psf_fin_blue,2);
```

where psf\_fin\_\* are TF2

and data ranges to twodimensional:

```auto
		rangeRed.SetRange(-5.5, 5.5, -5.5, 5.5);
```

Ofcourse I fit to TH2Ds. Should any additional changes be made to the GlobalChi2 function or anything else? Sorry, my own code is two long and complicated to attach it here…

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [December 9, 2010, 12:27pm UTC](https://root-forum.cern.ch/t/gradient-calculation-in-gslmultifit/11483/7 "2010-12-09T12:27:38Z")

</div>

Yes , these changes should be enough !

Lorenzo

---

<div class="post-metadata">

**Author:** ![LeWhoo](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/e8c25b/32.png) [@LeWhoo](https://root-forum.cern.ch/u/LeWhoo)\
**Post date:** [December 9, 2010, 12:57pm UTC](https://root-forum.cern.ch/t/gradient-calculation-in-gslmultifit/11483/8 "2010-12-09T12:57:57Z")

</div>

Found a bug in my copy& paste. Seems that this change not only skips unnecessary gradient calculations at the beginning, but also some calculations during normal iterations - normal iterations are now, I guess, a few dozen times faster 🙂
