diff src/samer/models/Covariance.java @ 0:bf79fb79ee13

Initial Mercurial check in.
author samer
date Tue, 17 Jan 2012 17:50:20 +0000
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/samer/models/Covariance.java	Tue Jan 17 17:50:20 2012 +0000
@@ -0,0 +1,84 @@
+/*
+ *	Copyright (c) 2000, Samer Abdallah, King's College London.
+ *	All rights reserved.
+ *
+ *	This software is provided AS iS and WITHOUT ANY WARRANTY; 
+ *	without even the implied warranty of MERCHANTABILITY or 
+ *	FITNESS FOR A PARTICULAR PURPOSE.
+ */
+
+package samer.models;
+import  samer.core.*;
+import  samer.core.types.*;
+import  samer.maths.*;
+import  samer.tools.*;
+import  java.util.*;
+
+/**
+	Collect covariance statistics assuming input is zero mean.
+*/
+public class Covariance extends NamedTask
+{
+	VVector		vector;
+	Matrix		C;
+	int			n, count;
+	double	[] 	x;
+	double	[][] _C;
+	VDouble	rate;
+	double	[] sig;
+
+	public Covariance(VVector vec) throws Exception
+	{
+		super("covariance");
+		Shell.push(node);
+		
+		n=vec.size();
+		C = new Matrix("C",n,n);	// second moment: sum products
+		rate=new VDouble("rate",0.001);
+		Shell.pop();
+
+		sig= new double[n];
+		_C=C.getArray();
+		x=vec.array();
+		vector=vec;
+	}
+
+	public void dispose() { rate.dispose(); C.dispose(); }
+
+	public void reset() { C.zero(); }
+	public void getCorrelation(Matrix R) {
+		double [][] _R=R.getArray();
+		
+		// first get std devs and put 1s down diagonal
+		for (int i=0; i<n; i++) {
+			sig[i] = Math.sqrt(_C[i][i]);
+		}
+
+		for (int i=0; i<n; i++) {
+			for (int j=0; j<i; j++) { // off diagonal
+				_R[i][j] = _C[i][j]/(sig[i]*sig[j]);
+//				_R[j][i] = _R[i][j];
+			}
+			_R[i][i]=1;
+		}
+		R.changed();
+	}
+
+	
+	public void starting() {}
+	public void stopping() {}
+	public void run()
+	{
+		double eta=rate.value;
+		
+		for (int i=0; i<n; i++) {
+			double  [] a=_C[i];
+			double  k=x[i];
+			for (int j=0; j<=i; j++) {
+				a[j] += eta*(k*x[j]-a[j]);
+			}
+		}
+		C.changed();
+	}
+}
+