Exemplo n.º 1
0
gsl_multifit_fdfsolver * Fit::fitGSL(gsl_multifit_function_fdf f, int &iterations, int &status)
{
	const gsl_multifit_fdfsolver_type *T;
	if (d_solver)
		T = gsl_multifit_fdfsolver_lmder;
	else
		T = gsl_multifit_fdfsolver_lmsder;

	gsl_set_error_handler_off();

	gsl_multifit_fdfsolver *s = gsl_multifit_fdfsolver_alloc (T, d_n, d_p);
	status = gsl_multifit_fdfsolver_set (s, &f, d_param_init);

	size_t iter = 0;
	bool inRange = true;
	for (int i=0; i<d_p; i++){
		double p = gsl_vector_get(d_param_init, i);
		d_results[i] = p;
		if (p < d_param_range_left[i] || p > d_param_range_right[i]){
			inRange = false;
			break;
		}
	}

	if (status){
	    gsl_multifit_covar (s->J, 0.0, covar);
	    iterations = 0;
	    return s;
	}

	do{
		iter++;
		status = gsl_multifit_fdfsolver_iterate (s);
		if (status)
			break;

		for (int i=0; i<d_p; i++){
			double p = gsl_vector_get(s->x, i);
			if (p < d_param_range_left[i] || p > d_param_range_right[i]){
				inRange = false;
				break;
			}
		}
		if (!inRange)
			break;

		for (int i = 0; i < d_p; i++)
			d_results[i] = gsl_vector_get(s->x, i);

		status = gsl_multifit_test_delta (s->dx, s->x, d_tolerance, d_tolerance);
	} while (inRange && status == GSL_CONTINUE && (int)iter < d_max_iterations);

	gsl_multifit_covar (s->J, 0.0, covar);

	iterations = iter;
	return s;
}
Exemplo n.º 2
0
gsl_multifit_fdfsolver *Fit::fitGSL(gsl_multifit_function_fdf f,
                                    int &iterations, int &status) {
  const gsl_multifit_fdfsolver_type *T;
  if (d_solver)
    T = gsl_multifit_fdfsolver_lmder;
  else
    T = gsl_multifit_fdfsolver_lmsder;

  gsl_multifit_fdfsolver *s = gsl_multifit_fdfsolver_alloc(T, d_n, d_p);
  gsl_multifit_fdfsolver_set(s, &f, d_param_init);

  size_t iter = 0;
  bool inRange = true;
  for (int i = 0; i < d_p; i++) {
    double p = gsl_vector_get(d_param_init, i);
    d_results[i] = p;
    if (p < d_param_range_left[i] || p > d_param_range_right[i]) {
      inRange = false;
      break;
    }
  }

  do {
    iter++;
    status = gsl_multifit_fdfsolver_iterate(s);
    if (status)
      break;

    for (int i = 0; i < d_p; i++) {
      double p = gsl_vector_get(s->x, i);
      if (p < d_param_range_left[i] || p > d_param_range_right[i]) {
        inRange = false;
        break;
      }
    }
    if (!inRange)
      break;

    for (int i = 0; i < d_p; i++)
      d_results[i] = gsl_vector_get(s->x, i);

    status = gsl_multifit_test_delta(s->dx, s->x, d_tolerance, d_tolerance);
  } while (inRange && status == GSL_CONTINUE && (int)iter < d_max_iterations);

#if GSL_MAJOR_VERSION < 2
  gsl_multifit_covar(s->J, 0.0, covar);
#else
  gsl_matrix *J = gsl_matrix_alloc(d_n, d_p);
  gsl_multifit_fdfsolver_jac(s, J);
  gsl_multifit_covar(J, 0.0, covar);
  gsl_matrix_free(J);
#endif
  iterations = static_cast<int>(iter);
  return s;
}
/* Calculates covariance matrix
 *
 * @param epsrel :: Is used to remove linear-dependent columns
 * @param covar :: Returned covariance matrix, here as
 */
void LevenbergMarquardtMinimizer::calCovarianceMatrix(double epsrel,
                                                      gsl_matrix *covar) {
#if GSL_MAJOR_VERSION < 2
  gsl_multifit_covar(m_gslSolver->J, epsrel, covar);
#else
  gsl_matrix *J = gsl_matrix_alloc(gslContainer.n, gslContainer.p);
  gsl_multifit_fdfsolver_jac(m_gslSolver, J);
  gsl_multifit_covar(J, epsrel, covar);
  gsl_matrix_free(J);
#endif
}
Exemplo n.º 4
0
//Test fitting result using error estimation from covariance matrix, not reliable. tol is the relative tolerance of error.
int covar_rel_test(const gsl_matrix* J, const gsl_vector* x, double tol)
{
    //Check if some parameters become NAN or INF.
    for (int iter = 0; iter < numOfPara; ++iter)
    {
        if (!isfinite(x->data[iter]))
        {
            return GSL_EOVRFLW;
        }
    }
    gsl_matrix* covar = gsl_matrix_alloc(numOfPara, numOfPara);
    gsl_multifit_covar(J, 0.0, covar);	//Get the covariance matrix.
    double fitErr[numOfPara];
    for (int iterpara = 0; iterpara<numOfPara; ++iterpara)	//err_i=\sqrt{c_{ii}}
    {
        fitErr[iterpara]=sqrt(gsl_matrix_get(covar, iterpara, iterpara));
    }
    gsl_matrix_free(covar);
    for (int iter = 0; iter < numOfPara; ++iter)
    {
        double relerr = abs(fitErr[iter] / x->data[iter]);
        if (relerr>tol)
        {
            return GSL_CONTINUE;
        }
    }
    return GSL_SUCCESS;
}
Exemplo n.º 5
0
void
test_lmder (gsl_multifit_function_fdf * f, double x0[], 
            double * X, double F[], double * cov)
{
  const gsl_multifit_fdfsolver_type *T;
  gsl_multifit_fdfsolver *s;

  const size_t n = f->n;
  const size_t p = f->p;

  int status;
  size_t iter = 0, i;
  
  gsl_vector_view x = gsl_vector_view_array (x0, p);

  T = gsl_multifit_fdfsolver_lmsder;
  s = gsl_multifit_fdfsolver_alloc (T, n, p);
  gsl_multifit_fdfsolver_set (s, f, &x.vector);

  do
    {
      status = gsl_multifit_fdfsolver_iterate (s);

      for (i = 0 ; i < p; i++)
        {
          gsl_test_rel (gsl_vector_get (s->x, i), X[p*iter+i], 1e-5, 
                        "lmsder, iter=%u, x%u", iter, i);
        }

      gsl_test_rel (gsl_blas_dnrm2 (s->f), F[iter], 1e-5, 
                    "lmsder, iter=%u, f", iter);

      iter++;
    }
  while (iter < 20);
  
  {
    size_t i, j;
    gsl_matrix * covar = gsl_matrix_alloc (4, 4);
    gsl_multifit_covar (s->J, 0.0, covar);

    for (i = 0; i < 4; i++) 
      {
        for (j = 0; j < 4; j++)
          {
            gsl_test_rel (gsl_matrix_get(covar,i,j), cov[i*p + j], 1e-7, 
                          "gsl_multifit_covar cov(%d,%d)", i, j) ;
          }
      }

    gsl_matrix_free (covar);
  }

  gsl_multifit_fdfsolver_free (s);

}
Exemplo n.º 6
0
double *Fit::fitGslMultimin(int &iterations, int &status) {
  double *result = new double[d_p];

  // declare input data
  struct FitData data = {static_cast<size_t>(d_n),
                         static_cast<size_t>(d_p),
                         d_x,
                         d_y,
                         d_y_errors,
                         this};
  gsl_multimin_function f;
  f.f = d_fsimplex;
  f.n = d_p;
  f.params = &data;

  // step size (size of the simplex)
  // can be increased for faster convergence
  gsl_vector *ss = gsl_vector_alloc(f.n);
  gsl_vector_set_all(ss, 10.0);

  // initialize minimizer
  const gsl_multimin_fminimizer_type *T = gsl_multimin_fminimizer_nmsimplex;
  gsl_multimin_fminimizer *s_min = gsl_multimin_fminimizer_alloc(T, f.n);
  gsl_multimin_fminimizer_set(s_min, &f, d_param_init, ss);

  // iterate minimization algorithm
  for (iterations = 0; iterations < d_max_iterations; iterations++) {
    status = gsl_multimin_fminimizer_iterate(s_min);
    if (status) break;

    double size = gsl_multimin_fminimizer_size(s_min);
    status = gsl_multimin_test_size(size, d_tolerance);
    if (status != GSL_CONTINUE) break;
  }

  // grab results
  for (int i = 0; i < d_p; i++) result[i] = gsl_vector_get(s_min->x, i);
  chi_2 = s_min->fval;
  gsl_matrix *J = gsl_matrix_alloc(d_n, d_p);
  d_df(s_min->x, (void *)f.params, J);
  gsl_multifit_covar(J, 0.0, covar);
  if (d_y_error_source == UnknownErrors) {
    // multiply covar by variance of residuals, which is used as an estimate for
    // the
    // statistical errors (this relies on the Y errors being set to 1.0)
    gsl_matrix_scale(covar, chi_2 / (d_n - d_p));
  }

  // free previously allocated memory
  gsl_matrix_free(J);
  gsl_multimin_fminimizer_free(s_min);
  gsl_vector_free(ss);

  return result;
}
Exemplo n.º 7
0
///Calculates covariance matrix - not implemented
void SimplexMinimizer::calCovarianceMatrix(double epsrel, gsl_matrix * covar)
{
  gsl_matrix * holdCalculatedJacobian;
  holdCalculatedJacobian =  gsl_matrix_alloc (m_gslLeastSquaresContainer.n, m_gslLeastSquaresContainer.p);

  int dummy = m_gslLeastSquaresContainer.df(m_gslSolver->x, m_gslLeastSquaresContainer.params, holdCalculatedJacobian);
  (void) dummy;
  gsl_multifit_covar (holdCalculatedJacobian, epsrel, covar);

  gsl_matrix_free (holdCalculatedJacobian);
}
Exemplo n.º 8
0
int NonLinearLSQ::curvefit() {

  size_t n(nSize());
  size_t p(nParms());

  //  Initialize the solver function information
  _nlsqPointer d = { this };
  gsl_multifit_function_fdf mf;
  mf.f      = &f;
  mf.df     = &df;
  mf.fdf    = &fdf;
  mf.n      =  n;
  mf.p      = p;
  mf.params =  &d;

  const gsl_multifit_fdfsolver_type *T = gsl_multifit_fdfsolver_lmsder;
  gsl_multifit_fdfsolver *s = gsl_multifit_fdfsolver_alloc(T, n, p);

  _fitParms = guess();
  gsl_vector *x = NlsqTogsl(_fitParms);
  gsl_matrix *covar = gsl_matrix_alloc(p, p);
  gsl_multifit_fdfsolver_set(s, &mf, x);

  _nIters = 0;
  checkIteration(_nIters, gslToNlsq(s->x), NLVector(p,999.0),
                  gsl_blas_dnrm2(s->f), GSL_CONTINUE);


  do {
    _nIters++;

    _status = gsl_multifit_fdfsolver_iterate(s);
    _fitParms = gslToNlsq(s->x);

    gsl_multifit_covar(s->J, 0.0, covar);
    _uncert = getUncertainty(covar);

    _status = checkIteration(_nIters, _fitParms, _uncert, gsl_blas_dnrm2(s->f),
                             _status);
    if ( _status  ) { break; }
    if(!doContinue()) { break; }

    _status = gsl_multifit_test_delta(s->dx, s->x, absErr(), relErr());
  } while ((_status == GSL_CONTINUE) && (_nIters < _maxIters));

  // Clean up
  gsl_multifit_fdfsolver_free(s);
  gsl_matrix_free(covar);

  return (_status);
}
Exemplo n.º 9
0
/**
  * Calculates covariance matrix for fitting function's active parameters.
  */
