Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
TUnuranContDist.cxx
Go to the documentation of this file.
1// @(#)root/unuran:$Id$
2// Authors: L. Moneta, J. Leydold Wed Feb 28 2007
3
4/**********************************************************************
5 * *
6 * Copyright (c) 2006 LCG ROOT Math Team, CERN/PH-SFT *
7 * *
8 * *
9 **********************************************************************/
10
11// Implementation file for class TUnuranContDist
12
13#include "TUnuranContDist.h"
15#include "Math/WrappedTF1.h"
16
17#include "Math/Integrator.h"
18
19#include "TF1.h"
20#include <cassert>
21#include <cmath>
22
23
25 const ROOT::Math::IGenFunction *cdf, bool isLogPdf, bool copyFunc)
26 : fPdf(pdf), fDPdf(dpdf), fCdf(cdf), fXmin(1.), fXmax(-1.), fMode(0), fArea(0), fIsLogPdf(isLogPdf), fHasDomain(false),
27 fHasMode(false), fHasArea(false), fOwnFunc(copyFunc)
28{
29 // Constructor from generic function interfaces
30 // manage the functions and clone them if flag copyFunc is true
31 if (fOwnFunc) {
32 if (fPdf)
33 fPdf = fPdf->Clone();
34 if (fDPdf)
35 fDPdf = fDPdf->Clone();
36 if (fCdf)
37 fCdf = fCdf->Clone();
38 }
39}
40
41TUnuranContDist::TUnuranContDist (const ROOT::Math::IGenFunction & pdf, const ROOT::Math::IGenFunction * deriv, bool isLogPdf, bool copyFunc ) :
42 TUnuranContDist(&pdf,deriv, nullptr, isLogPdf, copyFunc)
43{}
44
45TUnuranContDist::TUnuranContDist (TF1 * pdf, TF1 * deriv, TF1 * cdf, bool isLogPdf ) :
46 fPdf( (pdf) ? new ROOT::Math::WrappedTF1 ( *pdf) : nullptr ),
47 fDPdf( (deriv) ? new ROOT::Math::WrappedTF1 ( *deriv) : nullptr ),
48 fCdf( (cdf) ? new ROOT::Math::WrappedTF1 ( *cdf) : nullptr),
49 fXmin(1.),
50 fXmax(-1.),
51 fMode(0),
52 fArea(0),
53 fIsLogPdf(isLogPdf),
54 fHasDomain(false),
55 fHasMode(false),
56 fHasArea(false),
57 fOwnFunc(true)
58{
59 // Constructor from a TF1 objects
60 // function pointers are managed by class
61}
62
63TUnuranContDist::TUnuranContDist (TF1 * pdf, TF1 * deriv, bool isLogPdf ) :
64 TUnuranContDist(pdf,deriv, nullptr, isLogPdf)
65 {}
66
69 fPdf(nullptr),
70 fDPdf(nullptr),
71 fCdf(nullptr)
72{
73 // Implementation of copy constructor
74 operator=(rhs);
75}
76
78{
79 // Implementation of assignment operator.
80 if (this == &rhs) return *this; // time saving self-test
81 fXmin = rhs.fXmin;
82 fXmax = rhs.fXmax;
83 fMode = rhs.fMode;
84 fArea = rhs.fArea;
85 fIsLogPdf = rhs.fIsLogPdf;
87 fHasMode = rhs.fHasMode;
88 fHasArea = rhs.fHasArea;
89 fOwnFunc = rhs.fOwnFunc;
90 if (!fOwnFunc) {
91 fPdf = rhs.fPdf;
92 fDPdf = rhs.fDPdf;
93 fCdf = rhs.fCdf;
94 }
95 else {
96 if (fPdf) delete fPdf;
97 if (fDPdf) delete fDPdf;
98 if (fCdf) delete fCdf;
99 fPdf = (rhs.fPdf) ? rhs.fPdf->Clone() : nullptr;
100 fDPdf = (rhs.fDPdf) ? rhs.fDPdf->Clone() : nullptr;
101 fCdf = (rhs.fCdf) ? rhs.fCdf->Clone() : nullptr;
102 }
103
104 return *this;
105}
106
108 // destructor implementation
109 if (fOwnFunc) {
110 if (fPdf) delete fPdf;
111 if (fDPdf) delete fDPdf;
112 if (fCdf) delete fCdf;
113 }
114}
115
117 // set cdf distribution using a generic function interface
118 fCdf = (fOwnFunc) ? cdf.Clone() : &cdf;
119}
120
121
123 // set cumulative distribution function from a TF1
124 if (!fOwnFunc) {
125 // need to manage all functions now
126 if (fPdf) fPdf = fPdf->Clone();
127 if (fDPdf) fDPdf->Clone();
128 }
129 else
130 if (fCdf) delete fCdf;
131
132 fCdf = (cdf) ? new ROOT::Math::WrappedTF1 ( *cdf) : nullptr;
133 fOwnFunc = true;
134}
135
136double TUnuranContDist::Pdf ( double x) const {
137 // evaluate the pdf of the distribution. Return NaN if pdf is not available
138 return (fPdf) ? (*fPdf)(x) : TMath::QuietNaN();
139}
140
141double TUnuranContDist::DPdf( double x) const {
142 // evaluate the derivative of the pdf
143 // if derivative function is not given is evaluated numerically
144 // in case a pdf is available, otherwise a NaN is returned
145 if (fDPdf) {
146 return (*fDPdf)(x);
147 }
148 if (!fPdf) return TMath::QuietNaN();
149 // do numerical derivation using numerical derivation
151 static double gEps = 0.001;
152 double h = ( std::abs(x) > 0 ) ? gEps * std::abs(x) : gEps;
153 assert (fPdf != nullptr);
154 return rd.Derivative1( *fPdf, x, h);
155}
156
157double TUnuranContDist::Cdf(double x) const {
158 // evaluate the integral (cdf) on the domain
159 if (fCdf) {
160 return (*fCdf)(x);
161 }
162 // do numerical integration
163 if (!fPdf) return TMath::QuietNaN();
165 if (fXmin > fXmax) return ig.Integral( *fPdf );
166 else
167 return ig.Integral( *fPdf, fXmin, fXmax );
168
169}
170
#define h(i)
Definition RSha256.hxx:106
virtual IBaseFunctionOneDim * Clone() const =0
Clone a function.
double Integral(Function &f, double a, double b)
evaluate the Integral of a function f over the defined interval (a,b)
Definition Integrator.h:495
User class for calculating the derivatives of a function.
double Derivative1(double x)
Returns the first derivative of the function at point x, computed by Richardson's extrapolation metho...
Class to Wrap a ROOT Function class (like TF1) in a IParamFunction interface of one dimensions to be ...
Definition WrappedTF1.h:39
Definition TF1.h:182
TUnuranBaseDist, base class for Unuran distribution classes such as TUnuranContDist (for one-dimensio...
const ROOT::Math::IGenFunction * fDPdf
pointer to the derivative of the pdf
double fXmin
lower value of the domain
double DPdf(double x) const
evaluate the derivative of the pdf.
const ROOT::Math::IGenFunction * fPdf
pointer to the pdf
bool fHasArea
flag to control if distribution has a pre-computed area below the pdf
double Pdf(double x) const
evaluate the Probability Density function.
double fMode
mode of the distribution
double fArea
area below pdf
bool fHasMode
flag to control if distribution has a pre-computed mode
const ROOT::Math::IGenFunction * fCdf
pointer to the cdf (cumulative dist.)
bool fIsLogPdf
flag to control if function pointer represent log of pdf
double Cdf(double x) const
evaluate the integral (cdf) on the domain.
TUnuranContDist(TF1 *pdf=nullptr, TF1 *deriv=nullptr, bool isLogPdf=false)
Constructor from a TF1 objects specifying the pdf and optionally from another function representing t...
void SetCdf(TF1 *cdf)
set cdf distribution.
double fXmax
upper value of the domain
bool fHasDomain
flag to control if distribution has a defined domain (otherwise is [-inf,+inf]
~TUnuranContDist() override
Destructor.
bool fOwnFunc
flag to indicate if class manages the function pointers
TUnuranContDist & operator=(const TUnuranContDist &rhs)
Assignment operator.
Double_t x[n]
Definition legend1.C:17
Namespace for new Math classes and functions.
IntegratorOneDim Integrator
Definition Integrator.h:476
IBaseFunctionOneDim IGenFunction
Double_t QuietNaN()
Returns a quiet NaN as defined by IEEE 754.
Definition TMath.h:913