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
;
46 template<typename T
> TProb
<T
> min( const TProb
<T
> &a
, const TProb
<T
> &b
);
47 template<typename T
> TProb
<T
> max( const TProb
<T
> &a
, const TProb
<T
> &b
);
50 /// TProb<T> implements a probability vector of type T.
51 /// T should be castable from and to double.
52 template <typename T
> class TProb
{
58 /// Calculate x times log(x), or 0 if x == 0
59 Real
xlogx( Real x
) const { return( x
== 0.0 ? 0.0 : x
* std::log(x
)); }
62 /// NORMPROB means that the sum of all entries should be 1
63 /// NORMLINF means that the maximum absolute value of all entries should be 1
64 typedef enum { NORMPROB
, NORMLINF
} NormType
;
65 /// DISTL1 is the L-1 distance (sum of absolute values of pointwise difference)
66 /// DISTLINF is the L-inf distance (maximum absolute value of pointwise difference)
67 /// DISTTV is the Total Variation distance
68 typedef enum { DISTL1
, DISTLINF
, DISTTV
} DistType
;
70 /// Default constructor
73 /// Construct uniform distribution of given length
74 explicit TProb( size_t n
) : _p(std::vector
<T
>(n
, 1.0 / n
)) {}
76 /// Construct with given length and initial value
77 TProb( size_t n
, Real p
) : _p(n
, (T
)p
) {}
79 /// Construct with given length and initial array
80 TProb( size_t n
, const Real
* p
) : _p(p
, p
+ n
) {}
82 /// Provide read access to _p
83 const std::vector
<T
> & p() const { return _p
; }
85 /// Provide full access to _p
86 std::vector
<T
> & p() { return _p
; }
88 /// Provide read access to ith element of _p
89 T
operator[]( size_t i
) const {
97 /// Provide full access to ith element of _p
98 T
& operator[]( size_t i
) { return _p
[i
]; }
100 /// Set all elements to x
101 TProb
<T
> & fill(T x
) {
102 std::fill( _p
.begin(), _p
.end(), x
);
106 /// Set all elements to iid random numbers from uniform(0,1) distribution
107 TProb
<T
> & randomize() {
108 std::generate(_p
.begin(), _p
.end(), rnd_uniform
);
113 size_t size() const {
117 /// Make entries zero if (Real) absolute value smaller than epsilon
118 TProb
<T
>& makeZero (Real epsilon
) {
119 for( size_t i
= 0; i
< size(); i
++ )
120 if( fabs((Real
)_p
[i
]) < epsilon
)
122 // std::replace_if( _p.begin(), _p.end(), fabs((Real)boost::lambda::_1) < epsilon, 0.0 );
126 /// Make entries epsilon if they are smaller than epsilon
127 TProb
<T
>& makePositive (Real epsilon
) {
128 for( size_t i
= 0; i
< size(); i
++ )
129 if( (0 < (Real
)_p
[i
]) && ((Real
)_p
[i
] < epsilon
) )
134 /// Multiplication with T x
135 TProb
<T
>& operator*= (T x
) {
136 std::transform( _p
.begin(), _p
.end(), _p
.begin(), std::bind2nd( std::multiplies
<T
>(), x
) );
140 /// Return product of *this with T x
141 TProb
<T
> operator* (T x
) const {
142 TProb
<T
> prod( *this );
148 TProb
<T
>& operator/= (T x
) {
152 std::transform( _p
.begin(), _p
.end(), _p
.begin(), std::bind2nd( std::divides
<T
>(), x
) );
156 /// Return quotient of *this and T x
157 TProb
<T
> operator/ (T x
) const {
158 TProb
<T
> quot( *this );
164 TProb
<T
>& operator+= (T x
) {
165 std::transform( _p
.begin(), _p
.end(), _p
.begin(), std::bind2nd( std::plus
<T
>(), x
) );
169 /// Return sum of *this with T x
170 TProb
<T
> operator+ (T x
) const {
171 TProb
<T
> sum( *this );
176 /// Difference by T x
177 TProb
<T
>& operator-= (T x
) {
178 std::transform( _p
.begin(), _p
.end(), _p
.begin(), std::bind2nd( std::minus
<T
>(), x
) );
182 /// Return difference of *this and T x
183 TProb
<T
> operator- (T x
) const {
184 TProb
<T
> diff( *this );
189 /// Pointwise comparison
190 bool operator<= (const TProb
<T
> & q
) const {
192 assert( size() == q
.size() );
194 for( size_t i
= 0; i
< size(); i
++ )
195 if( !(_p
[i
] <= q
[i
]) )
200 /// Pointwise multiplication with q
201 TProb
<T
>& operator*= (const TProb
<T
> & q
) {
203 assert( size() == q
.size() );
205 std::transform( _p
.begin(), _p
.end(), q
._p
.begin(), _p
.begin(), std::multiplies
<T
>() );
209 /// Return product of *this with q
210 TProb
<T
> operator* (const TProb
<T
> & q
) const {
212 assert( size() == q
.size() );
214 TProb
<T
> prod( *this );
219 /// Pointwise addition with q
220 TProb
<T
>& operator+= (const TProb
<T
> & q
) {
222 assert( size() == q
.size() );
224 std::transform( _p
.begin(), _p
.end(), q
._p
.begin(), _p
.begin(), std::plus
<T
>() );
228 /// Pointwise subtraction of q
229 TProb
<T
>& operator-= (const TProb
<T
> & q
) {
231 assert( size() == q
.size() );
233 std::transform( _p
.begin(), _p
.end(), q
._p
.begin(), _p
.begin(), std::minus
<T
>() );
237 /// Return sum of *this and q
238 TProb
<T
> operator+ (const TProb
<T
> & q
) const {
240 assert( size() == q
.size() );
242 TProb
<T
> sum( *this );
247 /// Return *this minus q
248 TProb
<T
> operator- (const TProb
<T
> & q
) const {
250 assert( size() == q
.size() );
252 TProb
<T
> diff( *this );
257 /// Pointwise division by q (division by zero yields zero)
258 TProb
<T
>& operator/= (const TProb
<T
> & q
) {
260 assert( size() == q
.size() );
262 for( size_t i
= 0; i
< size(); i
++ ) {
271 /// Pointwise division by q (division by zero yields infinity)
272 TProb
<T
>& divide (const TProb
<T
> & q
) {
274 assert( size() == q
.size() );
276 std::transform( _p
.begin(), _p
.end(), q
._p
.begin(), _p
.begin(), std::divides
<T
>() );
280 /// Return quotient of *this with q
281 TProb
<T
> operator/ (const TProb
<T
> & q
) const {
283 assert( size() == q
.size() );
285 TProb
<T
> quot( *this );
290 /// Return pointwise inverse
291 TProb
<T
> inverse(bool zero
= false) const {
293 inv
._p
.reserve( size() );
295 for( size_t i
= 0; i
< size(); i
++ )
296 inv
._p
.push_back( _p
[i
] == 0.0 ? 0.0 : 1.0 / _p
[i
] );
298 for( size_t i
= 0; i
< size(); i
++ ) {
300 assert( _p
[i
] != 0.0 );
302 inv
._p
.push_back( 1.0 / _p
[i
] );
307 /// Return *this to the power of a (pointwise)
308 TProb
<T
>& operator^= (Real a
) {
310 std::transform( _p
.begin(), _p
.end(), _p
.begin(), std::bind2nd( std::ptr_fun
<T
, Real
, T
>(std::pow
), a
) );
314 /// Pointwise power of a
315 TProb
<T
> operator^ (Real a
) const {
316 TProb
<T
> power(*this);
322 TProb
<T
> sgn() const {
324 x
._p
.reserve( size() );
325 for( size_t i
= 0; i
< size(); i
++ ) {
336 /// Pointwise absolute value
337 TProb
<T
> abs() const {
339 x
._p
.reserve( size() );
340 for( size_t i
= 0; i
< size(); i
++ )
341 x
._p
.push_back( _p
[i
] < 0 ? (-p
[i
]) : p
[i
] );
346 const TProb
<T
>& takeExp() {
347 std::transform( _p
.begin(), _p
.end(), _p
.begin(), std::ptr_fun
<T
, T
>(std::exp
) );
352 const TProb
<T
>& takeLog() {
353 std::transform( _p
.begin(), _p
.end(), _p
.begin(), std::ptr_fun
<T
, T
>(std::log
) );
357 /// Pointwise log (or 0 if == 0)
358 const TProb
<T
>& takeLog0() {
359 for( size_t i
= 0; i
< size(); i
++ )
360 _p
[i
] = ( (_p
[i
] == 0.0) ? 0.0 : std::log( _p
[i
] ) );
365 TProb
<T
> exp() const {
372 TProb
<T
> log() const {
378 /// Pointwise log (or 0 if == 0)
379 TProb
<T
> log0() const {
385 /// Return distance of p and q
386 friend Real
dist( const TProb
<T
> & p
, const TProb
<T
> & q
, DistType dt
) {
388 assert( p
.size() == q
.size() );
393 for( size_t i
= 0; i
< p
.size(); i
++ )
394 result
+= fabs((Real
)p
[i
] - (Real
)q
[i
]);
398 for( size_t i
= 0; i
< p
.size(); i
++ ) {
399 Real z
= fabs((Real
)p
[i
] - (Real
)q
[i
]);
406 for( size_t i
= 0; i
< p
.size(); i
++ )
407 result
+= fabs((Real
)p
[i
] - (Real
)q
[i
]);
414 /// Return Kullback-Leibler distance with q
415 friend Real
KL_dist( const TProb
<T
> & p
, const TProb
<T
> & q
) {
417 assert( p
.size() == q
.size() );
420 for( size_t i
= 0; i
< p
.size(); i
++ ) {
421 if( (Real
) p
[i
] != 0.0 ) {
424 result
+= p_i
* (std::log(p_i
) - std::log(q_i
));
430 /// Return sum of all entries
432 T Z
= std::accumulate( _p
.begin(), _p
.end(), (T
)0 );
436 /// Converts entries to Real and returns maximum absolute value
439 for( size_t i
= 0; i
< size(); i
++ ) {
440 Real mag
= fabs( (Real
) _p
[i
] );
447 /// Returns maximum value
449 T Z
= *std::max_element( _p
.begin(), _p
.end() );
453 /// Returns minimum value
455 T Z
= *std::min_element( _p
.begin(), _p
.end() );
459 /// Normalize, using the specified norm
460 T
normalize( NormType norm
= NORMPROB
) {
462 if( norm
== NORMPROB
)
464 else if( norm
== NORMLINF
)
473 /// Return normalized copy of *this, using the specified norm
474 TProb
<T
> normalized( NormType norm
= NORMPROB
) const {
475 TProb
<T
> result(*this);
476 result
.normalize( norm
);
480 /// Returns true if one or more entries are NaN
481 bool hasNaNs() const {
482 return (std::find_if( _p
.begin(), _p
.end(), isnan
) != _p
.end());
485 /// Returns true if one or more entries are negative
486 bool hasNegatives() const {
487 return (std::find_if( _p
.begin(), _p
.end(), std::bind2nd( std::less
<Real
>(), 0.0 ) ) != _p
.end());
490 /// Returns true if one or more entries are non-positive (causes problems with logscale)
491 bool hasNonPositives() const {
492 return (std::find_if( _p
.begin(), _p
.end(), std::bind2nd( std::less_equal
<Real
>(), 0.0 ) ) != _p
.end());
496 Real
entropy() const {
498 for( size_t i
= 0; i
< size(); i
++ )
503 /// Returns TProb<T> containing the pointwise minimum of a and b (which should have equal size)
504 friend TProb
<T
> min
<> ( const TProb
<T
> &a
, const TProb
<T
> &b
);
506 /// Returns TProb<T> containing the pointwise maximum of a and b (which should have equal size)
507 friend TProb
<T
> max
<> ( const TProb
<T
> &a
, const TProb
<T
> &b
);
509 friend std::ostream
& operator<< (std::ostream
& os
, const TProb
<T
>& P
) {
511 std::copy( P
._p
.begin(), P
._p
.end(), std::ostream_iterator
<T
>(os
, " ") );
518 template<typename T
> TProb
<T
> min( const TProb
<T
> &a
, const TProb
<T
> &b
) {
519 assert( a
.size() == b
.size() );
520 TProb
<T
> result( a
.size() );
521 for( size_t i
= 0; i
< a
.size(); i
++ )
530 template<typename T
> TProb
<T
> max( const TProb
<T
> &a
, const TProb
<T
> &b
) {
531 assert( a
.size() == b
.size() );
532 TProb
<T
> result( a
.size() );
533 for( size_t i
= 0; i
< a
.size(); i
++ )
542 } // end of namespace dai