npstat is hosted by Hepforge, IPPP Durham
NPStat  5.10.0
SmoothedEMSparseUnfoldND.hh
Go to the documentation of this file.
1 #ifndef EMSUNFOLD_SMOOTHEDEMSPARSEUNFOLDND_HH_
2 #define EMSUNFOLD_SMOOTHEDEMSPARSEUNFOLDND_HH_
3 
4 /*!
5 // \file SmoothedEMSparseUnfoldND.hh
6 //
7 // \brief Multivariate expectation-maximization unfolding with smoothing
8 // using sparse matrices
9 //
10 // Author: I. Volobouev
11 //
12 // July 2014
13 */
14 
16 
17 namespace emsunfold {
18  template<class Matrix>
20  {
21  public:
23  typedef typename Base::response_matrix_type response_matrix_type;
24  typedef typename Base::input_covariance_type input_covariance_type;
25  typedef typename Base::output_covariance_type output_covariance_type;
26  typedef typename Base::filter_type filter_type;
27 
28  /**
29  // The constructor arguments are:
30  //
31  // responseMatrix -- Naturally, the problem response matrix.
32  //
33  // filter -- The filter to use for smoothing the
34  // unfolded values. This object will not
35  // make a copy of the filter. It is
36  // a responsibility of the caller to ensure
37  // that the argument filter exists while
38  // this object is in use.
39  //
40  // observedShape -- Expected shape of the observed data.
41  // The array of observations provided in
42  // all subsequent calls of the "unfold"
43  // method must have this shape.
44  //
45  // useConvolutions -- If "true", the code will call the
46  // "convolve" method of the filter rather
47  // than its "filter" method.
48  //
49  // smoothLastIter -- If "false", smoothing will not be
50  // applied after the last iteration.
51  // Setting this parameter to "false" is
52  // not recommended for production results
53  // because it is unclear how to compare
54  // such results with models.
55  //
56  // convergenceEpsilon -- Convergence criterion parameter for
57  // various iterations.
58  //
59  // maxIterations -- Maximum number of iterations allowed
60  // (both for the expectation-maximization
61  // iterations and for the code estimating
62  // the error propagation matrix).
63  */
64  SmoothedEMSparseUnfoldND(const response_matrix_type& responseMatrix,
65  const filter_type& filter,
66  const npstat::ArrayShape& observedShape,
67  bool useConvolutions,
68  bool smoothLastIter = true,
69  double convergenceEpsilon = 1.0e-10,
70  unsigned maxIterations = 100000U);
71 
72  inline virtual ~SmoothedEMSparseUnfoldND() {}
73 
74  /** Change maximum number of allowed iterations */
75  inline void setMaxIterations(const unsigned u) {maxIterations_ = u;}
76 
77  /**
78  // This method is included for compatibility with the
79  // npstat::SmoothedEMUnfoldND class. The call will be ignored.
80  */
81  inline void useMultinomialCovariance(bool) {}
82 
83  /** Switch between smoothing/not smoothing the last iteration */
84  inline void smoothLastIteration(const bool b) {smoothLast_ = b;}
85 
86  /** Change the convergence criterion */
87  void setConvergenceEpsilon(double eps);
88 
89  //@{
90  /** Simple inspector of object properties */
91  inline double convergenceEpsilon() const {return convergenceEpsilon_;}
92  inline unsigned maxIterations() const {return maxIterations_;}
93  inline bool usingMultinomialCovariance() const {return false;}
94  inline bool smoothingLastIteration() const {return smoothLast_;}
95  //@}
96 
97  /**
98  // Returns the last number of iterations used to calculate the unfolded
99  // results. This number will be filled after each "unfold" call.
100  */
101  inline unsigned lastNIterations() const {return lastIterations_;}
102 
103  /**
104  // The last number of iterations used to calculate the error
105  // propagation matrix
106  */
107  inline unsigned lastEPIterations() const {return lastEPIterations_;}
108 
109  /** The normalization factor applied during the last smoothing step */
110  inline double lastSmoothingNormfactor() const {return lastNormfactor_;}
111 
112  /** The main unfolding method */
113  virtual bool unfold(
114  const npstat::ArrayND<double>& observed,
115  const input_covariance_type* observationCovarianceMatrix,
116  npstat::ArrayND<double>* unfolded,
117  output_covariance_type* unfoldedCovarianceMatrix);
118 
119  protected:
120  /** Single expectation-maximization (a.k.a. D'Agostini) iteration */
121  void update(const npstat::ArrayND<double>& observed,
122  const npstat::ArrayND<double>* prev,
124  bool performSmoothing) const;
125 
126  private:
127  // Error propagation matrix for the expectation-maximization iterations
128  Matrix errorPropagationMatrix(
129  const double* observed, unsigned lenObserved,
130  const double* unfolded, unsigned lenUnfolded,
131  const double* yHat, double norm, bool smoothLast, unsigned maxiter,
132  double convergenceEps, unsigned* itersMade, bool* converged) const;
133 
134  double convergenceEpsilon_;
135  mutable double lastNormfactor_;
136  unsigned maxIterations_;
137  unsigned lastIterations_;
138  unsigned lastEPIterations_;
139  bool smoothLast_;
140 
141  // Memory buffers
142  npstat::ArrayND<double> v1_, v2_;
143  mutable npstat::ArrayND<double> yhatBuf_, unfoldedBuf_;
144  };
145 }
146 
147 #include "npstat/emsunfold/SmoothedEMSparseUnfoldND.icc"
148 
149 #endif // EMSUNFOLD_SMOOTHEDEMSPARSEUNFOLDND_HH_
Interface definition for multivariate unfolding algorithms that use sparse matrices.
Definition: AbsSparseUnfoldND.hh:22
virtual void useConvolutions(const bool b)
Definition: AbsSparseUnfoldND.hh:66
Definition: AbsSparseUnfoldingFilterND.hh:27
Definition: SmoothedEMSparseUnfoldND.hh:20
double convergenceEpsilon() const
Definition: SmoothedEMSparseUnfoldND.hh:91
void setMaxIterations(const unsigned u)
Definition: SmoothedEMSparseUnfoldND.hh:75
void update(const npstat::ArrayND< double > &observed, const npstat::ArrayND< double > *prev, npstat::ArrayND< double > *next, bool performSmoothing) const
SmoothedEMSparseUnfoldND(const response_matrix_type &responseMatrix, const filter_type &filter, const npstat::ArrayShape &observedShape, bool useConvolutions, bool smoothLastIter=true, double convergenceEpsilon=1.0e-10, unsigned maxIterations=100000U)
unsigned lastEPIterations() const
Definition: SmoothedEMSparseUnfoldND.hh:107
unsigned lastNIterations() const
Definition: SmoothedEMSparseUnfoldND.hh:101
void useMultinomialCovariance(bool)
Definition: SmoothedEMSparseUnfoldND.hh:81
virtual bool unfold(const npstat::ArrayND< double > &observed, const input_covariance_type *observationCovarianceMatrix, npstat::ArrayND< double > *unfolded, output_covariance_type *unfoldedCovarianceMatrix)
void smoothLastIteration(const bool b)
Definition: SmoothedEMSparseUnfoldND.hh:84
double lastSmoothingNormfactor() const
Definition: SmoothedEMSparseUnfoldND.hh:110
Definition: AbsSparseUnfoldingFilterND.hh:25
std::vector< unsigned > ArrayShape
Definition: ArrayShape.hh:21