Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

Distrfunc.hh

Go to the documentation of this file.
00001 #ifndef DISTRFUNC_HEADER
00002 #define DISTRFUNC_HEADER
00003 
00004 // ==== HEADER Distrfunc.hh ====
00005 
00011 // 19-Sep-2001. Andras Aszodi
00012 
00013 // ---- STANDARD HEADERS ----
00014 
00015 #include <math.h>
00016 #ifndef M_PI
00017 #define M_PI 3.14159265358979323846
00018 #endif
00019 
00020 #ifdef _STANDARD_C_PLUS_PLUS
00021 #include <vector>
00022     using namespace std;
00023 #else
00024 #include <vector.h>
00025 #endif
00026 
00027 // ---- MODULE HEADERS ---- 
00028 
00029 #include"Distrbase.hh"
00030 #include"Random.hh"
00031 #include "Rangedimexc.hh"    // out-of-range exceptions
00032 
00033 // ==== CLASSES ====
00034 
00035 namespace RazorBack {
00036 
00043 class Distrfunc_: public Distrbase_
00044 {
00045     // data
00046     vector<double> Parameters;  
00047     
00048     // methods
00049     public:
00050     
00052     Distrfunc_(unsigned int N=0): Parameters(N) {}
00053     
00054     virtual ~Distrfunc_() {}
00055     
00057     double param(unsigned int N) const;
00058     
00063     double param(unsigned int N, double X);
00064     
00065 };
00066 // END OF CLASS Distrfunc_
00067 
00068 // ==== PDF CLASSES ====
00069 
00070 // The classes below implement important distributions
00071 // which are often used in practice.
00072 // Other distributions may be implemented in the same way
00073 // by deriving a new class from Distrfunc_.
00074 
00076 class Uniform_: public Distrfunc_
00077 {
00079     Randomuni_ Rand;    
00080     double Range;   
00081     
00082     // methods
00083     public:
00084             
00086     Uniform_(double Low=0.0, double Up=1.0);
00087     
00089     double low() const { return param(0); }
00090     
00092     double low(double L)
00093     {
00094         return ((L<param(1))? param(0, L): param(0));
00095     }
00096     
00098     double up() const { return param(1); }
00099     
00101     double up(double U)
00102     {
00103         return ((U>param(0))? param(1, U): param(1));
00104     }
00105     
00107     virtual double pdf(double X) const
00108     {
00109         if (X<low() || X>up()) return 0.0;
00110         else return(1.0/Range);
00111     }
00112     
00114     virtual double cdf(double X) const
00115     {
00116         if (X<low()) return 0.0;
00117         if (X>up()) return 1.0;
00118         return (X-low())/Range;
00119     }
00120     
00122     virtual double inv_cdf(double Y) const
00123     {
00124         if (Y<0.0 || Y>1.0)
00125             throw Argexc_("Uniform_::inv_cdf()", Y, 0.0, 1.0);
00126         return (Y*Range+low());
00127     }
00128     
00130     virtual double mean() const { return (low()+up())/2.0; }
00131     
00133     virtual double var() const { return (Range*Range)/12.0; }
00134     
00136     virtual double skew() const { return 0.0; }
00137     
00139     virtual double kurt() const { return 1.8; /* exact is 9/5 :-) */ }
00140     
00142     virtual double entropy() const { return (-log(1.0/Range)); }
00143     
00145     virtual double random() const { return(Rand.rnd_lim(low(), up())); }
00146 };
00147 // END OF CLASS Uniform_
00148 
00150 class Gaussian_: public Distrfunc_
00151 {
00152     // data
00153     static const double SQRT_2PI, SQRT2;
00154     Randomnorm_ Rand;   
00155     
00156     // methods
00157     public:
00158         
00165     Gaussian_(double M=0.0, double S=1.0):
00166             Distrfunc_(2), Rand()
00167     {
00168         S=fabs(S);
00169         if (S==0.0)
00170             throw Badvalexc_("Gaussian_(M,S)", S);
00171         param(0, M); param(1, S);
00172     }
00173     
00174     // Returns the center (mu) parameter of the Gaussian.
00175     double mu() const { return param(0); }
00176     
00177     // Sets the center of the Gaussian, returns old value.
00178     double mu(double M) { return param(0, M); }
00179     
00180     // Returns the spread (sigma) parameter of the Gaussian.
00181     double sigma() const { return param(1); }
00182     
00188     double sigma(double S)
00189     {
00190         S=fabs(S);
00191         if (S==0.0)
00192             throw Badvalexc_("Gaussian_::sigma(S)", S);
00193         return param(1, S);
00194     }
00195     
00197     virtual double pdf(double X) const;
00198     
00200     virtual double cdf(double X) const;
00201     
00203     virtual double inv_cdf(double Y) const;
00204     
00206     virtual double mean() const { return mu(); }
00207     
00209     virtual double var() const { return sigma()*sigma(); }
00210     
00212     virtual double skew() const { return 0.0; }
00213     
00215     virtual double kurt() const { return 3.0; }
00216     
00218     virtual double entropy() const
00219     {
00220         return((log(2.0*M_PI*var())+1)*0.5);
00221     }
00222     
00224     virtual double random() const { return Rand.rnd_msd(mu(), sigma()); }
00225     
00226     // hidden methods
00227     protected:
00228             
00229     static double cdf_std(double Z);
00230     static double invcdf_ratio(double P);
00231 };
00232 // END OF CLASS Gaussian_
00233 
00234 } // RazorBack
00235 
00236 // ==== END OF HEADER Distrfunc.hh ====
00237 
00238 #endif  // DISTRFUNC_HEADER
00239 
00240 
00241 
00242 
00243 

Generated at Wed Aug 21 09:33:12 2002 for The Razorback C++ Library: Statistics by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001