Esempio n. 1
0
File: Test.cpp Progetto: stites/CSSR
//chstwo ---- calculates chi-square for two distributions
// After Numerical Recipes in C
void Test::chstwo(double bins1[], int n1, double bins2[], int n2, int nbins,
                  int knstrn, float *df, float *chsq, double *prob) {
  int j;
  float temp;

  *df = nbins - knstrn;
  *chsq = 0.0;
  float ratio1;
  float ratio2;

  if (n1 > 0) {
    ratio1 = (float) (((float) sqrt(n2)) / ((float) sqrt(n1)));
  }
  else {
    ratio1 = 0.0;
  }

  if (n2 > 0) {
    ratio2 = (float) (((float) sqrt(n1)) / ((float) sqrt(n2)));
  }
  else {
    ratio2 = 0.0;
  }

  for (j = 0; j < nbins; j++) {
    if (bins1[j] == 0.0 && bins2[j] == 0.0) {
      --(*df);                //No data means one less degree of freedom
    }
    else {
      temp = ratio1 * bins1[j] * n1 - ratio2 * bins2[j] * n2;
      *chsq += temp * temp / (bins1[j] * n1 + bins2[j] * n2);
    }
  }
  *prob = gammq(0.5 * (*df), 0.5 * (*chsq));
}
Esempio n. 2
0
float erffc(float x)
{
	float gammp(float a, float x);
	float gammq(float a, float x);

	return x < 0.0 ? 1.0+gammp(0.5,x*x) : gammq(0.5,x*x);
}
Esempio n. 3
0
File: fit.c Progetto: gnovak/bin
void fit(float x[], float y[], int ndata, float sig[], int mwt, float *a,
	float *b, float *siga, float *sigb, float *chi2, float *q)
{
	float gammq(float a, float x);
	int i;
	float wt,t,sxoss,sx=0.0,sy=0.0,st2=0.0,ss,sigdat;

	*b=0.0;
	if (mwt) {
		ss=0.0;
		for (i=1;i<=ndata;i++) {
			wt=1.0/SQR(sig[i]);
			ss += wt;
			sx += x[i]*wt;
			sy += y[i]*wt;
		}
	} else {
		for (i=1;i<=ndata;i++) {
			sx += x[i];
			sy += y[i];
		}
		ss=ndata;
	}
	sxoss=sx/ss;
	if (mwt) {
		for (i=1;i<=ndata;i++) {
			t=(x[i]-sxoss)/sig[i];
			st2 += t*t;
			*b += t*y[i]/sig[i];
		}
	} else {
		for (i=1;i<=ndata;i++) {
			t=x[i]-sxoss;
			st2 += t*t;
			*b += t*y[i];
		}
	}
	*b /= st2;
	*a=(sy-sx*(*b))/ss;
	*siga=sqrt((1.0+sx*sx/(ss*st2))/ss);
	*sigb=sqrt(1.0/st2);
	*chi2=0.0;
	if (mwt == 0) {
		for (i=1;i<=ndata;i++)
			*chi2 += SQR(y[i]-(*a)-(*b)*x[i]);
		*q=1.0;
		sigdat=sqrt((*chi2)/(ndata-2));
		*siga *= sigdat;
		*sigb *= sigdat;
	} else {
		for (i=1;i<=ndata;i++)
			*chi2 += SQR((y[i]-(*a)-(*b)*x[i])/sig[i]);
		*q=gammq(0.5*(ndata-2),0.5*(*chi2));
	}
}
Esempio n. 4
0
void fit(real *x, real *y,int ndata,real *sig,int mwt,
	 real *a, real *b,real *siga,real *sigb,real *chi2,real *q)
{
  int i;
  real wt,t,sxoss,sx=0.0,sy=0.0,st2=0.0,ss,sigdat;

  *b=0.0;
  if (mwt) {
    ss=0.0;
    for (i=0;i<ndata;i++) {
      wt=1.0/sqr(sig[i]);
      ss += wt;
      sx += x[i]*wt;
      sy += y[i]*wt;
    }
  } else {
    for (i=0;i<ndata;i++) {
      sx += x[i];
      sy += y[i];
    }
    ss=ndata;
  }
  sxoss=sx/ss;
  if (mwt) {
    for (i=0;i<ndata;i++) {
      t=(x[i]-sxoss)/sig[i];
      st2 += t*t;
      *b += t*y[i]/sig[i];
    }
  } else {
    for (i=0;i<ndata;i++) {
      t=x[i]-sxoss;
      st2 += t*t;
      *b += t*y[i];
    }
  }
  *b /= st2;
  *a=(sy-sx*(*b))/ss;
  *siga=sqrt((1.0+sx*sx/(ss*st2))/ss);
  *sigb=sqrt(1.0/st2);
  *chi2=0.0;
  if (mwt == 0) {
    for (i=0;i<ndata;i++)
      *chi2 += sqr(y[i]-(*a)-(*b)*x[i]);
    *q=1.0;
    sigdat=sqrt((*chi2)/(ndata-2));
    *siga *= sigdat;
    *sigb *= sigdat;
  } else {
    for (i=0;i<ndata;i++)
      *chi2 += sqr((y[i]-(*a)-(*b)*x[i])/sig[i]);
    *q=gammq(0.5*(ndata-2),0.5*(*chi2));	
  }
}
Esempio n. 5
0
void AssocChi::Calc(int ** nn, int ni, int nj)
   {
   int nnj, nni, j, i, minij;
   double expected, temp;
   Vector sumi(ni), sumj(nj);

   sumi.Zero();
   sumj.Zero();
   sum = 0.0;

   nni = ni;
   nnj = nj;

   // Get the row totals
   for (i = 0; i < ni; i++)
      {
      for ( j = 0; j < nj; j++)
         {
         sumi[i] += nn[i][j];
         sum += nn[i][j];
         }
      if ( sumi[i] < FPMIN) --nni;            // eliminate zero rows by reducing number
      }

   // Get the column totals
   for (j = 0; j < nj; j++)
      {
      for ( i = 0; i < ni; i++ )
         sumj[j] += nn[i][j];
      if ( sumj[j] < FPMIN) --nnj;        // eliminated any zero columns
      }

   df = nni * nnj - nni - nnj + 1;           // corrected degrees of freedom

   chisq = 0.0;

   for (i = 0; i < ni; i++)
      {
      for (j = 0; j < nj; j++)
         {
         expected = sumj[j] * sumi[i] / sum;
         temp = nn[i][j] - expected;
         chisq += temp * temp / (expected + TINY);
         }
      }

   prob = df ? gammq ( 0.5 * df, 0.5 * chisq ) : 1.0;
   lop = prob > 1e-100 ? -log10(prob) : 99.999;
   minij = nni < nnj ? nni - 1 : nnj - 1;
   cramrv = minij ? sqrt ( chisq / ( sum * minij ) ) : 0.0;
   ccc = sum ? sqrt ( chisq / ( chisq + sum) ) : 0.0;

   isValid = 1;
   }
