00001
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef __BCENGINEMCMC__H
00023 #define __BCENGINEMCMC__H
00024
00025
00026
00027 #include <iostream>
00028 #include <vector>
00029
00030 #include <math.h>
00031
00032 #include <TH1D.h>
00033 #include <TH2D.h>
00034 #include <TTree.h>
00035 #include <TRandom3.h>
00036 #include <TPrincipal.h>
00037
00038
00039
00040
00041 class BCEngineMCMC
00042 {
00043
00044 public:
00045
00047
00048
00053 BCEngineMCMC();
00054
00058 BCEngineMCMC(int n);
00059
00063 BCEngineMCMC(const BCEngineMCMC & enginemcmc);
00064
00068 virtual ~BCEngineMCMC();
00069
00070
00071
00073
00074
00078 BCEngineMCMC & operator = (const BCEngineMCMC & engineMCMC);
00079
00080
00081
00083
00084
00085
00086
00087
00088 int MCMCGetNParameters()
00089 { return fMCMCNParameters; };
00090
00091
00092
00093
00094 int MCMCGetNChains()
00095 { return fMCMCNChains; };
00096
00097
00098
00099
00100 std::vector <int> MCMCGetNIterations()
00101 { return fMCMCNIterations; };
00102
00103
00104
00105
00106 std::vector <int> * MCMCGetP2NIterations()
00107 { return &fMCMCNIterations; };
00108
00109
00110
00111
00112
00113 std::vector <int> MCMCGetNIterationsConvergenceLocal()
00114 { return fMCMCNIterationsConvergenceLocal; };
00115
00116
00117
00118
00119
00120 int MCMCGetNIterationsConvergenceGlobal()
00121 { return fMCMCNIterationsConvergenceGlobal; };
00122
00123
00124
00125
00126 bool MCMCGetFlagConvergenceGlobal()
00127 { return fMCMCFlagConvergenceGlobal; };
00128
00129
00130
00131
00132 int MCMCGetNIterationsMax()
00133 { return fMCMCNIterationsMax; };
00134
00135
00136
00137
00138
00139 int MCMCGetNIterationsBurnIn()
00140 { return fMCMCNIterationsBurnIn; };
00141
00142
00143
00144
00145
00146 int MCMCGetNIterationsPCA()
00147 { return fMCMCNIterationsPCA; };
00148
00149
00150
00151
00152 std::vector <int> MCMCGetNTrialsTrue()
00153 { return fMCMCNTrialsTrue; };
00154
00155
00156
00157
00158 std::vector <int> MCMCGetNTrialsFalse()
00159 { return fMCMCNTrialsFalse; };
00160
00161
00162
00163
00164
00165 std::vector <double> MCMCGetMean()
00166 { return fMCMCMean; };
00167
00168
00169
00170
00171
00172 std::vector <double> MCMCGetVariance()
00173 { return fMCMCVariance; };
00174
00175
00176
00177
00178
00179 bool MCMCGetFlagIterationsAuto()
00180 { return fMCMCFlagIterationsAuto; };
00181
00182
00183
00184
00185 double MCMCGetTrialFunctionScale()
00186 { return fMCMCTrialFunctionScale; };
00187
00188
00189
00190
00191 std::vector <double> MCMCGetTrialFunctionScaleFactor()
00192 { return fMCMCTrialFunctionScaleFactor; };
00193
00194
00195
00196
00197
00198
00199 std::vector <double> MCMCGetTrialFunctionScaleFactor(int i);
00200
00201
00202
00203
00204
00205
00206
00207 double MCMCGetTrialFunctionScaleFactor(int i, int j);
00208
00209
00210
00211
00212 std::vector <double> MCMCGetx()
00213 { return fMCMCx; };
00214
00215
00216
00217
00218 std::vector <double> * MCMCGetP2x()
00219 { return &fMCMCx; };
00220
00221
00222
00223
00224
00225 std::vector <double> MCMCGetx(int i);
00226
00227
00228
00229
00230
00231
00232 double MCMCGetx(int i, int j);
00233
00234
00235
00236
00237
00238 std::vector <double> MCMCGetLogProbx()
00239 { return fMCMCLogProbx; };
00240
00241
00242
00243
00244
00245
00246 double MCMCGetLogProbx(int i);
00247
00248
00249
00250
00251
00252 std::vector <double> * MCMCGetP2LogProbx()
00253 { return &fMCMCLogProbx; };
00254
00255
00256
00257
00258 std::vector <double> MCMCGetMaximumPoints()
00259 { return fMCMCMaximumPoints; };
00260
00261
00262
00263
00264
00265 std::vector <double> MCMCGetMaximumPoint(int i);
00266
00267
00268
00269
00270 std::vector <double> MCMCGetMaximumLogProb()
00271 { return fMCMCMaximumLogProb; };
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285 int MCMCGetFlagInitialPosition()
00286 { return fMCMCFlagInitialPosition; };
00287
00288
00289
00290
00291 double MCMCGetRValueCriterion()
00292 { return fMCMCRValueCriterion; };
00293
00294
00295
00296
00297 double MCMCGetRValueParametersCriterion()
00298 { return fMCMCRValueParametersCriterion; };
00299
00300
00301
00302
00303 double MCMCGetRValue()
00304 { return fMCMCRValue; };
00305
00306 double MCMCGetRValueParameters(int i)
00307 { return fMCMCRValueParameters.at(i); };
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318 bool MCMCGetFlagPCA()
00319 { return fMCMCFlagPCA; };
00320
00321
00322
00323
00324
00325
00326 TTree * MCMCGetMarkovChainTree(int i)
00327 { return fMCMCTrees.at(i); };
00328
00329
00330
00331
00332
00333
00334
00335 TH1D * MCMCGetH1Marginalized(int index1)
00336 { return fMCMCH1Marginalized[index1]; };
00337
00338
00339
00340
00341
00342
00343
00344
00345 TH2D * MCMCGetH2Marginalized(int index1, int index2);
00346
00347
00348
00350
00351
00352
00353
00354
00355
00356 void MCMCSetTrialFunctionScale(double scale)
00357 { fMCMCTrialFunctionScale = scale; };
00358
00359 void MCMCSetTrialFunctionScaleFactor(std::vector <double> scale)
00360 { fMCMCTrialFunctionScaleFactorStart = scale; };
00361
00362
00363
00364
00365
00366 void MCMCSetNParameters(int n);
00367
00368
00369
00370
00371
00372 void MCMCSetNChains(int n);
00373
00374
00375
00376
00377
00378 void MCMCSetNIterationsMax(int n)
00379 { fMCMCNIterationsMax = n; };
00380
00381
00382
00383
00384
00385 void MCMCSetNIterationsRun(int n)
00386 { fMCMCNIterationsRun = n; };
00387
00388
00389
00390
00391
00392 void MCMCSetNIterationsBurnIn(int n)
00393 { fMCMCNIterationsBurnIn = n; };
00394
00395
00396
00397
00398
00399 void MCMCSetNIterationsPCA(int n)
00400 { fMCMCNIterationsPCA = n; };
00401
00402
00403
00404
00405
00406
00407 void MCMCSetIterationsAuto(bool flag)
00408 { fMCMCFlagIterationsAuto = flag; };
00409
00410
00411
00412
00413 void MCMCSetMinimumEfficiency(double efficiency)
00414 { fMCMCEfficiencyMin = efficiency; };
00415
00416
00417
00418
00419 void MCMCSetMaximumEfficiency(double efficiency)
00420 { fMCMCEfficiencyMax = efficiency; };
00421
00422
00423
00424
00425
00426 void MCMCSetWriteChainToFile(bool flag)
00427 { fMCMCFlagWriteChainToFile = flag; };
00428
00429
00430
00431
00432
00433
00434
00435 void MCMCSetInitialPosition(std::vector<double> x0);
00436 void MCMCSetInitialPosition(int chain, std::vector<double> x0);
00437
00438
00439
00440
00441
00442 void MCMCSetInitialPositions(std::vector<double> x0s);
00443
00444
00445
00446
00447 void MCMCSetFlagInitialPosition(int flag)
00448 { fMCMCFlagInitialPosition = flag; };
00449
00450
00451
00452
00453 void MCMCSetRValueCriterion(double r)
00454 { fMCMCRValueCriterion = r; };
00455
00456
00457
00458
00459 void MCMCSetRValueParametersCriterion(double r)
00460 { fMCMCRValueParametersCriterion = r; };
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471 void MCMCSetFlagPCA(bool flag)
00472 { fMCMCFlagPCA = flag; };
00473
00474
00475
00476
00477 void MCMCSetMarkovChainTrees(std::vector <TTree *> trees);
00478
00479
00480
00481
00482
00483 void MCMCSetFlagPCATruncate(bool flag)
00484 { fMCMCFlagPCATruncate = flag; };
00485
00486
00487
00488
00489
00490 void MCMCSetPCAMinimumRatio(double ratio)
00491 { fMCMCPCAMinimumRatio = ratio; };
00492
00493
00494
00496
00497
00498
00499
00500
00501
00502
00503
00504 int MCMCAddParameter(double min, double max);
00505
00506
00507
00508
00509
00510
00511
00512 void MCMCTrialFunction(std::vector <double> &x);
00513 void MCMCTrialFunctionSingle(int ichain, int iparameter, std::vector <double> &x);
00514
00515
00516
00517
00518
00519
00520
00521
00522 double MCMCTrialFunctionIndependent(std::vector <double> &xnew, std::vector <double> &xold, bool newpoint);
00523
00524
00525
00526
00527
00528 void MCMCTrialFunctionAuto(std::vector <double> &x);
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540 double MCMCTrialFunctionRelativeNoPCA(std::vector <double> * xold, std::vector<double> * xnew, bool flag_compute);
00541
00542
00543
00544
00545 void MCMCGetProposalPoint(int chain, std::vector <double> xnew, std::vector <double> xold);
00546
00547
00548
00549
00550
00551
00552
00553
00554 bool MCMCGetProposalPointMetropolis(int chain, std::vector <double> &x, bool pca);
00555
00556
00557
00558
00559
00560
00561
00562
00563 bool MCMCGetProposalPointMetropolis(int chain, int parameter, std::vector <double> &x, bool pca);
00564
00565
00566
00567
00568
00569
00570
00571
00572 bool MCMCGetProposalPointMetropolisHastings(int chain, std::vector <double> &xnew, std::vector <double> &xold);
00573
00574
00575
00576
00577
00578 void MCMCGetNewPointPCA();
00579
00580
00581
00582
00583
00584
00585 bool MCMCGetNewPointMetropolis(int chain = 0, bool pca = false);
00586 bool MCMCGetNewPointMetropolis(int chain = 0, int parameter = 0, bool pca = false);
00587
00588
00589
00590
00591
00592 bool MCMCGetNewPointMetropolisHastings(int chain = 0);
00593
00594
00595
00596
00597
00598
00599 bool MCMCGetNewPointSimulatedAnnealing(int chain = 0, bool pca = false);
00600
00601
00602
00603
00604
00605 double MCMCAnnealingSchedule(int chain);
00606
00607
00608
00609
00610 void MCMCUpdateStatistics();
00611
00612 void MCMCUpdateStatisticsModeConvergence();
00613
00614 void MCMCUpdateStatisticsCheckMaximum();
00615
00616 void MCMCUpdateStatisticsFillHistograms();
00617
00618 void MCMCUpdateStatisticsTestConvergenceAllChains();
00619
00620 void MCMCUpdateStatisticsWriteChains();
00621
00622
00623
00624
00625 virtual double LogEval(std::vector <double> parameters);
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635 int MCMCMetropolis();
00636
00637
00638
00639
00640 int MCMCMetropolisPreRun();
00641
00642
00643
00644
00645 int MCMCMetropolisHastings();
00646
00647
00648
00649
00650 int MCMCSimulatedAnnealing();
00651
00652
00653
00654
00655 void MCMCInitialRun();
00656
00657
00658
00659
00660 void MCMCResetRunStatistics();
00661
00662
00663
00664
00665 void MCMCInitializeMarkovChains();
00666
00667
00668
00669
00670 int MCMCInitialize();
00671
00672
00673
00674
00675 virtual void MCMCUpdateFunctionFitting()
00676 { return; };
00677
00678
00679
00680
00681 virtual void MCMCUserInterface();
00682
00683
00684 int SetMarginalized(int index, TH1D * h);
00685 int SetMarginalized(int index1, int index2, TH2D * h);
00686
00687
00688
00689 private:
00690
00691
00692
00693
00694 void Copy(BCEngineMCMC & enginemcmc) const;
00695
00696
00697
00698
00699 typedef bool (BCEngineMCMC::*MCMCPointerToGetProposalPoint) (int chain, std::vector <double> xnew, std::vector <double> xold) const;
00700
00701
00702
00703
00704 MCMCPointerToGetProposalPoint fMCMCPointerToGetProposalPoint;
00705
00706 protected:
00707
00708
00709
00710
00711 int fMCMCNParameters;
00712
00713
00714
00715
00716 std::vector <double> fMCMCBoundaryMin;
00717 std::vector <double> fMCMCBoundaryMax;
00718
00719
00720
00721
00722 int fMCMCNChains;
00723
00724
00725
00726
00727
00728 std::vector<int> fMCMCNIterations;
00729
00730
00731
00732
00733 int fMCMCNIterationsUpdate;
00734
00735
00736
00737
00738
00739 std::vector<int> fMCMCNIterationsConvergenceLocal;
00740
00741
00742
00743
00744 int fMCMCNIterationsConvergenceGlobal;
00745
00746
00747
00748
00749 bool fMCMCFlagConvergenceGlobal;
00750
00751
00752
00753
00754 int fMCMCNIterationsMax;
00755
00756
00757
00758
00759 int fMCMCNIterationsRun;
00760
00761
00762
00763
00764 int fMCMCNIterationsPreRunMin;
00765
00766
00767
00768
00769
00770 int fMCMCNIterationsBurnIn;
00771
00772
00773
00774
00775
00776 int fMCMCNIterationsPCA;
00777
00778
00779
00780
00781 std::vector <double> fMCMCPCAMean;
00782
00783
00784
00785
00786 std::vector <double> fMCMCPCAVariance;
00787
00788
00789
00790
00791
00792 bool fMCMCFlagPCATruncate;
00793
00794
00795
00796
00797
00798 double fMCMCPCAMinimumRatio;
00799
00800
00801
00802
00803
00804 int fMCMCNDimensionsPCA;
00805
00806
00807
00808
00809
00810
00811
00812 std::vector<int> fMCMCNTrialsTrue;
00813 std::vector<int> fMCMCNTrialsFalse;
00814
00815
00816
00817
00818
00819 std::vector <double> fMCMCMean;
00820 std::vector <double> fMCMCVariance;
00821
00822
00823
00824
00825
00826
00827 std::vector <double> fMCMCSum;
00828 std::vector <double> fMCMCSum2;
00829
00830
00831
00832
00833
00834 std::vector <double> fMCMCMeanx;
00835 std::vector <double> fMCMCVariancex;
00836
00837
00838
00839
00840
00841 bool fMCMCFlagIterationsAuto;
00842
00843
00844
00845
00846 bool fMCMCFlagWriteChainToFile;
00847
00848
00849
00850
00851 double fMCMCTrialFunctionScale;
00852
00853
00854
00855
00856
00857 std::vector <double> fMCMCTrialFunctionScaleFactor;
00858
00859 std::vector <double> fMCMCTrialFunctionScaleFactorStart;
00860
00861
00862
00863
00864 bool fMCMCFlagPreRun;
00865
00866
00867
00868
00869
00870
00871
00872 std::vector <double> fMCMCInitialPosition;
00873
00874
00875
00876
00877 double fMCMCEfficiencyMin;
00878
00879
00880
00881
00882 double fMCMCEfficiencyMax;
00883
00884
00885
00886
00887
00888
00889 int fMCMCFlagInitialPosition;
00890
00891
00892
00893
00894
00895
00896
00897 std::vector <double> fMCMCx;
00898
00899
00900
00901
00902 std::vector <double> fMCMCxLocal;
00903
00904
00905
00906
00907
00908 std::vector<double> fMCMCLogProbx;
00909
00910
00911
00912
00913
00914
00915 std::vector <double> fMCMCMaximumPoints;
00916
00917
00918
00919
00920
00921 std::vector <double> fMCMCMaximumLogProb;
00922
00923
00924
00925
00926 double fMCMCRValueCriterion;
00927
00928 double fMCMCRValueParametersCriterion;
00929
00930
00931
00932
00933 double fMCMCRValue;
00934
00935 std::vector <double> fMCMCRValueParameters;
00936
00937
00938
00939
00940
00941
00942
00943
00944 std::vector <double> fMCMCNumericalPrecisionModeValues;
00945
00946
00947
00948
00949 double fMCMCSimulatedAnnealingT0;
00950
00951
00952
00953
00954 TRandom3 * fMCMCRandom;
00955
00956
00957
00958
00959 TPrincipal * fMCMCPCA;
00960
00961
00962
00963
00964 bool fMCMCFlagPCA;
00965
00966
00967
00968
00969 int fMCMCH1NBins;
00970 int fMCMCH2NBinsX;
00971 int fMCMCH2NBinsY;
00972
00973
00974
00975
00976 std::vector <TH1D *> fMCMCH1Marginalized;
00977 std::vector <TH2D *> fMCMCH2Marginalized;
00978
00979
00980
00981
00982
00983
00984
00985
00986
00987
00988
00989 std::vector<TTree *> fMCMCTrees;
00990
00991 };
00992
00993
00994
00995 #endif