LSST Applications g063fba187b+cac8b7c890,g0f08755f38+6aee506743,g1653933729+a8ce1bb630,g168dd56ebc+a8ce1bb630,g1a2382251a+b4475c5878,g1dcb35cd9c+8f9bc1652e,g20f6ffc8e0+6aee506743,g217e2c1bcf+73dee94bd0,g28da252d5a+1f19c529b9,g2bbee38e9b+3f2625acfc,g2bc492864f+3f2625acfc,g3156d2b45e+6e55a43351,g32e5bea42b+1bb94961c2,g347aa1857d+3f2625acfc,g35bb328faa+a8ce1bb630,g3a166c0a6a+3f2625acfc,g3e281a1b8c+c5dd892a6c,g3e8969e208+a8ce1bb630,g414038480c+5927e1bc1e,g41af890bb2+8a9e676b2a,g7af13505b9+809c143d88,g80478fca09+6ef8b1810f,g82479be7b0+f568feb641,g858d7b2824+6aee506743,g89c8672015+f4add4ffd5,g9125e01d80+a8ce1bb630,ga5288a1d22+2903d499ea,gb58c049af0+d64f4d3760,gc28159a63d+3f2625acfc,gcab2d0539d+b12535109e,gcf0d15dbbd+46a3f46ba9,gda6a2b7d83+46a3f46ba9,gdaeeff99f8+1711a396fd,ge79ae78c31+3f2625acfc,gef2f8181fd+0a71e47438,gf0baf85859+c1f95f4921,gfa517265be+6aee506743,gfa999e8aa5+17cd334064,w.2024.51
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)