mxlib
c++ tools for analyzing astronomical data and other tasks by Jared R. Males. [git repo]
Loading...
Searching...
No Matches
fitExpModGaussian.hpp
Go to the documentation of this file.
1/** \file fitExpModGaussian.hpp
2 * \author Jared R. Males
3 * \brief Tools for fitting the GammaDistribution distribution to data.
4 * \ingroup fitting_files
5 *
6 */
7
8//***********************************************************************//
9// Copyright 2023 Jared R. Males (jaredmales@gmail.com)
10//
11// This file is part of mxlib.
12//
13// mxlib is free software: you can redistribute it and/or modify
14// it under the terms of the GNU General Public License as published by
15// the Free Software Foundation, either version 3 of the License, or
16// (at your option) any later version.
17//
18// mxlib is distributed in the hope that it will be useful,
19// but WITHOUT ANY WARRANTY; without even the implied warranty of
20// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21// GNU General Public License for more details.
22//
23// You should have received a copy of the GNU General Public License
24// along with mxlib. If not, see <http://www.gnu.org/licenses/>.
25//***********************************************************************//
26
27#ifndef fitExpModGaussian_hpp
28#define fitExpModGaussian_hpp
29
30#include "levmarInterface.hpp"
32
33namespace mx
34{
35namespace math
36{
37namespace fit
38{
39
40template <typename realT>
42
43/** \defgroup expModGauss_peak_fit Exponetially Modified Gaussian Distribution
44 * \brief Fitting the Exponetially Modified Gaussian Distribution to data.
45 *
46 * The Exponetially Modified Gaussian Distribution is fit to data.
47 *
48 * \ingroup peak_fit
49 */
50
51/// Class to manage fitting the Exponetially Modified Gaussian Distribution to data via the \ref levmarInterface
52/** In addition to the requirements on fitterT specified by \ref levmarInterface
53 * this class also requires this definition in fitterT
54 * \code
55 * static const int nparams = 3;
56 * \endcode
57 * where the number 3 is replaced by the number of parameters that fitterT expects to fit.
58 *
59 * \tparam fitterT a type meeting the above requirements.
60 *
61 * \ingroup expModGauss_peak_fit
62 *
63 */
64template <typename fitterT>
65class fitExpModGaussian : public levmarInterface<fitterT>
66{
67
68 public:
69 typedef typename fitterT::realT realT;
70
71 static const int nparams = fitterT::nparams;
72
74
75 void initialize()
76 {
77 this->allocate_params( nparams );
78 this->adata = &arr;
79 }
80
81 fitExpModGaussian()
82 {
83 initialize();
84 }
85
86 ~fitExpModGaussian()
87 {
88 }
89
90 /// Set the initial guess.
91 void setGuess( realT G0, ///< [in] the location parameter
92 realT mu, ///< [in] the shape parameter
93 realT sigma, ///< [in] the scale parameter
94 realT lambda ///< [in] the denominator or 1/peak-scale
95 )
96 {
97 static_assert( nparams == 4, "fitExpModGaussian: Wrong setGuess called for no location parameter." );
98
99 this->p[3] = G0; // this is p[2] to make it easy to leave out
100 this->p[0] = mu;
101 this->p[1] = sigma;
102 this->p[2] = lambda;
103 }
104
105 /// Set the initial guess.
106 void setGuess( realT mu, ///< [in] the location parameter
107 realT sigma, ///< [in] the shape parameter
108 realT lambda ///< [in] the scale parameter
109 )
110 {
111 static_assert( nparams == 3, "fitExpModGaussian: Wrong setGuess called for no location parameter." );
112
113 this->p[0] = mu; // this is p[2] to make it easy to leave out
114 this->p[1] = sigma;
115 this->p[2] = lambda;
116 }
117
118 void setArray( realT *data, int n )
119 {
120 arr.data = data;
121 arr.n = n;
122
123 this->n = n;
124 }
125
126 void G0( realT nG0 )
127 {
128 arr.G0 = nG0;
129 if( nparams == 4 )
130 {
131 this->p[3] = nG0; // this is p[2] to make it easy to leave out
132 }
133 }
134
135 void mu( realT nmu )
136 {
137 arr.mu = nmu;
138 this->p[0] = nmu;
139 }
140
141 void sigma( realT ns )
142 {
143 arr.sigma = ns;
144 this->p[1] = ns;
145 }
146
147 void lambda( realT nl )
148 {
149 arr.lambda = nl;
150 this->p[2] = nl;
151 }
152
153 int fit()
154 {
156 }
157
158 realT G0()
159 {
160 if( nparams == 4 )
161 {
162 return this->p[3];
163 }
164 else
165 {
166 return 0;
167 }
168 }
169
170 realT mu()
171 {
172 return this->p[0];
173 }
174
175 realT sigma()
176 {
177 return this->p[1];
178 }
179
180 realT lambda()
181 {
182 return this->p[2];
183 }
184};
185
186/// Wrapper for a native array to pass to \ref levmarInterface, with Exponentially Modified Gaussian details.
187/** \ingroup gammaDist_peak_fit
188 */
189template <typename realT>
191{
192 realT *data{ nullptr }; ///< Pointer to the array
193 size_t n{ 0 }; ///< dimension of the array
194
195 realT G0{ 0 }; ///< the location parameter.
196 realT mu{ 0 }; ///< the shape parameter
197 realT sigma{ 0 }; ///< the scale parameter
198 realT lambda{ 0 }; ///< the denominator or 1/peak-scale
199};
200
201///\ref levmarInterface fitter structure for the Exponentially Modified Gaussian with fixed constant level.
202/**
203 *
204 * \ingroup gammaDist_peak_fit
205 *
206 */
207template <typename _realT>
209{
210 typedef _realT realT;
211
212 static const int nparams = 3;
213
214 static void func( realT *p, realT *hx, int m, int n, void *adata )
215 {
217
218 for( int i = 0; i < arr->n; i++ )
219 {
220 hx[i] = func::expModGaussian<realT>( i, p[0], p[1], p[2] ) - arr->data[i];
221 }
222 }
223};
224
225///\ref levmarInterface fitter structure for the Exponentially Modified Gaussian with arbitrary constant level.
226/**
227 *
228 * \ingroup gammaDist_peak_fit
229 *
230 */
231template <typename _realT>
233{
234 typedef _realT realT;
235
236 static const int nparams = 4;
237
238 static void func( realT *p, realT *hx, int m, int n, void *adata )
239 {
241
242 for( int i = 0; i < arr->n; i++ )
243 {
244 hx[i] = p[3] + func::expModGaussian<realT>( i, p[0], p[1], p[2] ) - arr->data[i];
245 }
246 }
247};
248
249} // namespace fit
250} // namespace math
251} // namespace mx
252
253#endif // fitExpModGaussian_hpp
void setGuess(realT mu, realT sigma, realT lambda)
Set the initial guess.
void setGuess(realT G0, realT mu, realT sigma, realT lambda)
Set the initial guess.
void allocate_params()
Allocate parameters array based on previous call to nParams.
realT * p
Parameter array. On input is the initial estimates. On output has the estimated solution.
int n
I: measurement vector dimension.
levmarInterface()
Default constructor.
void * adata
Pointer to possibly additional data, passed uninterpreted to func & jacf.
The Exponentially Modified Gaussian distribution.
realT expModGaussian(realT x, realT mu, realT sigma, realT lambda)
The Exponentially Modified Gaussian at a point.
A c++ interface to the templatized levmar minimization routines..
The mxlib c++ namespace.
Definition mxlib.hpp:37
Wrapper for a native array to pass to levmarInterface, with Exponentially Modified Gaussian details.
realT lambda
the denominator or 1/peak-scale
levmarInterface fitter structure for the Exponentially Modified Gaussian with fixed constant level.
levmarInterface fitter structure for the Exponentially Modified Gaussian with arbitrary constant leve...