50#define G_EVAL "GObservations::likelihood::eval(GOptimizerPars&)"
198 #if defined(G_EVAL_TIMING)
200 double t_start = omp_get_wtime();
202 clock_t t_start = clock();
210 int npars = pars.
size();
213 if (m_gradient != NULL)
delete m_gradient;
214 if (m_curvature != NULL)
delete m_curvature;
219 m_gradient =
new GVector(npars);
223 int stack_size = (2*npars > 100000) ? 2*npars : 100000;
224 int max_entries = 2*npars;
225 m_curvature->stack_init(stack_size, max_entries);
228 std::vector<GVector*> vect_cpy_grad;
229 std::vector<GMatrixSparse*> vect_cpy_curvature;
230 std::vector<double*> vect_cpy_value;
231 std::vector<double*> vect_cpy_npred;
243 GModels cpy_model(m_this->models());
246 double* cpy_npred =
new double(0.0);
247 double* cpy_value =
new double(0.0);
250 cpy_curvature->
stack_init(stack_size, max_entries);
254 #pragma omp critical(GObservations_likelihood_eval)
256 vect_cpy_grad.push_back(cpy_gradient);
257 vect_cpy_curvature.push_back(cpy_curvature);
258 vect_cpy_value.push_back(cpy_value);
259 vect_cpy_npred.push_back(cpy_npred);
265 for (
int i = 0; i < m_this->size(); ++i) {
268 *cpy_value += m_this->m_obs[i]->likelihood(cpy_model,
282 #pragma omp parallel sections
286 for (
int i = 0; i < vect_cpy_curvature.size() ; ++i) {
287 *m_curvature += *(vect_cpy_curvature.at(i));
288 delete vect_cpy_curvature.at(i);
294 for (
int i = 0; i < vect_cpy_grad.size(); ++i){
295 *m_gradient += *(vect_cpy_grad.at(i));
296 delete vect_cpy_grad.at(i);
302 for(
int i = 0; i < vect_cpy_npred.size(); ++i){
303 m_npred += *(vect_cpy_npred.at(i));
304 delete vect_cpy_npred.at(i);
310 for (
int i = 0; i < vect_cpy_value.size(); ++i){
311 m_value += *(vect_cpy_value.at(i));
312 delete vect_cpy_value.at(i);
318 m_curvature->stack_destroy();
324 for (
int ipar = 0; ipar < pars.
size(); ++ipar) {
325 if (pars[ipar]->is_free()) {
332 #if defined(G_USE_HESSIAN)
333 *m_curvature = hessian(pars);
337 #if defined(G_EVAL_DEBUG)
338 std::cout << *m_gradient << std::endl;
339 for (
int i = 0; i < pars.
size(); ++i) {
340 for (
int j = 0; j < pars.
size(); ++j) {
341 std::cout << (*m_curvature)(i,j) <<
" ";
343 std::cout << std::endl;
348 #if defined(G_EVAL_TIMING)
350 double t_elapse = omp_get_wtime()-t_start;
352 double t_elapse = (double)(clock() - t_start) / (
double)CLOCKS_PER_SEC;
354 std::cout <<
"GObservations::optimizer::eval: CPU usage = "
355 << t_elapse <<
" sec" << std::endl;
378 const int ncyles = 5;
379 const double step_tolerance = 0.3;
380 const double gradient_tolerance = 0.05;
391 int npars = wrk_pars.
size();
398 while (1.0+eps != 1.0) {
401 double eps2 = 2.0 * std::sqrt(eps);
408 double aimsag = std::sqrt(eps2) * std::abs(f);
411 std::vector<double> g2(npars, 0.0);
412 std::vector<double> grd(npars, 0.0);
413 std::vector<double> dir(npars, 0.0);
414 std::vector<double> yy(npars, 0.0);
417 for (
int i = 0; i < npars; ++i) {
430 double dmin = 8.0 * eps2 * std::abs(xtf);
437 for (
int icyc = 0; icyc < ncyles; ++icyc) {
446 for (
int multpy = 0; multpy < 5; ++multpy) {
463 sag = 0.5 * (fs1 + fs2 - 2.0*f);
466 if (std::abs(sag) > eps2 || sag == 0.0) {
477 double g2bfor = g2[i];
481 g2[i] = 2.0 * sag/(d*d);
482 grd[i] = (fs1-fs2)/(2.*d);
488 d = std::sqrt(2.0*aimsag/std::abs(g2[i]));
503 if (std::abs((d-dlast)/d) < step_tolerance) {
506 if (std::abs((g2[i]-g2bfor)/g2[i]) < gradient_tolerance) {
509 d = std::min(d, 10.*dlast);
510 d = std::max(d, 0.1*dlast);
515 hessian(i,i) = g2[i];
520 #if defined(G_HESSIAN)
521 std::cout <<
"GObservations::likelihood::hessian: ";
522 std::cout <<
"deltas and gradients:" << std::endl;
523 for (
int i = 0; i < npars; ++i) {
524 std::cout << dir[i] <<
" ";
525 std::cout << grd[i] << std::endl;
530 for (
int i = 0; i < npars; ++i) {
540 for (
int j = i+1; j < npars; ++j) {
558 double fs1 = value();
559 double element = (fs1 + f - yy[i] - yy[j])/(dir[i]*dir[j]);
562 hessian(i,j) = element;
563 hessian(j,i) = element;
576 #if defined(G_HESSIAN)
577 std::cout <<
"GObservations::likelihood::hessian: " << std::endl;
578 std::cout << hessian << std::endl;
607 if (curvature != NULL) {
610 covmat = curvature->
invert();
616 for (
int row = 0; row < covmat.
rows(); ++row) {
617 for (
int col = 0; col < covmat.
columns(); ++col) {
618 covmat(row,col) *= pars[row]->scale() * pars[col]->scale();
642 std::string filetype = filename.
type();
646 if (filetype ==
"fits") {
714 if (m_gradient != NULL)
delete m_gradient;
715 if (m_curvature != NULL)
delete m_curvature;
740 std::vector<std::string> names = covariance_names();
743 int size = names.size();
749 for (
int i = 0; i <
size; ++i) {
750 table.
string(0, i, names[i]);
754 for (
int i = 0; i <
size; ++i) {
755 for (
int j = 0; j <
size; j ++) {
756 table.
real(i+1, j, covmat(i,j));
761 table.
save(filename,
" ",
true);
782 std::vector<std::string> names = covariance_names();
785 int size = names.size();
794 for (
int i = 0; i <
size; ++i) {
795 par(0, i) = names[i];
796 for (
int j = 0; j <
size; ++j) {
797 cov(0, counter) = covmat(i,j);
803 std::vector<int> dim;
813 covmat_table.
extname(
"Covariance Matrix");
819 fits.
append(covmat_table);
822 fits.
saveto(filename,
true);
842 std::vector<std::string> names;
845 for (
int i = 0 ; i < m_this->m_models.size(); ++i) {
846 for (
int j = 0; j < m_this->m_models[i]->size(); ++j) {
847 names.push_back(m_this->m_models[i]->at(j).name() +
"(" +
848 m_this->m_models[i]->name() +
")");
Comma-separated values table class definition.
Abstract event bin base class definition.
Abstract event bin container class interface definition.
Abstract event atom container class interface definition.
Abstract event base class definition.
Filename class interface definition.
FITS binary table class definition.
FITS table double column class interface definition.
FITS table string column class interface definition.
Abstract model base class interface definition.
Observations container class interface definition.
Comma-separated values table class.
std::string string(const int &row, const int &col) const
Get string value.
double real(const int &row, const int &col) const
Get double precision value.
void save(const GFilename &filename, const std::string &sep=" ", const bool &clobber=false) const
Save CSV table.
std::string type(void) const
Return file type.
const std::string & extname(void) const
Return extension name.
void dim(const std::vector< int > &dim)
Set column dimension.
FITS table double column.
FITS table string column.
GFitsTableCol * append(const GFitsTableCol &column)
Append column to the table.
void saveto(const GFilename &filename, const bool &clobber=false)
Saves to specified FITS file.
GFitsHDU * append(const GFitsHDU &hdu)
Append HDU to FITS file.
void close(void)
Close FITS file.
const int & rows(void) const
Return number of matrix rows.
const int & columns(void) const
Return number of matrix columns.
Sparse matrix class interface definition.
void stack_destroy(void)
Destroy matrix stack.
void stack_init(const int &size=0, const int &entries=0)
Initialises matrix filling stack.
GMatrixSparse invert(void) const
Return inverted matrix.
void save(const GFilename &filename) const
Save likelihood fit results into a CSV or FITS file.
GObservations * m_this
Pointer to GObservations object.
GVector * m_gradient
Pointer to gradient vector.
~likelihood(void)
Destructor.
double m_value
Function value.
virtual void eval(const GOptimizerPars &pars)
Evaluate log-likelihood function.
void save_fits(const GFilename &filename) const
Save likelihood fit results into FITS file.
GMatrixSparse hessian(const GOptimizerPars &pars)
Compute Hessian matrix.
void init_members(void)
Initialise class members.
void free_members(void)
Delete class members.
GMatrixSparse covariance(void) const
Compute covariance matrix.
void save_csv(const GFilename &filename) const
Save likelihood fit results into CSV file.
likelihood & operator=(const likelihood &fct)
Assignment operator.
double m_npred
Total number of predicted events.
std::vector< std::string > covariance_names(void) const
Return covariance matrix row and column names.
likelihood(void)
Void constructor.
GMatrixSparse * m_curvature
Pointer to curvature matrix.
void copy_members(const likelihood &fct)
Copy class members.
Observation container class.
void init_members(void)
Initialise class members.
void copy_members(const GObservations &obs)
Copy class members.
int size(void) const
Return number of observations in container.
void eval(void)
Evaluate function.
void free_members(void)
Delete class members.
Optimizer function abstract base class.
virtual GOptimizerFunction & operator=(const GOptimizerFunction &fct)
Assignment operator.
Optimizer parameter class.
const double & factor_value(void) const
Return parameter factor value.
bool is_fixed(void) const
Signal if parameter is fixed.
const double & factor_gradient(void) const
Return parameter factor gradient.
Optimizer parameter container class.
int size(void) const
Return number of parameters in container.