Print

Print


Commit in lcsim/src/org/lcsim/recon/emid/hmatrix on MAIN
HMatrix.java+63-71.2 -> 1.3
HMatrixBuilder.java+7-31.1 -> 1.2
+70-10
2 modified files
Added layer variances to persistent format. Added diagonal chisquared.

lcsim/src/org/lcsim/recon/emid/hmatrix
HMatrix.java 1.2 -> 1.3
diff -u -r1.2 -r1.3
--- HMatrix.java	28 Jun 2005 05:29:23 -0000	1.2
+++ HMatrix.java	19 Jul 2005 15:22:29 -0000	1.3
@@ -79,6 +79,7 @@
 {
     private double[][] _invcov; // the inverse covariance matrix
     private double[] _vec; // the vector of measurements
+    private double[] _cov; // the variance on the measurements
     private int _dim; // the dimensionality
     private int _key; // the key for the HMatrix
     private double[] _tmp; // temporary array
@@ -93,16 +94,18 @@
      * @param   vec  The array of average values for the measurement space.
      * @param   cov  The inverse of the covariance matrix for the averages.
      */
-    public HMatrix(int dim, int key, double [] vec, double[][] cov)
+    public HMatrix(int dim, int key, double [] vec, double[] cov, double[][] invcov)
     {
         _dim = dim;
         _key = key;
         _vec = new double[_dim];
+        _cov = new double[_dim];
         System.arraycopy(vec, 0, _vec, 0, _dim);
+        System.arraycopy(cov, 0, _cov, 0, _dim);
         _invcov = new double[_dim][_dim];
         for(int i =0; i<_dim; ++i)
         {
-            System.arraycopy(cov[i], 0, _invcov[i], 0, _dim);
+            System.arraycopy(invcov[i], 0, _invcov[i], 0, _dim);
         }
         _tmp = new double[_dim];
         _tmp2 = new double[_dim];
@@ -161,7 +164,28 @@
         return chisqr;
     }
     
-    
+    /**
+     * Calculates the diagonal chi-squared for the measurement compared to
+     * the expected values and covariances represented by the HMatrix,
+     * neglecting correlations.
+     *
+     * @param   dat  The array of measured values
+     * @return  The value of the diagonal chi-squared.
+     */
+    public double chisquaredDiagonal(double[] dat)
+    {    
+        double chisqr = 0.;
+        // diagonal terms of covariance matrix times difference vector 
+        for(int i=0; i<_dim; ++i)
+        {
+            //measured-predicted values
+            _tmp[i] = dat[i]-_vec[i];
+            //squared, divided by the variance of the prediction
+            chisqr += _tmp[i]*_tmp[i]/_cov[i];
+        }
+        return chisqr;
+    }    
+       
     /**
      *Output stream
      *
@@ -190,6 +214,7 @@
     /**
      * Writes the HMatrix to a Serialized file
      */
+/*    
     public void writeSerialized(String filename)
     {
         
@@ -212,10 +237,11 @@
         }
         
     }
-    
+*/    
     /**
      * Reads the HMatrix from a Serialized file
      */
+ /*
     public static HMatrix readSerialized(String filename)
     {
         HMatrix hm = null;
@@ -241,7 +267,7 @@
         }
         return hm;
     }
-    
+*/    
     /**
      *  Writes out the HMatrix to an ASCII file
      */
@@ -264,6 +290,13 @@
                 for(int i = 0; i < _dim; i++) s5 = s5 + _vec[i] + " ";
                 printwriter.println(s5.substring(0, s5.length() - 1));
             }
