CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
List of all members | Classes | Public Types | Public Member Functions | Private Member Functions | Private Attributes
QuantileCalculator Class Reference

#include <JacknifeQuantile.h>

Classes

struct  point
 

Public Types

enum  Method { Simple, Sectioning, Jacknife }
 

Public Member Functions

std::pair< double, double > quantileAndError (double quantile, Method method)
 
 QuantileCalculator ()
 
 QuantileCalculator (const std::vector< double > &values, const std::vector< double > &weights=std::vector< double >())
 
 QuantileCalculator (const std::vector< float > &values, const std::vector< float > &weights=std::vector< float >())
 
 QuantileCalculator (const RooAbsData &data, const char *varName, int firstEntry=0, int lastEntry=-1)
 
void randomizePoints ()
 Randomize points before sectioning. More...
 
 ~QuantileCalculator ()
 

Private Member Functions

int guessPartitions (int size, double quantile)
 
template<typename T >
void import (const std::vector< T > &values, const std::vector< T > &weights)
 
void partition (int m, bool doJacknife)
 
void quantiles (double quantile, bool doJacknife)
 

Private Attributes

std::vector< pointpoints_
 
std::vector< float > quantiles_
 
std::vector< double > sumw_
 

Detailed Description

Definition at line 5 of file JacknifeQuantile.h.

Member Enumeration Documentation

Enumerator
Simple 
Sectioning 
Jacknife 

Definition at line 7 of file JacknifeQuantile.h.

Constructor & Destructor Documentation

QuantileCalculator::QuantileCalculator ( )

Definition at line 10 of file JacknifeQuantile.cc.

11 {
12 }
QuantileCalculator::~QuantileCalculator ( )

Definition at line 14 of file JacknifeQuantile.cc.

15 {
16 }
QuantileCalculator::QuantileCalculator ( const std::vector< double > &  values,
const std::vector< double > &  weights = std::vector<double>() 
)
QuantileCalculator::QuantileCalculator ( const std::vector< float > &  values,
const std::vector< float > &  weights = std::vector<float>() 
)
QuantileCalculator::QuantileCalculator ( const RooAbsData &  data,
const char *  varName,
int  firstEntry = 0,
int  lastEntry = -1 
)

Definition at line 28 of file JacknifeQuantile.cc.

References i, j, points_, runtimedef::set(), sumw_, and x.

29 {
30  if (lastEntry == -1) lastEntry = data.numEntries();
31  const RooArgSet *set = data.get();
32  const RooRealVar *x = dynamic_cast<const RooRealVar *>(set->find(varName));
33  if (x == 0) {
34  set->Print("V");
35  throw std::logic_error("Parameter of interest not in the idataset");
36  }
37  sumw_.resize(1, 0.0);
38  if (firstEntry < lastEntry) {
39  points_.resize(lastEntry - firstEntry);
40  for (int i = firstEntry, j = 0; i < lastEntry; ++i, ++j) {
41  data.get(i);
42  points_[j].x = x->getVal();
43  points_[j].w = data.weight();
44  sumw_[0] += points_[j].w;
45  }
46  }
47 }
int i
Definition: DBlmapReader.cc:9
int j
Definition: DBlmapReader.cc:9
std::vector< double > sumw_
std::vector< point > points_
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
Definition: DDAxes.h:10
void set(const std::string &name, int value)
set the flag, with a run-time name

Member Function Documentation

int QuantileCalculator::guessPartitions ( int  size,
double  quantile 
)
private

Definition at line 81 of file JacknifeQuantile.cc.

References gather_cfg::cout, min, and n.

Referenced by quantileAndError().

82 {
83  // number of events on the small side of the quantile for m=1
84  double smallnum = size * std::min(quantile, 1-quantile);
85  // now the naive idea is that err(q) ~ 1/sqrt(smallnum/m), while the error from averaging does as ~1/sqrt(m)
86  // so you want m ~ sqrt(smallnum)
87  int n = 5; //std::min(std::max(3., sqrt(smallnum)), 5.);
88  std::cout << " ..... will split the " << size << " events in " << n << " subsets, smallnum is " << smallnum << std::endl;
89  return n;
90 }
#define min(a, b)
Definition: mlp_lapack.h:161
tuple cout
Definition: gather_cfg.py:121
tuple size
Write out results.
template<typename T >
void QuantileCalculator::import ( const std::vector< T > &  values,
const std::vector< T > &  weights 
)
private

Definition at line 93 of file JacknifeQuantile.cc.

References i, n, points_, and sumw_.

94 {
95  int n = values.size();
96  points_.resize(values.size());
97  sumw_.resize(1); sumw_[0] = 0.0;
98  for (int i = 0; i < n; ++i) {
99  points_[i].x = values[i];
100  points_[i].w = weights.empty() ? 1 : weights[i];
101  points_[i].set = 0;
102  sumw_[0] += points_[i].w;
103  }
104 }
int i
Definition: DBlmapReader.cc:9
std::vector< double > sumw_
std::vector< point > points_
void QuantileCalculator::partition ( int  m,
bool  doJacknife 
)
private

Definition at line 107 of file JacknifeQuantile.cc.

References alpha, lumiContext::fill, i, j, m, n, points_, and sumw_.

Referenced by quantileAndError().