void CostFuncFitting::calActiveCovarianceMatrix(GSLMatrix &covar,
                                                double epsrel) {
  // construct the jacobian
  GSLJacobian J(m_function, m_values->size());
  size_t na = this->nParams(); // number of active parameters
  assert(J.getJ()->size2 == na);
  covar.resize(na, na);

  // calculate the derivatives
  m_function->functionDeriv(*m_domain, J);

  // let the GSL to compute the covariance matrix
  gsl_multifit_covar(J.getJ(), epsrel, covar.gsl());
}
Exemplo n.º 10
0
void FittingPerfomanceInfo::GetSolverResults( gsl_multifit_fdfsolver *s )
{
	double chi, dof, c;	size_t p=s->x->size, n=s->f->size; 
	gsl_matrix *covar = gsl_matrix_alloc (p, p);
	if(covar!=NULL)
	{
		gsl_multifit_covar (s->J, 0.0, covar);
		chi = gsl_blas_dnrm2(s->f); dof = n - p; c = GSL_MAX_DBL(1, chi / sqrt(dof)); chisq_dof=chi*chi / dof;
		for(size_t i=0;i<p;i++)
		{
			a[i]=gsl_vector_get(s->x, i); 
			da[i]=fabs(c*sqrt(gsl_matrix_get(covar,i,i)));
		}
		gsl_matrix_free(covar);
	}
}
Exemplo n.º 11
0
static void
test_fdf_checksol(const char *sname, const char *pname,
                  const double epsrel, gsl_multifit_fdfsolver *s,
                  test_fdf_problem *problem)
{
  gsl_multifit_function_fdf *fdf = problem->fdf;
  const double *sigma = problem->sigma;
  gsl_vector *f = gsl_multifit_fdfsolver_residual(s);
  gsl_vector *x = gsl_multifit_fdfsolver_position(s);
  double sumsq;

  /* check solution vector x and sumsq = ||f||^2 */
  gsl_blas_ddot(f, f, &sumsq);
  (problem->checksol)(x->data, sumsq, epsrel, sname, pname);

#if 1
  /* check variances */
  if (sigma)
    {
      const size_t n = fdf->n;
      const size_t p = fdf->p;
      size_t i;
      gsl_matrix * J = gsl_matrix_alloc(n, p);
      gsl_matrix * covar = gsl_matrix_alloc (p, p);

      gsl_multifit_fdfsolver_jac (s, J);
      gsl_multifit_covar(J, 0.0, covar);

      for (i = 0; i < p; i++) 
        {
          double ei = sqrt(sumsq/(n-p))*sqrt(gsl_matrix_get(covar,i,i));
          gsl_test_rel (ei, sigma[i], epsrel, 
                        "%s/%s, sigma(%d)", sname, pname, i) ;
        }

      gsl_matrix_free (J);
      gsl_matrix_free (covar);
    }
#endif
}
Exemplo n.º 12
0
int cspl_qrs_fit (void * params) {
    int status;
    unsigned int iter;
    struct cspl_qrs_data * data = (struct cspl_qrs_data *) params;
    /* This is the data to be fitted */

    gsl_multifit_function_fdf f;
    //    const gsl_rng_type * type;
    //    gsl_rng * r;

    //    gsl_rng_env_setup();

    //    type = gsl_rng_default;
    //    r = gsl_rng_alloc (type);

    f.f = &cspl_qrs_f;
    f.df = &cspl_qrs_df;
    f.fdf = &cspl_qrs_fdf;
    f.n = data->n;
    f.p = data->p;
    f.params = data;

    gsl_multifit_fdfsolver_set (data->s, &f, &data->x.vector);
    iter = 0;
    //print_state (iter, data->s);

    do
    {
        iter++;
        status = gsl_multifit_fdfsolver_iterate (data->s);
#ifdef DEBUG
        printf ("status = %s\n", gsl_strerror (status));

        print_state (iter, data->s);
#endif
        if (status)
            break;

        status = gsl_multifit_test_delta (data->s->dx, data->s->x,
                1e-12, 1e-12);
    }
    while (status == GSL_CONTINUE && iter < 500);

    gsl_multifit_covar (data->s->J, 0.0, data->covar);



    double chi = gsl_blas_dnrm2(data->s->f);
    double dof = data->n - data->p;
    double c = GSL_MAX_DBL(1, chi / sqrt(dof));
    data->c = c;
    data->chisq_pdof = pow(chi, 2.0) / dof;

#ifdef DEBUG
#define FIT(i) gsl_vector_get(data->s->x, i)
#define ERR(i) sqrt(gsl_matrix_get(data->covar,i,i))
    printf("chisq/dof = %g\n",  pow(chi, 2.0) / dof); 

    printf ("A        = %.5f +/- %.5f\n", FIT(0), c*ERR(0)); 
    printf ("t_beat   = %.5f +/- %.5f\n", FIT(1), c*ERR(1)); 
    printf ("S_0      = %.5f +/- %.5f\n", FIT(2), c*ERR(2)); 
    printf ("S_1      = %.5f +/- %.5f\n", FIT(3), c*ERR(3)); 


    printf ("status = %s\n", gsl_strerror (status));
#endif
    //    gsl_rng_free (r);
    return GSL_SUCCESS;
}
Exemplo n.º 13
0
Arquivo: magcal.c Projeto: pa345/lib
int
magcal_proc(gsl_vector *m, const satdata_mag *data, magcal_workspace *w)
{
  int s;
  gsl_multifit_function_fdf f;
  gsl_multifit_fdfsolver *fdf_s;
  magcal_params params;
  gsl_vector_view v;
  const double xtol = 1e-10;
  const double gtol = 1e-10;
  const double ftol = 0.0;
  int info;

  /* copy data arrays */
  s = magcal_init(data, w);
  if (s)
    return s;

  /* scale parameters to dimensionless units */
  magcal_scale(1, m, w);

  params.w = w;

  f.f = &magcal_f;
  f.df = &magcal_df;
  f.n = w->n;
  f.p = w->p;
  f.params = &params;

  v = gsl_vector_subvector(m, MAGCAL_IDX_DT, w->p);
  gsl_multifit_fdfsolver_set (w->fdf_s, &f, &v.vector);
  gsl_multifit_fdfridge_set (w->fdf_ridge, &f, &v.vector, w->lambda);

#if 0
  s = gsl_multifit_fdfsolver_driver(w->fdf_s, 500, xtol, gtol, ftol, &info);
  fdf_s = w->fdf_s;
#else
  s = gsl_multifit_fdfridge_driver(w->fdf_ridge, 500, xtol, gtol, ftol, &info);
  fdf_s = w->fdf_ridge->s;
#endif
  if (s != GSL_SUCCESS)
    {
      fprintf(stderr, "magcal_proc: error computing parameters: %s\n",
              gsl_strerror(s));
    }
  else
    {
      magcal_print_state (w, fdf_s);
      fprintf(stderr, "magcal_proc: total data processed: %zu\n", w->n);
      fprintf(stderr, "magcal_proc: number of iterations: %zu\n",
              fdf_s->niter);
      fprintf(stderr, "magcal_proc: function evaluations: %zu\n",
              fdf_s->fdf->nevalf);
      fprintf(stderr, "magcal_proc: jacobian evaluations: %zu\n",
              fdf_s->fdf->nevaldf);
      fprintf(stderr, "magcal_proc: reason for convergence: %d\n",
              info);

      /* save calibration parameters */
      gsl_vector_memcpy(&v.vector, fdf_s->x);

      /* restore time shift parameter */
      gsl_vector_set(m, MAGCAL_IDX_DT, 0.0);

      /* scale offsets back to nT */
      magcal_scale(-1, m, w);

#if 0
      /* compute covariance matrix */
      gsl_multifit_covar(fdf_s->J, 0.0, w->covar);
#endif
    }

  return s;
} /* magcal_proc() */
Exemplo n.º 14
0
int InterpolaVPR_GSL::interpola_VPR(const float* vpr, int hvprmax, int livmin)
{
    LOG_CATEGORY("radar.vpr");
    static const unsigned N = 10;
    const gsl_multifit_fdfsolver_type *T;
    gsl_multifit_fdfsolver *s;
    int status;
    unsigned int i;
    const size_t n = N;
    const size_t p = 5;
    char file_vprint[512];
    gsl_matrix *covar = gsl_matrix_alloc (p, p);
    double a[5];
    struct data d(N);
    gsl_multifit_function_fdf f;
    double x_init[5] = { 4, 0.2, 3. , 1.4, -0.4 };
    gsl_vector_view x = gsl_vector_view_array (x_init, p);

    //////////////////////////////////////////////////////////////////////////////
    int ier_int=0;
    double xint,yint;
    /* punti interessanti per inizializzare parametri*/
    int  in1=(int)((hvprmax-TCK_VPR/2)/TCK_VPR); //indice del massimo
    int  in2=(int)((hvprmax+HALF_BB)/TCK_VPR); //indice del massimo + 500 m
    int  in3=in2+1;
    int  in4=in2+5; //indice del massimo + 1000 m
    if (in4 > NMAXLAYER-1) {
        ier_int=1;
        return ier_int;
    }

    B=vpr[in1]-vpr[in2];
    E=hvprmax/1000.;
    G=0.25;
    C=vpr[in2-1];
    F=vpr[in4]<vpr[in3]?(vpr[in4]-vpr[in3])/((in4-in3)*TCK_VPR/1000.):0.;
    // fprintf(stderr, "const unsigned NMAXLAYER=%d;\n", NMAXLAYER);
    // fprintf(stderr, "float vpr[] = {");
    // for (unsigned i = 0; i < NMAXLAYER; ++i)
    //     fprintf(stderr, "%s%f", i==0?"":",", (double)vpr[i]);
    // fprintf(stderr, "};\n");

    x_init[0]= a[0]=B;
    x_init[1]= a[1]=E;
    x_init[2]= a[2]=G;
    x_init[3]= a[3]=C;
    x_init[4]= a[4]=F;


    /////////////////////////////////////////////////////////////////////////////////////////////////////////

    f.f = &expb_f;
    f.df = &expb_df;
    f.fdf = &expb_fdf;
    f.n = n;
    f.p = p;
    f.params = &d;

    /* This is the data to be fitted */

    for (i = 0; i < n; i++)
    {
        d.t[i]= ((hvprmax-1000.)>livmin)? (i*TCK_VPR+(hvprmax-800)-TCK_VPR)/1000. : (livmin+i*TCK_VPR)/1000.;
        d.y[i]= ((hvprmax-1000.)>livmin)? vpr[i+(int)(((hvprmax-800)-TCK_VPR)/TCK_VPR)] : vpr[i+(int)(livmin/TCK_VPR)];
        d.sigma[i] = 0.5;
    };

    T = gsl_multifit_fdfsolver_lmsder;
    s = gsl_multifit_fdfsolver_alloc (T, n, p);
    gsl_multifit_fdfsolver_set (s, &f, &x.vector);

    //print_state (0, s);
    bool found = false;
    for (unsigned iter = 0; !found && iter < 500; ++iter)
    {
        //fprintf(stderr, "Iter %d\n", iter);
        //d.print();
        int status = gsl_multifit_fdfsolver_iterate (s);
        if (status != 0)
        {
            LOG_ERROR("gsl_multifit_fdfsolver_iterate: %s", gsl_strerror(status));
            return 1;
        }

        //print_state (iter, s);

        status = gsl_multifit_test_delta (s->dx, s->x,
                1e-4, 1e-4);
        switch (status)
        {
            case GSL_SUCCESS: found = true; break;
            case GSL_CONTINUE: break;
            default:
                LOG_ERROR("gsl_multifit_test_delta: %s", gsl_strerror(status));
                return 1;
        }
    }

#if GSL_MAJOR_VERSION == 2
    // Use of GSL 2.0 taken from https://sft.its.cern.ch/jira/browse/ROOT-7776
    gsl_matrix* J = gsl_matrix_alloc(s->fdf->n, s->fdf->p);
    gsl_multifit_fdfsolver_jac(s, J);
    gsl_multifit_covar(J, 0.0, covar);
#else
    gsl_multifit_covar(s->J, 0.0, covar);
#endif

#define FIT(i) gsl_vector_get(s->x, i)
#define ERR(i) sqrt(gsl_matrix_get(covar,i,i))

    { 
        double chi = gsl_blas_dnrm2(s->f);
        double dof = n - p;
        double c = GSL_MAX_DBL(1, chi / sqrt(dof)); 

        // printf("chisq/dof = %g\n",  pow(chi, 2.0) / dof);

        // printf ("B      = %.5f +/- %.5f\n", FIT(0), c*ERR(0));
        // printf ("E = %.5f +/- %.5f\n", FIT(1), c*ERR(1));
        // printf ("G     = %.5f +/- %.5f\n", FIT(2), c*ERR(2));
        // printf ("C = %.5f +/- %.5f\n", FIT(3), c*ERR(3));
        // printf ("F     = %.5f +/- %.5f\n", FIT(4), c*ERR(4));
    }

    B = a[0] = FIT(0);
    E = a[1] = FIT(1);
    G = a[2] = FIT(2);
    C = a[3] = FIT(3);
    F = a[4] = FIT(4);

    gsl_multifit_fdfsolver_free (s);
    gsl_matrix_free (covar);

    /////////////////////////////////////////////////////////

    if (testfit(a) == 1)
        return 1;

    for (i=1; i<=N; i++)
    {
        xint=(i*TCK_VPR-TCK_VPR/2)/1000.;
        yint= lineargauss(xint, a);
        vpr_int[i-1] = yint;
    }

    return 0;
}
f_all_sol do_fit_duplex(f_info *f, int n, real *t_min, real *t_max, bool bVerbose, bool bUpdate)
{
	int i,j,k;
	int iter=0;
	int tot_points=0;
	int status;
	int tot_abs=0;
	int tot_inv_t=0;
	real inv_t=0;
	real tss_inv=0;
	real mean_inv_t=0;
	real abs_t=0;
	real mean_abs_t=0;
	real tss_abs=0;
	real tss_tot=0;
	f_all_sol fit;
	f_info *local_f;

	snew(local_f,n);

	// count points within boundaries, allocate 
	// and copy to new array. Only copy the xp (derivative)
	// as is the one that matters for the fitting
	for(i=0; i<n; i++){
		for(j=0; j<f[i].nts; j++){
			if( f[i].xp[0][j] >= t_min[i] && f[i].xp[0][j] <= t_max[i] ) { local_f[i].nts++ ; }
		}
		snew(local_f[i].xp[0],local_f[i].nts); 
		snew(local_f[i].xp[1],local_f[i].nts); 
	}
	// now copy the data
	// not only the derivatives of the absorbance, also the conc
	k=0;
	for(i=0; i<n; i++){
		local_f[i].conc = f[i].conc ;
		for(j=0; j<f[i].nts; j++){
			if( f[i].xp[0][j] >= t_min[i] && f[i].xp[0][j] <= t_max[i] ) { 
				local_f[i].xp[0][k] = f[i].xp[0][j] ;
				local_f[i].xp[1][k] = f[i].xp[1][j] ;
				// we use this loop to compute the TSS, the total sum of squares of the
				// "y" data, to be used for r_squared after we know chi_sq
				abs_t += local_f[i].xp[1][k] ;
				tot_abs++;
				k++;
			}
		}
		k=0;
	}

	// Total number of points to fit
	size_t pp = 4;
    const gsl_multifit_fdfsolver_type *T;
    T = gsl_multifit_fdfsolver_lmsder;
    gsl_multifit_fdfsolver *s;
    // do a fit for each triplex curve
    for (i=0; i<n; i++){
        // Total number of points to fit
        if (bVerbose){printf("Working on curve n %d\n",i);}
        struct fit_data d = { n,i, local_f};
        tot_points = local_f[i].nts ;
        gsl_matrix *covar = gsl_matrix_alloc (pp, pp);
        gsl_multifit_function_fdf ff;
        gsl_vector *x;
        x = gsl_vector_alloc(pp);
        gsl_vector_set(x,0,f[i].tm2);
        gsl_vector_set(x,1,f[i].c);
        gsl_vector_set(x,2,-70);
        gsl_vector_set(x,3,-0.1);
        s = gsl_multifit_fdfsolver_alloc (T, tot_points, pp);
        // copy data to function
        ff.f = &eq_fit;
        ff.df = NULL;
        ff.fdf = NULL;
        ff.p = pp;
        // total number of points is total of points
        // in the curve plus the number of points for the inv. fit
        ff.n = tot_points;
        ff.params = &d;
        gsl_multifit_fdfsolver_set (s, &ff, x);

        iter=0;
        do
        {
            iter++;
            status = gsl_multifit_fdfsolver_iterate (s);
            if(bVerbose){
                printf ("iter: %3u x = % 15.8f % 15.8f %15.8f "
                        "|f(x)| = %g\n",iter,
                        gsl_vector_get (s->x, 0),
                        gsl_vector_get (s->x, 1),
                        gsl_vector_get (s->x, 2),
                        gsl_blas_dnrm2 (s->f));
            }

            if (status)
                break;
            status = gsl_multifit_test_delta (s->dx, s->x,
                    1e-8, 1e-8);
        }
        while (status == GSL_CONTINUE && iter < 500);
        gsl_multifit_covar (s->J, 0.0, covar);
        gsl_matrix_free (covar);
        gsl_vector_free(x);
    // copy tm2 data adjusted from each curve
        local_f[i].tm2 = gsl_vector_get(s->x, 0);
    }

    //free first solver
    gsl_multifit_fdfsolver_free (s);

    // do the 1/tm vs ln(ct) fitting
    const gsl_multifit_fdfsolver_type *Tl;
    gsl_multifit_fdfsolver *sl;
    // fit params in the straight line
    int ppl = 2;
    gsl_matrix *covarl = gsl_matrix_alloc (ppl, ppl);
    struct fit_data dl = { n,i, local_f};
    gsl_multifit_function_fdf ffl;
    gsl_vector *xl;
    xl = gsl_vector_alloc(ppl);
    // DH and DS
    gsl_vector_set(xl,0,-70);
    gsl_vector_set(xl,1,-0.1);
    Tl = gsl_multifit_fdfsolver_lmsder;
    sl = gsl_multifit_fdfsolver_alloc (Tl, n, ppl);
    // copy data to function
    ffl.f=&eq_fit_straight;
    ffl.df = NULL;
    ffl.fdf = NULL;
    ffl.p = ppl;
    // total number of points the number of curves
    ffl.n = n;
    ffl.params = &dl;
    gsl_multifit_fdfsolver_set (sl, &ffl, xl);

    iter=0;
    do
    {
        iter++;
        status = gsl_multifit_fdfsolver_iterate (sl);
        if(bVerbose){
            printf ("iter: %3u x = % 15.8f % 15.8f "
                    "|f(x)| = %g\n",iter,
                    gsl_vector_get (sl->x, 0),
                    gsl_vector_get (sl->x, 1),
                    gsl_blas_dnrm2 (sl->f));
        }

        if (status)
            break;
        status = gsl_multifit_test_delta (sl->dx, sl->x,
                1e-8, 1e-8);
    }
    while (status == GSL_CONTINUE && iter < 500);
    gsl_multifit_covar (sl->J, 0.0, covarl);

    #define FIT(i) gsl_vector_get(sl->x, i)
    #define ERR(i) sqrt(gsl_matrix_get(covarl,i,i))

    // compute contribution of inverse temperature to TSS
    for(i=0;i<n;i++){
        inv_t += ((real)1.0/(real)local_f[i].tm2);
        tot_inv_t++;
    }
    mean_inv_t = inv_t / (real)tot_inv_t;
    for(i=0;i<n;i++){
        tss_inv += (1.0/(real)local_f[i].tm2 - mean_inv_t ) * (1.0/(real)local_f[i].tm2 - mean_inv_t);
    }

    if (bUpdate){
        fit.dh2 = gsl_vector_get(sl->x, 0);
        fit.ds2 = gsl_vector_get(sl->x, 1);
        fit.dg2   = fit.dh2 - 298.15*fit.ds2;
        for(i=0; i<n; i++){
            f[i].tm2 = local_f[i].tm2 ;
        }
    }

    tss_tot = tss_inv ;

    double chi = gsl_blas_dnrm2(sl->f);
    fit.r2 = 1.0 - ( chi*chi / tss_tot ) ;
    double dof = n - ppl;
    double c = GSL_MAX_DBL(1, chi / sqrt(dof));

    if(bVerbose)
    {
        printf ("chisq/dof = %g\n",  pow(chi, 2.0) / dof);
        printf ("r2      = %g\n",  fit.r2);
        printf ("DH3    = %.5f +/- %.5f\n", FIT(0), c*ERR(0));
        printf ("DS3    = %.5f +/- %.5f\n", FIT(1), c*ERR(1));
        printf ("DG3    = %.5f +/- %.5f\n", FIT(0)-298*FIT(1),c*ERR(1)+c*298*ERR(0));
        printf ("status = %s\n", gsl_strerror (status));
    }

    gsl_multifit_fdfsolver_free (sl);
    gsl_matrix_free (covarl);
    gsl_vector_free(xl);

    return fit;
}
Exemplo n.º 16
0
double *Fit::fitGslMultifit(int &iterations, int &status) {
  double *result = new double[d_p];

  // declare input data
  struct FitData data = {static_cast<size_t>(d_n),
                         static_cast<size_t>(d_p),
                         d_x,
                         d_y,
                         d_y_errors,
                         this};
  gsl_multifit_function_fdf f;
  f.f = d_f;
  f.df = d_df;
  f.fdf = d_fdf;
  f.n = d_n;
  f.p = d_p;
  f.params = &data;

  // initialize solver
  const gsl_multifit_fdfsolver_type *T;
  switch (d_solver) {
    case ScaledLevenbergMarquardt:
      T = gsl_multifit_fdfsolver_lmsder;
      break;
    case UnscaledLevenbergMarquardt:
      T = gsl_multifit_fdfsolver_lmder;
      break;
    default:
      break;
  }
  gsl_multifit_fdfsolver *s = gsl_multifit_fdfsolver_alloc(T, d_n, d_p);
  gsl_multifit_fdfsolver_set(s, &f, d_param_init);

  // iterate solver algorithm
  for (iterations = 0; iterations < d_max_iterations; iterations++) {
    status = gsl_multifit_fdfsolver_iterate(s);
    if (status) break;

    status = gsl_multifit_test_delta(s->dx, s->x, d_tolerance, d_tolerance);
    if (status != GSL_CONTINUE) break;
  }

  // grab results
  for (int i = 0; i < d_p; i++) result[i] = gsl_vector_get(s->x, i);
  gsl_blas_ddot(s->f, s->f, &chi_2);
#if GSL_MAJOR_VERSION < 2
  gsl_multifit_covar(s->J, 0.0, covar);
#else
  {
    gsl_matrix J;
    gsl_multifit_fdfsolver_jac(s, &J);
    gsl_multifit_covar(&J, 0.0, covar);
  }
#endif
  if (d_y_error_source == UnknownErrors) {
    // multiply covar by variance of residuals, which is used as an estimate for
    // the
    // statistical errors (this relies on the Y errors being set to 1.0, so that
    // s->f is properly normalized)
    gsl_matrix_scale(covar, chi_2 / (d_n - d_p));
  }

  // free memory allocated for fitting
  gsl_multifit_fdfsolver_free(s);

  return result;
}
Exemplo n.º 17
0
 /**
  * C++ version of gsl_multifit_covar().
  * @param J The Jacobian matrix
  * @param epsrel relative error (for removing linearly dependent columns)
  * @param covar The covariance matrix (return value)
  * @return Error code on failure
  */
 inline int covar( matrix const& J, double epsrel, matrix& covar ){
   return gsl_multifit_covar( J.get(), epsrel, covar.get() ); }
