6 gplib::rvec
CalcGradient(GeneralObjective &Objective,
const gplib::rvec &Model)
8 const double delta = 0.001;
9 const double poschange = 1. + delta;
10 const double negchange = 1. - delta;
12 const unsigned int nparams = Model.size();
13 double CurrentRMS = Objective.CalcPerformance(Model);
15 gplib::rvec posmodel(Model);
16 gplib::rvec negmodel(Model);
17 gplib::rvec Gradient(nparams);
18 for (
unsigned int i = 0; i < nparams; ++i)
22 posmodel(i - 1) = Model(i - 1);
23 negmodel(i - 1) = Model(i - 1);
25 posmodel(i) = Model(i) * poschange;
26 negmodel(i) = Model(i) * negchange;
27 double posrms = Objective.CalcPerformance(posmodel);
28 double negrms = Objective.CalcPerformance(negmodel);
32 double denominator = 1. / (2. * delta * Model(i));
33 if (fcmp(denominator, 0.0, 1e-10) != 0)
34 Gradient(i) = (posrms - negrms) * denominator;
43 const gplib::rvec &Model,
const gplib::rvec varfactor)
47 const double delta = 0.001;
48 const double poschange = 1. + delta;
49 const double negchange = 1. - delta;
51 const int nparams = Model.size();
52 Objective.CalcPerformance(Model);
53 const int ndata = Objective.GetSynthData().size();
54 gplib::rvec posdata(Objective.GetSynthData());
55 gplib::rvec negdata(Objective.GetSynthData());
56 gplib::rvec posmodel(Model);
57 gplib::rvec negmodel(Model);
58 gplib::rmat PartialDerivs(ndata, nparams);
60 for (i = 0; i < nparams; ++i)
64 posmodel(i - 1) = Model(i - 1);
65 negmodel(i - 1) = Model(i - 1);
67 posmodel(i) = Model(i) * poschange;
68 negmodel(i) = Model(i) * negchange;
69 Objective.CalcPerformance(posmodel);
70 posdata = Objective.GetSynthData();
71 Objective.CalcPerformance(negmodel);
72 negdata = Objective.GetSynthData();
73 if (fcmp(Model(i), 0.0, 1e-8) != 0)
74 denominator = 1. / (2. * delta * Model(i));
77 for (j = 0; j < ndata; ++j)
79 PartialDerivs(j, i) = (posdata(j) - negdata(j)) * denominator
91 s.resize(Model.size());
92 sinv.resize(Model.size(), Model.size());
93 vt.resize(Model.size(), Model.size());
94 vnull.resize(Model.size(), Model.size());
95 varfactor.resize(Model.size());
96 for (
size_t i = 0; i < varfactor.size(); ++i)
100 void ModelAnalysis::CalcSensitivityMatrix()
106 void ModelAnalysis::CalcSVD()
110 CalcSensitivityMatrix();
112 OldSensitivityMatrix = SensitivityMatrix;
116 SensitivityMatrix = OldSensitivityMatrix;
118 if (changedThreshold)
120 u.resize(SensitivityMatrix.size1(), Model.size());
121 boost::numeric::bindings::lapack::gesvd(SensitivityMatrix, s, u, vt);
126 changedThreshold =
false;
129 void ModelAnalysis::ApplyThreshold()
131 const double maxeval = *max_element(s.begin(), s.end());
132 std::cout <<
"Maximum Eval: " << maxeval << std::endl;
133 const double AbsoluteThreshold = maxeval * SVDThreshold;
134 std::cout <<
"Eval Threshold: " << AbsoluteThreshold << std::endl;
135 for (
int i = 0; i < s.size(); ++i)
137 column(sinv, i) *= 0.0;
138 if (fcmp(s(i), AbsoluteThreshold, std::numeric_limits<double>::epsilon()) == -1
139 || (fcmp(s(i), 0.0, std::numeric_limits<double>::epsilon()) == 0))
142 column(vnull, i) = row(vt, i);
149 column(vnull, i) *= 0.0;
150 sinv(i, i) = 1. / s(i);
158 return prec_prod(trans(vt), vt);
163 return prec_prod(u, trans(u));
168 return prec_prod(vnull, trans(vnull));
175 gplib::rmat temp1 = prec_prod(sinv, trans(u));
176 SensitivityInv = prec_prod(trans(vt), temp1);
179 return prec_prod(SensitivityInv, trans(SensitivityInv));
184 gplib::rmat temp2 = temp1;
185 for (
size_t i = 0; i < temp1.size1(); ++i)
186 for (
size_t j = 0; j < temp1.size2(); ++j)
188 if (std::abs(temp1(i, i)) < 1e-38 || std::abs(temp1(j, j))
193 temp2(i, j) = temp1(i, j) / (sqrt(temp1(i, i))
194 * sqrt(temp1(j, j)));
gplib::rmat GetModelCorrelation(const gplib::rmat &DataCovar)
gplib::rmat CalcPartialDerivs(GeneralObjective &Objective, const gplib::rvec &Model, const gplib::rvec varfactor)
Calculate the matrix of partial derivatives (or sensitivity matrix) dD/dm, for objective function Obj...
gplib::rmat GetDataResolution()
gplib::rmat GetModelNullspace()
gplib::rvec GetSingularValues()
gplib::rvec CalcGradient(GeneralObjective &Objective, const gplib::rvec &Model)
Calculate the Gradient dO(m)/dm of the objective function Objective at the point given by Model...
void SetModel(const gplib::rvec &TheModel)
gplib::rmat GetModelCovariance(const gplib::rmat &DataCovar)
The calculation of the Model Covariance is based on Menke, p. 122 formula 7.37.
gplib::rmat GetModelResolution()