npstat is hosted by Hepforge, IPPP Durham
NPStat  5.10.0
SmoothedEMUnfold1D.hh
Go to the documentation of this file.
1 #ifndef NPSTAT_SMOOTHEDEMUNFOLD1D_HH_
2 #define NPSTAT_SMOOTHEDEMUNFOLD1D_HH_
3 
4 /*!
5 // \file SmoothedEMUnfold1D.hh
6 //
7 // \brief Expectation-maximization (a.k.a. D'Agostini) unfolding with smoothing
8 //
9 // Author: I. Volobouev
10 //
11 // May 2014
12 */
13 
15 
16 namespace npstat {
18  {
19  public:
20  typedef AbsUnfold1D Base;
21 
22  /**
23  // The constructor arguments are:
24  //
25  // responseMatrix -- Naturally, the problem response matrix.
26  //
27  // filter -- The filter to use for smoothing the
28  // unfolded values. This object will not
29  // make a copy of the filter. It is the
30  // responsibility of the caller to ensure
31  // that the argument filter exists while
32  // this object is in use.
33  //
34  // useConvolutions -- If "true", the code will call the
35  // "convolve" method of the filter rather
36  // than its "filter" method.
37  //
38  // useMultinomialCovariance -- Specifies whether we should use
39  // multinomial distribution to estimate
40  // covariance of fitted observations
41  // (otherwise Poisson assumption is used).
42  //
43  // smoothLastIter -- If "false", smoothing will not be
44  // applied after the last iteration.
45  // Setting this parameter to "false" is
46  // not recommended for production results
47  // because it is unclear how to compare
48  // such results with models.
49  //
50  // convergenceEpsilon -- Convergence criterion parameter for
51  // various iterations.
52  //
53  // maxIterations -- Maximum number of iterations allowed
54  // (both for the expectation-maximization
55  // iterations and for the code estimating
56  // the error propagation matrix).
57  */
58  SmoothedEMUnfold1D(const Matrix<double>& responseMatrix,
59  const LocalPolyFilter1D& filter,
60  bool useConvolutions,
61  bool useMultinomialCovariance = false,
62  bool smoothLastIter = true,
63  double convergenceEpsilon = 1.0e-10,
64  unsigned maxIterations = 100000U);
65 
66  inline virtual ~SmoothedEMUnfold1D() {}
67 
68  /** Change maximum number of allowed iterations */
69  inline void setMaxIterations(const unsigned u) {maxIterations_ = u;}
70 
71  /** Switch between multinomial/Poisson covariance for observed space */
72  inline void useMultinomialCovariance(const bool b)
73  {useMultinomialCovariance_ = b;}
74 
75  /** Switch between smoothing/not smoothing the last iteration */
76  inline void smoothLastIteration(const bool b) {smoothLast_ = b;}
77 
78  /** Change the convergence criterion */
79  void setConvergenceEpsilon(double eps);
80 
81  //@{
82  /** Simple inspector of object properties */
83  inline double convergenceEpsilon() const {return convergenceEpsilon_;}
84  inline unsigned maxIterations() const {return maxIterations_;}
85  inline bool usingMultinomialCovariance() const
86  {return useMultinomialCovariance_;}
87  inline bool smoothingLastIteration() const {return smoothLast_;}
88  //@}
89 
90  /**
91  // Returns the last number of iterations used to calculate the unfolded
92  // results. This number will be filled after each "unfold" call.
93  */
94  inline unsigned lastNIterations() const {return lastIterations_;}
95 
96  /**
97  // The last number of iterations used to calculate the error
98  // propagation matrix
99  */
100  inline unsigned lastEPIterations() const {return lastEPIterations_;}
101 
102  /** The normalization factor applied during the last smoothing step */
103  inline double lastSmoothingNormfactor() const {return lastNormfactor_;}
104 
105  /**
106  // The main unfolding method. The structure of the covariance matrix
107  // of observations (if provided) will be ignored for optimizing
108  // the unfolded values -- it is only used to evaluate their covariance.
109  // The "unfoldedCovarianceMatrix" parameter can be NULL in which case
110  // the corresponding calculation is not performed (which saves some
111  // CPU time).
112  */
113  virtual bool unfold(const double* observed, unsigned lenObserved,
114  const Matrix<double>* observationCovarianceMatrix,
115  double* unfolded, unsigned lenUnfolded,
116  Matrix<double>* unfoldedCovarianceMatrix);
117  protected:
118  /** Single expectation-maximization (a.k.a. D'Agostini) iteration */
119  void update(const double* observed, unsigned lenObserved,
120  const double* prev, double* next, unsigned lenUnfolded,
121  bool performSmoothing) const;
122 
123  // Initial unfolding matrix a-la D'Agostini
124  Matrix<double> unfoldingMatrix0(const double* unfolded,
125  const double* yhat) const;
126 
127  // Correct error propagation matrix for D'Agostini's iterations
128  Matrix<double> 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  private:
135  // The following method can be overriden by derived classes
136  // in case they want to realize a more sophisticated iteration
137  // scheme in order to improve convergence. This function should
138  // return the number of iterations performed so far. These
139  // iterations will be counted towards the limit on the number
140  // of iterations.
141  inline virtual unsigned preIterate(
142  const double* /* observed */, unsigned /* lenObserved */,
143  double** /* prev */, double** /* next */, unsigned /* lenUnfold */)
144  {return 0U;}
145 
146  double convergenceEpsilon_;
147  mutable double lastNormfactor_;
148  unsigned maxIterations_;
149  unsigned lastIterations_;
150  unsigned lastEPIterations_;
151  bool useMultinomialCovariance_;
152  bool smoothLast_;
153 
154  // Memory buffers
155  std::vector<double> v1_, v2_;
156  mutable std::vector<double> v3_, yhatBuf_;
157  };
158 }
159 
160 #endif // NPSTAT_SMOOTHEDEMUNFOLD1D_HH_
Interface definition for 1-d unfolding algorithms.
Definition: AbsUnfold1D.hh:29
virtual void useConvolutions(const bool b)
Definition: AbsUnfold1D.hh:89
Definition: LocalPolyFilter1D.hh:38
Definition: SmoothedEMUnfold1D.hh:18
double lastSmoothingNormfactor() const
Definition: SmoothedEMUnfold1D.hh:103
unsigned lastEPIterations() const
Definition: SmoothedEMUnfold1D.hh:100
void update(const double *observed, unsigned lenObserved, const double *prev, double *next, unsigned lenUnfolded, bool performSmoothing) const
void setMaxIterations(const unsigned u)
Definition: SmoothedEMUnfold1D.hh:69
double convergenceEpsilon() const
Definition: SmoothedEMUnfold1D.hh:83
void setConvergenceEpsilon(double eps)
void smoothLastIteration(const bool b)
Definition: SmoothedEMUnfold1D.hh:76
virtual bool unfold(const double *observed, unsigned lenObserved, const Matrix< double > *observationCovarianceMatrix, double *unfolded, unsigned lenUnfolded, Matrix< double > *unfoldedCovarianceMatrix)
unsigned lastNIterations() const
Definition: SmoothedEMUnfold1D.hh:94
void useMultinomialCovariance(const bool b)
Definition: SmoothedEMUnfold1D.hh:72
SmoothedEMUnfold1D(const Matrix< double > &responseMatrix, const LocalPolyFilter1D &filter, bool useConvolutions, bool useMultinomialCovariance=false, bool smoothLastIter=true, double convergenceEpsilon=1.0e-10, unsigned maxIterations=100000U)
Definition: AbsArrayProjector.hh:14