Exemplo n.º 18
0
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
    
    /* inputs:
     * image array
     * prmVect
     * mode
     * {options}
     */
    
    dataStruct_t data;
    
    /* check inputs */
    if (nrhs < 3) mexErrMsgTxt("Inputs should be: data, prmVect, mode.");
    if (!mxIsDouble(prhs[0])) mexErrMsgTxt("Data input must be double array.");
    size_t nx = mxGetN(prhs[0]);
    size_t ny = mxGetM(prhs[0]);
    if (nx != ny) mexErrMsgTxt("Input image must be a square.");
    if (!(nx % 2)) mexErrMsgTxt("The side of the input image must be odd.");
    int N = nx*ny;
    if (mxGetNumberOfElements(prhs[1])!=NPARAMS || !mxIsDouble(prhs[1])) mexErrMsgTxt("Incorrect parameter vector format.");
    if (!mxIsChar(prhs[2])) mexErrMsgTxt("Mode needs to be a string.");
    if (nrhs < 4) {
        data.maxIter = 500;
        data.eAbs = 1e-8;
        data.eRel = 1e-8;
    } else {
        if (!mxIsDouble(prhs[3]) || mxGetNumberOfElements(prhs[3])!=3) mexErrMsgTxt("Options must must be double array with 3 elements.");
        double *options = mxGetPr(prhs[3]);
        data.maxIter = options[0];
        data.eAbs = options[1];
        data.eRel = options[2];
    }
    
    
    /* read mode input */
    int np = (int)mxGetNumberOfElements(prhs[2]);
    char *mode;
    mode = (char*)malloc(sizeof(char)*(np+1));
    mxGetString(prhs[2], mode, np+1);
    
    int i;
    for (i=0; i<strlen(mode); ++i) {
        mode[i] = tolower(mode[i]);
    }
    
    np = 0; /* number of parameters to fit */
    for (i=0; i<NPARAMS; ++i) {
        if (strchr(mode, refMode[i])!=NULL) { np++; }
    }
    if (np==0) mexErrMsgTxt("Unknown mode.");
    
    /* allocate */
    data.nx = nx;
    data.np = np;
    data.pixels = mxGetPr(prhs[0]);
    data.gx = (double*)malloc(sizeof(double)*nx);
    data.gy = (double*)malloc(sizeof(double)*nx);
    data.estIdx = (int*)malloc(sizeof(int)*np);
    memcpy(data.prmVect, mxGetPr(prhs[1]), NPARAMS*sizeof(double));
    data.dfunc = (pfunc_t*) malloc(sizeof(pfunc_t) * np);
    
    /* read mask/pixels */
    data.nValid = N;
    for (i=0; i<N; ++i) {
        data.nValid -= (int)mxIsNaN(data.pixels[i]);
    }
    if (data.nValid < 5) mexErrMsgTxt("Input image must contain at least 5 data points.");

    data.idx = (int*)malloc(sizeof(int)*data.nValid);
    int *nanIdx = (int*)malloc(sizeof(int)*(N-data.nValid));
    int k = 0, l = 0;
    for (i=0; i<N; ++i) {
        if (!mxIsNaN(data.pixels[i])) {
            data.idx[k++] = i;
        } else {
            nanIdx[l++] = i;
        }
    }
    
    np = 0;
    if (strchr(mode, 'x')!=NULL) {data.estIdx[np] = 0; data.dfunc[np++] = df_dx;}
    if (strchr(mode, 'y')!=NULL) {data.estIdx[np] = 1; data.dfunc[np++] = df_dy;}
    if (strchr(mode, 'a')!=NULL) {data.estIdx[np] = 2; data.dfunc[np++] = df_dA;}
    if (strchr(mode, 's')!=NULL) {data.estIdx[np] = 3; data.dfunc[np++] = df_ds;}
    if (strchr(mode, 'c')!=NULL) {data.estIdx[np] = 4; data.dfunc[np++] = df_dc;}
    
    data.x_init = (double*)malloc(sizeof(double)*np);
    for (i=0; i<np; ++i) {
        data.x_init[i] = data.prmVect[data.estIdx[i]];
    }
    
    MLalgo(&data);
    
    /* parameters */
    if (nlhs > 0) {
        plhs[0] = mxCreateDoubleMatrix(1, NPARAMS, mxREAL);
        memcpy(mxGetPr(plhs[0]), data.prmVect, NPARAMS*sizeof(double));
    }
    
    /* standard dev. of parameters & covariance matrix */
    double RSS = 0.0;
    double* resValid = NULL;
    if (nlhs > 1) {    
        resValid = (double*)malloc(data.nValid*sizeof(double));
        for (i=0; i<data.nValid; ++i) {
            resValid[i] = gsl_vector_get(data.residuals, i);
            RSS += resValid[i]*resValid[i];
        }
        gsl_matrix *covar = gsl_matrix_alloc(np, np);
        gsl_multifit_covar(data.J, 0.0, covar);
        double iRSS = RSS/(data.nValid - data.np - 1);
        plhs[1] = mxCreateDoubleMatrix(1, data.np, mxREAL);
        double *prmStd = mxGetPr(plhs[1]);
        for (i=0; i<data.np; ++i) {
            prmStd[i] = sqrt(iRSS*gsl_matrix_get(covar, i, i));
        }
        if (nlhs > 2) {
            plhs[2] = mxCreateDoubleMatrix(np, np, mxREAL);
            /* cov. matrix is symmetric, no need to transpose */
            memcpy(mxGetPr(plhs[2]), covar->data, np*np*sizeof(double));
        }
        gsl_matrix_free(covar);
    }
    
    /* residuals */
    if (nlhs > 3) {
        const char *fieldnames[] = {"data", "hAD", "mean", "std", "RSS"};
        mwSize dims[2] = {1, 1};
        plhs[3] = mxCreateStructArray(2, dims, 5, fieldnames);
        mxArray *val = mxCreateDoubleMatrix(nx, nx, mxREAL);
        double* res = mxGetPr(val);
        
        double mean = 0.0, std = 0.0;
        for (i=0; i<data.nValid; ++i) {
            res[data.idx[i]] = resValid[i];
            mean += resValid[i];
        }
        std = sqrt((RSS-mean*mean/data.nValid)/(data.nValid-1));
        mean /= data.nValid;

        for (i=0; i<N-data.nValid; ++i) {
            res[nanIdx[i]] = mxGetNaN();
        }
        
        // A-D test, case 2: mean known
        unsigned char hAD = adtest(resValid, data.nValid, 2, 0.0, std, 0.05);
        mxSetFieldByNumber(plhs[3], 0, 0, val);
        mxSetFieldByNumber(plhs[3], 0, 1, mxCreateLogicalScalar(hAD));
        mxSetFieldByNumber(plhs[3], 0, 2, mxCreateDoubleScalar(mean));
        mxSetFieldByNumber(plhs[3], 0, 3, mxCreateDoubleScalar(std));
        mxSetFieldByNumber(plhs[3], 0, 4, mxCreateDoubleScalar(RSS));
    }
    
    /* Jacobian */
    if (nlhs > 4) {
        /* convert row-major double* data.J->data to column-major double* */
        plhs[4] = mxCreateDoubleMatrix(N, np, mxREAL);
        double *J = mxGetPr(plhs[4]);
        int k;
        for (k=0; k<np; ++k) {
            for (i=0; i<data.nValid; ++i) {
                J[data.idx[i]+k*N] = gsl_matrix_get(data.J, i, k);
            }
            for (i=0; i<N-data.nValid; ++i) {
                J[nanIdx[i]+k*N] = mxGetNaN();
            }
        }
    }
    
    free(resValid);
    gsl_matrix_free(data.J);
    gsl_vector_free(data.residuals);
    free(data.x_init);
    free(nanIdx);
    free(data.idx);
    free(data.dfunc);
    free(data.estIdx);
    free(data.gy);
    free(data.gx);
    free(mode);
}
Exemplo n.º 19
0
static int
lmniel_covar(void *vstate, const double epsrel, gsl_matrix * covar)
{
  lmniel_state_t *state = (lmniel_state_t *) vstate;
  return gsl_multifit_covar(state->J, epsrel, covar);
}
Exemplo n.º 20
0
void
test_fdf (const char * name, gsl_multifit_function_fdf * f, 
          double x0[], double x_final[], 
          double f_sumsq, double sigma[])
{
  const gsl_multifit_fdfsolver_type *T;
  gsl_multifit_fdfsolver *s;

  const size_t n = f->n;
  const size_t p = f->p;

  int status;
  size_t iter = 0;

  gsl_vector_view x = gsl_vector_view_array (x0, p);

  T = gsl_multifit_fdfsolver_lmsder;
  s = gsl_multifit_fdfsolver_alloc (T, n, p);
  gsl_multifit_fdfsolver_set (s, f, &x.vector);

  do
    {
      status = gsl_multifit_fdfsolver_iterate (s);

#ifdef DEBUG
       printf("iter = %d  status = %d  |f| = %.18e x = \n", 
         iter, status, gsl_blas_dnrm2 (s->f));
         
         gsl_vector_fprintf(stdout, s->x, "%.8e");
#endif       
      status = gsl_multifit_test_delta (s->dx, s->x, 0.0, 1e-7);

      iter++;
    }
  while (status == GSL_CONTINUE && iter < 1000);
  
  {
    size_t i;
    gsl_matrix * covar = gsl_matrix_alloc (p, p);
    gsl_multifit_covar (s->J, 0.0, covar);

    for (i = 0 ; i < p; i++)
      {
        gsl_test_rel (gsl_vector_get (s->x, i), x_final[i], 1e-5, 
                      "%s, lmsder, x%u", name, i);
      }


    {
      double s2 = pow(gsl_blas_dnrm2 (s->f), 2.0);

      gsl_test_rel (s2, f_sumsq, 1e-5, "%s, lmsder, |f|^2", name);

      for (i = 0; i < p; i++) 
        {
          double ei = sqrt(s2/(n-p))*sqrt(gsl_matrix_get(covar,i,i));
          gsl_test_rel (ei, sigma[i], 1e-4, 
                        "%s, sigma(%d)", name, i) ;
        }
    }

    gsl_matrix_free (covar);
  }

  gsl_multifit_fdfsolver_free (s);
}
Exemplo n.º 21
0
int
main (void)
{
  const gsl_multifit_fdfsolver_type *T;
  gsl_multifit_fdfsolver *s;
  int status;
  unsigned int i, iter = 0;
  const size_t n = N;
  const size_t p = 3;

  gsl_matrix *covar = gsl_matrix_alloc (p, p);
  double y[N], sigma[N];
  struct data d = { n, y, sigma};
  gsl_multifit_function_fdf f;
  double x_init[3] = { 1.0, 0.0, 0.0 };
  gsl_vector_view x = gsl_vector_view_array (x_init, p);
  const gsl_rng_type * type;
  gsl_rng * r;

  gsl_rng_env_setup();

  type = gsl_rng_default;
  r = gsl_rng_alloc (type);

  f.f = &expb_f;
  f.df = &expb_df;
  f.fdf = &expb_fdf;
  f.n = n;
  f.p = p;
  f.params = &d;

  /* This is the data to be fitted */

  for (i = 0; i < n; i++)
    {
      double t = i;
      y[i] = 1.0 + 5 * exp (-0.1 * t) 
                 + gsl_ran_gaussian (r, 0.1);
      sigma[i] = 0.1;
      printf ("data: %u %g %g\n", i, y[i], sigma[i]);
    };

  T = gsl_multifit_fdfsolver_lmsder;
  s = gsl_multifit_fdfsolver_alloc (T, n, p);
  gsl_multifit_fdfsolver_set (s, &f, &x.vector);

  print_state (iter, s);

  do
    {
      iter++;
      status = gsl_multifit_fdfsolver_iterate (s);

      printf ("status = %s\n", gsl_strerror (status));

      print_state (iter, s);

      if (status)
        break;

      status = gsl_multifit_test_delta (s->dx, s->x,
                                        1e-4, 1e-4);
    }
  while (status == GSL_CONTINUE && iter < 500);

  gsl_multifit_covar (s->J, 0.0, covar);

#define FIT(i) gsl_vector_get(s->x, i)
#define ERR(i) sqrt(gsl_matrix_get(covar,i,i))

  { 
    double chi = gsl_blas_dnrm2(s->f);
    double dof = n - p;
    double c = GSL_MAX_DBL(1, chi / sqrt(dof)); 

    printf("chisq/dof = %g\n",  pow(chi, 2.0) / dof);

    printf ("A      = %.5f +/- %.5f\n", FIT(0), c*ERR(0));
    printf ("lambda = %.5f +/- %.5f\n", FIT(1), c*ERR(1));
    printf ("b      = %.5f +/- %.5f\n", FIT(2), c*ERR(2));
  }

  printf ("status = %s\n", gsl_strerror (status));

  gsl_multifit_fdfsolver_free (s);
  gsl_matrix_free (covar);
  gsl_rng_free (r);
  return 0;
}
/*******************************************************************************
 * fit_gaussian
 * Fit data to a guassian and return the results. Ideally, this should give the
 * same results as scipy.optimize.curve_fit.
 * Input:
 *      hist:           Histogram to fit the gaussian to
 * Output:
 *      chisq:          Chi^2 of the histogram
 *      ndf:            Number of degrees of freedom of the fit
 *      fit_params:     Fit parameters
 ******************************************************************************/
