npstat is hosted by Hepforge, IPPP Durham
NPStat  5.10.0
AbsCopulaSmootherBase.hh
Go to the documentation of this file.
1 #ifndef NPSTAT_ABSCOPULASMOOTHERBASE_HH_
2 #define NPSTAT_ABSCOPULASMOOTHERBASE_HH_
3 
4 /*!
5 // \file AbsCopulaSmootherBase.hh
6 //
7 // \brief Interface definition for classes which build discrete copulas
8 //
9 // Author: I. Volobouev
10 //
11 // September 2010
12 */
13 
14 #include <vector>
15 #include <utility>
16 #include <cassert>
17 
18 #include "geners/AbsArchive.hh"
19 
20 #include "npstat/stat/HistoND.hh"
22 
23 namespace npstat{
24  /**
25  // Interface definition for classes which build discrete copulas
26  // out of data samples (typically, by LOrPE, KDE, or some other
27  // nonparametric method). The derived classes are supposed to
28  // override the "smoothHistogram" member function.
29  */
31  {
32  public:
33  /**
34  // Parameters "tolerance" and "maxNormaCycles" define how many
35  // copula normalization cycles to perform after smoothing.
36  // These cycles make sure that the resulting distribution has
37  // uniform marginal densities. The cycles are needed because,
38  // in general, marginals are not preserved by smoothing procedures.
39  */
40  AbsCopulaSmootherBase(const unsigned* nBinsInEachDim, unsigned dim,
41  double tolerance, unsigned maxNormCycles);
42 
43  inline virtual ~AbsCopulaSmootherBase() {delete h_;}
44 
45  inline unsigned dim() const {return dim_;}
46 
47  /** Return the shape of the copula array */
48  inline ArrayShape copulaShape() const {return makeShape(shape_, dim_);}
49 
50  /** Set the archive for storing the histograms */
51  void setArchive(gs::AbsArchive* ar, const char* category = 0);
52 
53  /** Smoothing function for unweighted samples */
54  template <class Point>
56  unsigned long uniqueId,
57  std::vector<OrderedPointND<Point> >& in,
58  double* bandwidthUsed = 0);
59 
60  /** Smoothing function for weighted samples */
61  template <class Point>
63  unsigned long uniqueId,
64  const std::vector<std::pair<const Point*, double> >& in,
65  const unsigned* dimsToUse, unsigned nDimsToUse,
66  double* bandwidthUsed = 0);
67 
68  private:
72 
73  HistoND<double>& clearHisto();
74  void makeMarginalsUniform();
75  void storeHisto(unsigned long uniqueId, double bw) const;
76 
77  // Method to implement in derived classes
78  virtual void smoothHisto(HistoND<double>& histo,
79  double effectiveSampleSize,
80  double* bandwidthUsed,
81  bool isSampleWeighted) = 0;
82  HistoND<double>* h_;
83  double tol_;
84  unsigned shape_[CHAR_BIT*sizeof(unsigned long)];
85  unsigned dim_;
86  unsigned nCycles_;
87  gs::AbsArchive* ar_;
88  std::string category_;
89 
90 #ifdef SWIG
91  public:
92  template <class Point>
93  inline void smooth_2(
94  unsigned long uniqueId,
95  std::vector<OrderedPointND<Point> >& in,
96  double* bandwidthUsed)
97  {
98  assert(bandwidthUsed);
99  smooth(uniqueId, in, bandwidthUsed);
100  }
101 
102  template <class Point>
103  void weightedSmooth_2(
104  unsigned long uniqueId,
105  const std::vector<Point>& data,
106  const std::vector<double>& weights,
107  const unsigned* dimsToUse, unsigned nDimsToUse,
108  double* bandwidthUsed);
109 
110  inline HistoND<double,HistoAxis>* getHisto() const
111  {
112  if (!h_)
113  (const_cast<AbsCopulaSmootherBase*>(this))->clearHisto();
114  return new HistoND<double,HistoAxis>(*h_);
115  }
116 #endif // SWIG
117  };
118 }
119 
120 #include "npstat/stat/AbsCopulaSmootherBase.icc"
121 
122 #endif // NPSTAT_ABSCOPULASMOOTHERBASE_HH_
Arbitrary-dimensional histogram template.
Multidimensional points which can be sorted according to multiple sorting criteria.
Definition: AbsCopulaSmootherBase.hh:31
void setArchive(gs::AbsArchive *ar, const char *category=0)
AbsCopulaSmootherBase(const unsigned *nBinsInEachDim, unsigned dim, double tolerance, unsigned maxNormCycles)
const HistoND< double > & weightedSmooth(unsigned long uniqueId, const std::vector< std::pair< const Point *, double > > &in, const unsigned *dimsToUse, unsigned nDimsToUse, double *bandwidthUsed=0)
ArrayShape copulaShape() const
Definition: AbsCopulaSmootherBase.hh:48
const HistoND< double > & smooth(unsigned long uniqueId, std::vector< OrderedPointND< Point > > &in, double *bandwidthUsed=0)
Definition: OrderedPointND.hh:40
Definition: AbsArrayProjector.hh:14
ArrayShape makeShape()
std::vector< unsigned > ArrayShape
Definition: ArrayShape.hh:21