예제 #1
0
void ks2d1s(float x1[], float y1[], unsigned long n1,
	void (*quadvl)(float, float, float *, float *, float *, float *),
	float *d1, float *prob)
{
	void pearsn(float x[], float y[], unsigned long n, float *r, float *prob,
		float *z);
	float probks(float alam);
	void quadct(float x, float y, float xx[], float yy[], unsigned long nn,
		float *fa, float *fb, float *fc, float *fd);
	unsigned long j;
	float dum,dumm,fa,fb,fc,fd,ga,gb,gc,gd,r1,rr,sqen;

	*d1=0.0;
	for (j=1;j<=n1;j++) {
		quadct(x1[j],y1[j],x1,y1,n1,&fa,&fb,&fc,&fd);
		(*quadvl)(x1[j],y1[j],&ga,&gb,&gc,&gd);
		*d1=FMAX(*d1,fabs(fa-ga));
		*d1=FMAX(*d1,fabs(fb-gb));
		*d1=FMAX(*d1,fabs(fc-gc));
		*d1=FMAX(*d1,fabs(fd-gd));
	}
	pearsn(x1,y1,n1,&r1,&dum,&dumm);
	sqen=sqrt((double)n1);
	rr=sqrt(1.0-r1*r1);
	*prob=probks(*d1*sqen/(1.0+rr*(0.25-0.75/sqen)));
}
예제 #2
0
void ks2d2s(float x1[], float y1[], unsigned long n1, float x2[], float y2[],
	unsigned long n2, float *d, float *prob)
{
	void pearsn(float x[], float y[], unsigned long n, float *r, float *prob,
		float *z);
	float probks(float alam);
	void quadct(float x, float y, float xx[], float yy[], unsigned long nn,
		float *fa, float *fb, float *fc, float *fd);
	unsigned long j;
	float d1,d2,dum,dumm,fa,fb,fc,fd,ga,gb,gc,gd,r1,r2,rr,sqen;

	d1=0.0;
	for (j=1;j<=n1;j++) {
		quadct(x1[j],y1[j],x1,y1,n1,&fa,&fb,&fc,&fd);
		quadct(x1[j],y1[j],x2,y2,n2,&ga,&gb,&gc,&gd);
		d1=FMAX(d1,fabs(fa-ga));
		d1=FMAX(d1,fabs(fb-gb));
		d1=FMAX(d1,fabs(fc-gc));
		d1=FMAX(d1,fabs(fd-gd));
	}
	d2=0.0;
	for (j=1;j<=n2;j++) {
		quadct(x2[j],y2[j],x1,y1,n1,&fa,&fb,&fc,&fd);
		quadct(x2[j],y2[j],x2,y2,n2,&ga,&gb,&gc,&gd);
		d2=FMAX(d2,fabs(fa-ga));
		d2=FMAX(d2,fabs(fb-gb));
		d2=FMAX(d2,fabs(fc-gc));
		d2=FMAX(d2,fabs(fd-gd));
	}
	*d=0.5*(d1+d2);
	sqen=sqrt(n1*n2/(double)(n1+n2));
	pearsn(x1,y1,n1,&r1,&dum,&dumm);
	pearsn(x2,y2,n2,&r2,&dum,&dumm);
	rr=sqrt(1.0-0.5*(r1*r1+r2*r2));
	*prob=probks(*d*sqen/(1.0+rr*(0.25-0.75/sqen)));
}
예제 #3
0
do_line()
{
    real *x, *y, *dx, *dy, *dz;
    int i,j, mwt;
    real chi2,q,siga,sigb, sigma, d, sa, sb;
    real cov00, cov01, cov11, sumsq;
    real r, prob, z;
    void fit(), pearsn();

    if (nxcol < 1) error("nxcol=%d",nxcol);
    if (nycol < 1) error("nycol=%d",nycol);
    x = xcol[0].dat;
    y = ycol[0].dat;
    dx = (dxcolnr>0 ? dxcol.dat : NULL);
    dy = (dycolnr>0 ? dycol.dat : NULL);

#if HAVE_GSL
    if (dx) error("Cannot use GSL with errors in X column");
    gsl_fit_linear (x, 1, y, 1, npt, &b, &a, 
		    &cov00, &cov01, &cov11, &sumsq);
    printf("y=ax+b:  a=%g b=%g  cov00,01,11=%g %g %g   sumsq=%g\n",
	     a,b, cov00,cov01,cov11, sumsq);

    if (dy) {
      for (i=0; i<npt; i++)
	dy[i] = 1/(dy[i]*dy[i]);
      gsl_fit_wlinear (x, 1, dy, 1, y, 1, npt, &b, &a, 
		    &cov00, &cov01, &cov11, &chi2);
      printf("y=ax+b:  a=%g b=%g  cov00,01,11=%g %g %g   chi^2=%g\n",
	     a,b, cov00,cov01,cov11, chi2);

    }
#else

    if (dx) {
#if defined(TESTNR)
      warning("new FITEXY method");
      dz = (real *) allocate(npt*sizeof(real));
      for (i=0; i<npt; i++) dz[i] = 0.0;

      fitexy(x,y,npt,dx,dy,&b,&a,&sigb,&siga,&chi2,&q);
      printf("fitexy(x,y,dx,dy)    a=%g   b=%g  %g %g\n",b,a,sigb,siga);
      fitexy(x,y,npt,dz,dy,&a,&b,&siga,&sigb,&chi2,&q);
      printf("fitexy(x,y, 0,dy)    a=%g   b=%g  %g %g\n",a,b,siga,sigb);
      fitexy(x,y,npt,dx,dz,&a,&b,&siga,&sigb,&chi2,&q);
      printf("fitexy(x,y,dx, 0)    a=%g   b=%g  %g %g\n",a,b,siga,sigb);
      fit   (x,y,npt,dy, 1,&a,&b,&siga,&sigb,&chi2,&q);
      printf("fit   (x,y, 0,dy)    a=%g   b=%g  %g %g\n",a,b,siga,sigb);
      fit   (y,x,npt,dx, 1,&a,&b,&siga,&sigb,&chi2,&q);
      printf("fit   (y,x, 0,dx)    a=%g   b=%g  %g %g\n",a,b,siga,sigb);

      sa=sqrt(siga*siga+sqr(sigb*(a/b)))/b;
      sb=sigb/(b*b);
      printf("FITEXY: %11.6f %11.6f %11.6f %11.6f %11.6f %11.6f\n",
	     -a/b,1.0/b,sa,sb,chi2,q); 
#elif defined(TESTMP)
      struct vars_struct v;      /* private data for mpfit() */
      int status;
      mp_result result;
      double p[2] = {1.0, 1.0};
      double perror[2];

      warning("MPFIT method; mode mpfit=%d",mpfit_mode);

      bzero(&result,sizeof(result));
      result.xerror = perror;

      v.x = xcol[0].dat;
      v.y = ycol[0].dat;
      v.ex = dxcol.dat;
      v.ey = dycol.dat;
      v.mode = mpfit_mode;
      
      status = mpfit(linfitex, npt, 2, p, 0, 0, (void *) &v, &result);
      dprintf(1,"*** mpfit status = %d\n", status);

      printf("  CHI-SQUARE = %f    (%d DOF)\n", 
	     result.bestnorm, result.nfunc-result.nfree);
      printf("        NPAR = %d\n", result.npar);
      printf("       NFREE = %d\n", result.nfree);
      printf("     NPEGGED = %d\n", result.npegged);
      printf("       NITER = %d\n", result.niter);
      printf("        NFEV = %d\n", result.nfev);
      printf("\n");
      for (i=0; i<result.npar; i++) {
	printf("  P[%d] = %f +/- %f\n", 
	       i, p[i], result.xerror[i]);
      }
#else
      error("no dycol= implemented yet");
#endif
    } else {

      for(mwt=0;mwt<=1;mwt++) {

#if 1
	if (mwt>0 && nsigma>0) {
	  fit(x,y,npt,dy,0,&b,&a,&sigb,&siga,&chi2,&q);
	} else {
	  if (mwt>0 && dycolnr==0)
	    continue;             
	  fit(x,y,npt,dy,mwt,&b,&a,&sigb,&siga,&chi2,&q);
	}
#else
	if (mwt==0)
	  fit(x,y,npt,dy,mwt,&b,&a,&sigb,&siga,&chi2,&q);
	else
	  myfit(x,y,npt,dy,mwt,&b,&a,&sigb,&siga,&chi2,&q);
#endif
	dprintf(2,"\n");
	dprintf (1,"Fit:   y=ax+b\n");
	if (mwt == 0)
	  dprintf(1,"Ignoring standard deviations\n");
	else
	  dprintf(1,"Including standard deviation\n");
	printf("%12s %9.6f %18s %9.6f \n",
	       "a  =  ",a,"uncertainty:",siga);
	printf("%12s %9.6f %18s %9.6f \n",
	       "b  =  ",b,"uncertainty:",sigb);

	printf("%12s %9.6f %18s %9.6f \n",
	       "x0 =  ",-b/a,"uncertainty:",sqrt(sqr(sigb/a)+sqr(siga*b/(a*a))));
	printf("%12s %9.6f %18s %9.6f \n",
	       "y0 =  ",b,"uncertainty:",sigb);

	printf("%19s %14.6f \n","chi-squared: ",chi2);
	printf("%23s %10.6f %s\n","goodness-of-fit: ",q,
	       q==1 ? "(no Y errors supplied [dycol=])" : "");
      
	pearsn(x, y, npt, &r, &prob, &z);
      
	printf("%9s %g\n","r: ",r);
	printf("%12s %g\n","prob: ",prob);
	printf("%9s %g\n","z: ",z);
      
	if (mwt==0 && nsigma>0) {                
	  sigma = 0.0;
	  for(i=0; i<npt; i++)
	    sigma += sqr(y[i] - a*x[i] - b);
	  sigma /= (real) npt;
	  sigma = nsigma * sqrt(sigma);   /* critical distance */
	
	  for(i=0, j=0; i<npt; i++) {     /* loop over points */
	    d = ABS(y[i] - a*x[i] - b);
	    if (d > sigma) continue;
	    x[j] = x[i];                  /* shift them over */
	    y[j] = y[i];
	    if (dy) dy[j] = dy[i];
	    j++;
	  }
	  dprintf(0,"%d/%d points outside %g*sigma (%g)\n",
		  npt-j,npt,nsigma,sigma);
	  npt = j;
	}
      } /* mwt */
    } /* dxcol */
    
    if (outstr) write_data(outstr);
#endif
}