gsl_vector *fit_gaussian(gsl_histogram *hist,
        double *chisq, long *ndf, gsl_matrix *covar){
    double tol;
    double *hbin, *hrange, bin_width, xdata, min, max;
    double magnitude, mean, sigma;
    double error, ythr;
    int status;
    long gpars, nonzero, nbins;
    long i;
    gsl_vector *pars, *fit_params;
    gsl_multifit_fdfsolver *gfit;
    gsl_multifit_function_fdf gaus;
    const gsl_multifit_fdfsolver_type *ftype;

    /* Allowed relative error is what scipy uses */
    /* tol = 1.49012e-8; scipy least squares default */
    tol = 1e-14;

    /* get number of bins containing data */
    nbins = hist -> n;
    hbin = hist -> bin;
    hrange = hist -> range;
    nonzero = 0;
    for (i=0; i<nbins; i++){
        if (hbin[i]) nonzero++;
    }

    /* Set the function */
    gaus.f = &gaus_f;
    gaus.df = &gaus_df;
    gaus.fdf = &gaus_fdf;
    gaus.n = nonzero;
    gaus.p = 3;
    gaus.params = hist;

    /* Initialize the solver */
    gpars = 3;
    pars = gsl_vector_alloc(gpars);
    gsl_vector_set_all(pars, 1.0);
    ftype = gsl_multifit_fdfsolver_lmsder;
    gfit = gsl_multifit_fdfsolver_alloc(ftype, nonzero, gpars);
    gsl_multifit_fdfsolver_set(gfit, &gaus, pars);

    /* loop the solver and solve this thing */
    do {
        status = gsl_multifit_fdfsolver_iterate(gfit);
        status = gsl_multifit_test_delta(gfit -> dx, gfit -> x, 0, tol);
    } while (status == GSL_CONTINUE);

    magnitude = gsl_vector_get(gfit -> x, 0);
    mean = gsl_vector_get(gfit -> x, 1);
    /* The fitted sigma might be negative, but it is squared when computing the
     * gaussian, so taking the absolute value of sigma is ok */
    sigma = fabs(gsl_vector_get(gfit -> x, 2));

    /* Compute the chi^2 */
    min = hrange[0];
    max = hrange[nbins];
    bin_width = (max - min) / nbins;
    *chisq = 0;
    for (i = 0; i<nbins; i++){
        if (hbin[i]){
            xdata = hrange[i] + bin_width/2.0;
            error = sqrt(hbin[i]);
            ythr = gaussian(xdata, magnitude, mean, sigma);
            *chisq += pow((hbin[i] - ythr)/error, 2);
        }
    }
    *ndf = nonzero - gpars;

    /* Copy results to return vector */
    fit_params = gsl_vector_alloc(gpars);
    gsl_vector_memcpy(fit_params, gfit -> x);

    /* Compute the covariance matrix */
    gsl_multifit_covar(gfit -> J, 0.0, covar);

    /* Free the solver's memory */
    gsl_vector_free(pars);
    gsl_multifit_fdfsolver_free(gfit);

    /* Return the results of the fit */
    return fit_params;
}
/* Calculates covariance matrix
 *
 * @param epsrel :: Is used to remove linear-dependent columns
 * @param covar :: Returned covariance matrix, here as 
 */
