mxlib
c++ tools for analyzing astronomical data and other tasks by Jared R. Males. [git repo]
fitWeibull.hpp
Go to the documentation of this file.
1 /** \file fitWeibull.hpp
2  * \author Jared R. Males
3  * \brief Tools for fitting the Weibull 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 fitWeibull_hpp
28 #define fitWeibull_hpp
29 
30 #include "levmarInterface.hpp"
31 #include "../func/weibull.hpp"
32 
33 namespace mx
34 {
35 namespace math
36 {
37 namespace fit
38 {
39 
40 template<typename realT>
41 struct array2FitWeibull;
42 
43 
44 /** \defgroup weibull_peak_fit Weibull Distribution
45  * \brief Fitting the Weibull Distribution to data.
46  *
47  * The Weibull Distribution is fit to data.
48  *
49  * \ingroup peak_fit
50  */
51 
52 ///Class to manage fitting the Weibull Distribution to data via the \ref levmarInterface
53 /** In addition to the requirements on fitterT specified by \ref levmarInterface
54  * this class also requires this definition in fitterT
55  * \code
56  * static const int nparams = 3;
57  * \endcode
58  * where the number 3 is replaced by the number of parameters that fitterT expects to fit.
59  *
60  * \tparam fitterT a type meeting the above requirements.
61  *
62  * \ingroup weibull_peak_fit
63  *
64  */
65 template<typename fitterT>
66 class fitWeibull : public levmarInterface<fitterT>
67 {
68 
69 public:
70 
71  typedef typename fitterT::realT realT;
72 
73  static const int nparams = fitterT::nparams;
74 
76 
77  void initialize()
78  {
79  this->allocate_params(nparams);
80  this->adata = &arr;
81  }
82 
83  fitWeibull()
84  {
85  initialize();
86  }
87 
88  ~fitWeibull()
89  {
90  }
91 
92  ///Set the initial guess when platescale and central obscuration are fixed.
93  void setGuess( realT x0, ///< [in] the location parameter
94  realT k, ///< [in] the shape parameter
95  realT lambda ///< [in] the scale parameter
96  )
97  {
98  static_assert( nparams==3 , "fitWeibull: Wrong setGuess called for no location parameter.");
99 
100  this->p[2] = x0;
101  this->p[0] = k;
102  this->p[1] = lambda;
103  }
104 
105  ///Set the initial guess when platescale and central obscuration are fixed.
106  void setGuess( realT k, ///< [in] the shape parameter
107  realT lambda ///< [in] the scale parameter
108  )
109  {
110  static_assert( nparams==2 , "fitWeibull: Wrong setGuess called for location parameter.");
111 
112  this->p[0] = k;
113  this->p[1] = lambda;
114  }
115 
116  void setArray(realT *data, int n)
117  {
118  arr.data = data;
119  arr.n = n;
120 
121  this->n = n;
122 
123  }
124 
125  void x0( realT nx0 )
126  {
127  arr.x0 = nx0;
128  if(nparams == 3)
129  {
130  this->p[2] = nx0;
131  }
132  }
133 
134  void k( realT nk )
135  {
136  arr.k = nk;
137  this->p[0] = nk;
138  }
139 
140  void lambda( realT nl )
141  {
142  arr.lambda = nl;
143  this->p[1] = nl;
144  }
145 
146  int fit()
147  {
149  }
150 
151  realT x0()
152  {
153  if(nparams == 3)
154  {
155  return this->p[2];
156  }
157  else
158  {
159  return 0;
160  }
161  }
162 
163  realT k()
164  {
165  return this->p[0];
166  }
167 
168  realT lambda()
169  {
170  return this->p[1];
171  }
172 
173 
174 
175 };
176 
177 
178 ///Wrapper for a native array to pass to \ref levmarInterface, with Weibull details.
179 /** \ingroup weibull_peak_fit
180  */
181 template<typename realT>
183 {
184  realT * data {nullptr}; ///< Pointer to the array
185  size_t n {0}; ///< dimension of the array
186 
187  realT x0 {0}; ///< the location parameter.
188  realT k {0}; ///< the shape parameter
189  realT lambda {0}; ///< the scale parameter
190 };
191 
192 ///\ref levmarInterface fitter structure for the shifted Weibull Distribution
193 /**
194  *
195  * \ingroup weibull_peak_fit
196  *
197  */
198 template<typename _realT>
200 {
201  typedef _realT realT;
202 
203  static const int nparams = 3;
204 
205  static void func(realT *p, realT *hx, int m, int n, void *adata)
206  {
208 
209  for(int i=0;i<arr->n; i++)
210  {
211  hx[i] = func::weibull<realT>(i, p[2], p[0], p[1]) - arr->data[i];
212  }
213  }
214 };
215 
216 ///\ref levmarInterface fitter structure for the shifted Weibull Distribution
217 /**
218  *
219  * \ingroup weibull_peak_fit
220  *
221  */
222 template<typename _realT>
224 {
225  typedef _realT realT;
226 
227  static const int nparams = 2;
228 
229  static void func(realT *p, realT *hx, int m, int n, void *adata)
230  {
232 
233  for(int i=0;i<arr->n; i++)
234  {
235  hx[i] = func::weibull<realT>(i, 0.0, p[0], p[1]) - arr->data[i];
236  }
237  }
238 };
239 
240 
241 } //namespace fit
242 } //namespace math
243 } //namespace mx
244 
245 #endif //fitWeibull_hpp
246 
Class to manage fitting the Weibull Distribution to data via the levmarInterface.
Definition: fitWeibull.hpp:67
void setGuess(realT k, realT lambda)
Set the initial guess when platescale and central obscuration are fixed.
Definition: fitWeibull.hpp:106
void setGuess(realT x0, realT k, realT lambda)
Set the initial guess when platescale and central obscuration are fixed.
Definition: fitWeibull.hpp:93
A templatized interface to the levmar package.
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.
void * adata
Pointer to possibly additional data, passed uninterpreted to func & jacf.
A c++ interface to the templatized levmar minimization routines..
The mxlib c++ namespace.
Definition: mxError.hpp:107
Wrapper for a native array to pass to levmarInterface, with Weibull details.
Definition: fitWeibull.hpp:183
realT x0
the location parameter.
Definition: fitWeibull.hpp:187
size_t n
dimension of the array
Definition: fitWeibull.hpp:185
realT k
the shape parameter
Definition: fitWeibull.hpp:188
realT lambda
the scale parameter
Definition: fitWeibull.hpp:189
realT * data
Pointer to the array.
Definition: fitWeibull.hpp:184
levmarInterface fitter structure for the shifted Weibull Distribution
Definition: fitWeibull.hpp:224
levmarInterface fitter structure for the shifted Weibull Distribution
Definition: fitWeibull.hpp:200