root/library/bdm/stat/discrete.h @ 730

Revision 730, 4.7 kB (checked in by smidl, 15 years ago)

Reworked epdf_harness - testing is now more general.

Corrections of existing tests.

  • Property svn:eol-style set to native
Line 
1/*!
2  \file
3  \brief Probability distributions for discrete support densities
4  \author Vaclav Smidl.
5
6  -----------------------------------
7  BDM++ - C++ library for Bayesian Decision Making under Uncertainty
8
9  Using IT++ for numerical operations
10  -----------------------------------
11*/
12
13#ifndef DISCR_H
14#define DISCR_H
15
16
17#include "../shared_ptr.h"
18#include "../base/bdmbase.h"
19#include "../math/chmat.h"
20
21namespace bdm {
22
23//! Rectangular support
24//! Support points are located inbetween ranges! For example:
25//! For ranges=[0,1] and gridsizes=[1] the support point is 0.5
26class rectangular_support: public root {
27protected:
28        //! Array of boundaries (2D vectors: [begining,end]) for each dimension
29        Array<vec> ranges;
30        //! Number of support points in each dimension
31        ivec gridsizes;
32        //! dimension
33        int dim;
34        //! Number of data points
35        int Npoints;
36        //! active vector for first_vec and next_vec
37        vec actvec;
38        //! indeces of active vector
39        vec actvec_ind;
40        //! length of steps in each dimension
41        vec steps;
42public:
43        //! default constructor
44        rectangular_support() : dim ( 0 ), Npoints ( 0 ) {
45        }
46
47        //! set parameters
48        void set_parameters ( const Array<vec> &ranges0, const ivec &gridsize0 );
49
50        //! Internal functio to set temporaries correctly
51        void initialize();
52
53        //! return vector at position given by vector of indeces
54        vec get_vec ( const ivec &inds );
55
56        //! convert dimension indeces into linear index, the indexing is in the same way as in \c next_vec()
57        long linear_index ( const ivec inds );
58
59        //! set the first vector to corner and store result in actvec
60        const vec& first_vec();
61
62        //! Get next active vector, call ONLY after first_vector()!
63        const vec& next_vec();
64       
65        //! return active vector, call ONLY after first_vector()!
66        const vec& act_vec(){return actvec;};
67       
68        //! \todo to je asi navic .. v predkovi!
69        ivec nearest_point ( const vec &val );
70
71        //! Access function
72        int points() const {
73                return Npoints;
74        }
75
76        //! access function
77        //! \todo opet pouze do potomka..
78        const vec& _steps() const {
79                return steps;
80        }
81
82        void from_setting ( const Setting &set );
83};
84UIREGISTER ( rectangular_support );
85
86//! Discrete support with stored support points
87class discrete_support: public root {
88protected:
89        //! storage of support points
90        Array<vec> Spoints;
91        //! index in iterators
92        int idx;
93public:
94        //! Default constructor
95        discrete_support() : Spoints ( 0 ), idx ( 0 ) {}
96        //! Access function
97        int points() const {
98                return Spoints.length();
99        }
100        //! set the first vector to corner and store result in actvec
101        const vec& first_vec() {
102                bdm_assert_debug ( Spoints.length() > 0, "Empty support" );
103                idx = 0;
104                return Spoints ( idx );
105        }
106        //! set next vector after calling first_vec()
107        const vec& next_vec() {
108                bdm_assert_debug ( Spoints.length() > idx - 1, "Out of support points" );
109                return Spoints ( ++idx );
110        }
111
112        /*!
113        \code
114          class = "discrete_support";
115          points = ( [1,2..], [2,2..], ...); // list of points
116           === OR ===
117          epdf = {class="epdf_offspring",...}; // epdf rfom which to sample
118          npoints = 100;                     // number of samples
119        \endcode
120        */
121        void from_setting ( const Setting &set );
122
123        //! access function
124        Array<vec> & _Spoints() {
125                return Spoints;
126        }
127};
128UIREGISTER ( discrete_support );
129
130//! Function defined by values on a fixed grid and interpolated inbetween them
131class grid_fnc: public fnc {
132protected:
133        //! grid - function support
134        rectangular_support sup;
135        //! function values on the grid
136        vec values;
137public:
138        //! constructor function
139        void set_support ( rectangular_support &sup0 ) {
140                sup = sup0;
141                values = zeros ( sup.points() );
142        }
143        //! constructor function fills values by calling function \c f , double f(vec&), given by a pointer
144        void set_values ( double ( *evalptr ) ( const vec& ) );
145       
146        //! constructor function fills values by calling function \c f , double f(vec&), given by a pointer
147        void set_values ( const epdf &ep);
148       
149        //! get value nearest to the given point
150        double nearest_val ( const vec &val ) {
151                return values ( sup.linear_index ( sup.nearest_point ( val ) ) );
152        }
153
154        vec eval ( const vec &val ) {
155                return vec_1 ( nearest_val ( val ) );
156        }
157        const vec & _values() const { return values; }
158};
159UIREGISTER ( grid_fnc );
160
161//! Piecewise constant pdf on rectangular support
162//! Each point on the grid represents a centroid around which the density is constant.
163//! This is a trivial point-mass density where all points have the same mass.
164class egrid: public epdf {
165protected:
166        //! support of the pdf - grid
167        rectangular_support sup;
168        //! values at the grid
169        vec values;
170public:
171        //! we assume that evallog is not called too often otherwise we should cache log(values)
172        double evallog ( const vec &val ) {
173                return log ( values ( sup.linear_index ( sup.nearest_point ( val ) ) ) );
174        }
175
176};
177}
178#endif //DISCR_H
Note: See TracBrowser for help on using the browser.