void LevenbergMarquardtMinimizer::calCovarianceMatrix(double epsrel, gsl_matrix * covar)
{
  gsl_multifit_covar (m_gslSolver->J, epsrel, covar);
}
Exemplo n.º 24
0
//Fitting. Allow fitting multiple q curves simultaneously to decrease the chance of converging to local minimum.
void ddm::fitting()
{
    int cnum_fit=num_fit;
    int ctimeWindow=timeWindow;
    //Find the truncation time if time window is set
    for (int itert=0; itert<num_fit; ++itert)
    {
        if (tau[itert]>ctimeWindow)
        {
            cnum_fit=itert;
            break;
        }
    }
    
    //Local variables
    int cqsize=qsize-qIncreList[num_qCurve-1];  //number of fitting result
    int cnum_qCurve=num_qCurve;
    int ctnum_fit=cnum_fit*num_qCurve;
    int cnumOfPara=numOfPara+2*num_qCurve;  //Total number of parameters
    
    fittedPara=gsl_matrix_alloc(cqsize, cnumOfPara);
    //To store the fitting result and error.
    fitErr=gsl_matrix_alloc(cqsize, cnumOfPara);
    status = new int[cqsize];		//Record the status of fitting.
    
    //Using Levenberg-Marquardt algorithm as implemented in the scaled lmder routine in minpack. Jacobian is given.
    const gsl_multifit_fdfsolver_type *solverType = gsl_multifit_fdfsolver_lmsder;
    
    int progress=0;		//Indicator of progress.
    
    //Objects to do numerical inverse Laplace transformation
#ifdef ISFRTD
    NILT NILT1(OMP_NUM_THREADS), NILT2(OMP_NUM_THREADS), NILT3(OMP_NUM_THREADS), NILT4(OMP_NUM_THREADS);
#endif
    
#ifdef ISFRTDP
    NILT NILT1(OMP_NUM_THREADS), NILT2(OMP_NUM_THREADS), NILT3(OMP_NUM_THREADS), NILT4(OMP_NUM_THREADS), NILT5(OMP_NUM_THREADS);
#endif
    
#ifdef ISFRTDPTT
    NILT NILT1(OMP_NUM_THREADS), NILT2(OMP_NUM_THREADS), NILT3(OMP_NUM_THREADS), NILT4(OMP_NUM_THREADS), NILT5(OMP_NUM_THREADS), NILT6(OMP_NUM_THREADS);
#endif
    
#ifdef ISFRTDPfix
    NILT NILT1(OMP_NUM_THREADS), NILT2(OMP_NUM_THREADS);
    
    const long double vbar=vbarGuess;
    const long double sigma=sigmaGuess;
    
    const long double vbsigma2=vbar/sigma/sigma;
    const long double vb2sigma2=vbsigma2*vbar;
    const long double logvbsigma2=log(vbsigma2);
    const long double logfactor=vb2sigma2*logvbsigma2-gsl_sf_lngamma(vb2sigma2);
    const long double cpsiz1=logvbsigma2-gsl_sf_psi(vb2sigma2);
    const long double vb2sigma3=vb2sigma2/sigma;
#endif
    
#ifdef ISFRTDPTTfix
    NILT NILT1(OMP_NUM_THREADS), NILT2(OMP_NUM_THREADS), NILT3(OMP_NUM_THREADS);
    
    const long double vbar=vbarGuess;
    const long double sigma=sigmaGuess;
    
    const long double vbsigma2=vbar/sigma/sigma;
    const long double vb2sigma2=vbsigma2*vbar;
    const long double logvbsigma2=log(vbsigma2);
    const long double logfactor=vb2sigma2*logvbsigma2-gsl_sf_lngamma(vb2sigma2);
    const long double cpsiz1=logvbsigma2-gsl_sf_psi(vb2sigma2);
    const long double vb2sigma3=vb2sigma2/sigma;
#endif
    
#pragma omp parallel for
    for (int iterq=0; iterq<cqsize; ++iterq)
    {
        //Data array which is going to present to the fitting algorithm
        double* datafit=new double[ctnum_fit];
        double* qList=new double[cnum_qCurve];
        double* time=new double[ctnum_fit];
        //Truncate the data, and put multiple curves into one array
        for (int iterqc=0; iterqc<cnum_qCurve; ++iterqc)
        {
            for (int iterf = 0; iterf < cnum_fit; ++iterf)
            {
                datafit[iterf+iterqc*cnum_fit]=(gsl_matrix_get(datag, iterq+qIncreList[iterqc], iterf));		//Fitting in log scale.
                time[iterf+iterqc*cnum_fit]=tau[iterf];
            }
            qList[iterqc]=qabs[iterq+qIncreList[iterqc]];
        }
        
        gsl_multifit_function_fdf fitfun;		//Pointer of function to fit.
        dataStruct sdata;		//GSL data structure
        
        //Data is passed to ISFfun by sdata
        sdata.data=datafit;
        sdata.tau=time;
        sdata.q=qList;
        sdata.num_fit=cnum_fit;
        sdata.num_qCurve=cnum_qCurve;
        
#ifdef ISFRTD
        sdata.ISFILT=&NILT1;
        sdata.dvISFILT=&NILT2;
        sdata.dDISFILT=&NILT3;
        sdata.dlambdaISFILT=&NILT4;
#endif
        
#ifdef ISFRTDP
        sdata.ISFILT=&NILT1;
        sdata.dvbarISFILT=&NILT2;
        sdata.dsigmaISFILT=&NILT3;
        sdata.dDISFILT=&NILT4;
        sdata.dlambdaISFILT=&NILT5;
#endif
        
#ifdef ISFRTDPTT
        sdata.ISFILT=&NILT1;
        sdata.dvbarISFILT=&NILT2;
        sdata.dsigmaISFILT=&NILT3;
        sdata.dDISFILT=&NILT4;
        sdata.dlambdaISFILT=&NILT5;
        sdata.dTTISFILT=&NILT6;
#endif
        
#ifdef ISFRTDPfix
        sdata.alpha=alphaGuess;
        sdata.D=DGuess;
        sdata.vbar=vbar;
        sdata.sigma=sigma;
        
        sdata.vbsigma2=vbsigma2;
        sdata.logfactor=logfactor;
        sdata.vb2sigma2=vb2sigma2;
        sdata.cpsiz1=cpsiz1;
        sdata.vb2sigma3=vb2sigma3;
        sdata.ISFILT=&NILT1;
        sdata.dlambdaISFILT=&NILT2;
#endif
        
#ifdef ISFRTDPTTfix
        sdata.alpha=alphaGuess;
        sdata.D=DGuess;
        sdata.vbar=vbar;
        sdata.sigma=sigma;
        
        sdata.vbsigma2=vbsigma2;
        sdata.logfactor=logfactor;
        sdata.vb2sigma2=vb2sigma2;
        sdata.cpsiz1=cpsiz1;
        sdata.vb2sigma3=vb2sigma3;
        sdata.ISFILT=&NILT1;
        sdata.dlambdaISFILT=&NILT2;
        sdata.dTTISFILT=&NILT3;
#endif
        
        //API
        fitfun.f=&ISFfun;
#ifdef NoJacobian
        fitfun.df=0;
        fitfun.fdf=0;
#else
        fitfun.df=&dISFfun;
        fitfun.fdf=&fdISFfun;
#endif
        fitfun.n=ctnum_fit;
        fitfun.p=cnumOfPara;
        fitfun.params=&sdata;
        
        //Initialization of the parameters
        double* localinipara=new double[cnumOfPara];
        for (int iterp=0; iterp<numOfPara; ++iterp)
        {
            localinipara[iterp]=inipara[iterp];
        }
        //Estimation of A(q) and B(q)
        for (int iterqc=0; iterqc<num_qCurve; ++iterqc)
        {
            localinipara[numOfPara+1+2*iterqc] = gsl_matrix_get(datag, iterq+qIncreList[iterqc], 0);
            localinipara[numOfPara+2*iterqc] = gsl_matrix_get(datag, iterq+qIncreList[iterqc], num_fit-1)-localinipara[numOfPara+1+2*iterqc];
        }
        //Initiallization of the solver
        gsl_vector_view para=gsl_vector_view_array(localinipara, cnumOfPara);
        gsl_multifit_fdfsolver* solver = gsl_multifit_fdfsolver_alloc(solverType, ctnum_fit, cnumOfPara);
        gsl_multifit_fdfsolver_set(solver, &fitfun, &para.vector);
        int iter=0;
        //gsl_vector* g=gsl_vector_alloc(numOfPara);
        
        //For debugging and monitering the iterations
//        cout << qList[0] << ' ' << qList[1] << '\n';
//        for (int iterpara=0; iterpara<cnumOfPara; ++iterpara)
//        {
//            cout << gsl_vector_get(solver->x, iterpara) << '\n';
//        }
//        cout << '\n';
        
        int cstatus=GSL_CONTINUE;   //Current status
        do
        {
            gsl_multifit_fdfsolver_iterate(solver);		//Iterate one step.
            cstatus = norm0_rel_test(solver->dx, solver->x, 1e-7, 1e-7);  //Test the exiting criteria
            
            //For debugging and monitering the iterations
//            for (int iterpara=0; iterpara<cnumOfPara; ++iterpara)
//            {
//                cout << gsl_vector_get(solver->x, iterpara) << '\n';
//            }
//            cout << '\n';
            
            //If to use other exiting criteria
            //gsl_multifit_gradient(solver->J,solver->f, g);
            //status[iterq-1]=gsl_multifit_test_gradient(g, 1e-5);
            //			status[iterq - 1] = covar_rel_test(solver->J, solver->x, 1e-4);
            
            ++iter;
            //Number of iterations exceed certain limitation
            if (iter>maxIter)
            {
                cstatus=GSL_EMAXITER;
            }
        } while (cstatus == GSL_CONTINUE);
        status[iterq]=cstatus;
        //gsl_vector_free(g);
        
        //Estimating the error.
        gsl_matrix* covar=gsl_matrix_alloc(cnumOfPara, cnumOfPara);
        gsl_multifit_covar(solver->J, 0.0, covar);
        for (int iterpara=0; iterpara<cnumOfPara; ++iterpara)	//Record result.
        {
            gsl_matrix_set(fittedPara, iterq, iterpara, gsl_vector_get(solver->x, iterpara) );
            gsl_matrix_set(fitErr, iterq, iterpara, sqrt(gsl_matrix_get(covar, iterpara, iterpara)) );    //Not presice in log scale
        }
        gsl_matrix_free(covar);
        gsl_multifit_fdfsolver_free(solver);
        
        //Output to standard I/O
        progress+=1;
        cout << "Fitted q=" << qabs[iterq] << " at iter=" << iter << ", " << 100.0*progress / qsize << "% completed from thread No." << omp_get_thread_num() << ", "<< gsl_strerror(status[iterq]) << "." << '\n';
        for (int iterpara=0; iterpara<cnumOfPara; ++iterpara)
        {
            cout << gsl_matrix_get(fittedPara, iterq, iterpara) << '\n';
        }
        cout << '\n';
        delete [] datafit;
        delete [] qList;
        delete [] localinipara;
        delete [] time;
    }
}
Exemplo n.º 25
0
/** Executes the algorithm
 *
 *  @throw runtime_error Thrown if algorithm cannot execute
 */
