lcsim/src/org/lcsim/fit/line
diff -N SlopeInterceptLineFit.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ SlopeInterceptLineFit.java 28 Mar 2006 04:51:03 -0000 1.1
@@ -0,0 +1,116 @@
+/*
+ * SlopeInterceptLineFit.java
+ *
+ * Created on March 27, 2006, 3:19 PM
+ *
+ * $Id: SlopeInterceptLineFit.java,v 1.1 2006/03/28 04:51:03 ngraf Exp $
+ */
+
+package org.lcsim.fit.line;
+/**
+ * The result of a fit to a 2-D line in slope-intercept form y=a+bx .
+ * @author Norman Graf
+ */
+public class SlopeInterceptLineFit
+{
+ private double _slope;
+ private double _intercept;
+ private double _sigA;
+ private double _sigB;
+ private double _sigAB;
+ private double _chisq;
+ private int _ndf;
+ /**
+ * Creates a new instance of SlopeInterceptLineFit
+ *
+ *
+ * @param slope The slope of the line.
+ * @param intercept The intercept of the line.
+ * @param slopeUncertainty The uncertainty on the slope
+ * @param interceptUncertainty The uncertainty on the intercept
+ * @param sigAB The covariance term.
+ * @param chisq The chi-squared of the fit
+ * @param ndf The number of degrees of freedom for the fit
+ */
+ public SlopeInterceptLineFit(double slope, double intercept, double slopeUncertainty, double interceptUncertainty, double sigAB, double chisq, int ndf)
+ {
+ _slope = slope;
+ _intercept = intercept;
+ _sigA = interceptUncertainty;
+ _sigB = slopeUncertainty;
+ _sigAB = sigAB;
+ _chisq = chisq;
+ _ndf = ndf;
+ }
+
+ /**
+ *
+ * @return The fit slope of the line.
+ */
+ public double slope()
+ {
+ return _slope;
+ }
+
+ /**
+ *
+ * @return The uncertainty on the fit slope of the line.
+ */
+ public double slopeUncertainty()
+ {
+ return _sigB;
+ }
+
+ /**
+ *
+ * @return The fit intercept of the line.
+ */
+ public double intercept()
+ {
+ return _intercept;
+ }
+
+ /**
+ *
+ * @return The uncertainty on the fit intercept of the line.
+ */
+ public double interceptUncertainty()
+ {
+ return _sigA;
+ }
+
+ /**
+ *
+ * @return The covariance of the slope-intercept uncertainties.
+ */
+ public double covariance()
+ {
+ return _sigAB;
+ }
+
+ /**
+ *
+ * @return The chi-squared of the fit.
+ */
+ public double chisquared()
+ {
+ return _chisq;
+ }
+
+ /**
+ *
+ * @return The number of degrees of freedom in the fit.
+ */
+ public int ndf()
+ {
+ return _ndf;
+ }
+
+ public String toString()
+ {
+ StringBuffer sb = new StringBuffer("SlopeInterceptLineFit: \n");
+ sb.append("slope= "+_slope+" +/- "+_sigB+" intercept= "+_intercept+" +/- "+_sigA+" cov: "+_sigAB+ "\n");
+ sb.append("chi-squared for "+_ndf+" degrees of freedom is "+_chisq);
+ return sb.toString();
+ }
+}
lcsim/src/org/lcsim/fit/line
diff -N SlopeInterceptLineFitter.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ SlopeInterceptLineFitter.java 28 Mar 2006 04:51:03 -0000 1.1
@@ -0,0 +1,93 @@
+/*
+ * SlopeInterceptLineFitter.java
+ *
+ * Created on March 27, 2006, 3:19 PM
+ *
+ * $Id: SlopeInterceptLineFitter.java,v 1.1 2006/03/28 04:51:03 ngraf Exp $
+ */
+
+package org.lcsim.fit.line;
+import static java.lang.Math.sqrt;
+/**
+ * A least-squares fit to a 2-D line in slope-intercept form y=a+bx.
+ * It assumes that there is no error on the x-component and that all
+ * the error can be projected onto the y-component.
+ * @author Norman Graf
+ */
+public class SlopeInterceptLineFitter
+{
+ private SlopeInterceptLineFit _fit;
+ /** Creates a new instance of SlopeInterceptLineFitter */
+ public SlopeInterceptLineFitter()
+ {
+
+ }
+
+ /**
+ * Perform the fit.
+ * @param x The array of independent variables.
+ * @param y The array of dependent variables.
+ * @param sigma_y The array of uncertainties on the dependent variables.
+ * @param n The number of points to fit
+ * @return true if the fit is successful.
+ */
+ public boolean fit(double[] x, double[] y, double[] sigma_y, int n)
+ {
+ double sum,sx,sy,sxx,sxy,syy,det;
+ double chisq = 999999.0;
+ int i;
+
+ if (n < 2)
+ {
+ return false; //too few points, abort
+ }
+
+ //initialization
+ sum = sx = sy = sxx = sxy = syy = 0.;
+
+ //find sum , sumx ,sumy, sumxx, sumxy
+ double[] w = new double[n];
+ for (i=0; i<n; ++i)
+ {
+ w[i] = 1/(sigma_y[i]*sigma_y[i]);
+ sum += w[i];
+ sx += w[i]*x[i];
+ sy += w[i]*y[i];
+ sxx += w[i]*x[i]*x[i];
+ sxy += w[i]*x[i]*y[i];
+ syy += w[i]*y[i]*y[i];
+ }
+
+ det = sum*sxx-sx*sx;
+ if (Math.abs(det) < 1.0e-20) return false; //Zero determinant, abort
+
+ //compute the best fitted parameters A,B
+
+ double slope = (sum*sxy-sx*sy)/det;
+ double intercept = (sy*sxx-sxy*sx)/det;
+
+ //calculate chisq-square
+
+ chisq = 0.0;
+ for (i=0; i<n; ++i)
+ {
+ chisq += w[i]*((y[i])-slope*(x[i])-intercept)*((y[i])-slope*(x[i])-intercept);
+ }
+
+ double slopeErr = sqrt(sum/det);
+ double interceptErr = sqrt(sxx/det);
+ double sigab = -sx/det;
+
+ _fit = new SlopeInterceptLineFit(slope, intercept, slopeErr, interceptErr, sigab, chisq, n-2 );
+ return true;
+ }
+
+ /**
+ * Return the fit.
+ * @return The fit results. Returns null if the fit is not successful.
+ */
+ public SlopeInterceptLineFit getFit()
+ {
+ return _fit;
+ }
+}