Ejemplo n.º 1
0
bool LeastSquares1D::SolveWeighted()
{
  assert(w != NULL);
  assert(x.n == y.n);
  assert(x.n >= 2);
  Real sxx,syy,sxy;
  Real xmean = WeightedMean(x,*w);
  Real ymean = WeightedMean(y,*w);
  sxx = syy = sxy = 0;
  for(int i=0;i<x.n;i++) {
    Real w2 = Sqr((*w)(i));
    sxx += w2*Sqr(x(i) - xmean);
    syy += w2*Sqr(y(i) - ymean);
    sxy += w2*(x(i) - xmean)*(y(i) - ymean);
  }
  if(sxx == 0) {  //vertical line!
    b = xmean;
    a = Inf;
    stda = stdb = 0;
    corrCoeff = 1;
    return true;
  }
  a = sxy / sxx;
  b = ymean - b*xmean;
  if(x.n == 2) {
    stda = stdb = 0;
    corrCoeff = 1;
    return true;
  }
  Real wtotal = Sum(*w);
  Real s = Sqrt((syy - b*sxy)/(wtotal-2));
  stda = s / Sqrt(sxx);
  stdb = s*Sqrt(One/wtotal + Sqr(xmean)/sxx);
  return true;
}
Ejemplo n.º 2
0
double CKDE::MeanShift_Forward(bool bFindMax, double x, double& h, double alpha, double eplison, int maxIter, bool bNeighbor) 
// use Epanechnikov Kernel
{
	int  iter ;
	double diffX, oldX;
	double lowerBound;
	int		oldSgn, sgn, oscillation;

	
	iter = 0;
	oscillation = oldSgn = sgn = 0;
	do{
		oldSgn = sgn ;
		oldX = x;
		lowerBound = -(1-1.*exp(-1.*iter/alpha));
		x = WeightedMean(x, h, lowerBound, bFindMax);

		diffX = x - oldX;
		sgn = GSL_SIGN(diffX);
		oscillation += abs(sgn - oldSgn)/2;

//		TRACE("[%2d] diff X %.3f, new X = %.3f, h=%.3f, low=%.3f, FindMax %d\n", iter, diffX, x, h, lowerBound, bFindMax);
		if(fabs(diffX) < eplison || oscillation >20) {
			oscillation = 0;
//			break;
			h *= 0.85;
			if(h < 2) break;
		}
		iter++;
	}while(iter <maxIter);

//	gpMsgbar->ShowMessage("max_min %d, find (%.3f, %.3f) ==>", bFindMax, x,  m_pdf[(int)(x+0.5)]);
	// neighborhood search
	if(bNeighbor) {
		int i, start, end;
		start = (int)(x-h - 1);
		if(start < 0) start = 0;
		end = (int)(x+h+1);
		if(end >= m_nBin) end = m_nBin -1;

		for(i=start; i<=end; i++) {
			if(bFindMax) 
				if(m_pdf[i] > m_pdf[(int)(x+0.5)]) x =  i;
			if(! bFindMax) 
				if(m_pdf[i] < m_pdf[(int)(x+0.5)]) x =  i;
		}
	}
//	gpMsgbar->ShowMessage(" (%.3f, %.3f) \n", x,  m_pdf[(int)(x+0.5)]);

//	gpMainDlg->ShowMessage("best position %d\n", bestPos);

	return x;
}