108 {
109  int n = points_.size();
110  sumw_.resize(m+1);
111  std::fill(sumw_.begin(), sumw_.end(), 0.0);
112  double alpha = double(m)/n;
113  for (int i = 0, j = 0; i < n; ++i) {
114  j = floor(i*alpha);
115  points_[i].set = j;
116  sumw_[j] += points_[i].w;
117  }
118  if (doJacknife) {
119  // at this point sumw[j] has the weights of j...
120  // now I have to get the weights of everyhing else
121  // start with all weights
122  for (int j = 0; j < m; ++j) sumw_[m] += sumw_[j];
123  // and then subtract
124  for (int j = 0; j < m; ++j) sumw_[j] = (sumw_[m] - sumw_[j]);
125  }
126 }
int i
Definition: DBlmapReader.cc:9
float alpha
Definition: AMPTWrapper.h:95
string fill
Definition: lumiContext.py:319
int j
Definition: DBlmapReader.cc:9
std::vector< double > sumw_
std::vector< point > points_
std::pair< double, double > QuantileCalculator::quantileAndError ( double  quantile,
Method  method 
)

Definition at line 54 of file JacknifeQuantile.cc.

References guessPartitions(), i, Jacknife, m, partition(), points_, quantiles(), quantiles_, plotscripts::rms(), Sectioning, Simple, python.multivaluedict::sort(), and mathSSE::sqrt().

Referenced by MarkovChainMC::limitAndError(), and MarkovChainMC::limitFromChain().

55 {
56  if (method == Simple) {
57  std::sort(points_.begin(), points_.end());
58  quantiles(quantile, false);
59  return std::pair<double,double>(quantiles_[0], 0);
60  } else if (method == Sectioning || method == Jacknife) {
61  int m = guessPartitions(points_.size(), quantile);
62  partition(m, (method == Jacknife));
63  std::sort(points_.begin(), points_.end());
64  quantiles(quantile, (method == Jacknife));
65  double avg = 0;
66  for (int i = 0; i < m; ++i) {
67  avg += quantiles_[i];
68  }
69  avg /= m;
70  double rms = 0;
71  for (int i = 0; i < m; ++i) {
72  rms += (quantiles_[i] - avg)*(quantiles_[i] - avg);
73  }
74  rms = sqrt(rms/(m*(m-1)));
75  double onesigma = ROOT::Math::tdistribution_quantile_c(0.16, m-1);
76  return std::pair<double,double>(avg, rms * onesigma);
77  }
78  return std::pair<double,double>(0,-1);
79 }
int i
Definition: DBlmapReader.cc:9
std::vector< float > quantiles_
void partition(int m, bool doJacknife)
T sqrt(T t)
Definition: SSEVec.h:46
void quantiles(double quantile, bool doJacknife)
std::vector< point > points_
int guessPartitions(int size, double quantile)
void QuantileCalculator::quantiles ( double  quantile,
bool  doJacknife 
)
private

Definition at line 128 of file JacknifeQuantile.cc.

References i, j, m, n, points_, quantiles_, runtimedef::set(), sumw_, dtDQMClient_cfg::threshold, and w().

Referenced by quantileAndError().

129 {
130  int m = sumw_.size()-1;
131  int n = points_.size();
132  quantiles_.resize(m+1);
133  for (int j = 0; j <= m; ++j) {
134  double runningSum = 0;
135  double threshold = quantile * sumw_[j];
136  int ilow = 0, ihigh = n-1;
137  for (int i = 0; i < n; ++i) {
138  if ((points_[i].set == j) == doJacknife) continue; // if jacknife, cut away just one piece, otherwise cut away everthing else
139  //std::cout << "\t\t\t" << points_[i].x << std::endl;;
140  if (runningSum + points_[i].w <= threshold) {
141  runningSum += points_[i].w;
142  ilow = i;
143  } else {
144  ihigh = i;
145  break;
146  }
147  }
148  if (runningSum == threshold) { // possible if all unit weights
149  quantiles_[j] = points_[ilow].x;
150  } else {
151  quantiles_[j] = 0.5*(points_[ilow].x + points_[ihigh].x);
152  }
153  }
154  for (int j = 0; j <= m; ++j) {
155  //printf(" ... quantile of section %d: %6.3f\n", j, quantiles_[j]);
156  }
157  if (doJacknife) {
158  // now compute the pseudo-values alpha_[j] = m * quantile[m] - (m-1) * quantile[j]
159  for (int j = 0; j < m; ++j) {
160  quantiles_[j] = m * quantiles_[m] - (m-1) * quantiles_[j];
161  printf(" ... jacknife quantile of section %d: %6.3f\n", j, quantiles_[j]);
162  }
163  }
164 }
int i
Definition: DBlmapReader.cc:9
std::vector< float > quantiles_
int j
Definition: DBlmapReader.cc:9
std::vector< double > sumw_
std::vector< point > points_
T w() const
void set(const std::string &name, int value)
set the flag, with a run-time name
void QuantileCalculator::randomizePoints ( )

Randomize points before sectioning.

Definition at line 49 of file JacknifeQuantile.cc.

References points_.

50 {
51  std::random_shuffle(points_.begin(), points_.end());
52 }
std::vector< point > points_

Member Data Documentation

std::vector<point> QuantileCalculator::points_
private
std::vector<float> QuantileCalculator::quantiles_
private

Definition at line 25 of file JacknifeQuantile.h.

Referenced by quantileAndError(), and quantiles().

std::vector<double> QuantileCalculator::sumw_
private

Definition at line 24 of file JacknifeQuantile.h.

Referenced by import(), partition(), QuantileCalculator(), and quantiles().