Edinburgh Speech Tools  2.1-release
EST_multistats.cc
Go to the documentation of this file.
1 /*************************************************************************/
2 /* */
3 /* Centre for Speech Technology Research */
4 /* University of Edinburgh, UK */
5 /* Copyright (c) 1995,1996 */
6 /* All Rights Reserved. */
7 /* */
8 /* Permission is hereby granted, free of charge, to use and distribute */
9 /* this software and its documentation without restriction, including */
10 /* without limitation the rights to use, copy, modify, merge, publish, */
11 /* distribute, sublicense, and/or sell copies of this work, and to */
12 /* permit persons to whom this work is furnished to do so, subject to */
13 /* the following conditions: */
14 /* 1. The code must retain the above copyright notice, this list of */
15 /* conditions and the following disclaimer. */
16 /* 2. Any modifications must be clearly marked as such. */
17 /* 3. Original authors' names are not deleted. */
18 /* 4. The authors' names are not used to endorse or promote products */
19 /* derived from this software without specific prior written */
20 /* permission. */
21 /* */
22 /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */
23 /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */
24 /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */
25 /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */
26 /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */
27 /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */
28 /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */
29 /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */
30 /* THIS SOFTWARE. */
31 /* */
32 /*************************************************************************/
33 /* Author : Paul Taylor */
34 /* Date : July 1995 */
35 /*-----------------------------------------------------------------------*/
36 /* Basic Multivariate Statistical Functions */
37 /* */
38 /*=======================================================================*/
39 #include <cmath>
40 #include "EST_multistats.h"
41 
42 using namespace std;
43 
44 float mean(EST_FVector &v)
45 {
46  int i;
47  float u = 0.0;
48 
49  for (i = 0; i < v.n(); ++i)
50  u += v(i);
51 
52  return u / v.n();
53 }
54 
55 float sum(EST_FVector &v)
56 {
57  int i;
58  float u = 0.0;
59 
60  for (i = 0; i < v.n(); ++i)
61  u += v(i);
62 
63  return u;
64 }
65 
67 {
68  EST_FVector u(m.num_columns());
69  int i, j;
70 
71  for (i = 0; i < m.num_columns(); ++i)
72  {
73  u[i] = 0.0;
74  for (j = 0; j < m.num_rows(); ++j)
75  u[i] += m(j, i);
76  u[i] /= m.num_rows();
77  }
78 
79  return u;
80 }
81 
83 {
84  EST_FVector v(m.num_columns());
85  EST_FVector u(m.num_columns());
86  int i, j;
87 
88  u = mean(m);
89 
90  for (j = 0; j < m.num_columns(); ++j)
91  {
92  v[j] = 0.0;
93  for (i = 0; i < m.num_rows(); ++i)
94  v[j] += pow(m(i, j) - u(j), float(2.0));
95  v[j] /= m.num_rows() - 1; // sample variance
96  }
97 
98  return v;
99 }
100 
102 {
103  EST_FVector v;
104  v = sample_variance(m);
105  int j;
106 
107  for (j = 0; j < v.n(); ++j)
108  v[j] = sqrt(v(j));
109 
110  return v;
111 }
112 
114 {
115  EST_FVector u(m.num_columns());
116  EST_FMatrix c(m.num_columns(), m.num_columns());
117  int i, j, k;
118 
119  u = mean(m);
120 
121  for (j = 0; j < m.num_columns(); ++j)
122  for (k = 0; k < m.num_columns(); ++k)
123  {
124  c(j, k) = 0.0;
125  for (i = 0; i < m.num_rows(); ++i)
126  c(j, k) += (m(i, j) - u(j)) * (m(i, k) - u(k));
127  c(j, k) /= m.num_rows();
128  }
129 
130 
131  return c;
132 }
133 
135 {
136  EST_FMatrix r(m.num_columns(), m.num_columns());
137 
138  EST_FVector s = sample_stdev(m);
140 
141  int j, k;
142 
143  for (j = 0; j < m.num_columns(); ++j)
144  for (k = 0; k < m.num_columns(); ++k)
145  r(j, k) = c(j, k)/(s(j) * s(k));
146 
147  return r;
148 }
149 
151 {
152  // nowt yet
153  return m;
154 }
155 
156 // normalise matrix m, by subtracting vector "sub" from each column and
157 // then dividing each column by vector div;
158 
160 {
161  int i, j;
162  EST_FMatrix z(m.num_rows(), m.num_columns());
163 
164  for (j = 0; j < m.num_columns(); ++j)
165  for (i = 0; i < m.num_rows(); ++i)
166  z(i, j) = (m(i, j) - sub(j)) / div(j);
167 
168  return z;
169 }
170 
171 // calculate penrose distance for matrix of means gu of p variables
172 // (columns) in g populations (columns), and variance gv.
173 
175 {
176  int i, j, k;
177  int p = gu.num_columns();
178  int n = gu.num_rows();
179  EST_FMatrix P(n, n);
180 
181  cout << "pop mean " << gu;
182 
183  for (i = 0; i < n; ++i)
184  for (j = 0; j < n; ++j)
185  {
186  P(i, j) = 0.0;
187  for (k = 0; k < p; ++k)
188  P(i, j) += pow(gu(i, k) - gu(j, k), float(2.0)) / gv(k);
189  P(i, j) /= p;
190  }
191  return P;
192 }
193 
195 {
196  float e;
197  EST_FMatrix a, b,c,d;
198 
199  a = ui - uj;
200  transpose(a,b);
201  multiply(b,invv,c);
202  multiply(c,a,d);
203  e = d(0,0);
204  return e;
205 }
206 
208 {
209  int i, j;
210  int n = gu.num_rows();
211  EST_FMatrix P(n, n), invv, ui, uj;
212 
213  inverse(covar,invv); // should be inverse!!
214 
215  for (i = 0; i < n; ++i)
216  for (j = 0; j < n; ++j)
217  {
218  transpose(row(gu, i),ui);
219  transpose(row(gu, j),uj);
220  P(i, j) = single_mahal(ui, uj, invv);
221  }
222 
223  return P;
224 }
225 
227 {
228  int k;
229  int n = uj.n();
230  float P = 0.0;
231 
232  for (k = 0; k < n; ++k)
233  P += pow(ui(k) - uj(k), float(2.0)) / v(k);
234  P /= n;
235 
236  return P;
237 }
238 
240 {
241  int i, k;
242  EST_FMatrix pmean(num_pop, in[0].num_columns());
243  EST_FVector u(in[0].num_columns());
244 
245  for (i = 0; i < num_pop; ++i)
246  {
247  u = mean(in[i]);
248  for (k =0; k < in[i].num_columns(); ++k)
249  pmean(i, k) = u(k);
250  }
251  return pmean;
252 }
253 
255 {
256  int i, j, k, l, n;
257 
258  l = 0;
259  for (i = 0; i < num_pop; ++i)
260  l += in[i].num_rows();
261  n = in[0].num_columns();
262 
263  EST_FMatrix msum(l, n);
264 
265  for (k = l = 0; k < num_pop; ++k)
266  for (j =0; j < n; ++j, ++l)
267  for (i = 0; i < in[i].num_rows(); ++i)
268  msum(l, j) = in[k](i, j);
269 
270  return msum;
271 }
272 
EST_FMatrix mahalanobis_distance(EST_FMatrix &gu, EST_FMatrix &covar)
EST_FMatrix euclidean_distance(EST_FMatrix &m)
float single_mahal(EST_FMatrix &ui, EST_FMatrix &uj, EST_FMatrix &invv)
EST_FMatrix row(const EST_FMatrix &a, ssize_t row)
Definition: vec_mat_aux.cc:210
float mean(EST_FVector &v)
ssize_t num_columns() const
return number of columns
Definition: EST_TMatrix.h:179
A vector class for floating point numbers. EST_FVector x should be used instead of float *x wherever ...
Definition: EST_FMatrix.h:119
EST_FMatrix penrose_distance(EST_FMatrix &gu, EST_FVector &gv)
EST_FVector sample_variance(EST_FMatrix &m)
ssize_t num_rows() const
return number of rows
Definition: EST_TMatrix.h:177
float sum(EST_FVector &v)
EST_FMatrix sample_correlation(EST_FMatrix &m)
EST_FMatrix sub(const EST_FMatrix &a, ssize_t row, ssize_t col)
Definition: vec_mat_aux.cc:187
float simple_penrose(EST_FVector &ui, EST_FVector &uj, EST_FVector &v)
void multiply(const EST_DMatrix &a, const EST_DMatrix &b, EST_DMatrix &ab)
Definition: EST_DMatrix.cc:293
EST_FVector sample_stdev(EST_FMatrix &m)
EST_FMatrix population_mean(EST_FMatrix *in, int num_pop)
void transpose(const EST_FMatrix &a, EST_FMatrix &b)
exchange rows and columns
Definition: vec_mat_aux.cc:244
EST_FMatrix add_populations(EST_FMatrix *in, int num_pop)
EST_FMatrix normalise(EST_FMatrix &m, EST_FVector &sub, EST_FVector &div)
EST_FMatrix sample_covariance(EST_FMatrix &m)
INLINE ssize_t n() const
number of items in vector.
Definition: EST_TVector.h:251
int inverse(const EST_FMatrix &a, EST_FMatrix &inv)
inverse
Definition: vec_mat_aux.cc:298