Jpp test-rotations-old-533-g2bdbdb559
the software that should make you happy
Loading...
Searching...
No Matches
JTank.cc File Reference

Test application for estimate of maximam number from limited sampling. More...

#include <string>
#include <iostream>
#include <iomanip>
#include <vector>
#include "TRandom3.h"
#include "JTools/JStats.hh"
#include "JMath/JMathSupportkit.hh"
#include "Jeep/JParser.hh"

Go to the source code of this file.

Functions

int main (int argc, char **argv)
 

Detailed Description

Test application for estimate of maximam number from limited sampling.

This estimate is known as the German tank problem.

Author
mdejong

Definition in file JTank.cc.

Function Documentation

◆ main()

int main ( int argc,
char ** argv )

Definition at line 21 of file JTank.cc.

22{
23 using namespace std;
24 using namespace JPP;
25
26 size_t N;
27 size_t k;
28 size_t start;
29 size_t M;
30 Int_t seed;
31 int debug;
32
33 try {
34
35 JParser<> zap;
36
37 zap['N'] = make_field(N);
38 zap['k'] = make_field(k);
39 zap['s'] = make_field(start) = 1;
40 zap['M'] = make_field(M) = 1;
41 zap['S'] = make_field(seed) = 0;
42 zap['d'] = make_field(debug) = 3;
43
44 zap(argc, argv);
45 }
46 catch(const exception& error) {
47 FATAL(error.what() << endl);
48 }
49
50
51 gRandom->SetSeed(seed);
52
53
54 /*
55 // https://en.wikipedia.org/wiki/German_tank_problem
56 cout << getMinimumVarianceUnbiasedEstimator(60, 4) << endl;
57 cout << getBayesianMedian(60, 4) << endl;
58 cout << getBayesianMean(60, 4) << endl;
59 */
60
61 vector<size_t> buffer;
62
63 for (size_t i = 0; i != N; ++i) {
64 buffer.push_back(start + i); // serial numbers
65 }
66
67 JStats Q[] = {
68 JStats("frequentist"),
69 JStats("Bayesian median"),
70 JStats("Bayesian mean")
71 };
72
73 for (size_t counter = 0; counter != M; ++counter) {
74
75 size_t m = 0;
76
77 for (size_t i = 0; i != k; ++i) {
78
79 const size_t l = gRandom->Integer(N - i); // remaining serial numbers
80
81 if (buffer[l] > m) {
82 m = buffer[l];
83 }
84
85 swap(buffer[l], buffer[N - i - 1]); // remove observation
86 }
87
89 Q[1].put(getBayesianMedian(m, k, start));
90 Q[2].put(getBayesianMean(m, k, start));
91 }
92
93 if (debug >= debug_t) {
94
95 for (size_t i = 0; i != sizeof(Q)/sizeof(Q[0]); ++i) {
96
97 cout << setw(24) << left << Q[i].getTitle() << right << FIXED(7,3) << Q[i].getMean();
98
99 if (Q[i].getCount() > 1) {
100 cout << ' ' << FIXED(7,3) << Q[i].getSTDev();
101 }
102
103 cout << endl;
104 }
105 }
106}
#define FATAL(A)
Definition JMessage.hh:67
int debug
debug level
Definition JSirene.cc:72
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition JParser.hh:2142
const std::string & getTitle() const
Get title.
Definition JTitle.hh:55
Utility class to parse command line options.
Definition JParser.hh:1698
size_t getCount(const array_type< T > &buffer, const JCompare_t &compare)
Count number of unique values.
size_t getBayesianMean(const size_t m, const size_t k)
Get estimate of maximum number.
size_t getBayesianMedian(const size_t m, const size_t k)
Get estimate of maximum number.
size_t getMinimumVarianceUnbiasedEstimator(const size_t m, const size_t k)
Get estimate of maximum number.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary data structure for floating point format specification.
Definition JManip.hh:448
Auxiliary data structure for running average and standard deviation.
Definition JStats.hh:44
void put(const double x, const double w=1.0)
Put value.
Definition JStats.hh:119
double getMean() const
Get mean value.
Definition JStats.hh:234
double getSTDev() const
Get standard deviation.
Definition JStats.hh:263