Loading [MathJax]/extensions/tex2jax.js
LSST Applications 29.0.0,g04dff08e69+0440ebfea4,g0fba68d861+39c7333094,g1ec0fe41b4+f536777771,g1fd858c14a+a9301854fb,g35bb328faa+fcb1d3bbc8,g4af146b050+f5e27e7465,g4d2262a081+0d1d1daf5f,g53246c7159+fcb1d3bbc8,g5a012ec0e7+3632fc3ff3,g60b5630c4e+197d614feb,g67b6fd64d1+ed4b5058f4,g78460c75b0+2f9a1b4bcd,g786e29fd12+cf7ec2a62a,g8352419a5c+fcb1d3bbc8,g87b7deb4dc+1a86cc64c8,g8852436030+620f385cd0,g89139ef638+ed4b5058f4,g9125e01d80+fcb1d3bbc8,g94187f82dc+197d614feb,g95f8561545+197d614feb,g989de1cb63+ed4b5058f4,g9d31334357+197d614feb,g9f33ca652e+a02529f034,gabe3b4be73+1e0a283bba,gabf8522325+fa80ff7197,gb1101e3267+c9f6ee41af,gb58c049af0+f03b321e39,gb89ab40317+ed4b5058f4,gc0bb628dac+6dec70b359,gcf25f946ba+620f385cd0,gcf6002c91b+54dde5f3ac,gd6cbbdb0b4+bb83cc51f8,gde0f65d7ad+588c1dba32,ge278dab8ac+d65b3c2b70,ge410e46f29+ed4b5058f4,gf23fb2af72+b9bd8e2eff,gf5e32f922b+fcb1d3bbc8,gf67bdafdda+ed4b5058f4
LSST Data Management Base Package
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
gaussianprior.cc
Go to the documentation of this file.
1/*
2 * This file is part of gauss2d_fit.
3 *
4 * Developed for the LSST Data Management System.
5 * This product includes software developed by the LSST Project
6 * (https://www.lsst.org).
7 * See the COPYRIGHT file at the top-level directory of this distribution
8 * for details of code ownership.
9 *
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 */
23
24#include <cmath>
25#include <string>
26
29
31
32namespace lsst::gauss2d::fit {
34 bool transformed)
35 : _param(std::move(param)), _transformed(transformed) {
36 if (_param == nullptr) throw std::invalid_argument("param must not be null");
37 set_mean(mean);
38 set_stddev(stddev);
39 double loglike = this->evaluate().loglike;
40 if (!std::isfinite(loglike)) {
41 throw std::invalid_argument(this->str() + " has non-finite loglike=" + std::to_string(loglike)
42 + " on init");
43 }
44}
45
47
48PriorEvaluation GaussianPrior::evaluate(bool calc_jacobians, bool normalize) const {
49 double residual
50 = ((_transformed ? _param->get_value_transformed() : _param->get_value()) - _mean) / _stddev;
51 if (!std::isfinite(residual)) {
52 throw std::runtime_error(this->str()
53 + ".evaluate() got non-finite residual=" + std::to_string(residual));
54 }
55 double prior = -residual * residual / 2.;
56 if (normalize) prior += this->get_loglike_const_terms()[0];
58 // dresidual/dx = d((x - mean)/stddev)/dx = 1/stddev
59 if (calc_jacobians) {
60 jacobians[*_param] = std::vector<double>{1.0 / (_stddev * (_transformed ? 1.0 : _param->get_transform_derivative()))};
61 }
62
63 return PriorEvaluation{prior, {residual}, jacobians};
64}
65
69
70double GaussianPrior::get_mean() const { return _mean; };
71const ParamBase& GaussianPrior::get_param() const { return *_param; };
72double GaussianPrior::get_stddev() const { return _stddev; };
73bool GaussianPrior::get_transformed() const { return _transformed; };
74
75void GaussianPrior::set_mean(double mean) {
76 if (!std::isfinite(mean))
77 throw std::invalid_argument("mean=" + to_string_float(mean) + " must be finite");
78 _mean = mean;
79}
80void GaussianPrior::set_stddev(double stddev) {
81 if (!(std::isfinite(stddev) && (stddev > 0))) {
82 throw std::invalid_argument("stddev=" + to_string_float(stddev) + " must be finite and >0");
83 }
84 _stddev = stddev;
85}
86void GaussianPrior::set_transformed(bool transformed) { _transformed = transformed; };
87
88size_t GaussianPrior::size() const { return 1; };
89
90std::string GaussianPrior::repr(bool name_keywords, std::string_view namespace_separator) const {
91 return type_name_str<GaussianPrior>(false, namespace_separator) + "(" + (name_keywords ? "param=" : "")
92 + _param->repr(name_keywords, namespace_separator) + ", " + (name_keywords ? "mean=" : "")
93 + to_string_float(_mean) + ", " + (name_keywords ? "stddev=" : "") + to_string_float(_stddev)
94 + ", " + (name_keywords ? "transformed=" : "") + std::to_string(_transformed) + ")";
95}
96
98 return type_name_str<GaussianPrior>(true) + "(param=" + _param->str() + ", mean=" + to_string_float(_mean)
99 + ", stddev=" + std::to_string(_stddev) + ", transformed=" + to_string_float(_transformed) + ")";
100}
101} // namespace lsst::gauss2d::fit
PriorEvaluation evaluate(bool calc_jacobians=false, bool normalize_loglike=false) const override
Evaluate the log likelihood and residual-dependent terms.
GaussianPrior(std::shared_ptr< const ParamBase > param, double mean, double stddev, bool transformed)
Construct a GaussianPrior from a Parameter and mean/std.
size_t size() const override
void set_transformed(bool transformed)
std::string str() const override
Return a brief, human-readable string representation of this.
std::vector< double > get_loglike_const_terms() const override
Return the constant terms of the log likelihood (dependent on stddevs only)
const ParamBase & get_param() const
std::string repr(bool name_keywords=false, std::string_view namespace_separator=Object::CC_NAMESPACE_SEPARATOR) const override
Return a full, callable string representation of this.
Results from the evaluation of a prior probability function.
Definition prior.h:16
T isfinite(T... args)
const double LOG_1_M_LOG_SQRT_2_PI
Definition math.h:10
@ loglike
Compute the log likelihood only.
Definition model.h:34
lsst::modelfit::parameters::ParameterBase< double > ParamBase
Definition param_defs.h:9
std::string type_name_str(bool strip_namespace=false, std::string_view namespace_str=detail::NAMESPACE_SEPARATOR)
Get a string representation of an arbitrary C++ type, potentially modifying its namespace prefix.
Definition type_name.h:104
std::string to_string_float(const T value, const int precision=6, const bool scientific=true)
Definition to_string.h:15
STL namespace.
T to_string(T... args)