npstat is hosted by Hepforge, IPPP Durham
NPStat  5.10.0
LocationScaleTransform1.hh
Go to the documentation of this file.
1 #ifndef NPSTAT_LOCATIONSCALETRANSFORM1_HH_
2 #define NPSTAT_LOCATIONSCALETRANSFORM1_HH_
3 
4 /*!
5 // \file LocationScaleTransform1.hh
6 //
7 // \brief Transform in which density location and scale depend on one parameter
8 //
9 // Author: I. Volobouev
10 //
11 // April 2015
12 */
13 
14 #include <cfloat>
15 
17 
18 namespace npstat {
19  template<class LocationFunctor, class ScaleFunctor>
21  {
22  public:
23  inline LocationScaleTransform1(const LocationFunctor& location,
24  const ScaleFunctor& scale,
25  const double paramMin = -DBL_MAX,
26  const double paramMax = DBL_MAX)
28  loc_(location),
29  scale_(scale),
30  param_(paramMin),
31  paramMin_(paramMin),
32  paramMax_(paramMax),
33  changed_(true)
34  {
35  }
36 
37  inline virtual ~LocationScaleTransform1() {}
38 
39  inline virtual LocationScaleTransform1* clone() const
40  {return new LocationScaleTransform1(*this);}
41 
42  inline double getParamMin() const {return paramMin_;}
43  inline double getParamMax() const {return paramMax_;}
44  inline const LocationFunctor& getLocationFcn() const {return loc_;}
45  inline const ScaleFunctor& getScaleFcn() const {return scale_;}
46  inline double getLocation() const
47  {if (changed_) recalculate(); return m_;}
48  inline double getScale() const
49  {if (changed_) recalculate(); return s_;}
50 
51  inline double transformForward(const double x, double* dydx) const
52  {
53  if (changed_) recalculate();
54  if (dydx) *dydx = 1.0/s_;
55  return (x - m_)/s_;
56  }
57 
58  inline double transformBack(const double y) const
59  {
60  if (changed_) recalculate();
61  return y*s_ + m_;
62  }
63 
64  inline bool isIncreasing() const {return true;}
65 
66  //@{
67  /** Prototype needed for I/O */
68  inline virtual gs::ClassId classId() const {return gs::ClassId(*this);}
69  virtual bool write(std::ostream&) const;
70  //@}
71 
72  static const char* classname()
73  {
74  static const std::string myClassName(
75  gs::template_class_name<LocationFunctor,ScaleFunctor>(
76  "npstat::LocationScaleTransform1"));
77  return myClassName.c_str();
78  }
79  static inline unsigned version() {return 1;}
80  static LocationScaleTransform1* read(
81  const gs::ClassId& id, std::istream&);
82 
83  protected:
84  inline virtual bool isEqual(const AbsDistributionTransform1D& oth) const
85  {
86  const LocationScaleTransform1& r =
87  static_cast<const LocationScaleTransform1&>(oth);
88  return loc_ == r.loc_ && scale_ == r.scale_ && param_ == r.param_ &&
89  paramMin_ == r.paramMin_ && paramMax_ == r.paramMax_;
90  }
91 
92  private:
93  inline void recalculate() const
94  {
95  const double s = scale_(param_);
96  if (s <= 0.0) throw std::invalid_argument(
97  "In npstat::LocationScaleTransform1::recalculate:"
98  " obtained scale is not positive");
99  m_ = loc_(param_);
100  s_ = s;
101  changed_ = false;
102  }
103 
104  inline void setParameterChecked(unsigned, const double value)
105  {
106  if (value < paramMin_ || value > paramMax_)
107  throw std::invalid_argument(
108  "In npstat::LocationScaleTransform1::setParameterChecked:"
109  " parameter value out of range");
110  param_ = value;
111  changed_ = true;
112  }
113 
114  inline void setAllParametersChecked(const double* p)
115  {
116  if (p[0] < paramMin_ || p[0] > paramMax_)
117  throw std::invalid_argument(
118  "In npstat::LocationScaleTransform1::setAllParametersChecked:"
119  " parameter value out of range");
120  param_ = p[0];
121  changed_ = true;
122  }
123 
124  inline double getParameterChecked(unsigned) const {return param_;}
125 
126  LocationFunctor loc_;
127  ScaleFunctor scale_;
128  double param_;
129  double paramMin_;
130  double paramMax_;
131 
132  mutable double m_;
133  mutable double s_;
134  mutable bool changed_;
135  };
136 }
137 
138 #include "npstat/stat/LocationScaleTransform1.icc"
139 
140 #endif // NPSTAT_LOCATIONSCALETRANSFORM1_HH_
Interface definition for 1-d coordinate transformations used to build statistical distributions.
Definition: AbsDistributionTransform1D.hh:29
Definition: LocationScaleTransform1.hh:21
virtual LocationScaleTransform1 * clone() const
Definition: LocationScaleTransform1.hh:39
virtual gs::ClassId classId() const
Definition: LocationScaleTransform1.hh:68
virtual bool isEqual(const AbsDistributionTransform1D &oth) const
Definition: LocationScaleTransform1.hh:84
bool isIncreasing() const
Definition: LocationScaleTransform1.hh:64
double transformForward(const double x, double *dydx) const
Definition: LocationScaleTransform1.hh:51
Definition: AbsArrayProjector.hh:14