1 /* Copyright (C) 2006-2008 Joris Mooij [j dot mooij at science dot ru dot nl]
2 Radboud University Nijmegen, The Netherlands
4 This file is part of libDAI.
6 libDAI is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 libDAI is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with libDAI; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #ifndef __defined_libdai_prob_h
23 #define __defined_libdai_prob_h
41 template<typename T
> class TProb
;
42 typedef TProb
<Real
> Prob
;
45 /// TProb<T> implements a probability vector of type T.
46 /// T should be castable from and to double.
47 template <typename T
> class TProb
{
53 /// Calculate x times log(x), or 0 if x == 0
54 Real
xlogx( Real x
) const { return( x
== 0.0 ? 0.0 : x
* std::log(x
)); }
57 /// NORMPROB means that the sum of all entries should be 1
58 /// NORMLINF means that the maximum absolute value of all entries should be 1
59 typedef enum { NORMPROB
, NORMLINF
} NormType
;
60 /// DISTL1 is the L-1 distance (sum of absolute values of pointwise difference)
61 /// DISTLINF is the L-inf distance (maximum absolute value of pointwise difference)
62 /// DISTTV is the Total Variation distance
63 typedef enum { DISTL1
, DISTLINF
, DISTTV
} DistType
;
65 /// Default constructor
68 /// Construct uniform distribution of given length
69 explicit TProb( size_t n
) : _p(std::vector
<T
>(n
, 1.0 / n
)) {}
71 /// Construct with given length and initial value
72 TProb( size_t n
, Real p
) : _p(n
, (T
)p
) {}
74 /// Construct with given length and initial array
75 TProb( size_t n
, const Real
* p
) : _p(p
, p
+ n
) {}
77 /// Provide read access to _p
78 const std::vector
<T
> & p() const { return _p
; }
80 /// Provide full access to _p
81 std::vector
<T
> & p() { return _p
; }
83 /// Provide read access to ith element of _p
84 T
operator[]( size_t i
) const { return _p
[i
]; }
86 /// Provide full access to ith element of _p
87 T
& operator[]( size_t i
) { return _p
[i
]; }
89 /// Set all elements to x
90 TProb
<T
> & fill(T x
) {
91 std::fill( _p
.begin(), _p
.end(), x
);
95 /// Set all elements to iid random numbers from uniform(0,1) distribution
96 TProb
<T
> & randomize() {
97 std::generate(_p
.begin(), _p
.end(), rnd_uniform
);
102 size_t size() const {
106 /// Make entries zero if (Real) absolute value smaller than epsilon
107 TProb
<T
>& makeZero (Real epsilon
) {
108 for( size_t i
= 0; i
< size(); i
++ )
109 if( fabs((Real
)_p
[i
]) < epsilon
)
111 // std::replace_if( _p.begin(), _p.end(), fabs((Real)boost::lambda::_1) < epsilon, 0.0 );
115 /// Multiplication with T x
116 TProb
<T
>& operator*= (T x
) {
117 std::transform( _p
.begin(), _p
.end(), _p
.begin(), std::bind2nd( std::multiplies
<T
>(), x
) );
121 /// Return product of *this with T x
122 TProb
<T
> operator* (T x
) const {
123 TProb
<T
> prod( *this );
129 TProb
<T
>& operator/= (T x
) {
133 std::transform( _p
.begin(), _p
.end(), _p
.begin(), std::bind2nd( std::divides
<T
>(), x
) );
137 /// Return quotient of *this and T x
138 TProb
<T
> operator/ (T x
) const {
139 TProb
<T
> quot( *this );
145 TProb
<T
>& operator+= (T x
) {
146 std::transform( _p
.begin(), _p
.end(), _p
.begin(), std::bind2nd( std::plus
<T
>(), x
) );
150 /// Return sum of *this with T x
151 TProb
<T
> operator+ (T x
) const {
152 TProb
<T
> sum( *this );
157 /// Difference by T x
158 TProb
<T
>& operator-= (T x
) {
159 std::transform( _p
.begin(), _p
.end(), _p
.begin(), std::bind2nd( std::minus
<T
>(), x
) );
163 /// Return difference of *this and T x
164 TProb
<T
> operator- (T x
) const {
165 TProb
<T
> diff( *this );
170 /// Pointwise multiplication with q
171 TProb
<T
>& operator*= (const TProb
<T
> & q
) {
173 assert( size() == q
.size() );
175 std::transform( _p
.begin(), _p
.end(), q
._p
.begin(), _p
.begin(), std::multiplies
<T
>() );
179 /// Return product of *this with q
180 TProb
<T
> operator* (const TProb
<T
> & q
) const {
182 assert( size() == q
.size() );
184 TProb
<T
> prod( *this );
189 /// Pointwise addition with q
190 TProb
<T
>& operator+= (const TProb
<T
> & q
) {
192 assert( size() == q
.size() );
194 std::transform( _p
.begin(), _p
.end(), q
._p
.begin(), _p
.begin(), std::plus
<T
>() );
198 /// Pointwise subtraction of q
199 TProb
<T
>& operator-= (const TProb
<T
> & q
) {
201 assert( size() == q
.size() );
203 std::transform( _p
.begin(), _p
.end(), q
._p
.begin(), _p
.begin(), std::minus
<T
>() );
207 /// Return sum of *this and q
208 TProb
<T
> operator+ (const TProb
<T
> & q
) const {
210 assert( size() == q
.size() );
212 TProb
<T
> sum( *this );
217 /// Return *this minus q
218 TProb
<T
> operator- (const TProb
<T
> & q
) const {
220 assert( size() == q
.size() );
222 TProb
<T
> diff( *this );
227 /// Pointwise division by q (division by zero yields zero)
228 TProb
<T
>& operator/= (const TProb
<T
> & q
) {
230 assert( size() == q
.size() );
232 for( size_t i
= 0; i
< size(); i
++ ) {
241 /// Pointwise division by q (division by zero yields infinity)
242 TProb
<T
>& divide (const TProb
<T
> & q
) {
244 assert( size() == q
.size() );
246 std::transform( _p
.begin(), _p
.end(), q
._p
.begin(), _p
.begin(), std::divides
<T
>() );
250 /// Return quotient of *this with q
251 TProb
<T
> operator/ (const TProb
<T
> & q
) const {
253 assert( size() == q
.size() );
255 TProb
<T
> quot( *this );
260 /// Return pointwise inverse
261 TProb
<T
> inverse(bool zero
= false) const {
263 inv
._p
.reserve( size() );
265 for( size_t i
= 0; i
< size(); i
++ )
266 inv
._p
.push_back( _p
[i
] == 0.0 ? 0.0 : 1.0 / _p
[i
] );
268 for( size_t i
= 0; i
< size(); i
++ ) {
270 assert( _p
[i
] != 0.0 );
272 inv
._p
.push_back( 1.0 / _p
[i
] );
277 /// Return *this to the power of a (pointwise)
278 TProb
<T
>& operator^= (Real a
) {
280 std::transform( _p
.begin(), _p
.end(), _p
.begin(), std::bind2nd( std::ptr_fun
<T
, Real
, T
>(std::pow
), a
) );
284 /// Pointwise power of a
285 TProb
<T
> operator^ (Real a
) const {
286 TProb
<T
> power(*this);
292 const TProb
<T
>& takeExp() {
293 std::transform( _p
.begin(), _p
.end(), _p
.begin(), std::ptr_fun
<T
, T
>(std::exp
) );
298 const TProb
<T
>& takeLog() {
299 std::transform( _p
.begin(), _p
.end(), _p
.begin(), std::ptr_fun
<T
, T
>(std::log
) );
303 /// Pointwise log (or 0 if == 0)
304 const TProb
<T
>& takeLog0() {
305 for( size_t i
= 0; i
< size(); i
++ )
306 _p
[i
] = ( (_p
[i
] == 0.0) ? 0.0 : std::log( _p
[i
] ) );
311 TProb
<T
> exp() const {
318 TProb
<T
> log() const {
324 /// Pointwise log (or 0 if == 0)
325 TProb
<T
> log0() const {
331 /// Return distance of p and q
332 friend Real
dist( const TProb
<T
> & p
, const TProb
<T
> & q
, DistType dt
) {
334 assert( p
.size() == q
.size() );
339 for( size_t i
= 0; i
< p
.size(); i
++ )
340 result
+= fabs((Real
)p
[i
] - (Real
)q
[i
]);
344 for( size_t i
= 0; i
< p
.size(); i
++ ) {
345 Real z
= fabs((Real
)p
[i
] - (Real
)q
[i
]);
352 for( size_t i
= 0; i
< p
.size(); i
++ )
353 result
+= fabs((Real
)p
[i
] - (Real
)q
[i
]);
360 /// Return Kullback-Leibler distance with q
361 friend Real
KL_dist( const TProb
<T
> & p
, const TProb
<T
> & q
) {
363 assert( p
.size() == q
.size() );
366 for( size_t i
= 0; i
< p
.size(); i
++ ) {
367 if( (Real
) p
[i
] != 0.0 ) {
370 result
+= p_i
* (std::log(p_i
) - std::log(q_i
));
376 /// Return sum of all entries
378 T Z
= std::accumulate( _p
.begin(), _p
.end(), (T
)0 );
382 /// Converts entries to Real and returns maximum absolute value
385 for( size_t i
= 0; i
< size(); i
++ ) {
386 Real mag
= fabs( (Real
) _p
[i
] );
393 /// Returns maximum value
395 T Z
= *std::max_element( _p
.begin(), _p
.end() );
399 /// Normalize, using the specified norm
400 T
normalize( NormType norm
) {
402 if( norm
== NORMPROB
)
404 else if( norm
== NORMLINF
)
413 /// Return normalized copy of *this, using the specified norm
414 TProb
<T
> normalized( NormType norm
) const {
415 TProb
<T
> result(*this);
416 result
.normalize( norm
);
420 /// Returns true if one or more entries are NaN
421 bool hasNaNs() const {
422 return (std::find_if( _p
.begin(), _p
.end(), isnan
) != _p
.end());
425 /// Returns true if one or more entries are negative
426 bool hasNegatives() const {
427 return (std::find_if( _p
.begin(), _p
.end(), std::bind2nd( std::less
<Real
>(), 0.0 ) ) != _p
.end());
430 /// Returns true if one or more entries are non-positive (causes problems with logscale)
431 bool hasNonPositives() const {
432 return (std::find_if( _p
.begin(), _p
.end(), std::bind2nd( std::less_equal
<Real
>(), 0.0 ) ) != _p
.end());
436 Real
entropy() const {
438 for( size_t i
= 0; i
< size(); i
++ )
443 friend std::ostream
& operator<< (std::ostream
& os
, const TProb
<T
>& P
) {
444 std::copy( P
._p
.begin(), P
._p
.end(), std::ostream_iterator
<T
>(os
, " ") );
451 } // end of namespace dai