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

Revision 660, 4.4 kB (checked in by smidl, 15 years ago)

doc - doxygen warnings

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        //! \todo to je asi navic .. v predkovi!
66        ivec nearest_point ( const vec &val );
67
68        //! Access function
69        int points() const {
70                return Npoints;
71        }
72
73        //! access function
74        //! \todo opet pouze do potomka..
75        const vec& _steps() const {
76                return steps;
77        }
78
79        void from_setting ( const Setting &set );
80};
81UIREGISTER ( rectangular_support );
82
83//! Discrete support with stored support points
84class discrete_support: public root {
85protected:
86        //! storage of support points
87        Array<vec> Spoints;
88        //! index in iterators
89        int idx;
90public:
91        //! Default constructor
92        discrete_support() : Spoints ( 0 ), idx ( 0 ) {}
93        //! Access function
94        int points() const {
95                return Spoints.length();
96        }
97        //! set the first vector to corner and store result in actvec
98        const vec& first_vec() {
99                bdm_assert_debug ( Spoints.length() > 0, "Empty support" );
100                idx = 0;
101                return Spoints ( idx );
102        }
103        //! set next vector after calling first_vec()
104        const vec& next_vec() {
105                bdm_assert_debug ( Spoints.length() > idx - 1, "Out of support points" );
106                return Spoints ( ++idx );
107        }
108
109        /*!
110        \code
111          class = "discrete_support";
112          points = ( [1,2..], [2,2..], ...); // list of points
113           === OR ===
114          epdf = {class="epdf_offspring",...}; // epdf rfom which to sample
115          npoints = 100;                     // number of samples
116        \endcode
117        */
118        void from_setting ( const Setting &set );
119
120        //! access function
121        Array<vec> & _Spoints() {
122                return Spoints;
123        }
124};
125UIREGISTER ( discrete_support );
126
127//! Function defined by values on a fixed grid and interpolated inbetween them
128class grid_fnc: public fnc {
129protected:
130        //! grid - function support
131        rectangular_support sup;
132        //! function values on the grid
133        vec values;
134public:
135        //! constructor function
136        void set_support ( rectangular_support &sup0 ) {
137                sup = sup0;
138                values = zeros ( sup.points() );
139        }
140        //! constructor function fills values by calling function \c f , double f(vec&), given by a pointer
141        void set_values ( double ( *evalptr ) ( const vec& ) );
142
143        //! get value nearest to the given point
144        double nearest_val ( const vec &val ) {
145                return values ( sup.linear_index ( sup.nearest_point ( val ) ) );
146        }
147
148        vec eval ( const vec &val ) {
149                return vec_1 ( nearest_val ( val ) );
150        }
151};
152UIREGISTER ( grid_fnc );
153
154//! Piecewise constant pdf on rectangular support
155//! Each point on the grid represents a centroid around which the density is constant.
156//! This is a trivial point-mass density where all points have the same mass.
157class egrid: public epdf {
158protected:
159        //! support of the pdf - grid
160        rectangular_support sup;
161        //! values at the grid
162        vec values;
163public:
164        //! we assume that evallog is not called too often otherwise we should cache log(values)
165        double evallog ( const vec &val ) {
166                return log ( values ( sup.linear_index ( sup.nearest_point ( val ) ) ) );
167        }
168
169};
170}
171#endif //DISCR_H
Note: See TracBrowser for help on using the browser.