Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
BFGSErrorUpdator.cxx
Go to the documentation of this file.
1// @(#)root/minuit2:$Id$
2// Authors: M. Winkler, F. James, L. Moneta, A. Zsenei 2003-2005
3
4/**********************************************************************
5 * *
6 * Copyright (c) 2005 LCG ROOT Math team, CERN/PH-SFT *
7 * *
8 **********************************************************************/
9
12#include "Minuit2/MnPrint.h"
13
14#include <vector>
15
16namespace ROOT {
17
18namespace Minuit2 {
19
20// define here a square matrix that it is needed for computing the BFGS update
21// define just the class, no need for defining operations as done for the Symmetric matrices
22// since the square matrix will be converted in a symmetric one afterwards
23
25public:
26 LASquareMatrix(unsigned int n) : fNRow(n), fData(n * n) {}
27
28 double operator()(unsigned int row, unsigned int col) const
29 {
30 assert(row < fNRow && col < fNRow);
31 return fData[col + row * fNRow];
32 }
33
34 double &operator()(unsigned int row, unsigned int col)
35 {
36 assert(row < fNRow && col < fNRow);
37 return fData[col + row * fNRow];
38 }
39
40 unsigned int Nrow() const { return fNRow; }
41
42private:
43 unsigned int fNRow;
44 std::vector<double> fData;
45};
46
47// compute outer product of two vector of same size to return a square matrix
49{
50 assert(v1.size() == v2.size());
51 LASquareMatrix a(v1.size());
52 for (unsigned int i = 0; i < v1.size(); ++i) {
53 for (unsigned int j = 0; j < v2.size(); ++j) {
54 a(i, j) = v1[i] * v2[j];
55 }
56 }
57 return a;
58}
59// compute product of symmetric matrix with square matrix
60
62{
63 unsigned int n = m1.Nrow();
64 assert(n == m2.Nrow());
66 for (unsigned int i = 0; i < n; ++i) {
67 for (unsigned int j = 0; j < n; ++j) {
68 a(i, j) = 0;
69 for (unsigned int k = 0; k < n; ++k) {
70 a(i, j) += m1(i, k) * m2(k, j);
71 }
72 }
73 }
74 return a;
75}
76
77MinimumError
79{
80
81 // update of the covarianze matrix (BFGS formula, see Tutorial, par. 4.8 pag 26)
82 // in case of delgam > gvg (PHI > 1) use rank one formula
83 // see par 4.10 pag 30
84
85 const MnAlgebraicSymMatrix &v0 = s0.Error().InvHessian();
86 MnAlgebraicVector dx = p1.Vec() - s0.Vec();
87 MnAlgebraicVector dg = g1.Vec() - s0.Gradient().Vec();
88
89 double delgam = inner_product(dx, dg); // this is s^T y using wikipedia conventions
90 double gvg = similarity(dg, v0); // this is y^T B^-1 y
91
92 MnPrint print("BFGSErrorUpdator");
93 print.Debug("dx", dx, "dg", dg, "delgam", delgam, "gvg", gvg);
94
95 if (delgam == 0) {
96 print.Warn("delgam = 0 : cannot update - return same matrix");
97 return s0.Error();
98 }
99
100 if (delgam < 0) {
101 print.Warn("delgam < 0 : first derivatives increasing along search line");
102 }
103
104 if (gvg <= 0) {
105 // since v0 is pos def this gvg can be only = 0 if dg = 0 - should never be here
106 print.Warn("gvg <= 0");
107 // return s0.Error();
108 }
109
110 // compute update formula for BFGS
111 // see wikipedia https://en.wikipedia.org/wiki/Broyden–Fletcher–Goldfarb–Shanno_algorithm
112 // need to compute outer product dg . dx and it is not symmetric anymore
113
116
117 unsigned int n = v0.Nrow();
119 for (unsigned int i = 0; i < n; ++i) {
120 for (unsigned int j = i; j < n; ++j) {
121 v2(i, j) = (b(i, j) + b(j, i)) / (delgam);
122 }
123 }
124
126 vUpd -= v2;
127
128 double sum_upd = sum_of_elements(vUpd);
129 vUpd += v0;
130
131 double dcov = 0.5 * (s0.Error().Dcovar() + sum_upd / sum_of_elements(vUpd));
132
133 print.Debug("dcov", dcov);
134
135 return MinimumError(vUpd, dcov);
136}
137
138} // namespace Minuit2
139
140} // namespace ROOT
#define b(i)
Definition RSha256.hxx:100
#define s0(x)
Definition RSha256.hxx:90
#define a(i)
Definition RSha256.hxx:99
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
MinimumError Update(const MinimumState &, const MinimumParameters &, const FunctionGradient &) const override
double operator()(unsigned int row, unsigned int col) const
double & operator()(unsigned int row, unsigned int col)
Class describing a symmetric matrix of size n.
Definition MnMatrix.h:438
unsigned int Nrow() const
Definition MnMatrix.h:663
MinimumError keeps the inv.
MinimumState keeps the information (position, Gradient, 2nd deriv, etc) after one minimization step (...
void Debug(const Ts &... args)
Definition MnPrint.h:133
void Warn(const Ts &... args)
Definition MnPrint.h:121
const Int_t n
Definition legend1.C:16
double sum_of_elements(const LASymMatrix &)
Definition MnMatrix.cxx:98
double similarity(const LAVector &, const LASymMatrix &)
Definition MnMatrix.cxx:540
LASquareMatrix OuterProduct(const LAVector &v1, const LAVector &v2)
double inner_product(const LAVector &, const LAVector &)
Definition MnMatrix.cxx:227
LASquareMatrix MatrixProduct(const LASymMatrix &m1, const LASquareMatrix &m2)
ABObj< sym, VectorOuterProduct< ABObj< vec, M, T >, T >, T > Outer_product(const ABObj< vec, M, T > &obj)
Definition MnMatrix.h:390
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...