33 #ifndef __INCLUDE_MIST_STATISTICS__
34 #define __INCLUDE_MIST_STATISTICS__
40 #ifndef __INCLUDE_MIST_CONF_H__
44 #ifndef __INCLUDE_MIST_COLOR_H__
48 #ifndef __INCLUDE_MIST_H__
52 #ifndef __INCLUDE_MIST_MATRIX__
56 #ifndef __INCLUDE_MIST_NUMERIC__
67 struct ____value_type____
69 typedef double value_type;
73 struct ____value_type____< true >
75 typedef rgb< double > value_type;
81 typedef typename ____value_type____< is_color< T >::value >::value_type value_type;
83 static value_type t(
const value_type &v ){
return( v ); }
86 template <
class T,
class Allocator >
87 struct __value_type__< matrix< T, Allocator > >
89 typedef matrix< T, Allocator > value_type;
91 static value_type t(
const value_type &v ){
return( v.t( ) ); }
113 template <
class Array >
114 inline typename __utility__::__value_type__< typename Array::value_type >::value_type
average(
const Array &a )
116 typedef typename Array::size_type size_type;
117 typedef typename __utility__::__value_type__< typename Array::value_type >::value_type value_type;
121 return( value_type( ) );
125 value_type v = a[ 0 ];
126 for( size_type i = 1 ; i < a.size( ) ; i++ )
130 return( v / static_cast< double >( a.size( ) ) );
142 template <
class Array >
143 inline typename __utility__::__value_type__< typename Array::value_type >::value_type
variance(
const Array &a,
const typename __utility__::__value_type__< typename Array::value_type >::value_type &ave )
145 typedef typename Array::size_type size_type;
146 typedef __utility__::__value_type__< typename Array::value_type > __utility_type__;
147 typedef typename __utility_type__::value_type value_type;
151 return( value_type( ) );
157 value_type x = a[ 0 ] - ave;
158 v = x * __utility_type__::t( x );
161 for( size_type i = 1 ; i < a.size( ) ; i++ )
163 value_type x = a[ i ] - ave;
164 v += x * __utility_type__::t( x );
166 return( v / static_cast< double >( a.size( ) ) );
177 template <
class Array >
178 inline typename __utility__::__value_type__< typename Array::value_type >::value_type
variance(
const Array &a )
197 template <
class Array1,
class Array2 >
198 bool generate_histogram(
const Array1 &in, Array2 &out,
typename Array1::value_type min,
typename Array1::value_type max,
typename Array1::value_type bin )
205 typedef typename Array1::size_type size_type;
206 typedef typename Array1::difference_type difference_type;
207 typedef typename Array1::value_type value_type;
216 value_type tmp = min;
222 difference_type num =
static_cast< size_type
>( ( max - min + 1 ) / bin );
225 for( i = 0 ; i < out.size( ) ; i++ )
230 for( i = 0 ; i < in.size( ) ; i++ )
232 difference_type index =
static_cast< difference_type
>( ( in[ i ] - min ) / bin );
233 if( index >= 0 && index < num )
255 template <
class Array1,
class Array2 >
256 bool generate_histogram(
const Array1 &in, Array2 &out,
typename Array1::value_type min,
typename Array1::value_type max )
273 template <
class Array1,
class Array2 >
276 typedef typename Array1::size_type size_type;
277 typedef typename Array1::difference_type difference_type;
278 typedef typename Array1::value_type value_type;
285 value_type min = in[ 0 ];
286 value_type max = in[ 0 ];
287 for( size_type i = 1 ; i < in.size( ) ; i++ )
293 else if( max < in[ i ] )
312 template <
class Array1,
class Array2 >
337 template <
class Array1,
class Array2,
class T,
class Allocator >
340 typename Array1::value_type min1,
typename Array1::value_type max1,
341 typename Array1::value_type min2,
typename Array1::value_type max2,
342 typename Array1::value_type bin
350 typedef typename Array1::size_type size_type;
351 typedef typename Array1::difference_type difference_type;
352 typedef typename Array1::value_type value_type;
361 value_type tmp = min1;
368 value_type tmp = min2;
373 difference_type num1 =
static_cast< size_type
>( ( max1 - min1 + 1 ) / bin );
374 difference_type num2 =
static_cast< size_type
>( ( max2 - min2 + 1 ) / bin );
378 for( size_type i = 0 ; i < in1.size( ) ; i++ )
380 difference_type index1 =
static_cast< difference_type
>( ( in1[ i ] - min1 ) / bin );
381 difference_type index2 =
static_cast< difference_type
>( ( in2[ i ] - min2 ) / bin );
382 if( index1 >= 0 && index1 < num1 && index2 >= 0 && index2 < num2 )
384 out( index1, index2 ) += 1;
407 template <
class Array1,
class Array2,
class T,
class Allocator >
410 typename Array1::value_type min1,
typename Array1::value_type max1,
411 typename Array1::value_type min2,
typename Array1::value_type max2
430 template <
class Array1,
class Array2,
class T,
class Allocator >
433 typedef typename Array1::size_type size_type;
434 typedef typename Array1::difference_type difference_type;
435 typedef typename Array1::value_type value_type;
442 value_type min1 = in1[ 0 ];
443 value_type max1 = in1[ 0 ];
444 for( size_type i = 1 ; i < in1.size( ) ; i++ )
446 if( min1 > in1[ i ] )
450 else if( max1 < in1[ i ] )
456 value_type min2 = in2[ 0 ];
457 value_type max2 = in2[ 0 ];
458 for( size_type i = 1 ; i < in2.size( ) ; i++ )
460 if( min2 > in2[ i ] )
464 else if( max2 < in2[ i ] )
485 template <
class Array1,
class Array2,
class T,
class Allocator >
502 template<
typename T,
typename Allocator >
503 double central_moment(
const array2< T, Allocator > &img,
size_t x_order,
size_t y_order,
double x0,
double y0 )
506 for(
size_t y = 0 ; y < img.
height() ; ++y )
508 for(
size_t x = 0 ; x < img.
width() ; ++x )
510 r += pow( static_cast< double >( x ) - x0, x_order ) * pow( static_cast< double >( y ) - y0, y_order ) * img( x, y );
524 template<
typename T,
typename Allocator >
525 double normalized_central_moment(
const array2< T, Allocator > &img,
size_t x_order,
size_t y_order,
double u00,
double x0,
double y0 )
527 return( central_moment( img, x_order, y_order, x0, y0 ) / u00 );
536 template<
typename T,
typename Allocator >
541 for(
size_t y = 0 ; y < img.
height() ; ++y )
543 for(
size_t x = 0 ; x < img.
width() ; ++x )
545 r += pow( x, x_order ) * pow( y, y_order ) * img( x, y );
557 template<
typename T,
typename Allocator >
560 double u00 =
moment( img, 0, 0 );
561 double u10 =
moment( img, 1, 0 );
562 double u01 =
moment( img, 0, 1 );
564 double x0 = u10 / u00;
565 double y0 = u01 / u00;
567 return( detail::central_moment( img, x_order, y_order, x0, y0 ) );
577 template<
typename T,
typename Allocator >
580 double u00 =
moment( img, 0, 0 );
581 double u10 =
moment( img, 1, 0 );
582 double u01 =
moment( img, 0, 1 );
583 double x0 = u10 / u00;
584 double y0 = u01 / u00;
585 return( detail::normalized_central_moment( img, x_order, y_order, u00, x0, y0 ) );
592 template<
typename T,
typename Allocator >
595 double u00 =
moment( img, 0, 0 );
596 double u10 =
moment( img, 1, 0 );
597 double u01 =
moment( img, 0, 1 );
598 double x0 = u10 / u00;
599 double y0 = u01 / u00;
603 for(
size_t x = 0 ; x <= 3 ; ++x )
605 for(
size_t y = 0 ; y <= 3 ; ++y )
611 ncm( x, y ) = detail::normalized_central_moment( img, x, y, u00, x0, y0 );
617 moments( 0 ) = ncm( 2, 0 ) * ncm( 0, 2 );
618 moments( 1 ) = pow( ncm( 2, 0 ) - ncm( 0, 2 ), 2.0 ) + 4 * pow( ncm( 1, 1 ), 2.0 );
619 moments( 2 ) = pow( ncm( 3, 0 ) - 3 * ncm( 1, 2 ), 2.0 ) + pow( 3 * ncm( 2, 1 ) - ncm( 0, 3 ), 2.0 );
620 moments( 3 ) = pow( ncm( 3, 0 ) * ncm( 1, 2 ), 2.0 ) + pow( ncm( 2, 1 ) + ncm( 0, 3 ), 2.0 );
621 moments( 4 ) = ( ncm( 3, 0 ) - 3 * ncm( 1, 2 ) ) *
622 ( ncm( 3, 0 ) + ncm( 1, 2 ) ) *
623 ( pow( ncm( 3, 0 ) + ncm( 1, 2 ), 2.0 ) - 3 * pow( ncm( 2, 1 ) + ncm( 0, 3 ), 2.0 ) ) +
624 ( 3 * ncm( 2, 1 ) - ncm( 0, 3 ) ) *
625 ( ncm( 2, 1 ) + ncm( 0, 3 ) ) *
626 ( 3 * pow( ncm( 3, 0 ) + ncm( 1, 2 ), 2.0 ) - pow( ncm( 2, 1 ) + ncm( 0, 3 ), 2.0 ) );
627 moments( 5 ) = ( ncm( 2, 0 ) - ncm( 0, 2 ) ) *
628 ( pow( ncm( 3, 0 ) + ncm( 1, 2 ), 2.0 ) - pow( ncm( 2, 1 ) + ncm( 0, 3 ), 2.0 ) ) +
629 4 * ncm( 1, 1 ) * ( ncm( 3, 0 ) + ncm( 1, 2 ) ) * ( ncm( 2, 1 ) + ncm( 0, 3 ) );
630 moments( 6 ) = ( 3 * ncm( 2, 1 ) - ncm( 0, 3 ) ) *
631 ( ncm( 2, 1 ) + ncm( 0, 3 ) ) *
632 ( 3 * pow( ncm( 3, 0 ) + ncm( 1, 2 ), 2.0 ) - pow( ncm( 2, 1 ) + ncm( 0, 3 ), 2.0 ) ) -
633 ( ncm( 3, 0 ) - 3 * ncm( 2, 1 ) ) *
634 ( ncm( 2, 1 ) + ncm( 0, 3 ) ) *
635 ( 3 * pow( ncm( 3, 0 ) + ncm( 1, 2 ), 2.0 ) - pow( ncm( 2, 1 ) + ncm( 0, 3 ), 2.0 ) );
638 namespace normal_distribution
645 template<
typename T,
typename Allocator >
650 throw std::invalid_argument(
"" );
655 return ( 1.0 / pow( 2 * M_PI, 0.5 * x.
rows() ) ) * ( 1.0 / d ) * exp( -0.5 * t );
662 template<
typename T,
typename Allocator >
665 if( sample.
cols() <= 1 )
667 throw std::invalid_argument(
"" );
671 for(
size_t i = 0 ; i < sample.
cols() ; ++i )
673 for(
size_t j = 0 ; j < sample.
rows() ; ++j )
675 average( j, 0 ) += sample( j, i );
678 average /= sample.
cols();
681 for(
size_t i = 0 ; i < sample.
cols() ; ++i )
683 for(
size_t j = 0 ; j < sample.
rows() ; ++j )
685 tmp( j, i ) -=
average( j, 0 );
688 variance = ( tmp * tmp.
t() ) / ( sample.
cols() - 1 );
700 template<
typename T,
typename Allocator >
703 if( sample.
cols() == 0 )
705 throw std::invalid_argument(
"" );
710 throw std::invalid_argument(
"" );
715 throw std::invalid_argument(
"" );
719 for(
size_t i = 0 ; i < sample.
cols() ; ++i )
722 for(
size_t j = 0 ; j < sample.
rows() ; ++j )
724 double s = fabs( sample( j, i ) - x( j, 0 ) ) / band;
733 return ( 1.0 / ( pow( band, sample.
rows() ) * sample.
cols() ) ) * t;
741 template<
typename T,
typename Allocator >
744 if( sample.
cols() == 0 )
746 throw std::invalid_argument(
"" );
751 throw std::invalid_argument(
"" );
756 throw std::invalid_argument(
"" );
759 int ns = sample.
cols() / n;
760 int n1 = ns * ( n - 1 );
765 for(
int i = 0 ; i < n ; ++i )
768 for(
size_t j = 0 ; j < sample.
cols() ; ++j )
770 if( static_cast< int >( j / ns ) != i )
772 for(
size_t k = 0 ; k < sample.
rows() ; ++k )
774 a( k, idx ) = sample( k, j );
780 for(
size_t j = 0 ; j < sample.
cols() ; ++j )
782 if( static_cast< int >( j / ns ) == i )
785 sample.
trim( x, 0, j, -1, 1 );
786 J += log( density( a, x, band ) );
801 template<
typename T,
typename Allocator >
805 for(
size_t i = 0 ; i < sample.
cols() ; ++i )
808 for(
size_t j = 0 ; j < sample.
rows() ; ++j )
810 d( j, 0 ) -= sample( j, i );
813 t += 1.0 / pow( 2.0 * M_PI, 0.5 * sample.
rows() ) * exp( -0.5 * ( d.
t() * d )( 0, 0 ) );
815 return ( 1.0 / ( pow( b, sample.
rows() ) * sample.
cols() ) ) * t;
823 template<
typename T,
typename Allocator >
826 if( sample.
cols() == 0 )
828 throw std::invalid_argument(
"" );
833 throw std::invalid_argument(
"" );
838 throw std::invalid_argument(
"" );
841 int ns = sample.
cols() / n;
842 int n1 = ns * ( n - 1 );
847 for(
int i = 0 ; i < n ; ++i )
850 for(
size_t j = 0 ; j < sample.
cols() ; ++j )
852 if( static_cast< int >( j / ns ) != i )
854 for(
size_t k = 0 ; k < sample.
rows() ; ++k )
856 a( k, idx ) = sample( k, j );
862 for(
size_t j = 0 ; j < sample.
cols() ; ++j )
864 if( static_cast< int >( j / ns ) == i )
867 sample.
trim( x, 0, j, -1, 1 );
868 J += log( density( a, x, band ) );
883 template<
typename T,
typename Allocator >
886 std::vector< T, Allocator > ssample( sample.
size() );
887 for(
int i = 0 ; i < static_cast< int >( sample.
cols() ) ; ++i )
890 for(
int j = 0 ; j < static_cast< int >( sample.
rows() ) ; ++j )
892 dist += pow( x( j, 0 ) - sample( j, i ), 2.0 );
897 std::sort( ssample.begin(), ssample.end() );
899 double r = sqrt( ssample[ k - 1 ] );
902 int d =
static_cast< int >( sample.
rows() );
923 return ( g * k ) / ( pow( M_PI, 0.5 * ( sample.
rows() - h ) ) * pow( r, sample.
rows() ) * sample.
cols() );
931 template<
typename T,
typename Allocator >
934 if( sample.
cols() == 0 )
936 throw std::invalid_argument(
"" );
941 throw std::invalid_argument(
"" );
946 throw std::invalid_argument(
"" );
949 int ns = sample.
cols() / n;
950 int n1 = ns * ( n - 1 );
955 for(
int i = 0 ; i < n ; ++i )
958 for(
size_t j = 0 ; j < sample.
cols() ; ++j )
960 if( static_cast< int >( j / ns ) != i )
962 for(
size_t k = 0 ; k < sample.
rows() ; ++k )
964 a( k, idx ) = sample( k, j );
970 for(
size_t j = 0 ; j < sample.
cols() ; ++j )
972 if( static_cast< int >( j / ns ) == i )
975 sample.
trim( x, 0, j, -1, 1 );
976 J += log( density( a, x, k ) );
992 #endif // __INCLUDE_MIST_STATISTICS__