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) {
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) {
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();
746 GCsv table(size + 1, 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() +
")");
GObservations * m_this
Pointer to GObservations object.
likelihood(void)
Void constructor.
void save(const GFilename &filename, const std::string &sep=" ", const bool &clobber=false) const
Save CSV table.
FITS table double column class interface definition.
double m_value
Function value.
~likelihood(void)
Destructor.
Abstract model base class interface definition.
Optimizer function abstract base class.
const double & factor_gradient(void) const
Return parameter factor gradient.
void stack_destroy(void)
Destroy matrix stack.
Sparse matrix class interface definition.
void save_csv(const GFilename &filename) const
Save likelihood fit results into CSV file.
GVector abs(const GVector &vector)
Computes absolute of vector elements.
Abstract event bin base class definition.
Abstract event bin container class interface definition.
Optimizer parameter container class.
void save(const GFilename &filename) const
Save likelihood fit results into a CSV or FITS file.
GVector * m_gradient
Pointer to gradient vector.
GFitsTableCol * append(const GFitsTableCol &column)
Append column to the table.
GMatrixSparse hessian(const GOptimizerPars &pars)
Compute Hessian matrix.
Comma-separated values table class.
GMatrixSparse * m_curvature
Pointer to curvature matrix.
GMatrixSparse invert(void) const
Return inverted matrix.
double m_npred
Total number of predicted events.
double min(const GVector &vector)
Computes minimum vector element.
void save_fits(const GFilename &filename) const
Save likelihood fit results into FITS file.
Abstract event atom container class interface definition.
void free_members(void)
Delete class members.
FITS table string column.
std::string string(const int &row, const int &col) const
Get string value.
void saveto(const GFilename &filename, const bool &clobber=false)
Saves to specified FITS file.
void copy_members(const GObservations &obs)
Copy class members.
GVector sqrt(const GVector &vector)
Computes square root of vector elements.
const int & rows(void) const
Return number of matrix rows.
Abstract event base class definition.
std::vector< std::string > covariance_names(void) const
Return covariance matrix row and column names.
FITS table string column class interface definition.
void init_members(void)
Initialise class members.
likelihood & operator=(const likelihood &fct)
Assignment operator.
bool is_fixed(void) const
Signal if parameter is fixed.
void dim(const std::vector< int > &dim)
Set column dimension.
int size(void) const
Return number of observations in container.
const std::string & extname(void) const
Return extension name.
double max(const GVector &vector)
Computes maximum vector element.
void eval(void)
Evaluate function.
Observation container class.
std::string type(void) const
Return file type.
virtual void eval(const GOptimizerPars &pars)
Evaluate log-likelihood function.
double real(const int &row, const int &col) const
Get double precision value.
GMatrixSparse covariance(void) const
Compute covariance matrix.
void stack_init(const int &size=0, const int &entries=0)
Initialises matrix filling stack.
void copy_members(const likelihood &fct)
Copy class members.
GFitsHDU * append(const GFitsHDU &hdu)
Append HDU to FITS file.
FITS binary table class definition.
int size(void) const
Return number of parameters in container.
void free_members(void)
Delete class members.
void init_members(void)
Initialise class members.
Observations container class interface definition.
const int & columns(void) const
Return number of matrix columns.
void close(void)
Close FITS file.
FITS table double column.
Filename class interface definition.
Comma-separated values table class definition.
const double & factor_value(void) const
Return parameter factor value.
virtual GOptimizerFunction & operator=(const GOptimizerFunction &fct)
Assignment operator.
Optimizer parameter class.