Commit in lcsim/test/org/lcsim/fit/polynomial on MAIN
PolynomialFitterTest.java+64added 1.1
First release of lightweight least-squares polynomial fitter.

lcsim/test/org/lcsim/fit/polynomial
PolynomialFitterTest.java added at 1.1
diff -N PolynomialFitterTest.java
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ PolynomialFitterTest.java	21 Mar 2006 18:34:22 -0000	1.1
@@ -0,0 +1,64 @@
+/*
+ * PolynomialFitterTest.java
+ *
+ * Created on March 20, 2006, 3:57 PM
+ *
+ *
+ */
+
+package org.lcsim.fit.polynomial;
+
+import Jama.Matrix;
+import java.util.Random;
+import junit.framework.TestCase;
+
+/**
+ *
+ * @author Norman Graf
+ */
+public class PolynomialFitterTest extends TestCase
+{
+    
+    /** Creates a new instance of PolynomialFitterTest */
+    public void testPolynomialFitter()
+    {
+        Random r = new Random();
+        PolynomialFitter fitter = new PolynomialFitter();
+        // test a parabola
+        double[] pars = {15., 1.5, 3.2};
+        int npoints = 10;
+        int nparams = pars.length;
+        double[] x = new double[npoints];
+        double[] y = new double[npoints];
+        double[] sigma = new double[npoints];
+        double eps = .01;
+        
+        double x0 = 0.;
+        double delta = 1.;
+        for(int i = 0; i<npoints; ++i)
+        {
+            x[i] = x0+i*delta;
+            
+            for(int j=0; j<pars.length; ++j)
+            {
+                y[i]+=pars[j]*Math.pow(x[i], j);
+            }
+            // now smear the measurement
+            y[i]+=r.nextGaussian()*eps;
+            sigma[i] = eps;
+        }
+        
+        boolean success = fitter.fit(x, y, sigma, npoints, nparams);
+        assertTrue(success);
+        PolynomialFit fit = fitter.getFit();
+        Matrix p = fit.parameters();
+        Matrix c = fit.covariance();
+        System.out.println(p);
+        System.out.println(c);
+        for(int i=0; i<nparams; ++i)
+        {
+            assertEquals(pars[i],p.get(i,0),3*Math.sqrt(c.get(i,i))); // insist on 3 sigma
+        }
+    }
+    
+}
CVSspam 0.2.8