Commit e3898f69fea8b8925a97da3c11da1136d6b54d48
1 parent
651f73ff
fix numerical derivative step size for model values to 0.1
Showing
1 changed file
with
46 additions
and
51 deletions
src/obs/GObservation.cpp
| ... | ... | @@ -414,7 +414,7 @@ void GObservation::free_members(void) |
| 414 | 414 | |
| 415 | 415 | /*========================================================================== |
| 416 | 416 | = = |
| 417 | - = Npred integration methods = | |
| 417 | + = Model gradient methods = | |
| 418 | 418 | = = |
| 419 | 419 | ==========================================================================*/ |
| 420 | 420 | |
| ... | ... | @@ -446,10 +446,11 @@ double GObservation::model_grad(const GModel& model, const GEvent& event, |
| 446 | 446 | |
| 447 | 447 | // Get derivative |
| 448 | 448 | GDerivative derivative(&function); |
| 449 | - double x = model[ipar].value(); | |
| 449 | + double x = model[ipar].value(); | |
| 450 | + double dx = 0.1; | |
| 450 | 451 | derivative.eps(1.0e-4); |
| 451 | 452 | //derivative.silent(true); |
| 452 | - grad = derivative.value(x, 0.2*std::abs(x)); | |
| 453 | + grad = derivative.value(x, dx); | |
| 453 | 454 | |
| 454 | 455 | } // endelse: computed gradient numerically |
| 455 | 456 | |
| ... | ... | @@ -461,6 +462,48 @@ double GObservation::model_grad(const GModel& model, const GEvent& event, |
| 461 | 462 | |
| 462 | 463 | |
| 463 | 464 | /***********************************************************************//** |
| 465 | + * @brief Model function evaluation for gradient computation | |
| 466 | + * | |
| 467 | + * @param[in] x Function value. | |
| 468 | + * | |
| 469 | + * @todo We simply remove any parameter boundaries here for the computation | |
| 470 | + * to avoid any out of boundary errors. We may have models, however, | |
| 471 | + * for which out of bound parameters lead to illegal computations, such | |
| 472 | + * as division by zero or taking the square root of negative values. | |
| 473 | + * I cannot see any elegant method to catch this at this level. | |
| 474 | + * Eventually, the higher level method should avoid going in a | |
| 475 | + * parameter domain that is not defined. | |
| 476 | + ***************************************************************************/ | |
| 477 | +double GObservation::model_func::eval(double x) | |
| 478 | +{ | |
| 479 | + // Circumvent const correctness | |
| 480 | + GModel* model = (GModel*)m_model; | |
| 481 | + | |
| 482 | + // Save current model parameter | |
| 483 | + GModelPar current = (*model)[m_ipar]; | |
| 484 | + | |
| 485 | + // Set requested model value. Remove any boundaries to avoid limitations. | |
| 486 | + (*model)[m_ipar].value(x); | |
| 487 | + (*model)[m_ipar].remove_range(); | |
| 488 | + | |
| 489 | + // Compute model value | |
| 490 | + double value = model->eval(*m_event, *m_parent); | |
| 491 | + | |
| 492 | + // Restore current model parameter | |
| 493 | + (*model)[m_ipar] = current; | |
| 494 | + | |
| 495 | + // Return value | |
| 496 | + return value; | |
| 497 | +} | |
| 498 | + | |
| 499 | + | |
| 500 | +/*========================================================================== | |
| 501 | + = = | |
| 502 | + = Npred computation methods = | |
| 503 | + = = | |
| 504 | + ==========================================================================*/ | |
| 505 | + | |
| 506 | +/***********************************************************************//** | |
| 464 | 507 | * @brief Returns parameter gradient of Npred |
| 465 | 508 | * |
| 466 | 509 | * @param[in] model Gamma-ray source model. |
| ... | ... | @@ -682,54 +725,6 @@ double GObservation::npred_kern_spec::eval(double x) |
| 682 | 725 | } |
| 683 | 726 | |
| 684 | 727 | |
| 685 | -/*========================================================================== | |
| 686 | - = = | |
| 687 | - = Model gradient computation = | |
| 688 | - = = | |
| 689 | - ==========================================================================*/ | |
| 690 | - | |
| 691 | -/***********************************************************************//** | |
| 692 | - * @brief Model function evaluation for gradient computation | |
| 693 | - * | |
| 694 | - * @param[in] x Function value. | |
| 695 | - * | |
| 696 | - * @todo We simply remove any parameter boundaries here for the computation | |
| 697 | - * to avoid any out of boundary errors. We may have models, however, | |
| 698 | - * for which out of bound parameters lead to illegal computations, such | |
| 699 | - * as division by zero or taking the square root of negative values. | |
| 700 | - * I cannot see any elegant method to catch this at this level. | |
| 701 | - * Eventually, the higher level method should avoid going in a | |
| 702 | - * parameter domain that is not defined. | |
| 703 | - ***************************************************************************/ | |
| 704 | -double GObservation::model_func::eval(double x) | |
| 705 | -{ | |
| 706 | - // Circumvent const correctness | |
| 707 | - GModel* model = (GModel*)m_model; | |
| 708 | - | |
| 709 | - // Save current model parameter | |
| 710 | - GModelPar current = (*model)[m_ipar]; | |
| 711 | - | |
| 712 | - // Set requested model value. Remove any boundaries to avoid limitations. | |
| 713 | - (*model)[m_ipar].value(x); | |
| 714 | - (*model)[m_ipar].remove_range(); | |
| 715 | - | |
| 716 | - // Compute model value | |
| 717 | - double value = model->eval(*m_event, *m_parent); | |
| 718 | - | |
| 719 | - // Restore current model parameter | |
| 720 | - (*model)[m_ipar] = current; | |
| 721 | - | |
| 722 | - // Return value | |
| 723 | - return value; | |
| 724 | -} | |
| 725 | - | |
| 726 | - | |
| 727 | -/*========================================================================== | |
| 728 | - = = | |
| 729 | - = Npred gradient integration methods = | |
| 730 | - = = | |
| 731 | - ==========================================================================*/ | |
| 732 | - | |
| 733 | 728 | /***********************************************************************//** |
| 734 | 729 | * @brief Npred function evaluation for gradient computation |
| 735 | 730 | * | ... | ... |