/* In this header file, there are 5 classes.  First is the base class Test, out from which two other
   classes are derived, MeanTest and VarTest.  Then two more classes are derived from MeanTest, which
   are SvarMeanTest and DvarMeanTest.  These classes are used in TestData.C, to do various test on
   an inputed datafile(s).  Further comments are in the section called classes. 
*/

#include <math.h>
#include "SpecialFunction.h"
#include "Moment.h"

class Test {					// Base class

public:
            									               										   
     Test(float* data1,long nset1,float* data2,long nset2):
   
           n1(nset1),n2(nset2),d1(data1),d2(data2),dat1(data1,nset1),dat2(data2,nset2) {}
   							               							               							                 										         
     virtual float Prob() {return 0;}    
     
protected:
 
       float* d1;
       float* d2;
       long n1;
       long n2;
       Moment dat1;
       Moment dat2;
       SpecFunc func;          
};

class MeanTest: public Test {                		// Derived from Test

public:

     MeanTest(float* data1,long nset1,float* data2,long nset2):Test(data1,nset1,data2,nset2) {}

     float Prob();

protected:

     float df;
     float t;     
}; 

class SvarMeanTest: public MeanTest {				// Derived from MeanTest

public:

     SvarMeanTest(float* data1,long nset1,float* data2,long nset2):MeanTest(data1,nset1,data2,nset2) {}

     float Prob();
     
private:

     float poolvar;      
}; 

class DvarMeanTest:public MeanTest {				// Derived from MeanTest 

public:

      DvarMeanTest(float* data1,long nset1,float* data2,long nset2):MeanTest(data1,nset1,data2,nset2) {}

      float Prob();
};      
      
class VarTest: public Test {					// Derived from Test

public:

     VarTest(float* data1,long nset1,float* data2,long nset2):Test(data1,nset1,data2,nset2) {}

     float Prob();
     
private:

     float p;
     float f;
     float df1;
     float df2;     
}; 

// The code to calculate these tests are in "Numerical Recipes in C The Art of Scientific Computing 
// Second Edition"

                  
float MeanTest::Prob() {			// Test for significantly different means.
      
     VarTest V(d1,n1,d2,n2);

     if (V.Prob() < 0.05) {			// Decides whether to use calculation for
					// the same variance or different variance.	
     	 SvarMeanTest same(d1,n1,d2,n2);
     	 return same.Prob();
					// Then uses its derived class accordingly.
     } else {

     	 DvarMeanTest diff(d1,n1,d2,n2);
     	 return diff.Prob();
     }     
}

float SvarMeanTest::Prob() {    		// In "Numerical Recipes" Ch 14-1 p.616   
					// Calculates the probability for a 
					// significantly different mean, here for
					// case of having the same variance

     df=n1+n2-2;					     
     poolvar=((n1-1)*dat1.Var()+(n2-1)*dat2.Var())/df;	     

     t=(dat1.Mean()-dat2.Mean())/sqrt(poolvar*(1.0/n1 + 1.0/n2));

     return (1.0 - func.Betai(0.5*df,0.5,df/(df+t*t)));
}

float DvarMeanTest::Prob() {			// In "Numerical Recipes" Ch 14-1 p.617
					// Calculates the probability for a 
					// significantly different mean, here for
					// case of having different variance
					
     t=(dat1.Mean()-dat2.Mean())/sqrt(dat1.Var()/n1 + dat2.Var()/n2);

     df=func.Sqr(dat1.Var()/n1 + dat2.Var()/n2)/(func.Sqr(dat1.Var()/n1)/(n1-1)
        + func.Sqr(dat2.Var()/n2)/(n2-1));

     return (1 - func.Betai(0.5*df,0.5,df/(df + t*t)));     
}

float VarTest::Prob() {			// In "Numerical Recipes" Ch 14-1 p.613
					// Calculates the probability for a 
					// significantly different variance.

     if (dat1.Var() > dat2.Var()) {
     
          f=dat1.Var()/dat2.Var();
          df1=n1-1;
          df2=n2-1;
          
     } else {
          
          f=dat2.Var()/dat1.Var();
          df1=n2-1;
          df2=n1-1;
     }

     p= 2.0*func.Betai(0.5*df2,0.5*df1,df2/(df2 + df1*f));
     if (p > 1.0) p=2.0 - p; 
     return (1.0 - p);
}

	
	
	

          
    
     













     

     
          
          
     
     
     
