double Basic_MCT2_332_Calculator::mct2_332_Sq(const LorentzTransverseVector& visA, const LorentzTransverseVector& visB, const TwoVector& ptmiss, const double mEachInvisible){ mCT2Fcn theFCN(ptmiss.px(), ptmiss.py(), mEachInvisible, visA.px(), visA.py(), visA.mass(), visB.px(), visB.py(), visB.mass()); const double massScale = ( ptmiss.pt() + mEachInvisible + visA.pt() + visA.mass() + visB.pt() + visB.mass() )/6.0; // DANG! Try to get rid of Minuit output: //std::ofstream DANG_log("/dev/null"); //std::streambuf * DANG_save = std::cerr.rdbuf(); //if (DANG_log.is_open()) { // std::cerr.rdbuf(DANG_log.rdbuf()); // } double guessx = 0.5*(ptmiss.px()); double guessy = 0.5*(ptmiss.py()); MnUserParameters upar; upar.Add("etx", guessx, 0.02*massScale); upar.Add("ety", guessy, 0.02*massScale); const int highQuality=2; // Usually migrad produces the best minumum. // But when the minimum is in a fold, migrad can fail badly. // On the fold, simplex does well. We therefore do both separately // and record the answer of the one that did best. // Further to the above notes, it now seems that by choosing the massScale sensibly, and by making the "tolerance" (the second argument to the call that extracts the FunctionMinimum below) much smaller, the simplex algorithm seems to work so well that we don't need the migrad algorithm. This is good news, as the migrad algorithm produces lots of error output that we can't get rid of. MnSimplex simplex(theFCN, upar, highQuality); FunctionMinimum minS = simplex(0,massScale*0.000001); //const double etxAtMin = minS.UserState().Value("etx"); //const double etyAtMin = minS.UserState().Value("ety"); //MnMigrad migrad(theFCN, upar, highQuality); //FunctionMinimum minM = migrad(0,massScale*0.000001); //const double best = fmin(minS.Fval(), minM.Fval()); const double best = minS.Fval(); // DANG! Undoing our attempt to get rid of Minuit output: //if (DANG_log.is_open()) { // std::cerr.rdbuf(DANG_save); //} return best; }
int main ( int argc, char **argv ) { std::cout << "=== Program: " << argv[0] << std::endl; std::cout << "=== Version 1.0, (c) 2016 Erik Bartoš" << std::endl; /// Start TApplication theApp("Eta", &argc, argv); /// Fit std::cout << "\n> Eta fit:" << std::endl; std::cout << "> Input data: `" << dataFile1 << "'" << std::endl; std::cout << "> Start parameters: `" << parametersFile1 << "'" << std::endl; std::cout << "> Output parameters: `" << outputFile << "'" << std::endl; const int nPoints = 10000; double tMin; double tMax; double tStep; double tA; ExperimentalData Z; Z.ReadData(dataFile1); int nData = Z.size(); std::cout << "\nNumber of fitted points: " << nData << std::endl; std::vector<int> series = Z.Series(); std::vector<std::string> names = Z.Name(); std::vector<int> type = Z.Type(); std::vector<double> x = Z.X(); std::vector<double> val = Z.Val(); std::vector<double> errUp = Z.ErrUp(); std::vector<double> errDown = Z.ErrDown(); std::vector<double> theta = Z.Theta(); std::vector<double> energy = Z.Energy(); int tParticle = 1; FFactorT trans(tParticle); int nPar = trans.numberOfParameters; trans.LoadParameters(parametersFile1); trans.PrintParameters(); /// Create FCN function TransitionFcn fFCN(tParticle, Z.Type(), Z.X(), Z.Val(), Z.ErrUp(), Z.ErrDown(), Z.Theta(), Z.Energy()); /// Minuit double step = 0.01; MnUserParameters upar; for (int i = 0; i < nPar; ++i) { upar.Add(trans.v[i].name, trans.v[i].val, step, trans.v[i].down, trans.v[i].up); } MnStrategy(2); /// Create minimizer MnMigrad migrad(fFCN, upar); MnMinimize minimize(fFCN, upar); MnSimplex simplex(fFCN, upar); //migrad.Fix(0.); //migrad.Fix(1.); //migrad.Fix(2.); //migrad.Fix(3.); //migrad.Fix(4.); //migrad.Fix(5.); /// Fitting TStopwatch timer; timer.Start(); std::cout << "\n> Minimalization:" << std::endl; // operator(maxfcn,tolerance): // maxfcn: maximum function calls // migrad EDM: 0.001*tolerance*up // simplex: tolerance*up std::cout << "> 1. minimalization..." << std::endl; //FunctionMinimum min = migrad(100000,100.); //minimize.Fix(0.); //minimize.Fix(1.); //minimize.Fix(2.); //minimize.Fix(3.); //std::cout << "> ... 2. minimalization..." << std::endl; //FunctionMinimum min2 = migrad(10000,1.); std::cout << "> ... in progress..." << std::endl; //FunctionMinimum min9 = migrad(1000000,50.); FunctionMinimum min9 = minimize(1000000,50.); //FunctionMinimum min9 = simplex(1000000,1.); timer.Stop(); /// Standard output std::cout << "> Minimum: " << min9 << std::endl; std::cout << "> FCN value: " << min9.Fval() << std::endl; std::cout << "> FCN value/ndf: " << min9.Fval()/(nData-nPar) << std::endl; std::cout << "> Points : " << nData << std::endl; std::cout << min9.UserState() << std::endl; std::cout << min9.UserCovariance() << std::endl; std::cout << "> Real time of minimalization: " << timer.RealTime() << " sec." << std::endl; //~ // Scan and Plot parameters //~ { //~ MnScan scan(fFCN, upar, 1); //~ std::cout << "Scan parameters: " << scan.Parameters() << std::endl; //~ MnPlot plot; //~ for (unsigned int i = 0; i < upar.VariableParameters(); ++i) { //~ std::vector<std::pair<double, double> > xy = scan.Scan(i); //~ //std::vector<std::pair<double, double> > xy = scan.scan(0); //~ std::cout << i+1 << ". parameter - `" << upar.Name(i) << "'"<< std::endl; //~ plot(xy); //~ } //~ std::cout << scan.Parameters() << std::endl; //~ } /// Output parameters std::ofstream os; os.open(outputFile); os.precision(8); for (int i = 0; i < nPar; ++i) { os << i+1 << ", " << min9.UserParameters().Name(i) << ", "; os.width(12); os << min9.UserParameters().Value(i) << ", "; os.width(12); os << min9.UserParameters().Error(i) << ", "; os << trans.v[i].down << ", " << trans.v[i].up << std::endl; } os.close(); /// End output theApp.Run(); //theApp.Terminate(0); return EXIT_SUCCESS; }
int main( int argc, char *argv[]) { // Print a starting message banner("0.1, Option 4", "2016-04-28"); // We assume argv[1] is a filename to open std::ifstream ifs(argv[1]); std::vector< double > timestep, Year, Week, FishCatch, Targeted_Effort, Nontargeted_Effort; std::vector<string> YearType; double a, c, d, e, f, g; string b; long unsigned counter=0; try{ // Assess if data file is read entirely while( ifs >> a >> b >> c >> d >> e >> f >> g){ timestep.push_back( a ); YearType.push_back (b); Year.push_back(c); Week.push_back(d); FishCatch.push_back(e); Targeted_Effort.push_back(f); Nontargeted_Effort.push_back(g); //printf(" Reading %.2f %.2f %.2f from file \n", a, b, c, d); counter++; } // When finishing reading, if EOF not reached throw an error if(!ifs.eof()){ throw ReadingError();} ifs.close(); }catch(ReadingError){ std::cerr << "Error: could not read the entire file\n"; return 1;} std::cout << "In main, the size of FishCatch is " << FishCatch.size() << "\n"; ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //// Read the values of fixed parameters from file in local directory into global variables ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// std::string arg2(argv[2]); std::vector<Parameter> ModelInputParameters = ReadParameterDescription(arg2); //string FixParfilename = "FixParameters.txt"; //display_file(FixParfilename); // Assign fixed model's parameter set according to the information provided by the user in the csv file passed as 2nd argument rho = GetParameterValueAccordingToSymbol(ModelInputParameters, "rho"); wk = GetParameterValueAccordingToSymbol(ModelInputParameters, "wk"); wk_1 = GetParameterValueAccordingToSymbol(ModelInputParameters, "wk_1"); CatchabilityScalingFactor = GetParameterValueAccordingToSymbol(ModelInputParameters, "CatchabilityScalingFactor"); BiomassScalingFactor = GetParameterValueAccordingToSymbol(ModelInputParameters, "BiomassScalingFactor"); RecruitmentScalingFactor = GetParameterValueAccordingToSymbol(ModelInputParameters, "RecruitmentScalingFactor"); NIPY = (int) GetParameterValueAccordingToSymbol(ModelInputParameters, "NIPY"); //rho = fill_from_file(arg2, "Brody growth coefficient"); //wk = fill_from_file(arg2, "Estimated weight at recruitment"); //wk_1 = fill_from_file(arg2, "Parameter defining weight one timestep before recruitment"); ////M = fill_from_file(arg2, "Natural mortality"); // Natural mortality is estimated in this version of the delay difference //CatchabilityScalingFactor = fill_from_file(arg2, "Catchability scaling factor"); //BiomassScalingFactor = fill_from_file(arg2, "Biomass scaling factor"); //RecruitmentScalingFactor = fill_from_file(arg2, "Recruitment scaling factor"); //NIPY = (int) fill_from_file(arg2, "Number of intervals in a year"); ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //// Standard minimization using MIGRAD ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Declare the objective function (also called FCN function in documentation) LogLikelihoodFunction fFCN(FishCatch, Targeted_Effort, Nontargeted_Effort); //// create Minuit parameters with names MnUserParameters upar; //upar.Add("Natural mortality", 2.34 / (double) NIPY , 0.01); upar.Add("Natural mortality", GetParameterValueAccordingToShortName(ModelInputParameters, "Natural mortality"), 0.001); //upar.Add("Targeted q", 4, 2); upar.Add("Targeted q", GetParameterValueAccordingToShortName(ModelInputParameters, "Targeted q"), 0.5); //upar.Add("Nontargeted q", 2, 2); upar.Add("Nontargeted q", GetParameterValueAccordingToShortName(ModelInputParameters, "Nontargeted q"), 0.5); //upar.Add("sigma", 10, 0.1); upar.Add("sigma", GetParameterValueAccordingToShortName(ModelInputParameters, "sigma"), 1); //upar.Add("Biomass1", 1, 2); upar.Add("Biomass1", GetParameterValueAccordingToShortName(ModelInputParameters, "Biomass1"), 0.5); //upar.Add("Biomass2", 1, 2); upar.Add("Biomass2", GetParameterValueAccordingToShortName(ModelInputParameters, "Biomass2"), 0.5); //upar.Add("vm_mean", 0, 1); //upar.Add("vm_sigma", 5, 2); // variance of the recruitment distribution modelled with von Mises upar.Add("vm_sigma", GetParameterValueAccordingToShortName(ModelInputParameters, "vm_sigma"), 1); // Estimate 1 recruitment per year std::string RecVarName="Recruit year"; for(unsigned int i = 1; i <= counter / NIPY; ++i){ //upar.Add(RecVarName + " " + std::to_string(i), 0.2 * (i+1), .5); upar.Add(RecVarName + " " + std::to_string(i), GetParameterValueAccordingToShortName(ModelInputParameters, RecVarName + " " + std::to_string(i)), .5); } // Estimate 1 mean recruitment-distribution per year std::string RdistVarName="Mean Rec dist"; for(unsigned int i = 1; i <= counter / NIPY; ++i){ //upar.Add(RdistVarName + " " + std::to_string(i), 0.8, 0.5); // Starting at 0.8 for the tiger prawn Moreton Bay model upar.Add(RdistVarName + " " + std::to_string(i), GetParameterValueAccordingToShortName(ModelInputParameters, RdistVarName + " " + std::to_string(i)), .5); // Starting at 0.8 for the tiger prawn Moreton Bay model } // Assert parameters domain //upar.SetLimits("Natural mortality", 0.02, 0.07); upar.SetLimits("Natural mortality", GetParameterLowerLimitAccordingToShortName(ModelInputParameters, "Natural mortality"), GetParameterUpperLimitAccordingToShortName(ModelInputParameters, "Natural mortality")); //upar.SetLimits("Targeted q", 0, 1e2); upar.SetLimits("Targeted q", GetParameterLowerLimitAccordingToShortName(ModelInputParameters, "Targeted q"), GetParameterUpperLimitAccordingToShortName(ModelInputParameters, "Targeted q")); //upar.SetLimits("Nontargeted q", 0, 1e2); upar.SetLimits("Nontargeted q", GetParameterLowerLimitAccordingToShortName(ModelInputParameters, "Nontargeted q"), GetParameterUpperLimitAccordingToShortName(ModelInputParameters, "Nontargeted q")); //upar.SetLimits("sigma", 0, 1e2); upar.SetLimits("sigma", GetParameterLowerLimitAccordingToShortName(ModelInputParameters, "sigma"), GetParameterUpperLimitAccordingToShortName(ModelInputParameters, "sigma")); //upar.SetLimits("Biomass1", 0, 1e3); upar.SetLimits("Biomass1", GetParameterLowerLimitAccordingToShortName(ModelInputParameters, "Biomass1"), GetParameterUpperLimitAccordingToShortName(ModelInputParameters, "Biomass1")); //upar.SetLimits("Biomass2", 0, 1e3); upar.SetLimits("Biomass2", GetParameterLowerLimitAccordingToShortName(ModelInputParameters, "Biomass2"), GetParameterUpperLimitAccordingToShortName(ModelInputParameters, "Biomass2")); //upar.SetLimits("vm_mean", -10, 10); //upar.SetLimits("vm_sigma", 1e-3, 80); upar.SetLimits("vm_sigma", GetParameterLowerLimitAccordingToShortName(ModelInputParameters, "vm_sigma"), GetParameterUpperLimitAccordingToShortName(ModelInputParameters, "vm_sigma")); for(unsigned int i = 1; i <= counter/NIPY; ++i){ //upar.SetLimits(RecVarName + " " + std::to_string(i), 1e-2, 1e2); upar.SetLimits(RecVarName + " " + std::to_string(i), GetParameterLowerLimitAccordingToShortName(ModelInputParameters, RecVarName + " " + std::to_string(i)), GetParameterUpperLimitAccordingToShortName(ModelInputParameters, RecVarName + " " + std::to_string(i))); } for(unsigned int i = 1; i <= counter/NIPY; ++i){ //upar.SetLimits(RdistVarName + " " + std::to_string(i), -10, 10); upar.SetLimits(RdistVarName + " " + std::to_string(i), GetParameterLowerLimitAccordingToShortName(ModelInputParameters, RdistVarName + " " + std::to_string(i)), GetParameterUpperLimitAccordingToShortName(ModelInputParameters, RdistVarName + " " + std::to_string(i))); } cout << "The number of variable is " << upar.Params().size() << "\n"; // create MIGRAD minimizer with MnStrategy 0 (strategy to calculate first and second derivative with fewer function calls -- less precise result) MnMigrad migrad(fFCN, upar, 0); // Fix a parameter migrad.Fix("Natural mortality"); migrad.Fix("Targeted q"); migrad.Fix("Nontargeted q"); migrad.Fix("sigma"); for(unsigned int i = 1; i <= counter / NIPY; ++i){ migrad.Fix((RdistVarName + " " + std::to_string(i)).c_str()); } // Minimize FunctionMinimum min = migrad(); // output std::cout<<"minimum: "<< min << std::endl; migrad.Release("Natural mortality"); migrad.Release("Targeted q"); migrad.Release("Nontargeted q"); migrad.Release("sigma"); for(unsigned int i = 1; i <= counter / NIPY; ++i){ migrad.Release((RdistVarName + " " + std::to_string(i)).c_str()); } FunctionMinimum min2 = migrad(); std::cout<<"minimum2: "<< min2 << std::endl; ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // output results to file ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// std::vector<Parameter> ModelOutputParameters=ModelInputParameters; ofstream FitFile; FitFile.open ("Results/FitOutcome.txt"); FitFile << "# Minimum negative log likelihood\n" << min2 << "\n"; FitFile.close(); ofstream FitParamFile; FitParamFile.open ("Results/ParameterEstimates.txt"); for(unsigned int i = 0; i < upar.Params().size(); ++i){ SetParameterValueAccordingToShortName(ModelOutputParameters, upar.Name(i), min2.UserState().Value(i)); SetParameterUncertaintyAccordingToShortName(ModelOutputParameters, upar.Name(i),min2.UserState().Error(i)); FitParamFile << upar.Name(i) << "," << min2.UserState().Value(i) << "," << min2.UserState().Error(i) << "\n"; } FitParamFile.close(); // Write parameters to csv file WriteParameterToCSV("Results/DelayDifferenceModelParameters.csv", ModelOutputParameters); // Output estimates of fisheries quantities: fishing mortality, catch and biomass ofstream EFQ; EFQ.open ("Results/EstimatedFisheriesQuantities.txt"); EFQ << "timestep,YearType,Year,Week,EstimatedFishingMort,EstimatedBiomass,EstimatedCatches\n"; std::vector<double> EstimatedCatches(counter, 0.0), EstimatedBiomass(counter, 0.0), EstimatedFishingMort(counter, 0.0); std::vector<double> Residuals(counter, 0.0); // Get parameter estimates into a vector std::vector<double> EstimatedPar; for(unsigned int i = 0; i < upar.Params().size(); i++) EstimatedPar.push_back(min2.UserState().Value(i)); // Calculate estimated catch WeeklyDD(Targeted_Effort, Nontargeted_Effort, EstimatedBiomass, EstimatedPar); for(unsigned int iii=0; iii < counter; iii++) { EstimatedFishingMort.at(iii) = min2.UserState().Value("Targeted q") * CatchabilityScalingFactor * Targeted_Effort[iii] + min2.UserState().Value("Nontargeted q") * CatchabilityScalingFactor * Nontargeted_Effort[iii]; EstimatedCatches.at(iii) = EstimatedFishingMort[iii] / (min2.UserState().Value("Natural mortality") + EstimatedFishingMort[iii]) * EstimatedBiomass[iii] * \ (1 - exp( -(min2.UserState().Value("Natural mortality") + EstimatedFishingMort[iii] ) )); Residuals.at(iii) = (sqrt(EstimatedCatches[iii]) - sqrt(FishCatch[iii])) / min2.UserState().Value("sigma"); EFQ << timestep[iii] << "," << YearType[iii] << "," << Year[iii] << "," << Week[iii] << "," << EstimatedFishingMort[iii] << "," << EstimatedBiomass[iii] << "," << EstimatedCatches[iii] << "\n"; } EFQ.close(); return 0; }
int main() { std::vector<double> positions; std::vector<double> measurements; std::vector<double> var; double nmeas = 0; #ifdef USE_SEALBASE seal::Filename inputFile (seal::Filename ("$SEAL/src/MathLibs/Minuit/tests/MnSim/paul2.txt").substitute (seal::ShellEnvironment ())); std::ifstream in(inputFile.name() ); #else std::ifstream in("paul2.txt"); #endif if (!in) { std::cerr << "Error opening input data file" << std::endl; return 1; } // read input data { double x = 0., y = 0., width = 0., err = 0., un1 = 0., un2 = 0.; while(in>>x>>y>>width>>err>>un1>>un2) { if(err < 1.e-8) continue; positions.push_back(x); measurements.push_back(y); var.push_back(err*err); nmeas += y; } std::cout<<"size= "<<var.size()<<std::endl; assert(var.size() > 0); std::cout<<"nmeas: "<<nmeas<<std::endl; } // create FCN function GaussFcn theFCN(measurements, positions, var); std::vector<double> meas = theFCN.measurements(); std::vector<double> pos = theFCN.positions(); // create initial starting values for parameters double x = 0.; double x2 = 0.; double norm = 0.; double area = 0.; double dx = pos[1]-pos[0]; for(unsigned int i = 0; i < meas.size(); i++) { norm += meas[i]; x += (meas[i]*pos[i]); x2 += (meas[i]*pos[i]*pos[i]); area += dx*meas[i]; } double mean = x/norm; double rms2 = x2/norm - mean*mean; std::cout<<"initial mean: "<<mean<<std::endl; std::cout<<"initial sigma: "<<sqrt(rms2)<<std::endl; std::cout<<"initial area: "<<area<<std::endl; std::vector<double> init_val(3); init_val[0] = mean; init_val[1] = sqrt(rms2); init_val[2] = area; std::cout<<"initial fval: "<<theFCN(init_val)<<std::endl; MnUserParameters upar; upar.add("mean", mean, 1.); upar.add("sigma", sqrt(rms2), 1.); upar.add("area", area, 10.); MnMigrad migrad(theFCN, upar); std::cout<<"start migrad "<<std::endl; FunctionMinimum min = migrad(); std::cout<<"minimum: "<<min<<std::endl; std::cout<<"start minos"<<std::endl; MnMinos minos(theFCN, min); std::pair<double,double> e0 = minos(0); std::pair<double,double> e1 = minos(1); std::pair<double,double> e2 = minos(2); std::cout<<"par0: "<<min.userState().value("mean")<<" "<<e0.first<<" "<<e0.second<<std::endl; std::cout<<"par1: "<<min.userState().value(1)<<" "<<e1.first<<" "<<e1.second<<std::endl; std::cout<<"par2: "<<min.userState().value(2)<<" "<<e2.first<<" "<<e2.second<<std::endl; return 0; }