25 #include <queso/Defines.h>
28 #include <queso/GslVector.h>
29 #include <queso/GslMatrix.h>
30 #include <queso/GaussianVectorRV.h>
31 #include <queso/InfoTheory.h>
33 #include <gsl/gsl_sf_psi.h>
42 unsigned int dimX,
unsigned int dimY,
43 unsigned int xN,
unsigned int yN,
44 unsigned int k,
double eps )
57 for(
unsigned int i = 0; i < xN ; i++ )
59 kdTree->
annkSearch( dataX[ i ], k+1, nnIdx, nnDist, eps );
61 double my_dist = nnDist[ k ];
69 kdTree->
annkSearch( dataX[ i ], yN, nnIdx_tmp, nnDist_tmp, eps );
71 for(
unsigned int my_k = k + 1; my_k < yN; ++my_k )
72 if( nnDist_tmp[ my_k ] > 0.0 )
74 my_dist = nnDist_tmp[ my_k ];
81 distsXY[ i ] = my_dist;
110 for(
unsigned int i = 0; i < N; i++ ) {
111 for(
unsigned int j = 0; j < dimX; j++ ) {
112 meanXY[ j ] += dataXY[ i ][ j ];
114 for(
unsigned int j = 0; j < dimY; j++ ) {
115 meanXY[ dimX + j ] += dataXY[ i ][ dimX + j ];
118 for(
unsigned int j = 0; j < dimXY; j++ ) {
119 meanXY[ j ] = meanXY[ j ] / (double)N;
123 for(
unsigned int i = 0; i < N; i++ ) {
124 for(
unsigned int j = 0; j < dimXY; j++ ) {
125 stdXY[ j ] += pow( dataXY[ i ][ j ] - meanXY[ j ], 2.0 );
128 for(
unsigned int j = 0; j < dimXY; j++ ) {
129 stdXY[ j ] = sqrt( stdXY[ j ] / ((
double)N-1.0) );
141 for(
unsigned int i = 0; i < N; i++ ) {
143 for(
unsigned int j = 0; j < dimXY; j++ ) {
144 dataXY[ i ][ j ] = ( dataXY[ i ][ j ] - meanXY[ j ] ) / stdXY[ j ];
148 for(
unsigned int j = 0; j < dimX; j++ ) {
149 dataX[ i ][ j ] = dataXY[ i ][ j ];
151 for(
unsigned int j = 0; j < dimY; j++ ) {
152 dataY[ i ][ j ] = dataXY[ i ][ dimX + j ];
162 unsigned int dimX,
unsigned int dimY,
163 unsigned int k,
unsigned int N,
double eps )
172 unsigned int dimXY = dimX + dimY;
177 distsXY =
new double[N];
185 distANN_XY( dataXY, dataXY, distsXY, dimXY, dimXY, N, N, k, eps );
188 double marginal_contrib = 0.0;
189 for(
unsigned int i = 0; i < N; i++ ) {
191 int no_pts_X = kdTreeX->
annkFRSearch( dataX[ i ], distsXY[ i ], 0, NULL, NULL, eps);
192 int no_pts_Y = kdTreeY->
annkFRSearch( dataY[ i ], distsXY[ i ], 0, NULL, NULL, eps);
194 marginal_contrib += gsl_sf_psi_int( no_pts_X+1 ) + gsl_sf_psi_int( no_pts_Y+1 );
196 MI_est = gsl_sf_psi_int( k ) + gsl_sf_psi_int( N ) - marginal_contrib / (double)N;
213 template<
template <
class P_V,
class P_M>
class RV,
class P_V,
class P_M>
215 const unsigned int xDimSel[],
unsigned int dimX,
216 const unsigned int yDimSel[],
unsigned int dimY,
217 unsigned int k,
unsigned int N,
double eps )
222 unsigned int dimXY = dimX + dimY;
228 P_V smpRV( jointRV.imageSet().vectorSpace().zeroVector() );
229 for(
unsigned int i = 0; i < N; i++ ) {
231 jointRV.realizer().realization( smpRV );
234 for(
unsigned int j = 0; j < dimX; j++ ) {
235 dataXY[ i ][ j ] = smpRV[ xDimSel[j] ];
237 for(
unsigned int j = 0; j < dimY; j++ ) {
238 dataXY[ i ][ dimX + j ] = smpRV[ yDimSel[j] ];
257 template<
class P_V,
class P_M,
258 template <
class P_V,
class P_M>
class RV_1,
259 template <
class P_V,
class P_M>
class RV_2>
261 const RV_2<P_V,P_M>& yRV,
262 const unsigned int xDimSel[],
unsigned int dimX,
263 const unsigned int yDimSel[],
unsigned int dimY,
264 unsigned int k,
unsigned int N,
double eps )
269 unsigned int dimXY = dimX + dimY;
275 P_V smpRV_x( xRV.imageSet().vectorSpace().zeroVector() );
276 P_V smpRV_y( yRV.imageSet().vectorSpace().zeroVector() );
278 for(
unsigned int i = 0; i < N; i++ ) {
280 xRV.realizer().realization( smpRV_x );
281 yRV.realizer().realization( smpRV_y );
284 for(
unsigned int j = 0; j < dimX; j++ ) {
285 dataXY[ i ][ j ] = smpRV_x[ xDimSel[j] ];
287 for(
unsigned int j = 0; j < dimY; j++ ) {
288 dataXY[ i ][ dimX + j ] = smpRV_y[ yDimSel[j] ];
307 template <
class P_V,
class P_M,
308 template <
class P_V,
class P_M>
class RV_1,
309 template <
class P_V,
class P_M>
class RV_2>
312 unsigned int xDimSel[],
unsigned int dimX,
313 unsigned int yDimSel[],
unsigned int dimY,
314 unsigned int xN,
unsigned int yN,
315 unsigned int k,
double eps )
325 queso_error_msg(
"Error-KL: the dimensions should agree");
331 distsX =
new double[xN];
332 distsXY =
new double[xN];
335 P_V xSmpRV( xRV.imageSet().vectorSpace().zeroVector() );
336 for(
unsigned int i = 0; i < xN; i++ ) {
338 xRV.realizer().realization( xSmpRV );
340 for(
unsigned int j = 0; j < dimX; j++ ) {
341 dataX[ i ][ j ] = xSmpRV[ xDimSel[j] ];
346 P_V ySmpRV( yRV.imageSet().vectorSpace().zeroVector() );
347 for(
unsigned int i = 0; i < yN; i++ ) {
349 yRV.realizer().realization( ySmpRV );
351 for(
unsigned int j = 0; j < dimY; j++ ) {
352 dataY[ i ][ j ] = ySmpRV[ yDimSel[j] ];
357 distANN_XY( dataX, dataX, distsX, dimX, dimX, xN, xN, k+1, eps );
358 distANN_XY( dataX, dataY, distsXY, dimX, dimY, xN, yN, k, eps );
361 double sum_log_ratio = 0.0;
362 for(
unsigned int i = 0; i < xN; i++ )
364 sum_log_ratio += log( distsXY[i] / distsX[i] );
366 KL_est = (double)dimX/(
double)xN * sum_log_ratio + log( (
double)yN / ((
double)xN-1.0 ) );
382 template <
class P_V,
class P_M,
383 template <
class P_V,
class P_M>
class RV_1,
384 template <
class P_V,
class P_M>
class RV_2>
387 unsigned int xDimSel[],
unsigned int dimX,
388 unsigned int yDimSel[],
unsigned int dimY,
389 unsigned int xN,
unsigned int yN,
390 unsigned int k,
double eps )
399 queso_error_msg(
"Error-CE: the dimensions should agree");
405 distsXY =
new double[xN];
408 P_V xSmpRV( xRV.imageSet().vectorSpace().zeroVector() );
409 for(
unsigned int i = 0; i < xN; i++ ) {
411 xRV.realizer().realization( xSmpRV );
413 for(
unsigned int j = 0; j < dimX; j++ ) {
414 dataX[ i ][ j ] = xSmpRV[ xDimSel[j] ];
419 P_V ySmpRV( yRV.imageSet().vectorSpace().zeroVector() );
420 for(
unsigned int i = 0; i < yN; i++ ) {
422 yRV.realizer().realization( ySmpRV );
424 for(
unsigned int j = 0; j < dimY; j++ ) {
425 dataY[ i ][ j ] = ySmpRV[ yDimSel[j] ];
430 distANN_XY( dataX, dataY, distsXY, dimX, dimY, xN, yN, k, eps );
433 double sum_log = 0.0;
434 for(
unsigned int i = 0; i < xN; i++ )
436 sum_log += log( distsXY[i] );
438 CE_est = (double)dimX/(
double)xN * sum_log + log( (
double)yN ) - gsl_sf_psi_int( k );
450 #endif // QUESO_HAS_ANN
int annkFRSearch(ANNpoint q, ANNdist sqRad, int k, ANNidxArray nn_idx=NULL, ANNdistArray dd=NULL, double eps=0.0)
double estimateMI_ANN(const RV< P_V, P_M > &jointRV, const unsigned int xDimSel[], unsigned int dimX, const unsigned int yDimSel[], unsigned int dimY, unsigned int k, unsigned int N, double eps)
void annkSearch(ANNpoint q, int k, ANNidxArray nn_idx, ANNdistArray dd, double eps=0.0)
double estimateCE_ANN(RV_1< P_V, P_M > &xRV, RV_2< P_V, P_M > &yRV, unsigned int xDimSel[], unsigned int dimX, unsigned int yDimSel[], unsigned int dimY, unsigned int xN, unsigned int yN, unsigned int k, double eps)
void distANN_XY(const ANNpointArray dataX, const ANNpointArray dataY, double *distsXY, unsigned int dimX, unsigned int dimY, unsigned int xN, unsigned int yN, unsigned int k, double eps)
DLL_API void annDeallocPts(ANNpointArray &pa)
DLL_API ANNpoint annAllocPt(int dim, ANNcoord c=0)
double estimateKL_ANN(RV_1< P_V, P_M > &xRV, RV_2< P_V, P_M > &yRV, unsigned int xDimSel[], unsigned int dimX, unsigned int yDimSel[], unsigned int dimY, unsigned int xN, unsigned int yN, unsigned int k, double eps)
void normalizeANN_XY(ANNpointArray dataXY, unsigned int dimXY, ANNpointArray dataX, unsigned int dimX, ANNpointArray dataY, unsigned int dimY, unsigned int N)
DLL_API ANNpointArray annAllocPts(int n, int dim)
double computeMI_ANN(ANNpointArray dataXY, unsigned int dimX, unsigned int dimY, unsigned int k, unsigned int N, double eps)