void Fit1D::exec() {

  // Custom initialization
  prepare();

  // check if derivative defined in derived class
  bool isDerivDefined = true;
  gsl_matrix *M = NULL;
  try {
    const std::vector<double> inTest(m_parameterNames.size(), 1.0);
    std::vector<double> outTest(m_parameterNames.size());
    const double xValuesTest = 0;
    JacobianImpl J;
    M = gsl_matrix_alloc(m_parameterNames.size(), 1);
    J.setJ(M);
    // note nData set to zero (last argument) hence this should avoid further
    // memory problems
    functionDeriv(&(inTest.front()), &J, &xValuesTest, 0);
  } catch (Exception::NotImplementedError &) {
    isDerivDefined = false;
  }
  gsl_matrix_free(M);

  // Try to retrieve optional properties
  int histNumber = getProperty("WorkspaceIndex");
  const int maxInterations = getProperty("MaxIterations");

  // Get the input workspace
  MatrixWorkspace_const_sptr localworkspace = getProperty("InputWorkspace");

  // number of histogram is equal to the number of spectra
  const size_t numberOfSpectra = localworkspace->getNumberHistograms();
  // Check that the index given is valid
  if (histNumber >= static_cast<int>(numberOfSpectra)) {
    g_log.warning("Invalid Workspace index given, using first Workspace");
    histNumber = 0;
  }

  // Retrieve the spectrum into a vector
  const MantidVec &XValues = localworkspace->readX(histNumber);
  const MantidVec &YValues = localworkspace->readY(histNumber);
  const MantidVec &YErrors = localworkspace->readE(histNumber);

  // Read in the fitting range data that we were sent
  double startX = getProperty("StartX");
  double endX = getProperty("EndX");
  // check if the values had been set, otherwise use defaults
  if (isEmpty(startX)) {
    startX = XValues.front();
    modifyStartOfRange(startX); // does nothing by default but derived class may
                                // provide a more intelligent value
  }
  if (isEmpty(endX)) {
    endX = XValues.back();
    modifyEndOfRange(endX); // does nothing by default but derived class may
                            // previde a more intelligent value
  }

  int m_minX;
  int m_maxX;

  // Check the validity of startX
  if (startX < XValues.front()) {
    g_log.warning("StartX out of range! Set to start of frame.");
    startX = XValues.front();
  }
  // Get the corresponding bin boundary that comes before (or coincides with)
  // this value
  for (m_minX = 0; XValues[m_minX + 1] < startX; ++m_minX) {
  }

  // Check the validity of endX and get the bin boundary that come after (or
  // coincides with) it
  if (endX >= XValues.back() || endX < startX) {
    g_log.warning("EndX out of range! Set to end of frame");
    endX = XValues.back();
    m_maxX = static_cast<int>(YValues.size());
  } else {
    for (m_maxX = m_minX; XValues[m_maxX] < endX; ++m_maxX) {
    }
  }

  afterDataRangedDetermined(m_minX, m_maxX);

  // create and populate GSL data container warn user if l_data.n < l_data.p
  // since as a rule of thumb this is required as a minimum to obtained
  // 'accurate'
  // fitting parameter values.

  FitData l_data(this, getProperty("Fix"));

  l_data.n =
      m_maxX -
      m_minX; // m_minX and m_maxX are array index markers. I.e. e.g. 0 & 19.
  if (l_data.n == 0) {
    g_log.error("The data set is empty.");
    throw std::runtime_error("The data set is empty.");
  }
  if (l_data.n < l_data.p) {
    g_log.error(
        "Number of data points less than number of parameters to be fitted.");
    throw std::runtime_error(
        "Number of data points less than number of parameters to be fitted.");
  }
  l_data.X = new double[l_data.n];
  l_data.sigmaData = new double[l_data.n];
  l_data.forSimplexLSwrap = new double[l_data.n];
  l_data.parameters = new double[nParams()];

  // check if histogram data in which case use mid points of histogram bins

  const bool isHistogram = localworkspace->isHistogramData();
  for (unsigned int i = 0; i < l_data.n; ++i) {
    if (isHistogram)
      l_data.X[i] =
          0.5 * (XValues[m_minX + i] +
                 XValues[m_minX + i + 1]); // take mid-point if histogram bin
    else
      l_data.X[i] = XValues[m_minX + i];
  }

  l_data.Y = &YValues[m_minX];

  // check that no error is negative or zero
  for (unsigned int i = 0; i < l_data.n; ++i) {
    if (YErrors[m_minX + i] <= 0.0) {
      l_data.sigmaData[i] = 1.0;
    } else
      l_data.sigmaData[i] = YErrors[m_minX + i];
  }

  // create array of fitted parameter. Take these to those input by the user.
  // However, for doing the
  // underlying fitting it might be more efficient to actually perform the
  // fitting on some of other
  // form of the fitted parameters. For instance, take the Gaussian sigma
  // parameter. In practice it
  // in fact more efficient to perform the fitting not on sigma but 1/sigma^2.
  // The methods
  // modifyInitialFittedParameters() and modifyFinalFittedParameters() are used
  // to allow for this;
  // by default these function do nothing.

  m_fittedParameter.clear();
  for (size_t i = 0; i < nParams(); i++) {
    m_fittedParameter.push_back(getProperty(m_parameterNames[i]));
  }
  modifyInitialFittedParameters(
      m_fittedParameter); // does nothing except if overwritten by derived class
  for (size_t i = 0; i < nParams(); i++) {
    l_data.parameters[i] = m_fittedParameter[i];
  }

  // set-up initial guess for fit parameters

  gsl_vector *initFuncArg;
  initFuncArg = gsl_vector_alloc(l_data.p);

  for (size_t i = 0, j = 0; i < nParams(); i++) {
    if (l_data.active[i])
      gsl_vector_set(initFuncArg, j++, m_fittedParameter[i]);
  }

  // set-up GSL container to be used with GSL simplex algorithm

  gsl_multimin_function gslSimplexContainer;
  gslSimplexContainer.n = l_data.p; // n here refers to number of parameters
  gslSimplexContainer.f = &gsl_costFunction;
  gslSimplexContainer.params = &l_data;

  // set-up GSL least squares container

  gsl_multifit_function_fdf f;
  f.f = &gsl_f;
  f.df = &gsl_df;
  f.fdf = &gsl_fdf;
  f.n = l_data.n;
  f.p = l_data.p;
  f.params = &l_data;

  // set-up remaining GSL machinery for least squared

  const gsl_multifit_fdfsolver_type *T = gsl_multifit_fdfsolver_lmsder;
  gsl_multifit_fdfsolver *s = NULL;
  if (isDerivDefined) {
    s = gsl_multifit_fdfsolver_alloc(T, l_data.n, l_data.p);
    gsl_multifit_fdfsolver_set(s, &f, initFuncArg);
  }

  // set-up remaining GSL machinery to use simplex algorithm

  const gsl_multimin_fminimizer_type *simplexType =
      gsl_multimin_fminimizer_nmsimplex;
  gsl_multimin_fminimizer *simplexMinimizer = NULL;
  gsl_vector *simplexStepSize = NULL;
  if (!isDerivDefined) {
    simplexMinimizer = gsl_multimin_fminimizer_alloc(simplexType, l_data.p);
    simplexStepSize = gsl_vector_alloc(l_data.p);
    gsl_vector_set_all(simplexStepSize,
                       1.0); // is this always a sensible starting step size?
    gsl_multimin_fminimizer_set(simplexMinimizer, &gslSimplexContainer,
                                initFuncArg, simplexStepSize);
  }

  // finally do the fitting

  int iter = 0;
  int status;
  double finalCostFuncVal;
  double dof = static_cast<double>(
      l_data.n - l_data.p); // dof stands for degrees of freedom

  // Standard least-squares used if derivative function defined otherwise
  // simplex
  Progress prog(this, 0.0, 1.0, maxInterations);
  if (isDerivDefined) {

    do {
      iter++;
      status = gsl_multifit_fdfsolver_iterate(s);

      if (status) // break if error
        break;

      status = gsl_multifit_test_delta(s->dx, s->x, 1e-4, 1e-4);
      prog.report();
    } while (status == GSL_CONTINUE && iter < maxInterations);

    double chi = gsl_blas_dnrm2(s->f);
    finalCostFuncVal = chi * chi / dof;

    // put final converged fitting values back into m_fittedParameter
    for (size_t i = 0, j = 0; i < nParams(); i++)
      if (l_data.active[i])
        m_fittedParameter[i] = gsl_vector_get(s->x, j++);
  } else {
    do {
      iter++;
      status = gsl_multimin_fminimizer_iterate(simplexMinimizer);

      if (status) // break if error
        break;

      double size = gsl_multimin_fminimizer_size(simplexMinimizer);
      status = gsl_multimin_test_size(size, 1e-2);
      prog.report();
    } while (status == GSL_CONTINUE && iter < maxInterations);

    finalCostFuncVal = simplexMinimizer->fval / dof;

    // put final converged fitting values back into m_fittedParameter
    for (unsigned int i = 0, j = 0; i < m_fittedParameter.size(); i++)
      if (l_data.active[i])
        m_fittedParameter[i] = gsl_vector_get(simplexMinimizer->x, j++);
  }

  modifyFinalFittedParameters(
      m_fittedParameter); // do nothing except if overwritten by derived class

  // Output summary to log file

  std::string reportOfFit = gsl_strerror(status);

  g_log.information() << "Iteration = " << iter << "\n"
                      << "Status = " << reportOfFit << "\n"
                      << "Chi^2/DoF = " << finalCostFuncVal << "\n";
  for (size_t i = 0; i < m_fittedParameter.size(); i++)
    g_log.information() << m_parameterNames[i] << " = " << m_fittedParameter[i]
                        << "  \n";

  // also output summary to properties

  setProperty("OutputStatus", reportOfFit);
  setProperty("OutputChi2overDoF", finalCostFuncVal);
  for (size_t i = 0; i < m_fittedParameter.size(); i++)
    setProperty(m_parameterNames[i], m_fittedParameter[i]);

  std::string output = getProperty("Output");
  if (!output.empty()) {
    // calculate covariance matrix if derivatives available

    gsl_matrix *covar(NULL);
    std::vector<double> standardDeviations;
    std::vector<double> sdExtended;
    if (isDerivDefined) {
      covar = gsl_matrix_alloc(l_data.p, l_data.p);
      gsl_multifit_covar(s->J, 0.0, covar);

      int iPNotFixed = 0;
      for (size_t i = 0; i < nParams(); i++) {
        sdExtended.push_back(1.0);
        if (l_data.active[i]) {
          sdExtended[i] = sqrt(gsl_matrix_get(covar, iPNotFixed, iPNotFixed));
          iPNotFixed++;
        }
      }
      modifyFinalFittedParameters(sdExtended);
      for (size_t i = 0; i < nParams(); i++)
        if (l_data.active[i])
          standardDeviations.push_back(sdExtended[i]);

      declareProperty(
          new WorkspaceProperty<API::ITableWorkspace>(
              "OutputNormalisedCovarianceMatrix", "", Direction::Output),
          "The name of the TableWorkspace in which to store the final "
          "covariance matrix");
      setPropertyValue("OutputNormalisedCovarianceMatrix",
                       output + "_NormalisedCovarianceMatrix");

      Mantid::API::ITableWorkspace_sptr m_covariance =
          Mantid::API::WorkspaceFactory::Instance().createTable(
              "TableWorkspace");
      m_covariance->addColumn("str", "Name");
      std::vector<std::string>
          paramThatAreFitted; // used for populating 1st "name" column
      for (size_t i = 0; i < nParams(); i++) {
        if (l_data.active[i]) {
          m_covariance->addColumn("double", m_parameterNames[i]);
          paramThatAreFitted.push_back(m_parameterNames[i]);
        }
      }

      for (size_t i = 0; i < l_data.p; i++) {

        Mantid::API::TableRow row = m_covariance->appendRow();
        row << paramThatAreFitted[i];
        for (size_t j = 0; j < l_data.p; j++) {
          if (j == i)
            row << 1.0;
          else {
            row << 100.0 * gsl_matrix_get(covar, i, j) /
                       sqrt(gsl_matrix_get(covar, i, i) *
                            gsl_matrix_get(covar, j, j));
          }
        }
      }

      setProperty("OutputNormalisedCovarianceMatrix", m_covariance);
    }

    declareProperty(new WorkspaceProperty<API::ITableWorkspace>(
                        "OutputParameters", "", Direction::Output),
                    "The name of the TableWorkspace in which to store the "
                    "final fit parameters");
    declareProperty(
        new WorkspaceProperty<MatrixWorkspace>("OutputWorkspace", "",
                                               Direction::Output),
        "Name of the output Workspace holding resulting simlated spectrum");

    setPropertyValue("OutputParameters", output + "_Parameters");
    setPropertyValue("OutputWorkspace", output + "_Workspace");

    // Save the final fit parameters in the output table workspace
    Mantid::API::ITableWorkspace_sptr m_result =
        Mantid::API::WorkspaceFactory::Instance().createTable("TableWorkspace");
    m_result->addColumn("str", "Name");
    m_result->addColumn("double", "Value");
    if (isDerivDefined)
      m_result->addColumn("double", "Error");
    Mantid::API::TableRow row = m_result->appendRow();
    row << "Chi^2/DoF" << finalCostFuncVal;

    for (size_t i = 0; i < nParams(); i++) {
      Mantid::API::TableRow row = m_result->appendRow();
      row << m_parameterNames[i] << m_fittedParameter[i];
      if (isDerivDefined && l_data.active[i]) {
        // perhaps want to scale standard deviations with sqrt(finalCostFuncVal)
        row << sdExtended[i];
      }
    }
    setProperty("OutputParameters", m_result);

    // Save the fitted and simulated spectra in the output workspace
    MatrixWorkspace_const_sptr inputWorkspace = getProperty("InputWorkspace");
    int iSpec = getProperty("WorkspaceIndex");
    const MantidVec &inputX = inputWorkspace->readX(iSpec);
    const MantidVec &inputY = inputWorkspace->readY(iSpec);

    int histN = isHistogram ? 1 : 0;
    Mantid::DataObjects::Workspace2D_sptr ws =
        boost::dynamic_pointer_cast<Mantid::DataObjects::Workspace2D>(
            Mantid::API::WorkspaceFactory::Instance().create(
                "Workspace2D", 3, l_data.n + histN, l_data.n));
    ws->setTitle("");
    ws->getAxis(0)->unit() =
        inputWorkspace->getAxis(0)
            ->unit(); //    UnitFactory::Instance().create("TOF");

    for (int i = 0; i < 3; i++)
      ws->dataX(i)
          .assign(inputX.begin() + m_minX, inputX.begin() + m_maxX + histN);

    ws->dataY(0).assign(inputY.begin() + m_minX, inputY.begin() + m_maxX);

    MantidVec &Y = ws->dataY(1);
    MantidVec &E = ws->dataY(2);

    double *lOut =
        new double[l_data.n]; // to capture output from call to function()
    modifyInitialFittedParameters(m_fittedParameter); // does nothing except if
                                                      // overwritten by derived
                                                      // class
    function(&m_fittedParameter[0], lOut, l_data.X, l_data.n);
    modifyInitialFittedParameters(m_fittedParameter); // reverse the effect of
    // modifyInitialFittedParameters - if any

    for (unsigned int i = 0; i < l_data.n; i++) {
      Y[i] = lOut[i];
      E[i] = l_data.Y[i] - Y[i];
    }

    delete[] lOut;

    setProperty("OutputWorkspace",
                boost::dynamic_pointer_cast<MatrixWorkspace>(ws));

    if (isDerivDefined)
      gsl_matrix_free(covar);
  }

  // clean up dynamically allocated gsl stuff

  if (isDerivDefined)
    gsl_multifit_fdfsolver_free(s);
  else {
    gsl_vector_free(simplexStepSize);
    gsl_multimin_fminimizer_free(simplexMinimizer);
  }

  delete[] l_data.X;
  delete[] l_data.sigmaData;
  delete[] l_data.forSimplexLSwrap;
  delete[] l_data.parameters;
  gsl_vector_free(initFuncArg);

  return;
}
int OptimizationOptions::gslOptimize( NLSFunction *F, gsl_vector* x_vec, 
        gsl_matrix *v, IterationLogger *itLog ) {
  const gsl_multifit_fdfsolver_type *Tlm[] =
    { gsl_multifit_fdfsolver_lmder, gsl_multifit_fdfsolver_lmsder };
  const gsl_multimin_fdfminimizer_type *Tqn[] = 
    { gsl_multimin_fdfminimizer_vector_bfgs,
      gsl_multimin_fdfminimizer_vector_bfgs2, 
      gsl_multimin_fdfminimizer_conjugate_fr,
      gsl_multimin_fdfminimizer_conjugate_pr };
  const gsl_multimin_fminimizer_type *Tnm[] = 
    { gsl_multimin_fminimizer_nmsimplex, gsl_multimin_fminimizer_nmsimplex2, 
      gsl_multimin_fminimizer_nmsimplex2rand };
  int gsl_submethod_max[] = { sizeof(Tlm) / sizeof(Tlm[0]),
			  sizeof(Tqn) / sizeof(Tqn[0]),
			  sizeof(Tnm) / sizeof(Tnm[0]) };  
			  
  int status, status_dx, status_grad, k;
  double g_norm, x_norm;

  /* vectorize x row-wise */
  size_t max_ind, min_ind;
  double max_val, min_val, abs_max_val = 0, abs_min_val;
  
  if (this->method < 0 || 
      this->method > sizeof(gsl_submethod_max)/sizeof(gsl_submethod_max[0]) || 
      this->submethod < 0 || 
      this->submethod > gsl_submethod_max[this->method]) {
    throw new Exception("Unknown optimization method.\n");   
  }
  
  if (this->maxiter < 0 || this->maxiter > 5000) {
    throw new Exception("opt.maxiter should be in [0;5000].\n");   
  }

  /* LM */
  gsl_multifit_fdfsolver* solverlm;
  gsl_multifit_function_fdf fdflm = { &(F->_f_ls),  &(F->_df_ls), &(F->_fdf_ls), 
                                       F->getNsq(), F->getNvar(), F };
  gsl_vector *g;

  /* QN */
  double stepqn = this->step; 
  gsl_multimin_fdfminimizer* solverqn;
  gsl_multimin_function_fdf fdfqn = { 
    &(F->_f), &(F->_df), &(F->_fdf), F->getNvar(), F };

  /* NM */
  double size;
  gsl_vector *stepnm;
  gsl_multimin_fminimizer* solvernm;
  gsl_multimin_function fnm = { &(F->_f), F->getNvar(), F };

  /* initialize the optimization method */
  switch (this->method) {
  case SLRA_OPT_METHOD_LM: /* LM */
    solverlm = gsl_multifit_fdfsolver_alloc(Tlm[this->submethod], 
                   F->getNsq(), F->getNvar());
    gsl_multifit_fdfsolver_set(solverlm, &fdflm, x_vec);
    g = gsl_vector_alloc(F->getNvar());
    break;
  case SLRA_OPT_METHOD_QN: /* QN */
    solverqn = gsl_multimin_fdfminimizer_alloc(Tqn[this->submethod], 
						F->getNvar() );
    gsl_multimin_fdfminimizer_set(solverqn, &fdfqn, x_vec, 
				  stepqn, this->tol); 
    status_dx = GSL_CONTINUE;  
    break;
  case SLRA_OPT_METHOD_NM: /* NM */
    solvernm = gsl_multimin_fminimizer_alloc(Tnm[this->submethod], F->getNvar());
    stepnm = gsl_vector_alloc(F->getNvar());
    gsl_vector_set_all(stepnm, this->step); 
    gsl_multimin_fminimizer_set( solvernm, &fnm, x_vec, stepnm );
    break;
  }

  /* optimization loop */
  Log::lprintf(Log::LOG_LEVEL_FINAL, "SLRA optimization:\n");
    
  status = GSL_SUCCESS;  
  status_dx = GSL_CONTINUE;
  status_grad = GSL_CONTINUE;  
  this->iter = 0;
  
  switch (this->method) {
  case SLRA_OPT_METHOD_LM:
    gsl_blas_ddot(solverlm->f, solverlm->f, &this->fmin);
    gsl_multifit_gradient(solverlm->J, solverlm->f, g);
    gsl_vector_scale(g, 2);
    {
      gsl_vector *g2 = gsl_vector_alloc(g->size);
      F->computeFuncAndGrad(x_vec, NULL, g2);
      gsl_vector_sub(g2, g);
      if (gsl_vector_max(g2) > 1e-10 || gsl_vector_min(g2) < -1e-10) {
        Log::lprintf(Log::LOG_LEVEL_NOTIFY,
               "Gradient error, max = %14.10f,  min = %14.10f  ...",
               gsl_vector_max(g2), gsl_vector_min(g2));
        print_vec(g2);
      }
      gsl_vector_free(g2);
    }
    if (itLog != NULL) {
      itLog->reportIteration(0, solverlm->x, this->fmin, g);
    }
    break;
  case SLRA_OPT_METHOD_QN:
    this->fmin = gsl_multimin_fdfminimizer_minimum(solverqn);
    if (itLog != NULL) {
      itLog->reportIteration(0, solverqn->x, this->fmin, solverqn->gradient);
    }
    break;
  case SLRA_OPT_METHOD_NM:
    this->fmin = gsl_multimin_fminimizer_minimum( solvernm );
    if (itLog != NULL) {
      itLog->reportIteration(this->iter, solvernm->x, this->fmin, NULL);
    }
    break;
  }

  while (status_dx == GSL_CONTINUE && 
	 status_grad == GSL_CONTINUE &&
	 status == GSL_SUCCESS &&
	 this->iter < this->maxiter) {
  	if (this->method == SLRA_OPT_METHOD_LM && this->maxx > 0) {
  	  if (gsl_vector_max(solverlm->x) > this->maxx || 
  	      gsl_vector_min(solverlm->x) < -this->maxx ){
  	    break;
	    }
	  }

    this->iter++;
    switch (this->method) {
    case SLRA_OPT_METHOD_LM: /* Levenberg-Marquardt */
      status = gsl_multifit_fdfsolver_iterate(solverlm);
      gsl_multifit_gradient(solverlm->J, solverlm->f, g);
      gsl_vector_scale(g, 2);

      /* check the convergence criteria */
      if (this->epsabs != 0 || this->epsrel != 0) {
        status_dx = gsl_multifit_test_delta(solverlm->dx, solverlm->x, 
	  				  this->epsabs, this->epsrel);
	  	} else {
	  	  status_dx = GSL_CONTINUE;
	  	}
      status_grad = gsl_multifit_test_gradient(g, this->epsgrad);
      gsl_blas_ddot(solverlm->f, solverlm->f, &this->fmin);
      if (itLog != NULL) {
        itLog->reportIteration(this->iter, solverlm->x, this->fmin, g);
      }
      break;
    case SLRA_OPT_METHOD_QN:
      status = gsl_multimin_fdfminimizer_iterate( solverqn );

      /* check the convergence criteria */
      status_grad = gsl_multimin_test_gradient(
          gsl_multimin_fdfminimizer_gradient(solverqn), this->epsgrad);
      status_dx = gsl_multifit_test_delta(solverqn->dx, solverqn->x, 
	 				 this->epsabs, this->epsrel);  		    
      this->fmin = gsl_multimin_fdfminimizer_minimum(solverqn);      
      if (itLog != NULL) {
        itLog->reportIteration(this->iter, solverqn->x, this->fmin, solverqn->gradient);
      }
      break;
    case SLRA_OPT_METHOD_NM:
      status = gsl_multimin_fminimizer_iterate( solvernm );
      /* check the convergence criteria */
      size = gsl_multimin_fminimizer_size( solvernm );
      status_dx = gsl_multimin_test_size( size, this->epsx );
      this->fmin = gsl_multimin_fminimizer_minimum( solvernm );
      if (itLog != NULL) {
        itLog->reportIteration(this->iter, solvernm->x, this->fmin, NULL);
      }
      break;
    }
  } 
  if (this->iter >= this->maxiter) {
    status = EITER;
  }

  switch (this->method) {
  case  SLRA_OPT_METHOD_LM:
    gsl_vector_memcpy(x_vec, solverlm->x);
    if (v != NULL) {
      gsl_multifit_covar(solverlm->J, this->epscov, v); /* ??? Different eps */
    }
    gsl_blas_ddot(solverlm->f, solverlm->f, &this->fmin);
    break;
  case SLRA_OPT_METHOD_QN:
    gsl_vector_memcpy(x_vec, solverqn->x);
    this->fmin = solverqn->f;
    break;
  case SLRA_OPT_METHOD_NM:
    gsl_vector_memcpy(x_vec, solvernm->x);
    this->fmin = solvernm->fval;
    break;
  }
  
  /* print exit information */  
  if (Log::getMaxLevel() >= Log::LOG_LEVEL_FINAL) { /* unless "off" */
    switch (status) {
    case EITER: 
      Log::lprintf("SLRA optimization terminated by reaching " 
                  "the maximum number of iterations.\n" 
                  "The result could be far from optimal.\n");
      break;
    case GSL_ETOLF:
      Log::lprintf("Lack of convergence: "
                  "progress in function value < machine EPS.\n");
      break;
    case GSL_ETOLX:
      Log::lprintf("Lack of convergence: "
                  "change in parameters < machine EPS.\n");
      break;
    case GSL_ETOLG:
      Log::lprintf("Lack of convergence: "
                  "change in gradient < machine EPS.\n");
      break;
    case GSL_ENOPROG:
      Log::lprintf("Possible lack of convergence: no progress.\n");
      break;
    }
    
    if (status_grad != GSL_CONTINUE && status_dx != GSL_CONTINUE) {
      Log::lprintf("Optimization terminated by reaching the convergence "
                  "tolerance for both X and the gradient.\n"); 
    
    } else {
      if (status_grad != GSL_CONTINUE) {
        Log::lprintf("Optimization terminated by reaching the convergence "
	            "tolerance for the gradient.\n");
      } else {
        Log::lprintf("Optimization terminated by reaching the convergence "
                    "tolerance for X.\n");
      }
    }
  }

  /* Cleanup  */
  switch (this->method) {
  case SLRA_OPT_METHOD_LM: /* LM */
    gsl_multifit_fdfsolver_free(solverlm);
    gsl_vector_free(g);
    break;
  case SLRA_OPT_METHOD_QN: /* QN */
    gsl_multimin_fdfminimizer_free(solverqn);
    break;
  case SLRA_OPT_METHOD_NM: /* NM */
    gsl_multimin_fminimizer_free(solvernm);
    gsl_vector_free(stepnm);
    break;
  }

  return GSL_SUCCESS; /* <- correct with status */
}
Exemplo n.º 27
0
//------------------------------------------------------------------------------
// findCorrection () : Uses a GSL Levenberg-Marquardt algorithm to fit the lines
// in FittedLines to the wavenumbers in the user-specified calibration standard.
// The result is the optimal wavenumber correction factor for the uncalibrated
// data, which is stored in the class variable WaveCorrection. Information about
// the fit residuals are saved by calling calcDiffStats().
//
void ListCal::findCorrection () {

  // Prepare the GSL Solver and associated objects. A non-linear solver is used,
  // the precise type of which is determined by SOLVER_TYPE, defined in 
  // MgstFcn.h. 
  const size_t NumParameters = 1;
  const size_t NumLines = FittedLines.size ();
  
  double GuessArr [NumParameters];
  for (unsigned int i = 0; i < NumParameters; i ++) { GuessArr[i] = WaveCorrection; }

  const gsl_multifit_fdfsolver_type *SolverType;
  gsl_multifit_fdfsolver *Solver;  
  gsl_multifit_function_fdf FitFunction;
  gsl_matrix *Covariance = gsl_matrix_alloc (NumParameters, NumParameters);
  gsl_vector_view VectorView = gsl_vector_view_array (GuessArr, NumParameters);

  FitFunction.f = &fitFn;
  FitFunction.df = &derivFn;
  FitFunction.fdf = &fitAndDerivFns;
  FitFunction.n = NumLines;
  FitFunction.p = NumParameters;
  FitFunction.params = &FittedLines;
 
  SolverType = SOLVER_TYPE;
  Solver = gsl_multifit_fdfsolver_alloc(SolverType, NumLines, NumParameters);
  gsl_multifit_fdfsolver_set (Solver, &FitFunction, &VectorView.vector);

  // Perform the fitting, one iteration at a time until one of the following
  // conditions is met: The absolute and relative changes in the fit parameters
  // become smaller than SOLVER_TOL, or the max number of allowed iterations,
  // SOLVER_MAX_ITERATIONS, is reached.
  unsigned int Iteration = 0;
  int Status;
  do {
    Iteration ++;
    Status = gsl_multifit_fdfsolver_iterate (Solver);
    if (Status) break;
    Status = gsl_multifit_test_delta (Solver->dx, Solver->x, SOLVER_TOL, SOLVER_TOL);
  } while (Status == GSL_CONTINUE && Iteration < SOLVER_MAX_ITERATIONS);

  // Output all the fit parameters with their associated error.
  gsl_multifit_covar (Solver -> J, 0.0, Covariance);
#define FIT(i) gsl_vector_get (Solver -> x, i)
#define ERR(i) sqrt (gsl_matrix_get (Covariance, i, i))

  double chi = gsl_blas_dnrm2 (Solver -> f);
  double dof = NumLines - double(NumParameters);
  double c = chi / sqrt (dof);
  
  cout << "Correction factor: " << FIT(0) << " +/- " << c*ERR(0) << " ("
    << "reduced chi^2 = " << pow(chi, 2) / dof << ", "
    << "lines fitted = " << NumLines << ", c = " << c << ")" << endl;

  // Apply the wavenumber correction to all the lines loaded from the
  // uncalibrated spectrum
  WaveCorrection = FIT(0);
  WaveCorrectionError = c*ERR(0);
  calcDiffStats ();
  cout << "dSig/Sig Mean Residual: " << DiffMean / LC_DATA_SCALE 
    << ", StdDev: " << DiffStdDev / LC_DATA_SCALE
    << ", StdErr: " << DiffStdErr / LC_DATA_SCALE << endl;

  // Clean up the memory and exit
  gsl_multifit_fdfsolver_free (Solver);
  gsl_matrix_free (Covariance);
}
Exemplo n.º 28
0
/*
 * Gaussian parameters calculation y=A/sqrt(2*pi*sigma^2) exp(-(x-x_0)^2/2/sigma^2),
 * which approximates the points set pts
 * Parameters A_, sigma_, x0_ may be NULL (if you don't need any of them)
 */
