Ejemplo n.º 1
0
void	create_pipe_redirect(int pipefd[2][2], t_command *t, int i, int curr)
{
  if (i == t->command_count - 1 && t->output_file)
    mod_output(t);
  if (i == 0 && t->input_file)
    mod_input(t);
  safe_close(pipefd[curr][0]);
  safe_close(pipefd[!curr][1]);
  if (t->command_count > 1 && i < t->command_count - 1)
  {
    if (dup2(pipefd[curr][1], 1) == -1)
      exit_error("Error: fail to dup2 the pipe\n");
    else
      safe_close(pipefd[curr][1]);
  }
  if (t->command_count > 1 && i > 0)
  {
    if (dup2(pipefd[!curr][0], 0) == -1)
      exit_error("Error: fail to dup2 the pipe to 0\n");
    else
      safe_close(pipefd[!curr][0]);
  }
}
Ejemplo n.º 2
0
arma::field<arma::field<arma::mat> > model_select(arma::vec& data,
                                                  const std::set<std::vector<std::string > >& models,
                                                  const std::vector< std::string >& full_model,
                                                  std::string model_type,
                                                  bool bs_optimism,
                                                  double alpha,
                                                  std::string compute_v, 
                                                  unsigned int K, unsigned int H, unsigned int G, 
                                                  bool robust, double eff, unsigned int seed){
  
  // Number of data points
  unsigned int N = data.n_rows;
  
  // Number of models
  unsigned int num_models = models.size(); 
  
  // Store output from models
  arma::field<arma::field<arma::mat> > model_results(num_models);
  
  // Make an iterator to iterator through it
  std::set<std::vector<std::string > > ::const_iterator iter;
  iter = models.begin(); 
  
  // Get the first model
  std::vector<std::string> desc = full_model;
  
  // Find where the results should be input. (No protection needed, we know it is in the matrix)
  unsigned int full_model_index = std::distance(models.begin(), models.find(full_model));
  
  // Build the fields off of the first model's description
  arma::vec theta = model_theta(desc);
  arma::field<arma::vec> objdesc = model_objdesc(desc); 
  
  // Build matrix to store results
  arma::mat results(num_models, 4);
  
  Rcpp::Rcout << "Processing model 1 out of " << num_models << std::endl;
  
  set_seed(seed);
  
  // Obtain the largest models information
  arma::field<arma::mat> master = gmwm_master_cpp(data, 
                                                  theta,
                                                  desc,
                                                  objdesc, 
                                                  model_type, 
                                                  true, //starting
                                                  alpha, 
                                                  "fast", // compute V
                                                  K, H,
                                                  G, 
                                                  robust, eff);
  
  // Theta update
  theta = master(0);
  
  // Define WV Empirical
  arma::vec wv_empir = master(2);
  
  // Create WV Matrix
  
  arma::mat wv(wv_empir.n_elem,3);
  wv.col(0) = wv_empir;
  wv.col(1) = master(3);
  wv.col(2) = master(4);
  
  // Get the original "FAST" matrix
  arma::mat orgV = master(6); // Original V
  
  // Take the inverse
  arma::mat omega = inv(orgV); // Original V => Omega
  
  // Get expect_diff for guesses with DR
  double expect_diff = arma::as_scalar(master(7));
  
  // Obtain the theoretical WV
  arma::vec theo = master(8);
  
  // Obtain the obj_value of the function
  double obj_value = arma::as_scalar(master(10));
  
  // Calculate the values of the Scales 
  arma::vec scales = scales_cpp(floor(log2(N)));
  
  double dr_slope = arma::as_scalar(master(12));
  
  // ------------------------------------
  
  // Store output from default GMWM object
  arma::field<arma::mat> mod_output(13);
  mod_output(0) = theta;
  mod_output(1) = master(1);
  mod_output(2) = wv_empir;
  mod_output(3) = master(3);
  mod_output(4) = master(4);
  mod_output(5) = master(5);
  mod_output(6) = orgV;
  mod_output(7) = expect_diff;
  mod_output(8) = theo;
  mod_output(9) = master(9);
  mod_output(10) = obj_value;
  mod_output(11) = omega;
  mod_output(12) = dr_slope;
  
  
  // ------------------------------------
  
  // Here we set up specifics that are used not in a specific mode.
  
  // Asymptotic ----
  
  // Get bootstrapped V
  arma::mat V;
  
  // Bootstrap ----
  // Hold optimism result
  arma::mat cov_nu_nu_theta;
  
  // Hold the bootstrapped obj values
  arma::vec bs_obj_values;
  
  if(bs_optimism){
    results.row(full_model_index) = bs_optim_calc(theta,  desc,  objdesc, model_type, scales, omega, N,
                obj_value, alpha, compute_v, K, H, G, robust, eff);
  }else{
    
    Rcpp::Rcout << "Bootstrapping the covariance matrix... Please stand by." << std::endl;
    V = cov_bootstrapper(theta, desc, objdesc, N, robust, eff, H, false); // Bootstrapped V (largest model)
    
    // Calculate the model score according to model selection criteria paper
    results.row(full_model_index) = asympt_calc(theta, desc, objdesc, model_type, scales, V, omega, wv_empir, theo, obj_value);
    
  }
  
  // Custom GMWM Update Obj
  
  arma::field<arma::mat> model_update_info(6);
  model_update_info(0) = master(0); // theta
  model_update_info(1) = master(1); // guessed_theta
  model_update_info(2) = master(5); // V
  model_update_info(3) = master(8); // theo
  model_update_info(4) = master(9); // decomp_theo
  model_update_info(5) = master(10); // objective value
  
  model_results(full_model_index) = model_update_info;
  
  // Initialize counter to keep track of values
  unsigned int count = 0;
  
  unsigned int countModels = 1;
  
  while(iter != models.end()){
    
    if(full_model_index != count){
      countModels++;
      
      // Set guessing seed
      set_seed(seed);
      
      Rcpp::Rcout << "Processing model " << countModels << " out of " << num_models << std::endl;
      // Get the first model
      desc = *iter;
      
      // Build the fields off of the first model's description
      theta = model_theta(desc);
      objdesc = model_objdesc(desc); 
      
      // Run the update version of the GMWM
      arma::field<arma::mat> update = gmwm_update_cpp(theta, desc, objdesc, model_type, 
                                                      N, expect_diff, dr_slope,
                                                      orgV, scales, wv,
                                                      true, //starting
                                                      "fast", 
                                                      K,H,G, 
                                                      robust,eff);
      
      
      // Theta update
      theta = update(0);
      
      // Update theo
      theo = update(3);
      
      // Update objective function
      obj_value = arma::as_scalar(update(5));
      
      if(bs_optimism){
        results.row(count) = bs_optim_calc(theta,  desc,  objdesc, model_type, scales, omega, N,
                    obj_value, alpha, compute_v, K, H, G, robust, eff);
      }else{
        // Calculate the model score according to model selection criteria paper
        results.row(count) = asympt_calc(theta, desc, objdesc, model_type, scales, V, omega, wv_empir, theo, obj_value);
      }
      
      model_results(count) = update;
      
    }
    // end if
    
    // Increment iterator
    iter++;
    
    // Increase count
    count++;
  }
  
  
  
  // Only run if in asymptotic mode
  if(!bs_optimism){
    
    // std::set<std::vector<std::string > > ::const_iterator iter2;
    // 
    // iter = models.begin(); 
    // 
    // /*
    // * Iterate through all models
    // * If i != j, then 
    // * IF (Complex_i > Complex_j && Obj_i > Obj_j){
    // *  IF(Crit_i < Crit_J)
    // * }
    // */
    // while(iter != models.end()){
    //   iter2 = models.begin(); 
    //   while(iter2 != models.end()){
    //     if(iter != iter2){
    //       double m1_index = std::distance(models.begin(),iter);
    //       double m2_index = std::distance(models.begin(),iter2);
    //       
    //       if(count_params(*iter) > count_params(*iter2) && results(m1_index,0) >  results(m2_index,0)){
    //         if(results(m1_index,2) < results(m2_index,2)){
    //           results(m1_index,1) = results(m2_index,1) + fabs(R::rnorm(0, sqrt(results(m2_index,0)/10) ));
    //         }
    //       }
    //     }
    //     iter2++;
    //   }
    //   iter++; 
    // }
    
  }
  
  results.col(2) = results.col(0) + results.col(1);
  
  // Sort matrix
  arma::uvec sort_ind = sort_index(results.col(2));
  
  // Pick the "best" model
  unsigned int best_model_id = sort_ind(0);
  
  // Get the sort column index
  arma::vec ex_sort = arma::conv_to<arma::vec>::from(sort_ind);
  
  // Sort the result matrix by Criterion
  arma::mat sorted = sort_mat(results, 2);
  
  // ---------
  
  // Set up output feed
  arma::field< arma::field<arma::mat> > out(2);
  
  // Build the results matrix
  arma::field<arma::mat> ms(2);
  ms(0) = sorted;
  ms(1) = ex_sort + 1; // Sorted vector (so R can know desc IDs) ALSO change index
  
  // Create m 
  
  // If the output object is not the full model, let's inject the new results.
  if(best_model_id != full_model_index){
    
    arma::field<arma::mat> m;
    m = model_results(best_model_id);
    mod_output(0) = m(0);
    mod_output(1) = m(1);
    mod_output(5) = m(2);
    mod_output(8) = m(3);
    mod_output(9) = m(4);
    mod_output(10) = m(5);
  }
  
  
  // Output to me!
  out(0) = ms;
  out(1) = mod_output;
  
  
  return out;
}
Ejemplo n.º 3
0
void SOGP::train(const VectorXd &state, const VectorXd &output) {
    //check if we have initialised the system
    if (!this->initialized) {
        throw OTLException("SOGP not yet initialised");
    }

    double kstar = this->kernel->eval(state);

    //change the output format if this is a classification problem
    VectorXd mod_output;
    if (this->problem_type == SOGP::CLASSIFICATION) {
        mod_output = VectorXd::Zero(this->output_dim);
        for (unsigned int i=0; i<this->output_dim; i++) {
            mod_output(i) = -1;
        }
        mod_output(output(0)) = 1;
    } else {
        mod_output = output;
    }

    //we are just starting.
    if (this->current_size == 0) {
        this->alpha.block(0,0,1, this->output_dim) = (mod_output.array() / (kstar + this->noise)).transpose();
        this->C.block(0,0,1,1) = VectorXd::Ones(1)*-1/(kstar + this->noise);
        this->Q.block(0,0,1,1) = VectorXd::Ones(1)*1/(kstar);
        this->basis_vectors.push_back(state);
        this->current_size++;
        return;
    }

    //Test if this is a "novel" state
    VectorXd k;
    this->kernel->eval(state, this->basis_vectors, k);
    //cache Ck
    VectorXd Ck = this->C.block(0,0, this->current_size, this->current_size)*k;

    VectorXd m = k.transpose()*this->alpha.block(0,0,this->current_size, this->output_dim);
    double s2 = kstar + (k.dot(Ck));

    if (s2 < 1e-12) {
        //std::cout << "s2: " << s2 << std::endl;
        s2 = 1e-12;
    }

    double r = 0.0;
    VectorXd q;

    if (this->problem_type == SOGP::REGRESSION) {
        r = -1.0/(s2 + this->noise);
        q = (mod_output - m)*(-r);
    } else if (this->problem_type == SOGP::CLASSIFICATION) {

        double sx2 = this->noise + s2;
        double sx = sqrt(sx2);
        VectorXd z = VectorXd(this->output_dim);
        VectorXd Erfz = VectorXd(this->output_dim);
        for (unsigned int i=0; i<this->output_dim; i++) {
            z(i) = mod_output(i) * m(i) / sx;
            Erfz(i) = stdnormcdf(z(i));
            //dErfz(i) = 1.0/sqrt(2*M_PI)*exp(-(z(i)*z(i))/2.0);
            //dErfz2(i) = dErfz(i)*(-z(i));
        }

        /*
          TO CONNTINUE
        Erfz = Erf(z);

        dErfz = 1.0/sqrt(2*pi)*exp(-(z.^2)/2);
        dErfz2 = dErfz.*(-z);

        q = y/sx * (dErfz/Erfz);
        r = (1/sx2)*(dErfz2/dErfz - (dErfz/Erfz)^2);

        */
    } else {
        throw OTL::OTLException("Whoops! My problem type is wrong. How did this happen?");
    }
    VectorXd ehat = this->Q.block(0,0, this->current_size, this->current_size)*k;

    double gamma = kstar - k.dot(ehat);
    double eta = 1.0/(1.0 + gamma*r);

    if (gamma < 1e-12) {
        gamma = 0.0;
    }

    if (gamma >= this->epsilon*kstar) {
        //perform a full update
        VectorXd s = Ck;
        s.conservativeResize(this->current_size + 1);
        s(this->current_size) = 1;


        //update Q (inverse of C)
        ehat.conservativeResize(this->current_size+1);
        ehat(this->current_size) = -1;

        MatrixXd diffQ = Q.block(0,0,this->current_size+1, this->current_size+1)
                + (ehat*ehat.transpose())*(1.0/gamma);
        Q.block(0,0,this->current_size+1, this->current_size+1) = diffQ;


        //update alpha
        MatrixXd diffAlpha = alpha.block(0,0, this->current_size+1, this->output_dim)
                + (s*q.transpose());
        alpha.block(0,0, this->current_size+1, this->output_dim) = diffAlpha;

        //update C
        MatrixXd diffC = C.block(0,0, this->current_size+1, this->current_size+1)
                + r*(s*s.transpose());
        C.block(0,0, this->current_size+1, this->current_size+1) = diffC;

        //add to basis vectors
        this->basis_vectors.push_back(state);

        //increment current size
        this->current_size++;

    } else {
        //perform a sparse update
        VectorXd s = Ck + ehat;

        //update alpha
        MatrixXd diffAlpha = alpha.block(0,0, this->current_size, this->output_dim)
                + s*((q*eta).transpose());
        alpha.block(0,0, this->current_size, this->output_dim) = diffAlpha;

        //update C
        MatrixXd diffC = C.block(0,0, this->current_size, this->current_size) +
                r*eta*(s*s.transpose());
        C.block(0,0, this->current_size, this->current_size) = diffC;
    }

    //check if we need to reduce size
    if (this->basis_vectors.size() > this->capacity) {
        //std::cout << "Reduction!" << std::endl;
        double min_val = (alpha.row(0)).squaredNorm()/(Q(0,0) + C(0,0));
        unsigned int min_index = 0;
        for (unsigned int i=1; i<this->basis_vectors.size(); i++) {
            double scorei = (alpha.row(i)).squaredNorm()/(Q(i,i) + C(i,i));
            if (scorei < min_val) {
                min_val = scorei;
                min_index = i;
            }
        }

        this->reduceBasisVectorSet(min_index);
    }

    return;
}