/**
 * Compute the covariance matrix of a set of inputs
 * @param C The covariance matrix of X
 * @param X A matrix of inputs (one input per row)
 */
void CovarianceFunction::covariance(mat& C, const mat& X) const
{
	// ensure that data dimensions match supplied covariance matrix
	assert(C.rows() == X.rows());
	assert(C.cols() == X.rows());

	if (X.rows() == 1)
	{
	    C.set(0, 0, computeDiagonalElement(X.get_row(0)));
	    return;
	}
	
	// calculate the lower and upper triangles
	double d;
	
	for(int i=0; i<X.rows() ; i++)
	{
		for(int j=0; j<i; j++)
		{
		    d = computeElement(X.get_row(i), X.get_row(j));
		    C.set(i, j, d);
			C.set(j, i, d);
		}
	}

	// calculate the diagonal part
	for(int i=0; i<X.rows() ; i++)
	{
		C.set(i, i, computeDiagonalElement(X.get_row(i)));
	}
}
Exemplo n.º 2
0
//used for both spheric and hybrid multilateration
static
bool generate_meas(vec &meas, const bvec &method, const mat &bs_pos, const vec &ms_pos)
{
  unsigned int method_len = length(method);
  unsigned int nb_bs = bs_pos.cols();
  bool column = true;
  if(3 == nb_bs) {
    nb_bs = bs_pos.rows();
    column = false;
  }
  if((nb_bs < method_len) || (3 != length(ms_pos))) {
    return false;
  }
  meas.set_size(method_len);
  vec pos(3);
  vec pos_ref(3);
  pos_ref = column ? bs_pos.get_col(0) : bs_pos.get_row(0);
  for(unsigned int k = 0; k < method_len; ++k) {
    if(bin(1) == method[k]) {  /* hyperbolic */
      pos = column ? bs_pos.get_col(k + 1) : bs_pos.get_row(k + 1);
      meas[k] = get_dist(pos, ms_pos) - get_dist(pos_ref, ms_pos);
    }
    else { /* spherical */
      pos = column ? bs_pos.get_col(k) : bs_pos.get_row(k);
      meas[k] = get_dist(pos, ms_pos);
    }
  }
  return true;
}
Exemplo n.º 3
0
//used only for hyperbolic multilateration
static
bool generate_meas(mat &meas, const bvec &method, const mat &bs_pos, const vec &ms_pos)
{
  unsigned int k;
  unsigned int i;
  unsigned int method_len = length(method);
  unsigned int nb_bs = bs_pos.cols();
  bool column = true;
  if(3 == nb_bs) {
    nb_bs = bs_pos.rows();
    column = false;
  }
  if((nb_bs < method_len) || (3 != length(ms_pos))) {
    return false;
  }
  meas.set_size(nb_bs, nb_bs);
  vec pos_i(3);
  vec pos_k(3);
  for(k = 0; k < nb_bs; ++k) {
    pos_k = column ? bs_pos.get_col(k) : bs_pos.get_row(k);
    for(i = 0; i < nb_bs; ++i) {
      pos_i = column ? bs_pos.get_col(i) : bs_pos.get_row(i);
      meas(i, k) = get_dist(pos_i, ms_pos) - get_dist(pos_k, ms_pos);
    }
  }
  return true;
}
/**
 * Covariance between two sets of inputs X1 and X2
 *
 * @param C  The nxm covariance matrix cov(X1,X2)
 * @param X1 A set of n inputs
 * @param X2 A set of m inputs
 */
void CovarianceFunction::covariance(mat& C, const mat& X1, const mat& X2) const
{
	assert(C.rows() == X1.rows());
	assert(C.cols() == X2.rows());

	for(int i=0; i<X1.rows() ; i++)
	{
		for(int j=0; j<X2.rows(); j++)
		{
			C.set(i, j, computeElement(X1.get_row(i), X2.get_row(j)));
		}
	}
}
Exemplo n.º 5
0
/**
 * Returns the maximum distance between any 2 points from X
 */
double MaxMinDesign::distanceMin(mat X)
{
   double distMin = norm(X.get_row(1) - X.get_row(2));
   double d;
   
   for (int i=0; i<X.rows(); i++) {
       vec x1 = X.get_row(i);
       for (int j=0; j<i; j++) {
           d = norm(x1 - X.get_row(j));
           if (d<distMin) distMin = d;
       }
   }
   return distMin;
}
/**
 * Diagonal elements of the matrix cov(X,X).
 *
 * @param C A vector of diagonal elements C_i = cov(X_i, X_i)
 * @param X A set of inputs (one per row)
 */
void CovarianceFunction::computeDiagonal(vec& C, const mat& X) const
{
	// calculate the diagonal part
	for(int i=0; i<X.rows() ; i++)
	{
		C.set(i, computeDiagonalElement(X.get_row(i)));
	}
}
/**
 * Returns a vector of distances between the points in R and point y 
 */
vec GreedyMaxMinDesign::dist(mat R, vec y)
{
    vec distances(R.rows());
    
    // For each point x in R
    for (int i=0; i<R.rows(); i++) {
        vec x = R.get_row(i);
        distances(i) = weightedSquareNorm(x-y);
    }
    
    return distances;
}
ivec GreedyMaxMinDesign::subsample(mat X, int n)
{
    if (n <= 0)
        cerr << "Invalid sample size in GreedyMaxMinDesign::subsample(...)" << endl;
    
    int imax;
    
    ivec isample(n);        // Indices of points in sample
    ivec iremain;           // Indices of remaining points
    vec D;                  // Vector of min distances to sample
    
    iremain = to_ivec(linspace(0,X.rows()-1,X.rows()));
    
    // Start with location of maximum Z
    max(X.get_col(X.cols()-1), imax);
    isample(0) = imax;
    iremain.del(imax);

    // Add points having maximum minimum distance to sample
    // until subsample size is reached 
    for (int i=1; i<n; i++) 
    {
    	vec xnew = X.get_row(isample(i-1));                // Last point added 
        vec Dnew = dist(X.get_rows(iremain), xnew);        // Distances to xnew
                
        // Update minimum distances to sample
        if (D.length() == 0) 
            D = Dnew;
        else
            D = min(D,Dnew);
        
        // Find point with max min distance
        max(D,imax);
        
        // Add it to sample 
    	isample(i) = iremain(imax);
    	
    	// And delete it from remainder 
    	iremain.del(imax);
    	D.del(imax);
    }
    
    return isample;
}