39 #include <unsupported/Eigen/NonLinearOptimization>
92 template<
typename _Scalar,
int NX = Eigen::Dynamic,
int NY = Eigen::Dynamic>
97 InputsAtCompileTime = NX,
98 ValuesAtCompileTime = NY
100 typedef Eigen::Matrix<Scalar,InputsAtCompileTime,1>
InputType;
101 typedef Eigen::Matrix<Scalar,ValuesAtCompileTime,1>
ValueType;
102 typedef Eigen::Matrix<Scalar,ValuesAtCompileTime,InputsAtCompileTime>
JacobianType;
106 Functor() : m_inputs(InputsAtCompileTime), m_values(ValuesAtCompileTime) {}
107 Functor(
int inputs,
int values) : m_inputs(inputs), m_values(values) {}
119 m_data(data), m_weights(weights)
123 int operator()(
const Eigen::VectorXd &x, Eigen::VectorXd &fvec)
const
126 double sigma = fabs(x(1));
127 double logsigma = log(sigma);
128 auto wit = m_weights.cbegin();
129 for (
auto it = m_data.cbegin(); it != m_data.cend(); ++it, ++wit)
131 double diff = (*it - x(0)) / sigma;
132 fvec(0) += *wit * (-logsigma - diff - exp(-diff));
134 double foo = -fvec(0);
155 Eigen::VectorXd x_init (2);
156 x_init(0) = init_param_.a;
157 x_init(1) = init_param_.b;
159 Eigen::NumericalDiff<GumbelDistributionFunctor> numDiff(functor);
160 Eigen::LevenbergMarquardt<Eigen::NumericalDiff<GumbelDistributionFunctor>,
double> lm(numDiff);
161 Eigen::LevenbergMarquardtSpace::Status status = lm.minimize(x_init);
166 if (status <= Eigen::LevenbergMarquardtSpace::Status::ImproperInputParameters)
168 throw Exception::UnableToFit(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,
"UnableToFit-GumbelMaxLikelihoodFitter",
"Could not fit the gumbel distribution to the data");
171 #ifdef GUMBEL_DISTRIBUTION_FITTER_VERBOSE
173 stringstream formula;
174 formula <<
"f(x)=" <<
"(1/" << x_init(1) <<
") * " <<
"exp(( " << x_init(0) <<
"- x)/" << x_init(1) <<
") * exp(-exp((" << x_init(0) <<
" - x)/" << x_init(1) <<
"))";
175 cout << formula.str() << endl;
177 init_param_.a = x_init(0);
178 init_param_.b = fabs(x_init(1));
180 return {x_init(0), fabs(x_init(1))};
Exception used if an error occurred while fitting a model to a given dataset.
Definition: Exception.h:684
Implements a fitter for the Gumbel distribution.
Definition: GumbelMaxLikelihoodFitter.h:60
virtual ~GumbelMaxLikelihoodFitter()
Destructor.
GumbelMaxLikelihoodFitter & operator=(const GumbelMaxLikelihoodFitter &rhs)
assignment operator (not implemented)
GumbelMaxLikelihoodFitter(const GumbelMaxLikelihoodFitter &rhs)
Copy constructor (not implemented)
GumbelDistributionFitResult fitWeighted(const std::vector< double > &x, const std::vector< double > &w)
Fits a gumbel distribution to the given data x values. Fills a weighted histogram first and generates...
Definition: GumbelMaxLikelihoodFitter.h:152
GumbelDistributionFitResult init_param_
Definition: GumbelMaxLikelihoodFitter.h:185
GumbelMaxLikelihoodFitter(GumbelDistributionFitResult init)
Default constructor.
GumbelMaxLikelihoodFitter()
Default constructor.
void setInitialParameters(const GumbelDistributionFitResult &result)
sets the gumbel distribution start parameters a and b for the fitting
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
Definition: GumbelMaxLikelihoodFitter.h:94
Functor()
Definition: GumbelMaxLikelihoodFitter.h:106
Eigen::Matrix< Scalar, InputsAtCompileTime, 1 > InputType
Definition: GumbelMaxLikelihoodFitter.h:100
int values() const
Definition: GumbelMaxLikelihoodFitter.h:110
Eigen::Matrix< Scalar, ValuesAtCompileTime, InputsAtCompileTime > JacobianType
Definition: GumbelMaxLikelihoodFitter.h:102
Eigen::Matrix< Scalar, ValuesAtCompileTime, 1 > ValueType
Definition: GumbelMaxLikelihoodFitter.h:101
int m_inputs
Definition: GumbelMaxLikelihoodFitter.h:104
_Scalar Scalar
Definition: GumbelMaxLikelihoodFitter.h:95
Functor(int inputs, int values)
Definition: GumbelMaxLikelihoodFitter.h:107
int inputs() const
Definition: GumbelMaxLikelihoodFitter.h:109
struct to represent the parameters of a gumbel distribution
Definition: GumbelMaxLikelihoodFitter.h:66
double a
location parameter a
Definition: GumbelMaxLikelihoodFitter.h:74
double b
scale parameter b
Definition: GumbelMaxLikelihoodFitter.h:76
double log_eval_no_normalize(double x) const
GumbelDistributionFitResult(double local_a, double local_b)
Definition: GumbelMaxLikelihoodFitter.h:67
Definition: GumbelMaxLikelihoodFitter.h:115
GumbelDistributionFunctor(const std::vector< double > &data, const std::vector< double > &weights)
Definition: GumbelMaxLikelihoodFitter.h:117
int operator()(const Eigen::VectorXd &x, Eigen::VectorXd &fvec) const
Definition: GumbelMaxLikelihoodFitter.h:123
const std::vector< double > & m_data
Definition: GumbelMaxLikelihoodFitter.h:139
const std::vector< double > & m_weights
Definition: GumbelMaxLikelihoodFitter.h:140