Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package mx.itesm.a01139626.p3.src;
- public class DataSet {
- private double dXK;
- private double dSumX;
- private double dSumY;
- private double dSumXY;
- private double dSumX2;
- private double dSumY2;
- private int iN;
- private double dXAvg;
- private double dYAvg;
- private double dR;
- private double dR2;
- private double dB0;
- private double dB1;
- private double dYK;
- /**
- * DataSet constructor to initialize all DataSet
- * values with the default 0 value.
- */
- public DataSet() {
- this.dXK = 0;
- this.dSumX = 0;
- this.dSumY = 0;
- this.dSumXY = 0;
- this.dSumX2 = 0;
- this.dSumY2 = 0;
- this.iN = 0;
- this.dXAvg = 0;
- this.dYAvg = 0;
- this.dR = 0;
- this.dR2 = 0;
- this.dB0 = 0;
- this.dB1 = 0;
- this.dYK = 0;
- }
- /**
- * addPair
- *
- * This method adds a pair to the data set, modifying all
- * accumulators in the class, in order to calculate the correlation
- * and the betas.
- *
- * @param dX
- * @param dY
- */
- public void addPair(double dX, double dY) {
- dSumX += dX;
- dSumY += dY;
- dSumXY += dX * dY;
- dSumX2 += Math.pow(dX, 2);
- dSumY2 += Math.pow(dY, 2);
- iN += 1;
- }
- public boolean calculate() {
- if ( iN > 1 ) {
- // calculate dAvgX
- dXAvg = dSumX / iN;
- // calculate dAvgY
- dYAvg = dSumY / iN;
- // calculate dB1
- dB1 = ( dSumXY - iN * dXAvg * dYAvg ) / ( dSumX2 - iN * Math.pow(dXAvg, 2) );
- // calculate dB0
- dB0 = dYAvg - dB1 * dXAvg;
- // calculate dR
- dR = ( iN * dSumXY - dSumX * dSumY ) / Math.sqrt ( ( iN * dSumX2 - Math.pow(dSumX, 2) ) * ( iN * dSumY2 - Math.pow(dSumY, 2) ) );
- // calculate dR2
- dR2 = Math.pow(dR, 2);
- // calculate dYK
- dYK = dB0 + dB1 * dXK;
- return true;
- } else {
- return false;
- }
- }
- /**
- * @return the dXK
- */
- public double getdXK() {
- return dXK;
- }
- /**
- * @param dXK the dXK to set
- */
- public void setdXK(double dXK) {
- this.dXK = dXK;
- }
- /**
- * @return the dSumX
- */
- public double getdSumX() {
- return dSumX;
- }
- /**
- * @param dSumX the dSumX to set
- */
- public void setdSumX(double dSumX) {
- this.dSumX = dSumX;
- }
- /**
- * @return the dSumY
- */
- public double getdSumY() {
- return dSumY;
- }
- /**
- * @param dSumY the dSumY to set
- */
- public void setdSumY(double dSumY) {
- this.dSumY = dSumY;
- }
- /**
- * @return the dSumXY
- */
- public double getdSumXY() {
- return dSumXY;
- }
- /**
- * @param dSumXY the dSumXY to set
- */
- public void setdSumXY(double dSumXY) {
- this.dSumXY = dSumXY;
- }
- /**
- * @return the dSumX2
- */
- public double getdSumX2() {
- return dSumX2;
- }
- /**
- * @param dSumX2 the dSumX2 to set
- */
- public void setdSumX2(double dSumX2) {
- this.dSumX2 = dSumX2;
- }
- /**
- * @return the dSumY2
- */
- public double getdSumY2() {
- return dSumY2;
- }
- /**
- * @param dSumY2 the dSumY2 to set
- */
- public void setdSumY2(double dSumY2) {
- this.dSumY2 = dSumY2;
- }
- /**
- * @return the iN
- */
- public int getiN() {
- return iN;
- }
- /**
- * @param iN the iN to set
- */
- public void setiN(int iN) {
- this.iN = iN;
- }
- /**
- * @return the dXAvg
- */
- public double getdXAvg() {
- return dXAvg;
- }
- /**
- * @param dXAvg the dXAvg to set
- */
- public void setdXAvg(double dXAvg) {
- this.dXAvg = dXAvg;
- }
- /**
- * @return the dYAvg
- */
- public double getdYAvg() {
- return dYAvg;
- }
- /**
- * @param dYAvg the dYAvg to set
- */
- public void setdYAvg(double dYAvg) {
- this.dYAvg = dYAvg;
- }
- /**
- * @return the dR
- */
- public double getdR() {
- return dR;
- }
- /**
- * @param dR the dR to set
- */
- public void setdR(double dR) {
- this.dR = dR;
- }
- /**
- * @return the dR2
- */
- public double getdR2() {
- return dR2;
- }
- /**
- * @param dR2 the dR2 to set
- */
- public void setdR2(double dR2) {
- this.dR2 = dR2;
- }
- /**
- * @return the dB0
- */
- public double getdB0() {
- return dB0;
- }
- /**
- * @param dB0 the dB0 to set
- */
- public void setdB0(double dB0) {
- this.dB0 = dB0;
- }
- /**
- * @return the dB1
- */
- public double getdB1() {
- return dB1;
- }
- /**
- * @param dB1 the dB1 to set
- */
- public void setdB1(double dB1) {
- this.dB1 = dB1;
- }
- /**
- * @return the dYK
- */
- public double getdYK() {
- return dYK;
- }
- /**
- * @param dYK the dYK to set
- */
- public void setdYK(double dYK) {
- this.dYK = dYK;
- }
- public String toString() {
- String sFormat = "";
- return String.format(sFormat);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment