comparison src/samer/models/Covariance.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.core.types.*;
13 import samer.maths.*;
14 import samer.tools.*;
15 import java.util.*;
16
17 /**
18 Collect covariance statistics assuming input is zero mean.
19 */
20 public class Covariance extends NamedTask
21 {
22 VVector vector;
23 Matrix C;
24 int n, count;
25 double [] x;
26 double [][] _C;
27 VDouble rate;
28 double [] sig;
29
30 public Covariance(VVector vec) throws Exception
31 {
32 super("covariance");
33 Shell.push(node);
34
35 n=vec.size();
36 C = new Matrix("C",n,n); // second moment: sum products
37 rate=new VDouble("rate",0.001);
38 Shell.pop();
39
40 sig= new double[n];
41 _C=C.getArray();
42 x=vec.array();
43 vector=vec;
44 }
45
46 public void dispose() { rate.dispose(); C.dispose(); }
47
48 public void reset() { C.zero(); }
49 public void getCorrelation(Matrix R) {
50 double [][] _R=R.getArray();
51
52 // first get std devs and put 1s down diagonal
53 for (int i=0; i<n; i++) {
54 sig[i] = Math.sqrt(_C[i][i]);
55 }
56
57 for (int i=0; i<n; i++) {
58 for (int j=0; j<i; j++) { // off diagonal
59 _R[i][j] = _C[i][j]/(sig[i]*sig[j]);
60 // _R[j][i] = _R[i][j];
61 }
62 _R[i][i]=1;
63 }
64 R.changed();
65 }
66
67
68 public void starting() {}
69 public void stopping() {}
70 public void run()
71 {
72 double eta=rate.value;
73
74 for (int i=0; i<n; i++) {
75 double [] a=_C[i];
76 double k=x[i];
77 for (int j=0; j<=i; j++) {
78 a[j] += eta*(k*x[j]-a[j]);
79 }
80 }
81 C.changed();
82 }
83 }
84