/**
* This calculates B1 and B0 for the linear regression frorm the passed-in
* vectors of a set of n programs
*
* @version $Id: RegressionCalc.java,v 1.1 1998/10/14 17:58:40 tfm1584 Exp $
*
* @author Tom M
*
* Revisisons:
* $Log: RegressionCalc.java,v $
* Revision 1.1 1998/10/14 17:58:40 tfm1584
* Initial revision
*
*
*/
import java.util.Vector;
class RegressionCalc {
/**
* the constructor
*/
public RegressionCalc() {
}
/**
* This calculates B1
*
* @param x the vector of x values
* @param y the vector of y values
*
* @return the calculated value of B1
*
*/
public double calculateB1( Vector x, Vector y ) {
Vector xdouble = new Vector();
Vector ydouble = new Vector();
MathClass math = new MathClass();
int n = x.size();
int sumXY = 0;
int sumX2 = 0;
double XAvg = math.calculateMean( x );
double YAvg = math.calculateMean( y );
for ( int i = 0; i < n; i++ ) {
sumXY += ( (Double)x.elementAt(i) ).doubleValue() *
( (Double)y.elementAt(i) ).doubleValue();
sumX2 += ( (Double)x.elementAt(i) ).doubleValue() *
( (Double)x.elementAt(i) ).doubleValue();
}
double result = ( sumXY - ( n * XAvg * YAvg ) ) /
( sumX2 - ( n * XAvg * XAvg ) );
return result;
}
/**
* This calculates B0
*
* @param x the vector of x values
* @param y the vector of y values
*
* @return the calculated value of B0
*
*/
public double calculateB0( Vector x, Vector y, double B1 ) {
MathClass math = new MathClass();
Vector xdouble = new Vector();
Vector ydouble = new Vector();
double XAvg = math.calculateMean( x );
double YAvg = math.calculateMean( y );
double result = YAvg - ( B1 * XAvg );
return result;
}
/**
* This calculates the error sum of squares
*
* @param x the vector of x values
* @param y the vector of y values
* @param B0 the calculated value of B0
* @param B1 the calculated value of B1
*
* @return the calculated value of the error sum of squares
*
*/
public double calculateRSquared( Vector x, Vector y, double B0, double B1) {
int n = x.size();
double r;
double rSquared = 0;
for ( int i = 0; i < n; i++ ) {
r = ((Double)y.elementAt( i )).doubleValue() - (B0 +
( B1* ((Double)x.elementAt(i)).doubleValue()) );
rSquared += r*r;
}
return ( rSquared / ( n - 2 ) );
}
}