LSST Applications g1653933729+34a971ddd9,g1a997c3884+34a971ddd9,g2160c40384+da0d0eec6b,g28da252d5a+1236b942f7,g2bbee38e9b+e5a1bc5b38,g2bc492864f+e5a1bc5b38,g2ca4be77d2+192fe503f0,g2cdde0e794+704103fe75,g3156d2b45e+6e87dc994a,g347aa1857d+e5a1bc5b38,g35bb328faa+34a971ddd9,g3a166c0a6a+e5a1bc5b38,g3e281a1b8c+8ec26ec694,g4005a62e65+ba0306790b,g414038480c+9f5be647b3,g41af890bb2+c3a10c924f,g5065538af8+e7237db731,g5a0bb5165c+eae055db26,g717e5f8c0f+b65b5c3ae4,g80478fca09+4ce5a07937,g82479be7b0+08790af60f,g858d7b2824+b65b5c3ae4,g9125e01d80+34a971ddd9,ga5288a1d22+5df949a35e,gae0086650b+34a971ddd9,gb58c049af0+ace264a4f2,gbd397ab92a+2141afb137,gc28159a63d+e5a1bc5b38,gc805d3fbd4+b65b5c3ae4,gcf0d15dbbd+97632ccc20,gd6b7c0dfd1+de826e8718,gda6a2b7d83+97632ccc20,gdaeeff99f8+7774323b41,ge2409df99d+e6cadbf968,ge33fd446bb+b65b5c3ae4,ge79ae78c31+e5a1bc5b38,gf0baf85859+890af219f9,gf5289d68f6+a27069ed62,w.2024.37
LSST Data Management Base Package
Loading...
Searching...
No Matches
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
Interface for parameters with values and metadata.
Definition parameter.h:58
T isfinite(T... args)
const double LOG_1_M_LOG_SQRT_2_PI
Definition math.h:10
@ loglike
Compute the log likelihood only.
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)