22 #include "SealBase/Filename.h" 23 #include "SealBase/ShellEnvironment.h" 32 PowerLawFunc(
double _p0,
double _p1) : fP0(_p0), fP1(_p1) {}
36 double operator()(
double x)
const {
40 double p0()
const {
return fP0;}
41 double p1()
const {
return fP1;}
48 class PowerLawChi2FCN :
public FCNBase {
52 PowerLawChi2FCN(
const std::vector<double>& meas,
53 const std::vector<double>& pos,
54 const std::vector<double>& mvar) : fMeasurements(meas),
60 double operator()(
const std::vector<double>&
par)
const {
61 assert(par.size() == 2);
62 PowerLawFunc pl(par[0], par[1]);
65 for(
unsigned int n = 0;
n < fMeasurements.size();
n++) {
66 chi2 += ((pl(fPositions[
n]) - fMeasurements[
n])*(pl(fPositions[
n]) - fMeasurements[
n])/fMVariances[n]);
72 double Up()
const {
return 1.;}
75 std::vector<double> fMeasurements;
76 std::vector<double> fPositions;
77 std::vector<double> fMVariances;
80 class PowerLawLogLikeFCN :
public FCNBase {
84 PowerLawLogLikeFCN(
const std::vector<double>& meas,
85 const std::vector<double>& pos) :
86 fMeasurements(meas), fPositions(pos) {}
88 ~PowerLawLogLikeFCN() {}
90 double operator()(
const std::vector<double>& par)
const {
91 assert(par.size() == 2);
92 PowerLawFunc pl(par[0], par[1]);
95 for(
unsigned int n = 0;
n < fMeasurements.size();
n++) {
96 double k = fMeasurements[
n];
97 double mu = pl(fPositions[
n]);
98 logsum += (k*
log(mu) - mu);
104 double Up()
const {
return 0.5;}
107 std::vector<double> fMeasurements;
108 std::vector<double> fPositions;
113 std::vector<double> positions;
114 std::vector<double> measurements;
115 std::vector<double> var;
119 seal::Filename inputFile (seal::Filename (
"$SEAL/src/MathLibs/Minuit/tests/MnSim/paul4.txt").substitute (seal::ShellEnvironment ()));
120 std::ifstream in(inputFile.Name() );
122 std::ifstream in(
"paul4.txt");
125 std::cerr <<
"Error opening input data file" << std::endl;
130 double x = 0.,
y = 0., err = 0.;
131 while(in>>x>>
y>>err) {
133 positions.push_back(x);
134 measurements.push_back(
y);
135 var.push_back(err*err);
137 std::cout<<
"size= "<<var.size()<<std::endl;
141 std::cout<<
">>> test Chi2"<<std::endl;
142 PowerLawChi2FCN fFCN(measurements, positions, var);
145 upar.
Add(
"p0", -2.3, 0.2);
146 upar.
Add(
"p1", 1100., 10.);
149 std::cout<<
"start migrad "<<std::endl;
153 std::cout<<
"FM is invalid, try with strategy = 2."<<std::endl;
157 std::cout<<
"minimum: "<<min<<std::endl;
160 std::cout<<
">>> test log LikeliHood"<<std::endl;
162 PowerLawLogLikeFCN fFCN(measurements, positions);
165 upar.
Add(
"p0", -2.1, 0.2);
166 upar.
Add(
"p1", 1000., 10.);
169 std::cout<<
"start migrad "<<std::endl;
173 std::cout<<
"FM is invalid, try with strategy = 2."<<std::endl;
177 std::cout<<
"minimum: "<<min<<std::endl;
180 std::cout<<
">>> test Simplex"<<std::endl;
181 PowerLawChi2FCN chi2(measurements, positions, var);
182 PowerLawLogLikeFCN mlh(measurements, positions);
185 std::vector<double>
par; par.push_back(-2.3); par.push_back(1100.);
186 std::vector<double> err; err.push_back(1.); err.push_back(1.);
190 std::cout<<
"start simplex"<<std::endl;
192 std::cout<<
"minimum: "<<min<<std::endl;
194 std::cout<<
"minimum: "<<min2<<std::endl;
API class for minimization using Variable Metric technology ("MIGRAD"); allows for user interaction: ...
bool Add(const std::string &, double, double)
Add free Parameter Name, Value, Error.
class holding the full result of the minimization; both internal and external (MnUserParameterState) ...
Interface (abstract class) defining the function to be minimized, which has to be implemented by the ...
static double p1(double t, double a, double b)
API class for the user interaction with the parameters; serves as input to the minimizer as well as o...
virtual FunctionMinimum Minimize(const FCNBase &, const std::vector< double > &, const std::vector< double > &, unsigned int stra=1, unsigned int maxfcn=0, double toler=0.1) const
Class implementing the required methods for a minimization using Simplex.