void LinearFit::fit()
{
    if (d_init_err)
        return;

	if (d_p > d_n){
  		QMessageBox::critical((ApplicationWindow *)parent(), tr("QtiPlot - Fit Error"),
  	    tr("You need at least %1 data points for this fit operation. Operation aborted!").arg(d_p));
  		return;
  	}

	double c0, c1, cov00, cov01, cov11;
	if (d_weighting == NoWeighting)
		gsl_fit_linear(d_x, 1, d_y, 1, d_n, &c0, &c1, &cov00, &cov01, &cov11, &chi_2);
	else
		gsl_fit_wlinear(d_x, 1, d_w, 1, d_y, 1, d_n, &c0, &c1, &cov00, &cov01, &cov11, &chi_2);

	d_results[0] = c0;
	d_results[1] = c1;

	gsl_matrix_set(covar, 0, 0, cov00);
	gsl_matrix_set(covar, 0, 1, cov01);
	gsl_matrix_set(covar, 1, 1, cov11);
	gsl_matrix_set(covar, 1, 0, cov01);

	generateFitCurve();

	ApplicationWindow *app = (ApplicationWindow *)parent();
	if (app->writeFitResultsToLog)
		app->updateLog(logFitInfo(0, 0));
}
Exemple #2
0
void least_squares_straightline_fit(void) {
	int i, n = 4;
	double x[4] = { 1970, 1980, 1990, 2000 };
	double y[4] = { 12, 11, 14, 13 };
	double w[4] = { 0.1, 0.2, 0.3, 0.4 };

	double c0, c1, cov00, cov01, cov11, chisq;

	gsl_fit_wlinear(x, 1, w, 1, y, 1, n, &c0, &c1, &cov00, &cov01, &cov11,
			&chisq);

	printf("# best fit: Y = %g + %g X\n", c0, c1);
	printf("# covariance matrix:\n");
	printf("# [ %g, %g\n#   %g, %g]\n", cov00, cov01, cov01, cov11);
	printf("# chisq = %g\n", chisq);

	for (i = 0; i < n; i++)
		printf("data: %g %g %g\n", x[i], y[i], 1 / sqrt(w[i]));

	printf("\n");

	for (i = -30; i < 130; i++) {
		double xf = x[0] + (i / 100.0) * (x[n - 1] - x[0]);
		double yf, yf_err;

		gsl_fit_linear_est(xf, c0, c1, cov00, cov01, cov11, &yf, &yf_err);

		printf("fit: %g %g\n", xf, yf);
		printf("hi : %g %g\n", xf, yf + yf_err);
		printf("lo : %g %g\n", xf, yf - yf_err);
	}
}
Exemple #3
0
CAMLprim value ml_gsl_fit_linear(value wo, value x, value y)
{
  value r;
  size_t N=Double_array_length(x);
  double c0,c1,cov00,cov01,cov11,sumsq;

  if(Double_array_length(y) != N)
    GSL_ERROR("array sizes differ", GSL_EBADLEN);

  if(wo == Val_none)
    gsl_fit_linear(Double_array_val(x), 1, 
		   Double_array_val(y), 1, N,
		   &c0, &c1, &cov00, &cov01, &cov11, &sumsq);
  else {
    value w=Field(wo, 0);
    if(Double_array_length(w) != N)
      GSL_ERROR("array sizes differ", GSL_EBADLEN);
    gsl_fit_wlinear(Double_array_val(x), 1, 
		    Double_array_val(w), 1,
		    Double_array_val(y), 1, N,
		    &c0, &c1, &cov00, &cov01, &cov11, &sumsq);
  }
  r=alloc_small(6 * Double_wosize, Double_array_tag);
  Store_double_field(r, 0, c0);
  Store_double_field(r, 1, c1);
  Store_double_field(r, 2, cov00);
  Store_double_field(r, 3, cov01);
  Store_double_field(r, 4, cov11);
  Store_double_field(r, 5, sumsq);
  return r;
}
Exemple #4
0
/* linear fit with weights: y = c0 + c1 x */
static VALUE rb_gsl_fit_wlinear(int argc, VALUE *argv, VALUE obj)
{
  double *ptrx, *ptry, *ptrw;
  double c0, c1, cov00, cov01, cov11, sumsq;
  int status;
  size_t n, stridex, stridey, stridew;
  switch (argc) {
  case 3:
    ptrx = get_vector_ptr(argv[0], &stridex, &n);
    ptrw = get_vector_ptr(argv[1], &stridew, &n);
    ptry = get_vector_ptr(argv[2], &stridey, &n);
    break;
  case 4:   
    CHECK_FIXNUM(argv[3]);
    ptrx = get_vector_ptr(argv[0], &stridex, &n);
    ptrw = get_vector_ptr(argv[1], &stridew, &n);
    ptry = get_vector_ptr(argv[2], &stridey, &n);
    n = FIX2INT(argv[3]);
    break;
  default:
    rb_raise(rb_eArgError, "wrong number of arguments (%d for 2 or 3)", argc);
    break;
  }
  status = gsl_fit_wlinear(ptrx, stridex, ptrw, stridew, ptry, stridey,
			   n,
			   &c0, &c1, &cov00, &cov01, &cov11, &sumsq);
  return rb_ary_new3(7, rb_float_new(c0), rb_float_new(c1), rb_float_new(cov00),
		     rb_float_new(cov01), rb_float_new(cov11), rb_float_new(sumsq),
		     INT2FIX(status));
}
Exemple #5
0
pure_expr* wrap_gsl_fit_wlinear(double* x, double* w, double* y, size_t n)
{
  double c0, c1, cov00, cov01, cov11, chisq;
  
  gsl_fit_wlinear(x, 1, w, 1, y, 1, n, &c0, &c1, &cov00, &cov01, &cov11, 
		  &chisq);
  return pure_listl(6, pure_double(c0), pure_double(c1), pure_double(cov00),
		    pure_double(cov01), pure_double(cov11), 
		    pure_double(chisq));
}
Exemple #6
0
int
main (void)
{


  double x[1000], y[1000], w[1000];

  size_t xstride = 2, wstride = 3, ystride = 5;
  size_t i;

  for (i = 0; i < norris_n; i++) 
    {
      x[i*xstride] = norris_x[i];
      w[i*wstride] = 1.0;
      y[i*ystride] = norris_y[i];
    }

  gsl_ieee_env_setup();

  {
    double c0, c1, cov00, cov01, cov11, sumsq;
       
    double expected_c0 = -0.262323073774029;
    double expected_c1 =  1.00211681802045; 
    double expected_cov00 = pow(0.232818234301152, 2.0);
    double expected_cov01 = -7.74327536339570e-05;  /* computed from octave */
    double expected_cov11 = pow(0.429796848199937E-03, 2.0);
    double expected_sumsq = 26.6173985294224;
    
    gsl_fit_linear (x, xstride, y, ystride, norris_n, 
                    &c0, &c1, &cov00, &cov01, &cov11, &sumsq);
    
    /* gsl_fit_wlinear (x, xstride, w, wstride, y, ystride, norris_n, 
                     &c0, &c1, &cov00, &cov01, &cov11, &sumsq); */
  
    gsl_test_rel (c0, expected_c0, 1e-10, "norris gsl_fit_linear c0") ;
    gsl_test_rel (c1, expected_c1, 1e-10, "norris gsl_fit_linear c1") ;
    gsl_test_rel (cov00, expected_cov00, 1e-10, "norris gsl_fit_linear cov00") ;
    gsl_test_rel (cov01, expected_cov01, 1e-10, "norris gsl_fit_linear cov01") ;
    gsl_test_rel (cov11, expected_cov11, 1e-10, "norris gsl_fit_linear cov11") ;
    gsl_test_rel (sumsq, expected_sumsq, 1e-10, "norris gsl_fit_linear sumsq") ;
  }

  {
    double c0, c1, cov00, cov01, cov11, sumsq;
       
    double expected_c0 = -0.262323073774029;
    double expected_c1 =  1.00211681802045; 
    double expected_cov00 = 6.92384428759429e-02;  /* computed from octave */
    double expected_cov01 = -9.89095016390515e-05; /* computed from octave */
    double expected_cov11 = 2.35960747164148e-07;  /* computed from octave */
    double expected_sumsq = 26.6173985294224;
    
    gsl_fit_wlinear (x, xstride, w, wstride, y, ystride, norris_n, 
                     &c0, &c1, &cov00, &cov01, &cov11, &sumsq);
  
    gsl_test_rel (c0, expected_c0, 1e-10, "norris gsl_fit_wlinear c0") ;
    gsl_test_rel (c1, expected_c1, 1e-10, "norris gsl_fit_wlinear c1") ;
    gsl_test_rel (cov00, expected_cov00, 1e-10, "norris gsl_fit_wlinear cov00") ;
    gsl_test_rel (cov01, expected_cov01, 1e-10, "norris gsl_fit_wlinear cov01") ;
    gsl_test_rel (cov11, expected_cov11, 1e-10, "norris gsl_fit_wlinear cov11") ;
    gsl_test_rel (sumsq, expected_sumsq, 1e-10, "norris gsl_fit_wlinear sumsq") ;
  }

  for (i = 0; i < noint1_n; i++) 
    {
      x[i*xstride] = noint1_x[i];
      w[i*wstride] = 1.0;
      y[i*ystride] = noint1_y[i];
    }

  {
    double c1, cov11, sumsq;
       
    double expected_c1 = 2.07438016528926; 
    double expected_cov11 = pow(0.165289256198347E-01, 2.0);  
    double expected_sumsq = 127.272727272727;
    
    gsl_fit_mul (x, xstride, y, ystride, noint1_n, &c1, &cov11, &sumsq);
  
    gsl_test_rel (c1, expected_c1, 1e-10, "noint1 gsl_fit_mul c1") ;
    gsl_test_rel (cov11, expected_cov11, 1e-10, "noint1 gsl_fit_mul cov11") ;
    gsl_test_rel (sumsq, expected_sumsq, 1e-10, "noint1 gsl_fit_mul sumsq") ;
  }

  {
    double c1, cov11, sumsq;
       
    double expected_c1 = 2.07438016528926; 
    double expected_cov11 = 2.14661371686165e-05; /* computed from octave */
    double expected_sumsq = 127.272727272727;
    
    gsl_fit_wmul (x, xstride, w, wstride, y, ystride, noint1_n, &c1, &cov11, &sumsq);

    gsl_test_rel (c1, expected_c1, 1e-10, "noint1 gsl_fit_wmul c1") ;
    gsl_test_rel (cov11, expected_cov11, 1e-10, "noint1 gsl_fit_wmul cov11") ;
    gsl_test_rel (sumsq, expected_sumsq, 1e-10, "noint1 gsl_fit_wmul sumsq") ;
  }


  for (i = 0; i < noint2_n; i++) 
    {
      x[i*xstride] = noint2_x[i];
      w[i*wstride] = 1.0;
      y[i*ystride] = noint2_y[i];
    }

  {
    double c1, cov11, sumsq;
       
    double expected_c1 = 0.727272727272727; 
    double expected_cov11 = pow(0.420827318078432E-01, 2.0);  
    double expected_sumsq = 0.272727272727273;
    
    gsl_fit_mul (x, xstride, y, ystride, noint2_n, &c1, &cov11, &sumsq);
  
    gsl_test_rel (c1, expected_c1, 1e-10, "noint2 gsl_fit_mul c1") ;
    gsl_test_rel (cov11, expected_cov11, 1e-10, "noint2 gsl_fit_mul cov11") ;
    gsl_test_rel (sumsq, expected_sumsq, 1e-10, "noint2 gsl_fit_mul sumsq") ;
  }

  {
    double c1, cov11, sumsq;
       
    double expected_c1 = 0.727272727272727; 
    double expected_cov11 = 1.29870129870130e-02 ; /* computed from octave */
    double expected_sumsq = 0.272727272727273;
    
    gsl_fit_wmul (x, xstride, w, wstride, y, ystride, noint2_n, &c1, &cov11, &sumsq);

    gsl_test_rel (c1, expected_c1, 1e-10, "noint2 gsl_fit_wmul c1") ;
    gsl_test_rel (cov11, expected_cov11, 1e-10, "noint2 gsl_fit_wmul cov11") ;
    gsl_test_rel (sumsq, expected_sumsq, 1e-10, "noint2 gsl_fit_wmul sumsq") ;
  }

  /* now summarize the results */

  exit (gsl_test_summary ());
}
Exemple #7
0
void Linear::exec()
{
  // Get the input workspace
  MatrixWorkspace_const_sptr inputWorkspace = getProperty("InputWorkspace");
  // Get the spectrum to fit
  const int histNumber = getProperty("WorkspaceIndex");
  // Check validity
  if ( histNumber >= static_cast<int>(inputWorkspace->getNumberHistograms()) )
  {
    g_log.error() << "WorkspaceIndex set to an invalid value of " << histNumber << std::endl;
    throw Exception::IndexError(histNumber,inputWorkspace->getNumberHistograms(),"Linear WorkspaceIndex property");
  }

  // Get references to the data in the chosen spectrum
  const MantidVec& X = inputWorkspace->dataX(histNumber);
  const MantidVec& Y = inputWorkspace->dataY(histNumber);
  const MantidVec& E = inputWorkspace->dataE(histNumber);
  // Check if this spectrum has errors
  double errorsCount = 0.0;

  // Retrieve the Start/EndX properties, if set
  this->setRange(X,Y);
  
  const bool isHistogram = inputWorkspace->isHistogramData();
  // If the spectrum to be fitted has masked bins, we want to exclude them (even if only partially masked)
  const MatrixWorkspace::MaskList * const maskedBins = 
    ( inputWorkspace->hasMaskedBins(histNumber) ? &(inputWorkspace->maskedBins(histNumber)) : NULL );
  // Put indices of masked bins into a set for easy searching later
  std::set<size_t> maskedIndices;
  if (maskedBins)
  {
    MatrixWorkspace::MaskList::const_iterator it;
    for (it = maskedBins->begin(); it != maskedBins->end(); ++it)
      maskedIndices.insert(it->first);
  }
  
  progress(0);

  // Declare temporary vectors and reserve enough space if they're going to be used
  std::vector<double> XCen, unmaskedY, weights;
  int numPoints = m_maxX - m_minX;
  if (isHistogram) XCen.reserve(numPoints);
  if (maskedBins) unmaskedY.reserve(numPoints);
  weights.reserve(numPoints);

  for (int i = 0; i < numPoints; ++i)
  {
    // If the current bin is masked, skip it
    if ( maskedBins && maskedIndices.count(m_minX+i) ) continue;
    // Need to adjust X to centre of bin, if a histogram
    if (isHistogram) XCen.push_back( 0.5*(X[m_minX+i]+X[m_minX+i+1]) );
    // If there are masked bins present, we need to copy the unmasked Y values
    if (maskedBins) unmaskedY.push_back(Y[m_minX+i]);
    // GSL wants the errors as weights, i.e. 1/sigma^2
    // We need to be careful if E is zero because that would naively lead to an infinite weight on the point.
    // Solution taken here is to zero weight if error is zero, which typically means Y is zero
    //   (so it is effectively excluded from the fit).
    const double& currentE = E[m_minX+i];
    weights.push_back( currentE ? 1.0/(currentE*currentE) : 0.0 );
    // However, if the spectrum given has all errors of zero, then we should use the gsl function that
    //   doesn't take account of the errors.
    if ( currentE ) ++errorsCount;
  }
  progress(0.3);
  
  // If masked bins present, need to recalculate numPoints here
  if (maskedBins) numPoints = static_cast<int>(unmaskedY.size());
  // If no points left for any reason, bail out
  if (numPoints == 0)
  {
    g_log.error("No points in this range to fit");
    throw std::runtime_error("No points in this range to fit");
  }

  // Set up pointer variables to pass to gsl, pointing them to the right place
  const double * const xVals = ( isHistogram ? &XCen[0] : &X[m_minX] );
  const double * const yVals = ( maskedBins ? &unmaskedY[0] : &Y[m_minX] );

  // Call the gsl fitting function
  // The stride value of 1 reflects that fact that we want every element of our input vectors
  const int stride = 1;
  double *c0(new double),*c1(new double),*cov00(new double),*cov01(new double),*cov11(new double),*chisq(new double);
  int status;
  // Unless our spectrum has error values for vast majority of points, 
  //   call the gsl function that doesn't use errors
  if ( errorsCount/numPoints < 0.9 )
  {
    g_log.debug("Calling gsl_fit_linear (doesn't use errors in fit)");
    status = gsl_fit_linear(xVals,stride,yVals,stride,numPoints,c0,c1,cov00,cov01,cov11,chisq);
  }
  // Otherwise, call the one that does account for errors on the data points
  else
  {
    g_log.debug("Calling gsl_fit_wlinear (uses errors in fit)");
    status = gsl_fit_wlinear(xVals,stride,&weights[0],stride,yVals,stride,numPoints,c0,c1,cov00,cov01,cov11,chisq);
  }
  progress(0.8);

  // Check that the fit succeeded
  std::string fitStatus = gsl_strerror(status);
  // For some reason, a fit where c0,c1 & chisq are all infinity doesn't report as a 
  //   failure, so check explicitly.
  if ( !gsl_finite(*chisq) || !gsl_finite(*c0) || !gsl_finite(*c1) )
    fitStatus = "Fit gives infinities";
  if (fitStatus != "success") g_log.error() << "The fit failed: " << fitStatus << "\n";
  else
    g_log.information() << "The fit succeeded, giving y = " << *c0 << " + " << *c1 << "*x, with a Chi^2 of " << *chisq << "\n";
  
  // Set the fit result output properties
  setProperty("FitStatus",fitStatus);
  setProperty("FitIntercept",*c0);
  setProperty("FitSlope",*c1);
  setProperty("Cov00",*cov00);
  setProperty("Cov11",*cov11);
  setProperty("Cov01",*cov01);
  setProperty("Chi2",*chisq);
  
  // Create and fill a workspace2D with the same bins as the fitted spectrum and the value of the fit for the centre of each bin
  const size_t YSize = Y.size();
  MatrixWorkspace_sptr outputWorkspace = WorkspaceFactory::Instance().create(inputWorkspace,1,X.size(),YSize);
  
  // Copy over the X bins
  outputWorkspace->dataX(0).assign(X.begin(),X.end());
  // Now loop over the spectrum and use gsl function to calculate the Y & E values for the function
  for (size_t i = 0; i < YSize; ++i)
  {
    const double x = ( isHistogram ? 0.5*(X[i]+X[i+1]) : X[i] );
    const int err = gsl_fit_linear_est(x,*c0,*c1,*cov00,*cov01,*cov11,&(outputWorkspace->dataY(0)[i]),&(outputWorkspace->dataE(0)[i]));
    if (err) g_log.warning() << "Problem in filling the output workspace: " << gsl_strerror(err) << std::endl;
  }
  setProperty("OutputWorkspace",outputWorkspace);
  progress(1);
  // Clean up
  delete c0;
  delete c1;
  delete cov00;
  delete cov01;
  delete cov11;
  delete chisq;
}
Exemple #8
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
}
Exemple #9
0
int kstfit_linear_weighted(const double *const inArrays[], const int inArrayLens[],
		const double inScalars[],
		double *outArrays[], int outArrayLens[],
		double outScalars[])
{
  int i = 0;
  int	iLength;
  int iReturn = -1;
  double* pResult[4];
  double c0 = 0.0;
  double c1 = 0.0;
  double cov00 = 0.0;
  double cov01 = 0.0;
  double cov11 = 0.0;
  double dSumSq = 0.0;
  double y;
  double yErr;
  
  if (inArrayLens[Y] >= 2 && inArrayLens[X] >= 2) {
    iLength = inArrayLens[Y];
    if( inArrayLens[X] < iLength ) {
      iLength = inArrayLens[X];
    }
    if( inArrayLens[W] < iLength ) {
      iLength = inArrayLens[W];
    }
    
    for( i=0; i<4; i++ ) {
      if( outArrayLens[0] != iLength ) {
        pResult[i] = (double*)realloc( outArrays[i], iLength * sizeof( double ) );
      } else {
        pResult[i] = outArrays[i];
      }
    }
    
    if( pResult[0] != NULL && 
        pResult[1] != NULL && 
        pResult[2] != NULL && 
        pResult[3] != NULL )
    {
      for( i=0; i<4; i++ ) {
        outArrays[i] 		= pResult[i];
        outArrayLens[i] = iLength;
      }
            
      if( !gsl_fit_wlinear( inArrays[X], 1, inArrays[W], 1, inArrays[Y], 1, iLength, &c0, &c1, &cov00, &cov01, &cov11, &dSumSq ) ) {
        
        for( i=0; i<iLength; i++ ) {
          gsl_fit_linear_est( inArrays[X][i], c0, c1, cov00, cov01, cov11, &y, &yErr );
          outArrays[0][i] = y;
          outArrays[1][i] = y - yErr;
          outArrays[2][i] = y + yErr;
          outArrays[3][i] = inArrays[Y][i] - y;
        }
        
        outScalars[0] = c0;
        outScalars[1] = c1;
        outScalars[2] = cov00;
        outScalars[3] = cov01;
        outScalars[4] = cov11;
        outScalars[5] = dSumSq;
        
        iReturn = 0;
      }
    } 
  }
  
  return iReturn;
}