void gauss_fit(Points *pts, double *C_, double *A_, double *sigma_, double *x0_){
	// VVVV lower parameters may be formed as a structure to change as function argument
	double
		epsabs   = 1e-8,// absolute error
		epsrel   = 1e-5,// relative error
		chi_max  = 0.01;// max chi value for iterations criteria
	int max_iter = 300; // limit iterations number of gsl_multifit_fdfsolver
	size_t N_MIN = 10;	// minimum points for approximation
	double x_init[4];
	// AAAA upper parameters may be formed as a structure to change as function argument
/* x_init, the best approximations:
 * x0 - not far from real (the nearest is the better)
 * sigma - not far from real (the nearest is the better)
 * A - not large ~10 (it has a weak effect)
 */
	const gsl_multifit_fdfsolver_type *T;
	gsl_multifit_fdfsolver *s;
	int status;
	#ifdef EBUG
	int appNo = 0;
	#endif
	int iter;
	size_t i, j, n = pts->n, oldn;
	const size_t p = 4;
	gsl_matrix *covar = gsl_matrix_alloc (p, p);
	#ifdef EBUG
	double t0;
	#endif
	double *x, *y, *dy, chi, C, A, sigma, x0;
	if(n < 1) return;
	x = malloc(n * sizeof(double));
	y = malloc(n * sizeof(double));
	dy = malloc(n * sizeof(double));
	struct data d = {n, x, y, dy};
	gsl_multifit_function_fdf f;
	gsl_vector_view xx = gsl_vector_view_array(x_init, p);
	const gsl_rng_type *type;
	gsl_rng *r;

	gsl_rng_env_setup();
	type = gsl_rng_default;
	r = gsl_rng_alloc (type);
	f.f = &gauss_f;
	f.df = &gauss_df;
	f.fdf = &gauss_fdf;
	f.n = n;
	f.p = p;
	f.params = &d;
	// fill data structure. Don't forget Okkam's razor!!!
	{
		Point *pt = pts->data;
		double *px = x, *py = y, *pdy = dy, sum = 0.;
		for(i = 0; i < n; i++, pt++){
			*pdy++ = 1.; // I have no idea what is it, so init by 1
			*px++  = pt->x;
			*py++  = pt->y;
			sum   += pt->y;
			//DBG("point %d: (%g, %g)", i, pt->x, pt->y);
		}
		// fill x_init: x0, sigma, C, A (it can be a funtion parameter)
		x_init[3] = (*(--px) + *x) / 2.;
		x_init[2] = fabs((*x - *px) / 4.);
		x_init[0] = sum/(double)n;
		x_init[1] = sum;
		DBG("\nInitial parameters: x0=%.1f, sigma=%.1f, A=%.1f, C=%.1f",
			x_init[3], x_init[2], x_init[1], x_init[0]);
	}
	T = gsl_multifit_fdfsolver_lmder; // or also gsl_multifit_fdfsolver_lmsder
	s = gsl_multifit_fdfsolver_alloc(T, n, p);
	#ifdef EBUG
	t0 = dtime();
	#endif
	do{
		double dof, tres, c;
		DBG("\n************ Approximation %d ******************\n", appNo++);
		iter = 0;
		gsl_multifit_fdfsolver_set(s, &f, &xx.vector);
		do{
			iter++;
			status = gsl_multifit_fdfsolver_iterate(s);
			if(status)
				break;
			status = gsl_multifit_test_delta(s->dx, s->x, epsabs, epsrel);
		}while(status == GSL_CONTINUE && iter < max_iter);
		DBG("time=%g\n", dtime()-t0);
		gsl_multifit_covar(s->J, 0.0, covar);
		chi = gsl_blas_dnrm2(s->f);
		dof = n - p;
		tres = chi;
		c = chi / sqrt(dof); // GSL_MAX_DBL(1., chi / sqrt(dof));
		C = FIT(0), A = FIT(1), sigma = FIT(2), x0 = FIT(3);
		DBG("Number of iteratons = %d\n", iter);
		DBG("chi = %g, chi/dof = %g\n", chi, chi / sqrt(dof));
		DBG("C      = %.5f +/- %.5f\n", C, c*ERR(0));
		DBG("A      = %.5f +/- %.5f\n", A, c*ERR(1));
		DBG("sigma = %.5f +/- %.5f\n", sigma, c*ERR(2));
		DBG("x0     = %.5f +/- %.5f\n", x0, c*ERR(3));
		j = 0;
		oldn = n;
		if(c < chi_max) break;
		// throw out bad (by chi) data
		for(i = 0; i < n; i++){
			if(fabs(FN(i)) < tres){
				if(i != j){
					x[j] = x[i];
					y[j] = y[i];
					dy[j] = dy[i];
				}
				j++; continue;
			}
		}
		if(j != n){
			DBG("Chi tresholding %g, %zd points of %zd\n", tres, j, n);
			n = j;
			d.n = n;
		}
	}while(chi > chi_max && n != oldn && n > N_MIN);
	if(C_) *C_ = C;
	if(A_) *A_ = A;
	if(sigma_) *sigma_ = sigma;
	if(x0_) *x0_ = x0;
	//printf ("status = %s\n", gsl_strerror (status));
	gsl_multifit_fdfsolver_free(s);
	gsl_matrix_free(covar);
	gsl_rng_free(r);
	free(x); free(y); free(dy);
}
Exemplo n.º 29
0
int
main (void)
{
  const gsl_multifit_fdfsolver_type *T = gsl_multifit_fdfsolver_lmsder;
  gsl_multifit_fdfsolver *s;
  int status, info;
  size_t i;
  const size_t n = N;
  const size_t p = 3;

  gsl_matrix *J = gsl_matrix_alloc(n, p);
  gsl_matrix *covar = gsl_matrix_alloc (p, p);
  double y[N], weights[N];
  struct data d = { n, y };
  gsl_multifit_function_fdf f;
  double x_init[3] = { 1.0, 0.0, 0.0 };
  gsl_vector_view x = gsl_vector_view_array (x_init, p);
  gsl_vector_view w = gsl_vector_view_array(weights, n);
  const gsl_rng_type * type;
  gsl_rng * r;
  gsl_vector *res_f;
  double chi, chi0;

  const double xtol = 1e-8;
  const double gtol = 1e-8;
  const double ftol = 0.0;

  gsl_rng_env_setup();

  type = gsl_rng_default;
  r = gsl_rng_alloc (type);

  f.f = &expb_f;
  f.df = &expb_df;   /* set to NULL for finite-difference Jacobian */
  f.n = n;
  f.p = p;
  f.params = &d;

  /* This is the data to be fitted */

  for (i = 0; i < n; i++)
    {
      double t = i;
      double yi = 1.0 + 5 * exp (-0.1 * t);
      double si = 0.1 * yi;
      double dy = gsl_ran_gaussian(r, si);

      weights[i] = 1.0 / (si * si);
      y[i] = yi + dy;
      printf ("data: %zu %g %g\n", i, y[i], si);
    };

  s = gsl_multifit_fdfsolver_alloc (T, n, p);

  /* initialize solver with starting point and weights */
  gsl_multifit_fdfsolver_wset (s, &f, &x.vector, &w.vector);

  /* compute initial residual norm */
  res_f = gsl_multifit_fdfsolver_residual(s);
  chi0 = gsl_blas_dnrm2(res_f);

  /* solve the system with a maximum of 20 iterations */
  status = gsl_multifit_fdfsolver_driver(s, 20, xtol, gtol, ftol, &info);

  gsl_multifit_fdfsolver_jac(s, J);
  gsl_multifit_covar (J, 0.0, covar);

  /* compute final residual norm */
  chi = gsl_blas_dnrm2(res_f);

#define FIT(i) gsl_vector_get(s->x, i)
#define ERR(i) sqrt(gsl_matrix_get(covar,i,i))

  fprintf(stderr, "summary from method '%s'\n",
          gsl_multifit_fdfsolver_name(s));
  fprintf(stderr, "number of iterations: %zu\n",
          gsl_multifit_fdfsolver_niter(s));
  fprintf(stderr, "function evaluations: %zu\n", f.nevalf);
  fprintf(stderr, "Jacobian evaluations: %zu\n", f.nevaldf);
  fprintf(stderr, "reason for stopping: %s\n",
          (info == 1) ? "small step size" : "small gradient");
  fprintf(stderr, "initial |f(x)| = %g\n", chi0);
  fprintf(stderr, "final   |f(x)| = %g\n", chi);

  { 
    double dof = n - p;
    double c = GSL_MAX_DBL(1, chi / sqrt(dof)); 

    fprintf(stderr, "chisq/dof = %g\n",  pow(chi, 2.0) / dof);

    fprintf (stderr, "A      = %.5f +/- %.5f\n", FIT(0), c*ERR(0));
    fprintf (stderr, "lambda = %.5f +/- %.5f\n", FIT(1), c*ERR(1));
    fprintf (stderr, "b      = %.5f +/- %.5f\n", FIT(2), c*ERR(2));
  }

  fprintf (stderr, "status = %s\n", gsl_strerror (status));

  gsl_multifit_fdfsolver_free (s);
  gsl_matrix_free (covar);
  gsl_matrix_free (J);
  gsl_rng_free (r);
  return 0;
}
Exemplo n.º 30
0
double fit_n(set_const* Init, double n0){
		const gsl_multifit_fdfsolver_type *T;
		gsl_multifit_fdfsolver *s;
		int status;
		unsigned int i, iter = 0;
		const size_t n = 11;
		const size_t p = 5;
		double k = n0/0.16;
		gsl_matrix *covar = gsl_matrix_alloc (p, p);
		double y[11] = {4.45, 6.45 , 9.65, 13.29, 17.94, 22.92, 27.49, 38.82, 54.95, 75.13, 99.75}; 
		double t[11] = {k*0.02,k*0.04, k*0.08,k*0.12,k*0.16,k*0.2,k*0.24, k*0.32, k*0.4,k*0.48, k*0.56};
		struct data d = { n, y, t, Init};
		gsl_multifit_function_fdf f;
		double x_init[5] = {Init->C_s,Init->C_o, Init->b,Init->c, Init->C_r};

		//double x_init[6]  = {11.56279437,7.49931859,0.00871711,0.00267620,0.86859184,0.5};
		//double x_init[4] = { sqrt(130.746),sqrt(120.7244),1.0,10.0};
		gsl_vector_view x = gsl_vector_view_array (x_init, p);
		const gsl_rng_type * type;
		gsl_rng * r;

		gsl_rng_env_setup();

		type = gsl_rng_default;
		r = gsl_rng_alloc (type);

		f.f = &func_fit_n;
		f.df = NULL;
		f.fdf = NULL;
		f.n = n;
		f.p = p;
		f.params = &d;

		/* This is the data to be fitted */

		/*for (i = 0; i < n; i++)
		{
			double t = i;
			y[i] = 1.0 + 5 * exp (-0.1 * t) 
				+ gsl_ran_gaussian (r, 0.1);
			sigma[i] = 0.1;
			printf ("data: %u %g %g\n", i, y[i], sigma[i]);
		};*/

		T = gsl_multifit_fdfsolver_lmsder;
		
		s = gsl_multifit_fdfsolver_alloc (T, n, p);

		gsl_multifit_fdfsolver_set (s, &f, &x.vector);
	
		print_state (iter, s);

		do
		{
			iter++;
			status = gsl_multifit_fdfsolver_iterate (s);

			//printf ("status = %s\n", gsl_strerror (status));

			print_state (iter, s);

			if (status)
				break;

			status = gsl_multifit_test_delta (s->dx, s->x,
				1e-15, 0.0);
		}
		while (status == GSL_CONTINUE && iter < 2000);

		gsl_multifit_covar (s->J, 0.0, covar);

#define FIT(i) gsl_vector_get(s->x, i)
#define ERR(i) sqrt(gsl_matrix_get(covar,i,i))


		cond(Init, FIT(0), FIT(1), FIT(2), FIT(3), FIT(4));

		{ 
			double chi = gsl_blas_dnrm2(s->f);
			double dof = n - p;
			double c = GSL_MAX_DBL(1, chi / sqrt(dof)); 
			//double c = 1.0;
			/*printf("chisq/dof = %g\n",  pow(chi, 2.0) / dof);

			printf ("Cs      = %.5f +/- %.5f\n", Init->C_s, c*ERR(0));
			printf ("Co = %.5f +/- %.5f\n", Init->C_o, c*ERR(1));
			printf ("b      = %.5f +/- %.5f\n", Init->c, c*ERR(2));
			printf ("c      = %.5f +/- %.5f\n", Init->b, c*ERR(3));
			printf ("Cr      = %.5f +/- %.5f\n", Init->C_r, c*ERR(4));*/
		}
		
	//	printf ("status = %s\n", gsl_strerror (status));
		double z = 0.65;

		
		gsl_matrix_free (covar);
		gsl_rng_free (r);

		double yi = 0;
		/*for (int i = 0; i < 11; i++){
		double yi = EoS::t_E(t[i],0, Init)/(D*t[i]) - m_n ;
		printf("n = %.3f, %.3f  %.3f  %.3f \n",
		t[i],
		yi,
		y[i],
		yi-y[i]);

		}*/
	
		/*return *(new set_const("APR_fit return constant set",FIT(0), FIT(1), 10.0, FIT(2),abs(FIT(3)), z, 
			[](double f){return (1-f);},
			[](double f){return 1.0;},
			[=](double f){return eta_o(f);},
			[](double f){return 1.0;}));*/
		double rr = gsl_blas_dnrm2(s->x);
		gsl_multifit_fdfsolver_free (s);
		return rr;
	}