void Metropolis::regression_adapt(int numSteps, int stepSize) { std::vector<STM::ParName> parNames (parameters.names()); std::map<STM::ParName, std::map<std::string, double *> > regressionData; for(const auto & par : parNames) { regressionData[par]["log_variance"] = new double [numSteps]; regressionData[par]["variance"] = new double [numSteps]; regressionData[par]["acceptance"] = new double [numSteps]; } for(int i = 0; i < numSteps; i++) { // compute acceptance rates for the current variance term parameters.set_acceptance_rates(do_sample(stepSize)); for(const auto & par : parNames) { // save regression data for each parameter regressionData[par]["log_variance"][i] = std::log(parameters.sampler_variance(par)); regressionData[par]["variance"][i] = parameters.sampler_variance(par); regressionData[par]["acceptance"][i] = parameters.acceptance_rate(par); // choose new variances at random for each parameter; drawn from a gamma with mean 2.38 and sd 2 parameters.set_sampler_variance(par, gsl_ran_gamma(rng.get(), 1.4161, 1.680672)); } } // perform regression for each parameter and clean up for(const auto & par : parNames) { // first compute the correlation for variance and log_variance, use whichever is higher double corVar = gsl_stats_correlation(regressionData[par]["variance"], 1, regressionData[par]["acceptance"], 1, numSteps); double corLogVar = gsl_stats_correlation(regressionData[par]["log_variance"], 1, regressionData[par]["acceptance"], 1, numSteps); double beta0, beta1, cov00, cov01, cov11, sumsq, targetVariance; if(corVar >= corLogVar) { gsl_fit_linear(regressionData[par]["variance"], 1, regressionData[par]["acceptance"], 1, numSteps, &beta0, &beta1, &cov00, &cov01, &cov11, &sumsq); targetVariance = (parameters.optimal_acceptance_rate() - beta0)/beta1; } else { gsl_fit_linear(regressionData[par]["log_variance"], 1, regressionData[par]["acceptance"], 1, numSteps, &beta0, &beta1, &cov00, &cov01, &cov11, &sumsq); targetVariance = std::exp((parameters.optimal_acceptance_rate() - beta0)/beta1); } parameters.set_sampler_variance(par, targetVariance); delete [] regressionData[par]["log_variance"]; delete [] regressionData[par]["variance"]; delete [] regressionData[par]["acceptance"]; } }
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)); }
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; }
double my_fit_linear(vector<double> &x,vector<double> &y,double c[2]) { double *xt,*yt; size_t i,num=x.size(); if(num<=0) return 0; xt=new double[num]; yt=new double[num]; double my=0; for(i=0;i<num;i++) { xt[i]=x[i]; yt[i]=y[i]; my+=yt[i]; } my/=num; double cov[3]; double sumsq; gsl_fit_linear(xt,1,yt,1,num,c,c+1,cov,cov+1,cov+2,&sumsq); double yvs=0; for(i=0;i<num;i++) { yvs+=(yt[i]-my)*(yt[i]-my); } sumsq=1-sumsq/yvs; delete []xt; delete []yt; return sumsq; }
/* This returns 7 elements array */ static VALUE rb_gsl_fit_linear(int argc, VALUE *argv, VALUE obj) { double *ptrx, *ptry; double c0, c1, cov00, cov01, cov11, sumsq; int status; size_t n, stridex, stridey; switch (argc) { case 2: ptrx = get_vector_ptr(argv[0], &stridex, &n); ptry = get_vector_ptr(argv[1], &stridey, &n); break; case 3: CHECK_FIXNUM(argv[2]); ptrx = get_vector_ptr(argv[0], &stridex, &n); ptry = get_vector_ptr(argv[1], &stridey, &n); n = FIX2INT(argv[2]); break; default: rb_raise(rb_eArgError, "wrong number of arguments (%d for 2 or 3)", argc); break; } status = gsl_fit_linear(ptrx, stridex, 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)); }
void Normalization::fit(int k, int p, int iLength, double arr[], double cof[], KstVectorPtr vector_out) { if(k+p < iLength) { double v1[p]; double v2[p]; int j=0; for(int i=k; i<k+p; i++) { v1[j] = (double)i; v2[j] = arr[i]; j++; } double c0, c1, cov00, cov01, cov11, chisq; gsl_fit_linear(v1, 1, v2, 1, p, &c0, &c1, &cov00, &cov01, &cov11, &chisq); cof[0] = c0; cof[1] = c1; for(int i=k; i<k+p; i++) { vector_out->value()[i] = cof[0]+cof[1]*i; } } else { for(int i=k; i<iLength; i++) { vector_out->value()[i] = cof[0]+cof[1]*i; } } }
TransformationModelLinear::TransformationModelLinear( const TransformationModel::DataPoints & data, const Param & params) { params_ = params; data_given_ = !data.empty(); if (!data_given_ && params.exists("slope") && (params.exists("intercept"))) { // don't estimate parameters, use given values slope_ = params.getValue("slope"); intercept_ = params.getValue("intercept"); } else // estimate parameters from data { Param defaults; getDefaultParameters(defaults); params_.setDefaults(defaults); symmetric_ = params_.getValue("symmetric_regression") == "true"; size_t size = data.size(); if (size == 0) // no data { throw Exception::IllegalArgument(__FILE__, __LINE__, __PRETTY_FUNCTION__, "no data points for 'linear' model"); } else if (size == 1) // degenerate case, but we can still do something { slope_ = 1.0; intercept_ = data[0].second - data[0].first; } else // compute least-squares fit { vector<double> x(size), y(size); for (size_t i = 0; i < size; ++i) { if (symmetric_) { x[i] = data[i].second + data[i].first; y[i] = data[i].second - data[i].first; } else { x[i] = data[i].first; y[i] = data[i].second; } } double cov00, cov01, cov11, sumsq; // covariance values, sum of squares double * x_start = &(x[0]), * y_start = &(y[0]); gsl_fit_linear(x_start, 1, y_start, 1, size, &intercept_, &slope_, &cov00, &cov01, &cov11, &sumsq); if (symmetric_) // undo coordinate transformation: { slope_ = (1.0 + slope_) / (1.0 - slope_); intercept_ = intercept_ * 1.41421356237309504880; // 1.41... = sqrt(2) } } } }
pure_expr* wrap_gsl_fit_linear(double* x, double* y, size_t n) { double c0, c1, cov00, cov01, cov11, sumsq; gsl_fit_linear(x, 1, y, 1, n, &c0, &c1, &cov00, &cov01, &cov11, &sumsq); return pure_listl(6, pure_double(c0), pure_double(c1), pure_double(cov00), pure_double(cov01), pure_double(cov11), pure_double(sumsq)); }
double transfer_function(double k) { static int initFlag = 1; static int currCosmoNum; static gsl_spline *cosmocalc_transfer_function_spline = NULL; static gsl_interp_accel *cosmocalc_transfer_function_acc = NULL; static double c0,c1; double transfer_function_table[COSMOCALC_TRANSFER_FUNCTION_TABLE_LENGTH]; double k_table[COSMOCALC_TRANSFER_FUNCTION_TABLE_LENGTH]; long i; double cov00,cov01,cov11,sumsq; if(initFlag == 1 || currCosmoNum != cosmoData.cosmoNum) { initFlag = 0; currCosmoNum = cosmoData.cosmoNum; for(i=0;i<COSMOCALC_TRANSFER_FUNCTION_TABLE_LENGTH;++i) { k_table[i] = log(K_MAX/K_MIN)/(COSMOCALC_TRANSFER_FUNCTION_TABLE_LENGTH-1.0)*((double) i) + log(K_MIN); if(cosmoData.useSmoothTransFunc) transfer_function_table[i] = log(transfunct_eh98_smooth(exp(k_table[i]))); else if(cosmoData.useNoTransFunc) transfer_function_table[i] = 0.0; else transfer_function_table[i] = log(transfunct_eh98(exp(k_table[i]))); } //init the spline and accelerators if(cosmocalc_transfer_function_spline != NULL) gsl_spline_free(cosmocalc_transfer_function_spline); cosmocalc_transfer_function_spline = gsl_spline_alloc(GSL_SPLINE_TYPE,(size_t) (COSMOCALC_TRANSFER_FUNCTION_TABLE_LENGTH)); gsl_spline_init(cosmocalc_transfer_function_spline,k_table,transfer_function_table,(size_t) (COSMOCALC_TRANSFER_FUNCTION_TABLE_LENGTH)); if(cosmocalc_transfer_function_acc != NULL) gsl_interp_accel_reset(cosmocalc_transfer_function_acc); else cosmocalc_transfer_function_acc = gsl_interp_accel_alloc(); gsl_fit_linear(k_table+COSMOCALC_TRANSFER_FUNCTION_TABLE_LENGTH-COSMOCALC_TRANSFER_FUNCTION_FIT_LENGTH,(size_t) 1, transfer_function_table+COSMOCALC_TRANSFER_FUNCTION_TABLE_LENGTH-COSMOCALC_TRANSFER_FUNCTION_FIT_LENGTH,(size_t) 1, (size_t) COSMOCALC_TRANSFER_FUNCTION_FIT_LENGTH,&c0,&c1,&cov00,&cov01,&cov11,&sumsq); } if(k < K_MIN) return 1.0; else if(k < K_MAX) return exp(gsl_spline_eval(cosmocalc_transfer_function_spline,log(k),cosmocalc_transfer_function_acc)); else return exp(c0+c1*log(k)); }
int fit_params(double *x, int n, double * beta, double * alpha, double * s) { double cov00, cov01, cov11, chisq; double * y = x + 1; gsl_fit_linear (x, 1, y, 1, n-1, beta, alpha, &cov00, &cov01, &cov11, &chisq); int i; double u; double sum = 0.0; for (i = 0; i < n - 1; i++) { u = y[i] - (* beta) - (* alpha) * x[i]; sum += u * u; } *s = sqrt(sum / (n - 1)); return 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 ()); }
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; }
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 }
int core_oph_gsl_fit_linear_multi(oph_multistring * byte_array, oph_multistring * result, oph_gsl_fit_linear_param * fit) { int j, k; double tmp, *pointer; double cov00, cov01, cov11, sumsq; char *in_string = byte_array->content, *out_string = result->content; char *in_pointer, *out_pointer; for (j = 0; j < byte_array->num_measure; ++j) { in_pointer = in_string; switch (byte_array->type[j]) { case OPH_DOUBLE:{ pointer = (double *) in_string; break; } case OPH_FLOAT:{ pointer = fit->tmp; for (k = 0; k < byte_array->numelem; ++k) { fit->tmp[k] = *(float *) in_pointer; in_pointer += byte_array->blocksize; } break; } case OPH_INT:{ pointer = fit->tmp; for (k = 0; k < byte_array->numelem; ++k) { fit->tmp[k] = *(int *) in_pointer; in_pointer += byte_array->blocksize; } break; } case OPH_SHORT:{ pointer = fit->tmp; for (k = 0; k < byte_array->numelem; ++k) { fit->tmp[k] = *(short *) in_pointer; in_pointer += byte_array->blocksize; } break; } case OPH_BYTE:{ pointer = fit->tmp; for (k = 0; k < byte_array->numelem; ++k) { fit->tmp[k] = *(char *) in_pointer; in_pointer += byte_array->blocksize; } break; } case OPH_LONG:{ pointer = fit->tmp; for (k = 0; k < byte_array->numelem; ++k) { fit->tmp[k] = *(long long *) in_pointer; in_pointer += byte_array->blocksize; } break; } default: pmesg(1, __FILE__, __LINE__, "Type not recognized\n"); return -1; } if (gsl_fit_linear(fit->old_x, 1, pointer, 1, fit->n, &fit->c[0], &fit->c[1], &cov00, &cov01, &cov11, &sumsq)) return -1; out_pointer = out_string; for (k = 0; k < result->numelem; ++k) { tmp = fit->c[0] + fit->new_x[k] * fit->c[1]; if (core_oph_type_cast(&tmp, out_pointer, OPH_DOUBLE, result->type[j], byte_array->missingvalue)) return -1; out_pointer += result->blocksize; } in_string += byte_array->elemsize[j]; out_string += result->elemsize[j]; } return 0; }
int kstfit_linear_unweighted(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]; } 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_linear( inArrays[X], 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; }
void Clamp::fitData() { if (splot->dataExists()) { int n = splot->dataSize(); if (n >= 2) { const double* tempx = splot->xData(); const double* tempy = splot->yData(); double x[n]; double y[n]; int j = 0; double maxVal = 0; double minVal = 0; for (int i = 0; i < n; i++) { // exclude any inf or NaN or zero rates from the FI linear fit if (gsl_finite(tempy[i]) == 1 || tempy[i] == 0) { x[j] = tempx[i]; y[j] = tempy[i]; if (x[j] > maxVal) maxVal = x[j]; if (x[j] < minVal) minVal = x[j]; j++; } } double c0, c1, cov00, cov01, cov11, sumsq; gsl_fit_linear(x, 1, y, 1, n, &c0, &c1, &cov00, &cov01, &cov11, &sumsq); // gsl_stats_tss does not exist in GSL 1.10 included in Ubuntu 9.10 // use this function if you manually upgrade GSL, otherwise use the next section of code //double SST = gsl_stats_tss(y,1,j); double SST = 0; //calculating total sum of squares around the mean double ymean = gsl_stats_mean(y, 1, j); for (int i = 0; i < j; i++) { double delta = y[i] - ymean; SST += delta * delta; } if (gsl_finite(c0) == 1 && gsl_finite(c1) == 1) { printf("Best fit: Y = %g + %g X\n", c0, c1); printf("SSE = %g\n", sumsq); printf("SST = %g\n", SST); printf("R^2 = %g\n", 1 - sumsq / SST); eqnmsg = "Y = " + QString::number(c0) + " + " + QString::number(c1) + " X, R^2 = " + QString::number(1 - sumsq / SST); emit setEqnMsg(eqnmsg); } else { eqnmsg = "Error."; emit setEqnMsg(eqnmsg); } double fitx[2] = { minVal, maxVal }; double fity[2]; fity[0] = c0 + c1 * fitx[0]; fity[1] = c0 + c1 * fitx[1]; emit drawFit(fitx, fity, 2); } else { eqnmsg = "No data for a linear fit."; emit setEqnMsg(eqnmsg); } } else { eqnmsg = "Not enough data for a linear fit."; emit setEqnMsg(eqnmsg); } }