Exemple #1
0
/* Call this function for every case in the data set.
   After all cases have been passed, call covariance_calculate
 */
void
covariance_accumulate (struct covariance *cov, const struct ccase *c)
{
  size_t i, j, m;
  const double weight = cov->wv ? case_data (c, cov->wv)->f : 1.0;

  assert (cov->passes == 1);

  if ( !cov->pass_one_first_case_seen)
    {
      assert ( cov->state == 0);
      cov->state = 1;
    }

  for (i = 0 ; i < cov->dim; ++i)
    {
      const union value *val1 = case_data (c, cov->vars[i]);

      if ( is_missing (cov, i, c))
	continue;

      for (j = 0 ; j < cov->dim; ++j)
	{
	  double pwr = 1.0;
	  int idx;
	  const union value *val2 = case_data (c, cov->vars[j]);

	  if ( is_missing (cov, j, c))
	    continue;

	  idx = cm_idx (cov, i, j);
	  if (idx >= 0)
	    {
	      cov->cm [idx] += val1->f * val2->f * weight;
	    }

	  for (m = 0 ; m < n_MOMENTS; ++m)
	    {
	      double *x = gsl_matrix_ptr (cov->moments[m], i, j);

	      *x += pwr * weight;
	      pwr *= val1->f;
	    }
	}
    }

  cov->pass_one_first_case_seen = true;
}
Exemple #2
0
// true if all values in ['.', '?']
bool cif_array::is_missing_all() const {
  int n = get_nrows();

  for (int i = 0; i < n; ++i) {
    if (!is_missing(i))
      return false;
  }

  return true;
}
Exemple #3
0
/* Call this function for every case in the data set */
void
covariance_accumulate_pass1 (struct covariance *cov, const struct ccase *c)
{
  size_t i, j, m;
  const double weight = cov->wv ? case_data (c, cov->wv)->f : 1.0;

  assert (cov->passes == 2);
  if (!cov->pass_one_first_case_seen)
    {
      assert (cov->state == 0);
      cov->state = 1;
    }

  if (cov->categoricals)
    categoricals_update (cov->categoricals, c);

  for (i = 0 ; i < cov->dim; ++i)
    {
      double v1 = get_val (cov, i, c);

      if ( is_missing (cov, i, c))
	continue;

      for (j = 0 ; j < cov->dim; ++j)
	{
	  double pwr = 1.0;

	  if ( is_missing (cov, j, c))
	    continue;

	  for (m = 0 ; m <= MOMENT_MEAN; ++m)
	    {
	      double *x = gsl_matrix_ptr (cov->moments[m], i, j);

	      *x += pwr * weight;
	      pwr *= v1;
	    }
	}
    }

  cov->pass_one_first_case_seen = true;
}
int create_decoding_matrix(int *gen_matrix, int *dec_matrix, int *missing_idxs, int k, int m)
{
  int i, j;
  int n = k+m;

  for (i = 0, j = 0; i < n && j < k; i++) {
    if (!is_missing(missing_idxs, i)) {
      copy_row(gen_matrix, dec_matrix, i, j, k);
      j++; 
    } 
  }

  return j == k;
}
Exemple #5
0
    GLint shader::operator[](const std::string& variable)
    {
        if (is_missing(variable))
            return -1;

        auto it = std::lower_bound(m_vars.begin(), m_vars.end(),
                                    std::make_pair(variable, 0));

        if ((it == m_vars.end()) || (it->first != variable))
        {
            GLint loc = glGetUniformLocation(m_prog, variable.c_str());
            if (loc == -1) {
                LOG(WARNING) << "Uniform '" << variable << "' not found in <"
                                << m_vert_name << ", " << m_frag_name << ">.";
                mark_missing(variable); /* Will not be reported again. */
            } else m_vars.insert(it, std::make_pair(variable, loc));
            return loc;
        }
        else return it->second;
    }
Exemple #6
0
/* Call this function for every case in the data set */
void
covariance_accumulate_pass2 (struct covariance *cov, const struct ccase *c)
{
  size_t i, j;
  const double weight = cov->wv ? case_data (c, cov->wv)->f : 1.0;

  assert (cov->passes == 2);
  assert (cov->state >= 1);

  if (! cov->pass_two_first_case_seen)
    {
      size_t m;
      assert (cov->state == 1);
      cov->state = 2;

      if (cov->categoricals)
      	categoricals_done (cov->categoricals);

      cov->dim = cov->n_vars;
      
      if (cov->categoricals)
	cov->dim += categoricals_df_total (cov->categoricals);

      cov->n_cm = (cov->dim * (cov->dim - 1)  ) / 2;
      cov->cm = xcalloc (cov->n_cm, sizeof *cov->cm);

      /* Grow the moment matrices so that they're large enough to accommodate the
	 categorical elements */
      for (i = 0; i < n_MOMENTS; ++i)
	{
	  cov->moments[i] = resize_matrix (cov->moments[i], cov->dim);
	}

      /* Populate the moments matrices with the categorical value elements */
      for (i = cov->n_vars; i < cov->dim; ++i)
	{
	  for (j = 0 ; j < cov->dim ; ++j) /* FIXME: This is WRONG !!! */
	    {
	      double w = categoricals_get_weight_by_subscript (cov->categoricals, i - cov->n_vars);

	      gsl_matrix_set (cov->moments[MOMENT_NONE], i, j, w);

	      w = categoricals_get_sum_by_subscript (cov->categoricals, i - cov->n_vars);

	      gsl_matrix_set (cov->moments[MOMENT_MEAN], i, j, w);
	    }
	}

      /* FIXME: This is WRONG!!  It must be fixed to properly handle missing values.  For
       now it assumes there are none */
      for (m = 0 ; m < n_MOMENTS; ++m)
	{
	  for (i = 0 ; i < cov->dim ; ++i)
	    {
	      double x = gsl_matrix_get (cov->moments[m], i, cov->n_vars -1);
	      for (j = cov->n_vars; j < cov->dim; ++j)
		{
		  gsl_matrix_set (cov->moments[m], i, j, x);
		}
	    }
	}

      /* Divide the means by the number of samples */
      for (i = 0; i < cov->dim; ++i)
	{
	  for (j = 0; j < cov->dim; ++j)
	    {
	      double *x = gsl_matrix_ptr (cov->moments[MOMENT_MEAN], i, j);
	      *x /= gsl_matrix_get (cov->moments[MOMENT_NONE], i, j);
 	    }
	}
    }

  for (i = 0 ; i < cov->dim; ++i)
    {
      double v1 = get_val (cov, i, c);

      if ( is_missing (cov, i, c))
	continue;

      for (j = 0 ; j < cov->dim; ++j)
	{
	  int idx;
	  double ss ;
	  double v2 = get_val (cov, j, c);

	  const double s = pow2 (v1 - gsl_matrix_get (cov->moments[MOMENT_MEAN], i, j)) * weight;

	  if ( is_missing (cov, j, c))
	    continue;

	  {
	    double *x = gsl_matrix_ptr (cov->moments[MOMENT_VARIANCE], i, j);
	    *x += s;
	  }

	  ss = 
	    (v1 - gsl_matrix_get (cov->moments[MOMENT_MEAN], i, j))
	    * 
	    (v2 - gsl_matrix_get (cov->moments[MOMENT_MEAN], i, j))
	    * weight
	    ;

	  idx = cm_idx (cov, i, j);
	  if (idx >= 0)
	    {
	      cov->cm [idx] += ss;
	    }
	}
    }

  cov->pass_two_first_case_seen = true;
}