-
Notifications
You must be signed in to change notification settings - Fork 0
/
MIhigherdim.C
119 lines (103 loc) · 3.82 KB
/
MIhigherdim.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
This file is part of the MutualInformationICA package
Copyright 2005, 2008, 2019 Alexander Kraskov, Harald Stoegbauer, Sergey Astakhov, Peter Grassberger
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
//####################################################################
//Mutual information estimator (with embedding), rectangular version
//####################################################################
//uses redr_embed
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <limits.h>
#include <float.h>
#include "miutils.h"
int main(int argc, char **argv) {
FILE *fin;
int i;
double **x;
double *min;
double *max;
double *psi;
int K,N;
int d;
double mir;
int dim,edim,tau;
double s,me;
double addnoise=-1;
if (argc<7) {
fprintf(stderr,"\nMutual Infomation (MI) k-nearest neighbours statistics (rectangular)\n\n");
fprintf(stderr,"Usage:\n%s <filename> <dim> <edim> <tau> <# points> <# neighbours> [addnoise]\n\n",argv[0]);
fprintf(stderr,"Input:\n\t<filename>\ttext file with <dim> columns and <# points> rows\n");
fprintf(stderr,"\t<dim>\t\tnumber of columns in file\n");
fprintf(stderr,"\t<edim>\t\tembedding dimension\n");
fprintf(stderr,"\t<tau>\t\tembedding delay\n");
fprintf(stderr,"\t<# points>\tnumber of rows (length of characteristic vector)\n");
fprintf(stderr,"\t<# neighbours>\tnumber of the nearest neighbours for MI estimator\n");
fprintf(stderr,"\t[addnoise]\tnoise amplitude; default 1e-8\n");
fprintf(stderr,"\nOutput:\n");
fprintf(stderr,"\nMI\n");
fprintf(stderr,"\nContact: kraskov@its.caltech.edu\n");
exit(-1);
}
dim=atoi(argv[2]);
edim=atoi(argv[3]);
tau=atoi(argv[4]);
N=atoi(argv[5]);
K=atoi(argv[6]);
if (argc==8) {addnoise=atof(argv[7]);}
if (argc>=9) {fprintf(stderr,"Too many input arguments\n");exit(-1);}
x=(double**)calloc(dim,sizeof(double*));
for (d=0;d<dim;d++) x[d]=(double*)calloc(N,sizeof(double));
min=(double*)calloc(dim,sizeof(double));
max=(double*)calloc(dim,sizeof(double));
for (d=0;d<dim;d++) {min[d]=DBL_MAX/2;max[d]=-DBL_MAX/2;}
//reading of the data
fin=fopen(argv[1],"r");
if (fin)
for (i=0;i<N;i++) {
for (d=0;d<dim;d++) {
fscanf(fin,"%lf",&(x[d][i]));
}
}
else { fprintf(stderr,"File %s doesn't exist\n",argv[1]);exit(-1);}
fclose(fin);
// add noise
if (addnoise) {
srand((dim+edim+tau)*N*K*int(x[(dim)/2][N/10]));
if (addnoise==-1) for (d=0;d<dim;d++) for (i=0;i<N;i++) x[d][i]+=(1.0*rand()/RAND_MAX)*1e-8;
else for (d=0;d<dim;d++) for (i=0;i<N;i++) x[d][i]+=(1.0*rand()/RAND_MAX)*addnoise;
}
//normalization
for (d=0;d<dim;d++) {
me=s=0; for (i=0;i<N;i++) me+=x[d][i];
me/=N; for (i=0;i<N;i++) s+=(x[d][i]-me)*(x[d][i]-me);
s/=(N-1);s=sqrt(s);
if (s==0) {;}
for (i=0;i<N;i++) {
x[d][i] = (x[d][i]-me)/s;
if (x[d][i]<min[d]) min[d]=x[d][i];
if (x[d][i]>max[d]) max[d]=x[d][i];
}
for (i=0;i<N;i++) x[d][i]=x[d][i]-min[d];
}
psi=(double*)calloc(N+1,sizeof(double));
psi[1]=-(double).57721566490153;
for (i=1;i<N;i++) psi[i+1]=psi[i]+1/(double)i;
redr_embed(x, dim, edim, tau, N, K,psi,&mir);
fprintf(stdout,"%1.8f\n",mir);
for (d=0;d<dim;d++) free(x[d]); free(x);
free(min);free(max);
free(psi);
}