LSST Applications g0f08755f38+82efc23009,g12f32b3c4e+e7bdf1200e,g1653933729+a8ce1bb630,g1a0ca8cf93+50eff2b06f,g28da252d5a+52db39f6a5,g2bbee38e9b+37c5a29d61,g2bc492864f+37c5a29d61,g2cdde0e794+c05ff076ad,g3156d2b45e+41e33cbcdc,g347aa1857d+37c5a29d61,g35bb328faa+a8ce1bb630,g3a166c0a6a+37c5a29d61,g3e281a1b8c+fb992f5633,g414038480c+7f03dfc1b0,g41af890bb2+11b950c980,g5fbc88fb19+17cd334064,g6b1c1869cb+12dd639c9a,g781aacb6e4+a8ce1bb630,g80478fca09+72e9651da0,g82479be7b0+04c31367b4,g858d7b2824+82efc23009,g9125e01d80+a8ce1bb630,g9726552aa6+8047e3811d,ga5288a1d22+e532dc0a0b,gae0086650b+a8ce1bb630,gb58c049af0+d64f4d3760,gc28159a63d+37c5a29d61,gcf0d15dbbd+2acd6d4d48,gd7358e8bfb+778a810b6e,gda3e153d99+82efc23009,gda6a2b7d83+2acd6d4d48,gdaeeff99f8+1711a396fd,ge2409df99d+6b12de1076,ge79ae78c31+37c5a29d61,gf0baf85859+d0a5978c5a,gf3967379c6+4954f8c433,gfb92a5be7c+82efc23009,gfec2e1e490+2aaed99252,w.2024.46
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)