+            //Now the vector of variance of average values
+            {
+                String s5 = "";
+                for(int i = 0; i < _dim; i++) s5 = s5 + _cov[i] + " ";
+                printwriter.println(s5.substring(0, s5.length() - 1));
+            }
+
             //Now the covariance matrix
             for(int i = 0; i < _dim; i++)
             {
@@ -466,12 +499,14 @@
     {
         boolean flag = false;
         boolean readVec = false;
+        boolean readCovDiag = false;
         int i = 0;
         int ii = 0;
         int j = 0;
         int dim=0;
         int key = 0;
         double[] vec=null;
+        double[] covDiag=null;
         double[][] cov=null;
         for(String s1 = new String(); (s1 = bufferedreader.readLine()) != null;)
             if(s1.startsWith("#"))
@@ -482,6 +517,7 @@
                     dim = Integer.parseInt(s1.substring(0, s1.indexOf(" ")));
                     key = Integer.parseInt(s1.substring(s1.indexOf(" ") + 1, s1.length()));
                     vec = new double[dim];
+                    covDiag = new double[dim];
                     cov = new double[dim][dim];
                     flag = true;
                 }
@@ -489,7 +525,8 @@
                 {
                     if (!readVec)
                     {
-                        
+//                        System.out.println("read vec");
+//                        System.out.println(s1);
                         // Read the vector of averages
                         while(s1.length() > 0 && s1.indexOf(" ") != -1)
                         {
@@ -501,9 +538,28 @@
                         double d1 = Double.valueOf(s1).doubleValue();
                         vec[ii] = d1;
                         readVec = true;
+                        ii=0;
+                    }
+                    else if(!readCovDiag)
+                    {
+//                        System.out.println("read covDiag");
+//                        System.out.println(s1);
+                        // Read the vector of variance of averages
+                        while(s1.length() > 0 && s1.indexOf(" ") != -1)
+                        {
+                            double d = Double.valueOf(s1.substring(0, s1.indexOf(" "))).doubleValue();
+                            s1 = s1.substring(s1.indexOf(" ") + 1, s1.length());
+                            covDiag[ii] = d;
+                            ii++;
+                        }
+                        double d1 = Double.valueOf(s1).doubleValue();
+                        covDiag[ii] = d1;
+                        readCovDiag = true;                        
                     }
                     else
                     {
+//                        System.out.println("read cov");
+//                        System.out.println(s1);
                         // Read the covariance matrix
                         while(s1.length() > 0 && s1.indexOf(" ") != -1)
                         {
@@ -518,7 +574,7 @@
                         j = 0;
                     }
                 }
-        return new HMatrix(dim, key, vec, cov);
+        return new HMatrix(dim, key, vec, covDiag, cov);
     }
     
 }
\ No newline at end of file

lcsim/src/org/lcsim/recon/emid/hmatrix
HMatrixBuilder.java 1.1 -> 1.2
diff -u -r1.1 -r1.2
--- HMatrixBuilder.java	24 Jun 2005 18:11:01 -0000	1.1
+++ HMatrixBuilder.java	19 Jul 2005 15:22:29 -0000	1.2
@@ -63,6 +63,7 @@
      */
     public void validate()
     {
+        double[] _covDiag = new double[_dim];
         for(int i=0; i<_dim; ++i)
         {
             _vec[i] = _vals[i]/_ndata;
@@ -71,9 +72,10 @@
                 double data = (double)_ndata;
                 _cov[i][j] = _valsq[i][j]/(data) - (_vals[i]/(data)*(_vals[j]/(data)));
             }
+            _covDiag[i] = _cov[i][i];
         }
         
-        _hmx = new HMatrix(_dim, _key, _vec, invert(_cov,_dim));
+        _hmx = new HMatrix(_dim, _key, _vec, _covDiag, invert(_cov,_dim));
         _isValid = true;
     }
     
@@ -110,6 +112,7 @@
      *
      *  @param   filename  The file to which to write.
      */
+    /*
     public void writeSerialized(String filename)
     {
         if (_isValid)
@@ -122,18 +125,19 @@
             System.out.println("HMatrix not valid! Cannot write!");
         }
     }
-    
+    */
     /**
      * Reads the HMatrix from a Serialized file
      *
      * @param   filename The Serialized file from which to read.
      * @return  The HMatrix
      */
+    /*
     public HMatrix readSerialized(String filename)
     {
         return HMatrix.readSerialized(filename);
     }
-    
+    */
     /**
      * Reads the HMatrix from an ASCII file
      *
CVSspam 0.2.8