File: BinnedStatistics.h

package info (click to toggle)
herwig%2B%2B 2.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 27,128 kB
  • ctags: 24,739
  • sloc: cpp: 188,949; fortran: 23,193; sh: 11,365; python: 5,069; ansic: 3,539; makefile: 1,865; perl: 2
file content (239 lines) | stat: -rw-r--r-- 5,801 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// -*- C++ -*-
//
// GeneralStatictis.h is a part of Herwig++ - A multi-purpose Monte Carlo event generator
// Copyright (C) 2002-2012 The Herwig Collaboration
//
// Herwig++ is licenced under version 2 of the GPL, see COPYING for details.
// Please respect the MCnet academic guidelines, see GUIDELINES for details.
//
#ifndef Herwig_BinnedStatistics_H
#define Herwig_BinnedStatistics_H
//
// This is the declaration of the BinnedStatistics class.
//

#include "GeneralStatistics.h"
#include "ThePEG/Repository/UseRandom.h"

#include <boost/utility.hpp>
using boost::next;
using boost::prior;

namespace Herwig {

using namespace ThePEG;

/**
 * \ingroup Matchbox
 * \author Simon Platzer
 *
 * \brief Aka histogram, yet not intented for analyses.
 *
 */
class BinnedStatistics {

public:

  /** @name Standard constructors and destructors. */
  //@{
  /**
   * The default constructor.
   */
  BinnedStatistics() 
    : lastPoint(0.), lastStatistics(0), theWeightThreshold(0.001) {}

  /**
   * The standard constructor.
   */
  BinnedStatistics(unsigned int bins, double threshold = 0.001) 
    : lastPoint(0.), lastStatistics(0) {
    initialize(bins);
    theWeightThreshold = threshold;
  }

  /**
   * The destructor.
   */
  virtual ~BinnedStatistics();
  //@}

public:

  /**
   * Sample a point and return its weight to be divided out as a bias.
   */
  double sample(double& point) {
    const pair<double,double>& range =
      selectorMap.upper_bound(UseRandom::rnd())->second;
    lastPoint = UseRandom::rnd(range.first,range.second);
    point = lastPoint;
    lastStatistics = &(statisticsMap.upper_bound(lastPoint)->second);
    double weight = weightMap.upper_bound(lastPoint)->second;
    return 1./weight;
  }

  /**
   * Get a bin corresponding to a given point.
   */
  void bin(double point) {
    lastPoint = point;
    lastStatistics = &(statisticsMap.upper_bound(lastPoint)->second);
  }

  /**
   * Select the last sampled point with a given weight.
   */
  void select(double w) {
    lastStatistics->select(w);
  }

  /**
   * Accept the last sampled point.
   */
  void accept() {
    lastStatistics->accept();
  }

  /**
   * Reject the last sampled point.
   */
  void reject() {
    lastStatistics->reject();
  }

  /**
   * Initialize with flat sampling over the complete range,
   * using the given number of bins to accumulate statistics.
   */
  void initialize(unsigned int bins);

  /**
   * Return the bins.
   */
  const map<double,GeneralStatistics>& statistics() const {
    return statisticsMap;
  }

  /**
   * Update the sampling bins to reflect the accumulated statistics and
   * binning used.
   */
  template<class Adaptor>
  void update(const Adaptor& ap) {
    double avgweight = 0.;
    size_t bins = 0;
    for ( map<double,GeneralStatistics>::const_iterator s =
	    statisticsMap.begin(); s != statisticsMap.end(); ++s ) {
      avgweight += ap.importanceMeasure(s->second);
      ++bins;
    }
    avgweight /= bins;
    weightMap.clear();
    double norm = 0.;
    for ( map<double,GeneralStatistics>::const_iterator s =
	    statisticsMap.begin(); s != statisticsMap.end(); ++s ) {
      double weight = ap.importanceMeasure(s->second);
      if ( weight < theWeightThreshold*avgweight )
	weight = theWeightThreshold*avgweight;
      weightMap[s->first] = weight;
      norm += 
	weight *
	(s != statisticsMap.begin() ? (s->first - prior(s)->first) : s->first);
    }
    selectorMap.clear();
    double current = 0.;
    for ( map<double,double>::iterator bw = weightMap.begin(); 
	  bw != weightMap.end(); ++bw ) {
      bw->second /= norm;
      pair<double,double> range = 
	make_pair(bw != weightMap.begin() ? prior(bw)->first : 0.,
		  bw->first);
      current += bw->second*(range.second-range.first);
      selectorMap[current] = range;
    }
  }

  /**
   * Half those bins, which meet the given predicate
   * and update the statistics.
   */
  template<class Adaptor>
  void adapt(const Adaptor& ap) {
    update(ap);
    map<double,GeneralStatistics> newBins;
    for ( map<double,GeneralStatistics>::const_iterator b
	    = statisticsMap.begin(); b != statisticsMap.end(); ++b ) {
      newBins[b->first] = GeneralStatistics();
      if ( ap.adapt(b->second) ) {
	double bound =
	  b != statisticsMap.begin() ? (prior(b)->first + b->first)/2. : b->first/2.;
	newBins[bound] = GeneralStatistics();
      }
    }
    statisticsMap = newBins;
  }

public:

  /** @name Functions used by the persistent I/O system. */
  //@{
  /**
   * Function used to write out object persistently.
   * @param os the persistent output stream written to.
   */
  void put(PersistentOStream & os) const;

  /**
   * Function used to read in object persistently.
   * @param is the persistent input stream read from.
   * @param version the version number of the object when written.
   */
  void get(PersistentIStream & is);
  //@}

private:

  /**
   * Map upper bin boundaries to statistics contained.
   * The lower bin boundary is always 0.
   */
  map<double,GeneralStatistics> statisticsMap;

  /**
   * Map upper bin boundaries to bin weights currently used.
   */
  map<double,double> weightMap;

  /**
   * Selector map to sample a point
   */
  map<double,pair<double,double> > selectorMap;

  /**
   * The last sampled point.
   */
  double lastPoint;

  /**
   * The statistics relevant to the last sampled point.
   */
  GeneralStatistics* lastStatistics;

  /**
   * The weight threshold which governs the minimum bin weight.
   */
  double theWeightThreshold;

};

inline PersistentOStream& operator<<(PersistentOStream& os, const BinnedStatistics& s) {
  s.put(os); return os;
}

inline PersistentIStream& operator>>(PersistentIStream& is, BinnedStatistics& s) {
  s.get(is); return is;
}

}

#endif /* Herwig_BinnedStatistics_H */