int main( int argc, char *argv[]){ int nobs; if ( 3!=argc){ fputs("Usage: wLR nobs filename\n",stderr); return EXIT_FAILURE;} FILE * fp = fopen(argv[2],"r"); sscanf(argv[1],"%d",&nobs); real_t * vars = readmatrix(fp,nobs,3); fputs("* Standard linear regression.\n",stdout); real_t * res = linearRegression(vars+nobs,vars+2*nobs,NULL,nobs,NULL); fprintf(stdout,"Slope = %f\nConstant = %f\ndiffLS = %f\n",res[0],res[1],res[2]); fputs("* Weighted linear regression, using weights from file.\n",stdout); res = wLinearRegression(vars,vars+nobs,vars+2*nobs,NULL,nobs,res); fprintf(stdout,"Slope = %f\nConstant = %f\ndiffLS = %f\n",res[0],res[1],res[2]); fputs("* Iteratively reWeighted Least Squares, Cauchy.\n",stdout); res = iwlsLinearRegression(cauchy,vars+nobs,vars+2*nobs,NULL,100,nobs,res); fprintf(stdout,"Slope = %f\nConstant = %f\ndiffLS = %f\n",res[0],res[1],res[2]); fputs("* Iteratively reWeighted Least Squares, Tukey biweight.\n",stdout); res = iwlsLinearRegression(tukey_biweight,vars+nobs,vars+2*nobs,NULL,100,nobs,res); fprintf(stdout,"Slope = %f\nConstant = %f\ndiffLS = %f\n",res[0],res[1],res[2]); return EXIT_SUCCESS; }
// Estimation of information dimension double estimate(int xnum, double *yall, int width, _Bool cov, double minent) { _Bool flag; int i, j, i_end; double a, b, rsq, rsq_before = 0, coef = 0, *x, *y; i_end = xnum - width + 1; x = (double *)malloc(sizeof(double) * width); y = (double *)malloc(sizeof(double) * width); for (i = 1; i <= i_end; i++) { flag = 0; for (j = 0; j < width; j++) { x[j] = i + j; y[j] = yall[i + j - 1]; if (j > 0 && y[j] == y[j - 1]) flag = 1; } if (flag == 1) { a = 0; rsq = 0; } else { linearRegression(width, x, y, &a, &b, &rsq); } if (cov == 0) { if (rsq > rsq_before) coef = a; } else { if (rsq > rsq_before && a > minent) coef = a; } rsq_before = rsq; } if (cov == 1 && coef == 0) coef = minent; return coef; }
void shakeData::loadKNET(string filename) { file=filename; ifstream input(ofToDataPath(file).c_str()); string buffer; int lineCount=0; double scale; double averg=0; vector<double> val; while(lineCount<18){ getline(input, buffer); string result; KNETresults rslt=getVal(buffer,result); switch(rslt){ case FREQ: sampFreq=ofToInt(result); break; case SCALE_AMOUNT: scale=ofToInt(result.substr(0,result.find_first_of("("))); scale/=ofToInt(result.substr(result.find_last_of("/")+1)); break; case MAX_ACCEL: break; default: break; } lineCount++; } while(!input.eof()){ getline(input, buffer); for(int i=0; i<8; i++){ if(buffer.length()>(i+1)*9){ val.push_back(ofToInt(buffer.substr(i*9,9))); } } } input.close(); double a1,a0; linearRegression(val,a1,a0); for(unsigned int i=0; i<val.size(); i++){ val[i]-=a0+a1*i; val[i]*=scale; } bandpassFilter(val,sampFreq,3); for(unsigned int i=0; i<val.size(); i++){ uData.push_back(dataPoint(val[i],1/sampFreq)); } processData(); }
void PlotDataFitFunction::fitData() { if (fittingValues_.regressionType == FunctionLibrary::LINEAR) linearRegression(fittingValues_.column); else if (fittingValues_.regressionType == FunctionLibrary::SQRT) sqrtRegression(fittingValues_.column); else if (fittingValues_.regressionType == FunctionLibrary::SIMPLESPLINE) simpleSplineRegression(fittingValues_.column); else if (fittingValues_.regressionType == FunctionLibrary::CONSTANTSPLINE) constantSpline(fittingValues_.column); else if (fittingValues_.regressionType == FunctionLibrary::BSPLINE) bSplineRegression(fittingValues_.column); else if (fittingValues_.regressionType == FunctionLibrary::CUBICSPLINE) cubicSplineRegression(fittingValues_.column); else if (fittingValues_.regressionType == FunctionLibrary::SQUARE) squareRegression(fittingValues_.column); else if (fittingValues_.regressionType == FunctionLibrary::CUBIC) cubicRegression(fittingValues_.column); else if (fittingValues_.regressionType == FunctionLibrary::POLYNOMIAL) multiRegression(fittingValues_.dimension,fittingValues_.column); else if (fittingValues_.regressionType == FunctionLibrary::LOGARITHMIC) logarithmicRegression(fittingValues_.column); else if (fittingValues_.regressionType == FunctionLibrary::POWER) powerRegression(fittingValues_.column); else if (fittingValues_.regressionType == FunctionLibrary::EXPONENTIAL) exponentialRegression(fittingValues_.column); else if (fittingValues_.regressionType == FunctionLibrary::SIN) sinRegression(fittingValues_.column); else if (fittingValues_.regressionType == FunctionLibrary::COS) cosRegression(fittingValues_.column); else if (fittingValues_.regressionType == FunctionLibrary::INTERPOLATION) interpolRegression(fittingValues_.column); else { if (getProcessorWidget()){ getProcessorWidget()->updateFromProcessor(); } } }
//implementation of the ransac algorithm void ransac(){ int i, j, k = 0, index_p1, index_p2, n_inliers = 0, n_inliers2; float slope, intercept; point inliers[1000], inliers2[1000]; segment segTemp1, segTemp2; while(n_data >= 8){ segments[n_segments].n_inliers = 0; //try to find a segment to different models for(i = 0; i < 500; i++){ //get 2 random points of the data set index_p1 = getRandomPoint(); index_p2 = getRandomPoint(); while(index_p1 == index_p2 || data[index_p2].x == data[index_p1].x) index_p2 = getRandomPoint(); //fit the model slope = (data[index_p2].y - data[index_p1].y) / (data[index_p2].x - data[index_p1].x); intercept = data[index_p1].y - (slope * data[index_p1].x); //find the inliers findInliers(slope, intercept, inliers, &n_inliers); //find the biggest segment segTemp1 = getBiggestSegment(slope, intercept, inliers, &n_inliers); //there is no point in continue trying if the segment explains all data if(segTemp1.n_inliers == n_data){ segments[n_segments] = segTemp1; break; } //apply regression to the segment until it gets less inliers segTemp2.n_inliers = 0; while(1){ copyPoints(inliers2, inliers, n_inliers); n_inliers2 = n_inliers; segTemp2 = linearRegression(inliers2, &n_inliers2); if(segTemp2.n_inliers > segTemp1.n_inliers){ segTemp1 = segTemp2; copyPoints(inliers, inliers2, n_inliers2); n_inliers = n_inliers2; } else break; } //compare with the biggest segments until now if(segTemp1.n_inliers > segments[n_segments].n_inliers) segments[n_segments] = segTemp1; } if(segments[n_segments].n_inliers >= 8){ //remove inliers of the segment removeInliers(segments[n_segments]); n_segments++; } else break; } }
map<long int, tTPSRegion> tTPSFilter::tagPathSequenceFilter(tNode *n, bool css) { wstring originalTPS; vector<tNode *> originalNodeSequence; queue<pair<wstring,long int>> seqQueue; vector<long int> start; map<long int,tTPSRegion> structured; int originalTPSsize; long int sizeThreshold; buildTagPath("",n,false,css,false); originalTPS = tagPathSequence; originalNodeSequence = nodeSequence; originalTPSsize = originalTPS.size(); sizeThreshold = (originalTPSsize*5)/100; // % page size seqQueue.push(make_pair(originalTPS,0)); // insert first sequence in queue for processing while (seqQueue.size()) { long int len,off,rlen,pos; wstring tps; auto s = seqQueue.front(); seqQueue.pop(); tps = s.first; len = tps.size(); off = s.second; pos = searchRegion(tps); rlen = tagPathSequence.size(); if (len > rlen) { if (pos > 0) seqQueue.push(make_pair(tps.substr(0,pos),off)); if ((len-pos-rlen) > 0) seqQueue.push(make_pair(tps.substr(pos+rlen),off+pos+rlen)); if (rlen > sizeThreshold) { _regions[off+pos].len=rlen; _regions[off+pos].tps = originalTPS.substr(off+pos,rlen); } } } if (_regions.size()) { auto r=_regions.begin(); if ((*r).first > sizeThreshold) { _regions[0].len = (*r).first; _regions[0].tps = originalTPS.substr(0,(*r).first); } } else { _regions[0].len=originalTPSsize; _regions[0].tps = originalTPS; } // select structured regions float angCoeffThreshold=0.1; for (auto i=_regions.begin();i!=_regions.end();i++) { tLinearCoeff lc; lc = linearRegression((*i).second.tps); (*i).second.lc.a = lc.a; (*i).second.lc.b = lc.b; (*i).second.lc.e = lc.e; cerr << "size: " << (*i).second.len << " ang.coeff.: " << (*i).second.lc.a << endl; if (abs((*i).second.lc.a) < angCoeffThreshold) structured.insert(*i); } tagPathSequence = originalTPS; nodeSequence = originalNodeSequence; return structured; }