-
Notifications
You must be signed in to change notification settings - Fork 2
/
stat.cxx
executable file
·195 lines (161 loc) · 4.63 KB
/
stat.cxx
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include "stat.h"
/// \bief sample mean
///
/// \param samples sample list (input)
/// \param mean mean (output)
/// \param dim dimension of the input samples
///
/// \return none
///
/// \author Jorge Sanchez
/// \date August 2009
template<class T>
void
sample_mean( const std::vector<T*> &samples, T *mean, int dim )
{
int N=samples.size();
assert(N>0);
double *acc = new double[dim];
memset( acc, 0, dim*sizeof(double) );
for( int n=N; n--; )
{
for( int i=dim; i--; )
{
acc[i] += (double)samples[n][i];
}
}
double iN = 1.0/double(N);
for( int i=dim; i--; )
{
mean[i] = (T)(acc[i]*iN);
}
delete[] acc;
}
/// \bief sample variance (over dimensions)
///
/// \param samples sample list (input)
/// \param mean precomputed sample mean (input)
/// \param variance variance (output)
/// \param dim dimension of the input samples
///
/// \return none
///
/// \author Jorge Sanchez
/// \date August 2009
template<class T>
void
sample_variance( const std::vector<T*> &samples, const T *mean, T *variance, int dim )
{
int N=samples.size();
assert(N>1);
double *acc = new double[dim];
memset( acc, 0, dim*sizeof(double) );
for( int n=N; n--; )
{
for( int i=dim; i--; )
{
T dm = samples[n][i]-mean[i];
acc[i] += (double)(dm*dm);
}
}
double iN1 = 1.0/double(N-1);
for( int i=dim; i--; )
{
variance[i] = (T)(acc[i]*iN1);
}
delete[] acc;
}
/// \bief sample variance (over dimensions)
///
/// \param samples sample list (input)
/// \param variance variance (output)
/// \param dim dimension of the input samples
///
/// \return none
///
/// \author Jorge Sanchez
/// \date August 2009
template<class T>
void
sample_variance( const std::vector<T*> &samples, T *variance, int dim )
{
T *mean = new T[dim];
sample_mean( samples, mean, dim );
sample_variance( samples, mean, variance, dim );
delete[] mean;
return;
}
/// \bief Sample standardization (-> zero mean, unit variance)
///
/// \param samples sample list (input) -> standarized samples (output)
/// \param mean sample mean (output)
/// \param variance sample variance (output)
/// \param dim dimension of the input samples
///
/// \return none
///
/// \author Jorge Sanchez
/// \date August 2009
template<class T>
void
standardize( std::vector<T*> &samples, T *mean, T *variance, int dim )
{
sample_mean( samples, mean, dim );
sample_variance( samples, mean, variance, dim );
T i_stddev[dim];
for( int i=dim; i--; )
{
if( variance[i]>0.0 )
i_stddev[i] = 1.0/sqrt(variance[i]);
else
i_stddev[i] = 0.0;
}
#pragma omp parallel for
for( int n=0; n<(int)samples.size(); n++ )
{
for( int i=0; i<dim; i++ )
{
samples[n][i] = (samples[n][i]-mean[i])*i_stddev[i];
}
}
}
/// \bief Samples de-standardization (zero mean, unit variance -> original mean and var)
///
/// \param samples standardized sample list (input) -> original standarized samples (output)
/// \param mean original sample mean (input)
/// \param variance original sample variance (input)
/// \param dim dimension of the input samples
///
/// \return none
///
/// \author Jorge Sanchez
/// \date August 2009
template<class T>
void
destandardize( std::vector<T*> &samples, const T *mean, const T *variance, int dim )
{
assert(mean && variance);
T stddev[dim];
for( int i=0; i<dim; i++ )
{
stddev[i] = sqrt(variance[i]);
}
#pragma omp parallel for
for( int n=0; n<(int)samples.size(); n++ )
{
for( int i=0; i<dim; i++ )
{
samples[n][i] = samples[n][i]*stddev[i]+mean[i];
}
}
}
template void sample_mean<double>( const std::vector<double*> &samples, double *mean, int dim );
template void sample_variance<double>( const std::vector<double*> &samples, const double *mean, double *variance, int dim );
template void sample_variance<double>( const std::vector<double*> &samples, double *variance, int dim );
template void standardize<double>( std::vector<double*> &samples, double *mean, double *variance, int dim );
template void destandardize<double>( std::vector<double*> &samples, const double *mean, const double *variance, int dim );
template void sample_mean<float>( const std::vector<float*> &samples, float *mean, int dim );
template void sample_variance<float>( const std::vector<float*> &samples, const float *mean, float *variance, int dim );
template void sample_variance<float>( const std::vector<float*> &samples, float *variance, int dim );
template void standardize<float>( std::vector<float*> &samples, float *mean, float *variance, int dim );
template void destandardize<float>( std::vector<float*> &samples, const float *mean, const float *variance, int dim );