npstat is hosted by Hepforge, IPPP Durham
NPStat  5.10.0
AbsMarginalSmootherBase.hh
Go to the documentation of this file.
1 #ifndef NPSTAT_ABSMARGINALSMOOTHERBASE_HH_
2 #define NPSTAT_ABSMARGINALSMOOTHERBASE_HH_
3 
4 /*!
5 // \file AbsMarginalSmootherBase.hh
6 //
7 // \brief Interface definition for 1-d nonparametric density estimation
8 //
9 // Author: I. Volobouev
10 //
11 // June 2015
12 */
13 
14 #include <vector>
15 #include <utility>
16 #include <limits>
17 
18 #include "geners/AbsArchive.hh"
19 #include "npstat/stat/HistoND.hh"
20 
21 #ifdef SWIG
22 #include <stdexcept>
23 #include "npstat/wrap/arrayNDToNumpy.hh"
24 #include "npstat/wrap/histoBinCoords.hh"
25 #endif // SWIG
26 
27 namespace npstat {
29  {
30  public:
31  /** Constructor takes binning parameters for the internal histogram */
32  AbsMarginalSmootherBase(unsigned nbins, double xmin, double xmax,
33  const char* axisLabel = 0);
34 
35  inline virtual ~AbsMarginalSmootherBase() {delete histo_;}
36 
37  void setAxisLabel(const char* axisLabel);
38 
39  //@{
40  /** Simple inspector of the object properties */
41  inline unsigned nBins() const {return nbins_;}
42  inline double xMin() const {return xmin_;}
43  inline double xMax() const {return xmax_;}
44  inline double binWidth() const {return (xmax_ - xmin_)/nbins_;}
45  inline const std::string& getAxisLabel() const {return axlabel_;}
46  inline gs::AbsArchive* getArchive() const {return ar_;}
47  inline const std::string& getArchiveCategory() const
48  {return category_;}
49  //@}
50 
51  /**
52  // The bandwidth used by the most recent "smooth" or
53  // "weightedSmooth" call
54  */
55  inline double lastBandwidth() const {return lastBw_;}
56 
57  /** Set the archive for storing original and smoothed histograms */
58  void setArchive(gs::AbsArchive* ar, const char* category = 0);
59 
60  /** Stop storing histograms */
61  inline void unsetArchive() {setArchive(0);}
62 
63  /**
64  // Smoothing function for unweighted samples. The region
65  // used for smoothing will be defined by the overlap of
66  // the intervals [minValue, maxValue) and [xMin(), xMax()).
67  */
68  template <typename Numeric>
70  const std::vector<Numeric>& inputPoints,
71  double minValue = -std::numeric_limits<double>::max(),
72  double maxValue = std::numeric_limits<double>::max(),
73  double* bandwidthUsed = 0);
74 
75  /**
76  // Smoothing function for unweighted samples. The region
77  // used for smoothing will be defined by the overlap of
78  // the intervals [minValue, maxValue) and [xMin(), xMax()).
79  // Parameters "uniqueId" and "dimNumber" are used for
80  // identification purposes only -- they will be used to
81  // create the item name in the archive if the archive is set.
82  */
83  template <typename Numeric>
85  unsigned long uniqueId, unsigned dimNumber,
86  const std::vector<Numeric>& inputPoints,
87  double minValue = -std::numeric_limits<double>::max(),
88  double maxValue = std::numeric_limits<double>::max(),
89  double* bandwidthUsed = 0);
90 
91  /**
92  // Smoothing function for weighted samples. The region
93  // used for smoothing will be defined by the overlap of
94  // the intervals [minValue, maxValue) and [xMin(), xMax()).
95  */
96  template <typename Numeric>
98  const std::vector<std::pair<Numeric,double> >& inputPoints,
99  double minValue = -std::numeric_limits<double>::max(),
100  double maxValue = std::numeric_limits<double>::max(),
101  double* bandwidthUsed = 0);
102 
103  /**
104  // Smoothing function for weighted samples. The region
105  // used for smoothing will be defined by the overlap of
106  // the intervals [minValue, maxValue) and [xMin(), xMax()).
107  // Parameters "uniqueId" and "multivariatePointDimNumber"
108  // are used for identification purposes only -- they will be used
109  // to create the item name in the archive if the archive is set.
110  */
111  template <typename Numeric>
113  unsigned long uniqueId, unsigned multivariatePointDimNumber,
114  const std::vector<std::pair<Numeric,double> >& inputPoints,
115  double minValue = -std::numeric_limits<double>::max(),
116  double maxValue = std::numeric_limits<double>::max(),
117  double* bandwidthUsed = 0);
118 
119  private:
123 
124  HistoND<double>& clearHisto(double xmin, double xmax);
125  void storeHisto(unsigned long uniqueId, unsigned dim, double bw) const;
126 
127  // Method to implement in derived classes
128  virtual void smoothHisto(HistoND<double>& histo,
129  double effectiveSampleSize,
130  double* bandwidthUsed,
131  bool isSampleWeighted) = 0;
132  HistoND<double>* histo_;
133  double xmin_;
134  double xmax_;
135  double lastBw_;
136  gs::AbsArchive* ar_;
137  std::string category_;
138  std::string axlabel_;
139  unsigned nbins_;
140 
141 #ifdef SWIG
142  inline PyObject* buildResultFromHisto(const HistoND<double>& h)
143  {
144  PyObject* x = 0;
145  PyObject* array = 0;
146  try
147  {
148  x = histoBinCenters(h.axis(0));
149  array = arrayNDToNumpy(h.binContents());
150  }
151  catch (const std::exception& e)
152  {
153  Py_XDECREF(x);
154  Py_XDECREF(array);
155  throw e;
156  }
157  return Py_BuildValue("(OOd)", x, array, lastBw_);
158  }
159 
160  public:
161  /** Simplified smooth method without archiving */
162  template <typename Vec>
163  inline PyObject* smooth2(const Vec& inputPoints)
164  {
165  const HistoND<double>& h = smooth(inputPoints);
166  return buildResultFromHisto(h);
167  }
168 
169  /** Simplified smooth method with archiving */
170  template <typename Vec>
171  inline PyObject* smoothArch(
172  const Vec& inputPoints, unsigned long uniqueId, unsigned dimNumber)
173  {
174  if (!ar_) throw std::runtime_error(
175  "In npstat::AbsMarginalSmootherBase::smoothArch: "
176  "archive is not set");
177  const HistoND<double>& h = smooth(uniqueId, dimNumber, inputPoints);
178  return buildResultFromHisto(h);
179  }
180 
181  /** Simplified smooth method for weigted samples without archiving */
182  template <typename Vec>
183  inline PyObject* weightedSmooth2(const Vec& inputPoints)
184  {
185  const HistoND<double>& h = weightedSmooth(inputPoints);
186  return buildResultFromHisto(h);
187  }
188 
189  /** Simplified smooth method for weigted samples with archiving */
190  template <typename Vec>
191  inline PyObject* weightedSmoothArch(
192  const Vec& inputPoints, unsigned long uniqueId, unsigned dimNumber)
193  {
194  if (!ar_) throw std::runtime_error(
195  "In npstat::AbsMarginalSmootherBase::weightedSmoothArch: "
196  "archive is not set");
197  const HistoND<double>& h = weightedSmooth(uniqueId, dimNumber, inputPoints);
198  return buildResultFromHisto(h);
199  }
200 #endif // SWIG
201  };
202 }
203 
204 #include "npstat/stat/AbsMarginalSmootherBase.icc"
205 
206 #endif // NPSTAT_ABSMARGINALSMOOTHERBASE_HH_
Arbitrary-dimensional histogram template.
Definition: AbsMarginalSmootherBase.hh:29
const HistoND< double > & smooth(const std::vector< Numeric > &inputPoints, double minValue=-std::numeric_limits< double >::max(), double maxValue=std::numeric_limits< double >::max(), double *bandwidthUsed=0)
unsigned nBins() const
Definition: AbsMarginalSmootherBase.hh:41
double lastBandwidth() const
Definition: AbsMarginalSmootherBase.hh:55
const HistoND< double > & smooth(unsigned long uniqueId, unsigned dimNumber, const std::vector< Numeric > &inputPoints, double minValue=-std::numeric_limits< double >::max(), double maxValue=std::numeric_limits< double >::max(), double *bandwidthUsed=0)
void unsetArchive()
Definition: AbsMarginalSmootherBase.hh:61
AbsMarginalSmootherBase(unsigned nbins, double xmin, double xmax, const char *axisLabel=0)
const HistoND< double > & weightedSmooth(unsigned long uniqueId, unsigned multivariatePointDimNumber, const std::vector< std::pair< Numeric, double > > &inputPoints, double minValue=-std::numeric_limits< double >::max(), double maxValue=std::numeric_limits< double >::max(), double *bandwidthUsed=0)
void setArchive(gs::AbsArchive *ar, const char *category=0)
const HistoND< double > & weightedSmooth(const std::vector< std::pair< Numeric, double > > &inputPoints, double minValue=-std::numeric_limits< double >::max(), double maxValue=std::numeric_limits< double >::max(), double *bandwidthUsed=0)
const Axis & axis(const unsigned i) const
Definition: HistoND.hh:222
const ArrayND< Numeric > & binContents() const
Definition: HistoND.hh:197
Definition: AbsArrayProjector.hh:14