Jpp test-rotations-new
the software that should make you happy
Loading...
Searching...
No Matches
JQuantile.cc File Reference

Example program to test JTOOLS::JQuantile calculation. More...

#include <string>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <vector>
#include <random>
#include "TRandom.h"
#include "JTools/JQuantile.hh"
#include "Jeep/JParser.hh"
#include "Jeep/JMessage.hh"

Go to the source code of this file.

Functions

int main (int argc, char **argv)
 

Detailed Description

Example program to test JTOOLS::JQuantile calculation.

Author
mdejong

Definition in file JQuantile.cc.

Function Documentation

◆ main()

int main ( int argc,
char ** argv )

Definition at line 23 of file JQuantile.cc.

24{
25 using namespace std;
26 using namespace JPP;
27
28 int numberOfEvents;
29 double x;
30 double sigma;
31 ULong_t seed;
32 double precision;
33 int debug;
34
35 try {
36
37 JParser<> zap("Example program to test quantile calculation.");
38
39 zap['n'] = make_field(numberOfEvents);
40 zap['x'] = make_field(x) = 0.0;
41 zap['s'] = make_field(sigma) = 1.0;
42 zap['S'] = make_field(seed) = 0;
43 zap['e'] = make_field(precision) = 1.0e-2;
44 zap['d'] = make_field(debug) = 3;
45
46 zap(argc, argv);
47 }
48 catch(const exception &error) {
49 FATAL(error.what() << endl);
50 }
51
52 if (numberOfEvents < 2) {
53 FATAL("Fatal error: number of events " << numberOfEvents << endl);
54 }
55
56 gRandom->SetSeed(seed);
57
58 {
59 vector<double> buffer;
60
61 JQuantile Q;
62
63 for (int i = 0; i != numberOfEvents; ++i) {
64
65 double x = gRandom->Uniform(-1.0e6, +1.0e6);
66
67 buffer.push_back(x);
68
69 Q.put(x);
70 }
71
72 random_device rd;
73 mt19937 g(rd());
74
75 for (int i = 0; i != 100; ++i) {
76
77 std::shuffle(buffer.begin(), buffer.end(), g);
78
79 DEBUG("buffer[0] " << FIXED(15,6) << buffer[0] << endl);
80
81 JQuantile R;
82
83 for (const double x : buffer) {
84 R.put(x);
85 }
86
87 ASSERT( Q.getCount() == R.getCount(), "Test shuffle counts.");
88 ASSERT(fabs(Q.getMean() - R.getMean()) <= precision, "Test shuffle means.");
89 ASSERT(fabs(Q.getSTDev() - R.getSTDev()) <= precision, "Test shuffle sigmas.");
90 }
91 }
92 {
93 JQuantile Q;
94
95 for (int i = 0; i != numberOfEvents; ++i) {
96 Q.put(gRandom->Gaus(x, sigma));
97 }
98
99 NOTICE("quantity " << CENTER(10) << "calculated" << " | " << CENTER(10) << "true" << endl);
100 NOTICE("mean " << FIXED(10,3) << Q.getMean() << " | " << FIXED(10,3) << x << endl);
101 NOTICE("RMS " << FIXED(10,3) << Q.getSTDev() << " | " << FIXED(10,3) << sigma << endl);
102
103 ASSERT(numberOfEvents > 0, "Test number of events.");
104 ASSERT(fabs(Q.getMean() - x) <= precision, "Test mean.");
105 ASSERT(fabs(Q.getSTDev() - sigma) <= precision, "Test sigma.");
106 }
107 {
108 JQuantile Q("", true);
109
110 for (int i = 0; i != numberOfEvents; ++i) {
111 Q.put(gRandom->Uniform(0.0, 1.0));
112 }
113
114 for (const double x : { 0.1, 0.5, 0.9}) {
115
116 NOTICE("forward quantile " << FIXED(6,3) << x << ' ' << FIXED(6,3) << Q.getQuantile(x, JQuantile::forward_t) << endl);
117 NOTICE("symmetric quantile " << FIXED(6,3) << x << ' ' << FIXED(6,3) << Q.getQuantile(x, JQuantile::symmetric_t) << endl);
118 NOTICE("backward quantile " << FIXED(6,3) << x << ' ' << FIXED(6,3) << Q.getQuantile(x, JQuantile::backward_t) << endl);
119
120 ASSERT(fabs(Q.getQuantile(x, JQuantile::forward_t) - ( x )) <= precision, "Test forward quantile ");
121 ASSERT(fabs(Q.getQuantile(x, JQuantile::symmetric_t) - ( x )) <= precision, "Test symmetric quantile ");
122 ASSERT(fabs(Q.getQuantile(x, JQuantile::backward_t) - (1.0 - x)) <= precision, "Test backward quantile ");
123 }
124 }
125
126 return 0;
127}
#define DEBUG(A)
Message macros.
Definition JMessage.hh:62
#define ASSERT(A,...)
Assert macro.
Definition JMessage.hh:90
#define NOTICE(A)
Definition JMessage.hh:64
#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
Utility class to parse command line options.
Definition JParser.hh:1698
const double sigma[]
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary data structure for alignment of data.
Definition JManip.hh:368
Auxiliary data structure for floating point format specification.
Definition JManip.hh:448
Auxiliary data structure for running average, standard deviation and quantiles.
Definition JQuantile.hh:46
double getQuantile(const double Q, const Quantile_t option=forward_t) const
Get quantile.
Definition JQuantile.hh:349
double getSTDev() const
Get standard deviation.
Definition JQuantile.hh:281
void put(const double x, const double w=1.0)
Put value.
Definition JQuantile.hh:133
long long int getCount() const
Get total count.
Definition JQuantile.hh:186
double getMean() const
Get mean value.
Definition JQuantile.hh:252