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

Revision 1066, 5.1 kB (checked in by mido, 14 years ago)

another part - all conditional pdfs untill bdm::euni

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