Esempio n. 6
0
//Returns the complementary error function erfc(x).
double erffc(double x)
{
    double retval;
   
    if ( x < 0.0 ) {
	retval = gammp(0.5,x*x);
	return 1+ retval;
    } else {
	gammq(0.5,x*x, & retval);
	return retval;
    }
}
Esempio n. 7
0
/**
 * Convert a chi^2 value with a given number of degrees of freedom to a p-value.  
 * 
 * @author Matt Steigert
 * @param x2val The chi^2 distributed statistic
 * @param df Degrees of freedom
 * @return Value of chi2(x2val, df)
 * 
 * @throw InvalidChiSquareException() If df < 1 or x2val < 0.0
 */
double Statistics::chi2prob(double x2val, double df)
{
   if(df < 1){
      throw InvalidChiSquareException();
      cerr << "Chi Square Value = " << x2val;
   }
   if(x2val < 0.0){
      throw InvalidChiSquareException();
      cerr << "Degrees of Freedom = " << df;
   }
   
   double pval;
   pval = gammq(df/2.0, x2val/2.0);

   return pval;
}
Esempio n. 8
0
void chsone(double bins[], double ebins[], int nbins, int knstrn, double *df,
	double *chsq, double *prob)
{
	double gammq(double a, double x);
	int j;
	double temp;

	*df=nbins-knstrn;
	*chsq=0.0;
	for (j=1;j<=nbins;j++) {
		if (ebins[j] <= 0.0) my_error("Bad expected number in chsone");
		temp=bins[j]-ebins[j];
		*chsq += temp*temp/ebins[j];
	}
	*prob=gammq(0.5*(*df),0.5*(*chsq));
}
Esempio n. 9
0
void dchstwo(double bins1[], double bins2[], int nbins, int knstrn,
	double *df, double *chsq, double *prob)
{
 int j;
 double temp;
 
 *df=nbins-knstrn;
 *chsq=0.0;
 for (j=0;j<nbins;j++)
    if (bins1[j] == 0.0 && bins2[j] == 0.0)
       --(*df);
    else {
       temp = bins1[j]-bins2[j];
       *chsq += temp*temp/(bins1[j]+bins2[j]);
    }
 *prob=gammq(0.5*(*df),0.5*(*chsq));
}
Esempio n. 10
0
void chsone(float bins[], float ebins[], int nbins, int knstrn, float *df,
	float *chsq, float *prob)
{
	float gammq(float a, float x);
	void nrerror(char error_text[]);
	int j;
	float temp;

	*df=nbins-knstrn;
	*chsq=0.0;
	for (j=1;j<=nbins;j++) {
		if (ebins[j] <= 0.0) nrerror("Bad expected number in chsone");
		temp=bins[j]-ebins[j];
		*chsq += temp*temp/ebins[j];
	}
	*prob=gammq(0.5*(*df),0.5*(*chsq));
}
Esempio n. 11
0
void chstwo(float bins1[], float bins2[], int nbins, int knstrn, float *df,
	float *chsq, float *prob)
{
	float gammq(float a, float x);
	int j;
	float temp;

	*df=nbins-knstrn;
	*chsq=0.0;
	for (j=1;j<=nbins;j++)
		if (bins1[j] == 0.0 && bins2[j] == 0.0)
			--(*df);
		else {
			temp=bins1[j]-bins2[j];
			*chsq += temp*temp/(bins1[j]+bins2[j]);
		}
	*prob=gammq(0.5*(*df),0.5*(*chsq));
}
Esempio n. 12
0
void NR::chstwo(Vec_I_DP &bins1, Vec_I_DP &bins2, const int knstrn, DP &df,
	DP &chsq, DP &prob)
{
	int j;
	DP temp;

	int nbins=bins1.size();
	df=nbins-knstrn;
	chsq=0.0;
	for (j=0;j<nbins;j++)
		if (bins1[j] == 0.0 && bins2[j] == 0.0)
			--df;
		else {
			temp=bins1[j]-bins2[j];
			chsq += temp*temp/(bins1[j]+bins2[j]);
		}
	prob=gammq(0.5*df,0.5*chsq);
}
Esempio n. 13
0
void chstwo(double bins1[], double bins2[], int nbins, int knstrn, double *df,
	double *chsq, double *prob)
{
	double gammq(double a, double x);
	int j;
	double temp;

	*df=nbins-knstrn;
	*chsq=0.0;
	for (j=1;j<=nbins;j++)
		if (bins1[j] == 0.0 && bins2[j] == 0.0)
			--(*df);
		else {
			temp=bins1[j]-bins2[j];
			*chsq += temp*temp/(bins1[j]+bins2[j]);
			//printf("%lf %lf\n",bins1[j],bins2[j]);
		}
	*prob=gammq(0.5*(*df),0.5*(*chsq));
}
Esempio n. 14
0
int chisquare ( double * array1, double * array2,
		int arrlength,  double *df, double *chsq, double *prob) {

    /* normalize distributions */

    int gammq(double a, double x, double * retval);
    int j;
    double total1, total2;
    double temp;
    double prefactor1, prefactor2;
    
    total1 = 0;
    total2 = 0;
    for (j=0; j < arrlength; j++) {
	total1 +=  array1[j];
	total2 +=  array2[j];
	printf ( " ** %2d  %8.3le   %8.3le  \n",
		 j, array1[j], array2[j]);
    }

    prefactor1 = sqrt (total2/total1);
    prefactor2 = 1.0/prefactor1;
  
    *df = arrlength;
    *chsq = 0.0;
    for (j=0; j < arrlength;j++) {
	if (array1[j] == 0.0 && array2[j] == 0.0) {
	    --(*df);   
	} else {  
	    temp =prefactor1* array1[j] - prefactor2*array2[j];
	    *chsq += temp*temp/(array1[j]+array2[j]);
	}
    }
    if ( gammq(0.5*(*df),0.5*(*chsq), prob) ) return 1;
    

    printf ( "AAA  %8.0lf  %8.1le     %8.1le \n", *df, *chsq, *prob);
    

    return 0;
}
Esempio n. 15
0
void cntab1(int **nn, int ni, int nj, float *chisq, float *df, float *prob,
	float *cramrv, float *ccc)
{
	float gammq(float a, float x);
	int nnj,nni,j,i,minij;
	float sum=0.0,expctd,*sumi,*sumj,temp;

	sumi=vector(1,ni);
	sumj=vector(1,nj);
	nni=ni;
	nnj=nj;
	for (i=1;i<=ni;i++) {
		sumi[i]=0.0;
		for (j=1;j<=nj;j++) {
			sumi[i] += nn[i][j];
			sum += nn[i][j];
		}
		if (sumi[i] == 0.0) --nni;
	}
	for (j=1;j<=nj;j++) {
		sumj[j]=0.0;
		for (i=1;i<=ni;i++) sumj[j] += nn[i][j];
		if (sumj[j] == 0.0) --nnj;
	}
	*df=nni*nnj-nni-nnj+1;
	*chisq=0.0;
	for (i=1;i<=ni;i++) {
		for (j=1;j<=nj;j++) {
			expctd=sumj[j]*sumi[i]/sum;
			temp=nn[i][j]-expctd;
			*chisq += temp*temp/(expctd+TINY);
		}
	}
	*prob=gammq(0.5*(*df),0.5*(*chisq));
	minij = nni < nnj ? nni-1 : nnj-1;
	*cramrv=sqrt(*chisq/(sum*minij));
	*ccc=sqrt(*chisq/(*chisq+sum));
	free_vector(sumj,1,nj);
	free_vector(sumi,1,ni);
}
Esempio n. 16
0
int chisquare ( int * population_1, int * population_2, int no_bins, int kstr, double *df, double *chsq, double *prob) {

    /* normalize distributions */

    int gammq(double a, double x, double * retval);
    int j;
    double total_1, total_2;
    double temp;
    double p_prefactor, n_prefactor;
    
    total_1 = 0;
    total_2 = 0;
    for (j=0; j < no_bins;j++) {
	total_1 +=  population_1[j];
	total_2 +=  population_2[j];
    }

    p_prefactor = sqrt (total_2/total_1);
    n_prefactor = 1.0/p_prefactor;
  
    *df = no_bins - kstr;
    *chsq = 0.0;
    for (j=0; j < no_bins;j++) {
	if (population_1[j] == 0.0 && population_2[j] == 0.0) {
	    --(*df);   
	} else {  
	    temp   = p_prefactor* population_1[j] - n_prefactor*population_2[j];
	    *chsq += temp*temp/(population_1[j]+population_2[j]);
	}
    }
    if ( gammq(0.5*(*df),0.5*(*chsq), prob) ) return 1;
    

    //printf ( " %8.1le  %8.1le     %8.1le \n", *df, *chsq, *prob);


    return 0;
}
Esempio n. 17
0
double chival2prob(int dgf, double val){
	return gammq((double)dgf/(double)2,val/(double)2);
}
Esempio n. 18
0
void fitexy(float x[], float y[], int ndat, float sigx[], float sigy[],
	float *a, float *b, float *siga, float *sigb, float *chi2, float *q)
{
	void avevar(float data[], unsigned long n, float *ave, float *var);
	float brent(float ax, float bx, float cx,
		float (*f)(float), float tol, float *xmin);
	float chixy(float bang);
	void fit(float x[], float y[], int ndata, float sig[], int mwt,
		float *a, float *b, float *siga, float *sigb, float *chi2, float *q);
	float gammq(float a, float x);
	void mnbrak(float *ax, float *bx, float *cx, float *fa, float *fb,
		float *fc, float (*func)(float));
	float zbrent(float (*func)(float), float x1, float x2, float tol);
	int j;
	float swap,amx,amn,varx,vary,ang[7],ch[7],scale,bmn,bmx,d1,d2,r2,
		dum1,dum2,dum3,dum4,dum5;

	xx=vector(1,ndat);
	yy=vector(1,ndat);
	sx=vector(1,ndat);
	sy=vector(1,ndat);
	ww=vector(1,ndat);
	avevar(x,ndat,&dum1,&varx);
	avevar(y,ndat,&dum1,&vary);
	scale=sqrt(varx/vary);
	nn=ndat;
	for (j=1;j<=ndat;j++) {
		xx[j]=x[j];
		yy[j]=y[j]*scale;
		sx[j]=sigx[j];
		sy[j]=sigy[j]*scale;
		ww[j]=sqrt(SQR(sx[j])+SQR(sy[j]));
	}
	fit(xx,yy,nn,ww,1,&dum1,b,&dum2,&dum3,&dum4,&dum5);
	offs=ang[1]=0.0;
	ang[2]=atan(*b);
	ang[4]=0.0;
	ang[5]=ang[2];
	ang[6]=POTN;
	for (j=4;j<=6;j++) ch[j]=chixy(ang[j]);
	mnbrak(&ang[1],&ang[2],&ang[3],&ch[1],&ch[2],&ch[3],chixy);
	*chi2=brent(ang[1],ang[2],ang[3],chixy,ACC,b);
	*chi2=chixy(*b);
	*a=aa;
	*q=gammq(0.5*(nn-2),*chi2*0.5);
	for (r2=0.0,j=1;j<=nn;j++) r2 += ww[j];
	r2=1.0/r2;
	bmx=BIG;
	bmn=BIG;
	offs=(*chi2)+1.0;
	for (j=1;j<=6;j++) {
		if (ch[j] > offs) {
			d1=fabs(ang[j]-(*b));
			while (d1 >= PI) d1 -= PI;
			d2=PI-d1;
			if (ang[j] < *b) {
				swap=d1;
				d1=d2;
				d2=swap;
			}
			if (d1 < bmx) bmx=d1;
			if (d2 < bmn) bmn=d2;
		}
	}
	if (bmx < BIG) {
		bmx=zbrent(chixy,*b,*b+bmx,ACC)-(*b);
		amx=aa-(*a);
		bmn=zbrent(chixy,*b,*b-bmn,ACC)-(*b);
		amn=aa-(*a);
		*sigb=sqrt(0.5*(bmx*bmx+bmn*bmn))/(scale*SQR(cos(*b)));
		*siga=sqrt(0.5*(amx*amx+amn*amn)+r2)/scale;
	} else (*sigb)=(*siga)=BIG;
	*a /= scale;
	*b=tan(*b)/scale;
	free_vector(ww,1,ndat);
	free_vector(sy,1,ndat);
	free_vector(sx,1,ndat);
	free_vector(yy,1,ndat);
	free_vector(xx,1,ndat);
}
Esempio n. 19
0
DP NR::erffc(const DP x)
{
	return x < 0.0 ? 1.0+gammp(0.5,x*x) : gammq(0.5,x*x);
}
 REAL_TYPE erff(REAL_TYPE x)
 {
     return x<0.0?1.0+gammp(0.5,x*x):gammq(0.5,x*x);
 }
Esempio n. 21
0
Scalar erfc(Scalar x)
{
//	Scalar gammp(),gammq();

	return x < 0.0 ? 1.0+gammp(0.5,x*x) : gammq(0.5,x*x);
}
Esempio n. 22
0
double erffc(double x)
   {
   return x < 0.0 ? 1.0 + gammp(0.5, x*x) : gammq(0.5, x*x);
   }
Esempio n. 23
0
// The chi-squared distribution
//
double chidist(double x, double v)
   { return gammq (0.5 * v, 0.5 * x); }