comparison src/samer/models/GaussianStats.java @ 0:bf79fb79ee13

Initial Mercurial check in.
author samer
date Tue, 17 Jan 2012 17:50:20 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:bf79fb79ee13
1 /*
2 * Copyright (c) 2000, Samer Abdallah, King's College London.
3 * All rights reserved.
4 *
5 * This software is provided AS iS and WITHOUT ANY WARRANTY;
6 * without even the implied warranty of MERCHANTABILITY or
7 * FITNESS FOR A PARTICULAR PURPOSE.
8 */
9
10 package samer.models;
11 import samer.core.*;
12 import samer.maths.*;
13 import samer.tools.*;
14 import java.util.*;
15
16 /**
17 Collect statistics about a vector: accumulates
18 sums and sums of products, then computes mean
19 and covariance.
20
21 This is NOT a model! Computing energies and probabilities
22 directly from the first and second moments is horrible, since
23 you need the inverse of the covariance matrix. You could,
24 however, use this to train a Gaussian model.
25 */
26
27 public class GaussianStats extends NullTask
28 {
29 VVector vector;
30 VVector sumx;
31 Matrix sumxx;
32 int n, count;
33 double [] _sumx, x;
34 double [][] _sumxx;
35 double [] sig;
36
37 public GaussianStats(VVector vec) throws Exception
38 {
39 n=vec.size();
40 sumx = new VVector("t1",n); // first moment: sum
41 sumxx = new Matrix("t2",n,n); // second moment: sum products
42
43 _sumx=sumx.array();
44 _sumxx=sumxx.getArray();
45 x=vec.array();
46 vector=vec;
47 sig= new double[n];
48
49 }
50
51 public void dispose() {
52 sumx.dispose();
53 sumxx.dispose();
54 }
55
56 public void getMean(VVector mu) { getMean(mu.array()); mu.changed(); }
57 public void getMean(double [] mu) { Mathx.mul(mu, _sumx, 1.0/count); }
58 public void getCovariance(Matrix C) {
59 // compute mean products
60 C.assign(sumxx);
61 C.timesEquals(1.0/count);
62
63 // subtract square mean from mean square
64 double [][] _C=C.getArray();
65
66 for (int i=0; i<n; i++) {
67 double tmp=_sumx[i]/count; // diagonal
68 for (int j=0; j<i; j++) { // off diagonal
69 _C[i][j] -= tmp*(_sumx[j]/count);
70 _C[j][i] = _C[i][j];
71 }
72 _C[i][i] -= tmp*tmp;
73 }
74 C.changed();
75 }
76
77 public void getCorrelation(Matrix R) {
78 // compute mean products
79 R.assign(sumxx);
80 R.timesEquals(1.0/count);
81
82 double [][] _R=R.getArray();
83
84 // first get std devs and put 1s down diagonal
85 for (int i=0; i<n; i++) {
86 double tmp=_sumx[i]/count; // diagonal
87 sig[i] = Math.sqrt(_R[i][i] - tmp*tmp);
88 _R[i][i]=1;
89 }
90
91 for (int i=0; i<n; i++) {
92 double tmp=_sumx[i]/count;
93 for (int j=0; j<i; j++) { // off diagonal
94 _R[i][j] -= tmp*(_sumx[j]/count);
95 _R[i][j] /= sig[i]*sig[j];
96 _R[j][i] = _R[i][j];
97 }
98 }
99 R.changed();
100 }
101
102 public void reset() {
103 Mathx.zero(_sumx);
104 sumxx.zero(); count=0;
105 }
106
107 public void starting() {}
108 public void stopping() {}
109 public void run()
110 {
111 Mathx.add(_sumx,x);
112
113 for (int i=0; i<n; i++) {
114 double [] a=_sumxx[i];
115 double k=x[i];
116 for (int j=0; j<=i; j++) {
117 a[j] += k*x[j];
118 }
119 }
120 sumx.changed();
121 sumxx.changed();
122 count++;
123 }
124 }
125