コード例 #1
0
ファイル: SARD_ego.c プロジェクト: pmangg/AdaptSAW
void assign_sard_params
    (SARDState* p_sard_state, SAWData *p_saw_data, const SARDInputModel *p_in_model,
     SARDSimParams *p_sim_params, double *sard_params, int normalize_mix_p)
{
    p_sim_params->iters_per_move = (int)round(sard_params[0]);
    p_sim_params->SAW_length_min = (int)round(sard_params[1]);
    p_sim_params->SAW_length_max = (int)round(sard_params[1]+sard_params[2]);
    
    p_sim_params->P_LL = sard_params[3];
    p_sim_params->P_LH = sard_params[4];
    p_sim_params->P_HL = sard_params[5];
    
    p_sim_params->gamma_hi = sard_params[7]+sard_params[6];
    p_sim_params->gamma_lo = sard_params[7];
    
    if (!normalize_mix_p)
    {
	resetSAWHeaps(p_saw_data, p_in_model, p_sim_params);
        return;
    }

    // Pick P_LL, P_LH and P_HL, satisfying the constraint that
    // P_LL + P_LH + P_HL = 1 by normalizing.
    double mix_Z =
        exp(log_sum_exp(log_sum_exp(log(p_sim_params->P_LL),
                                    log(p_sim_params->P_HL)),
                        log(p_sim_params->P_LH)));

    p_sim_params->P_LL /= mix_Z;
    p_sim_params->P_LH /= mix_Z;
    p_sim_params->P_HL /= mix_Z;
    
    int debug = 0;
    if (debug) {
	printf("iters_per_move: %d\n", p_sim_params->iters_per_move);
	printf("length_min: %d\n", p_sim_params->SAW_length_min);
	printf("length_max: %d\n", p_sim_params->SAW_length_max);
	
	printf("p_sim_params->P_LL: %f\n", p_sim_params->P_LL);
	printf("p_sim_params->P_LH: %f\n", p_sim_params->P_LH);
	printf("p_sim_params->P_HL: %f\n", p_sim_params->P_HL);
	
	printf("p_sim_params->gamma_hi: %f\n", p_sim_params->gamma_hi);
	printf("p_sim_params->gamma_lo: %f\n", p_sim_params->gamma_lo);
    }
    
    resetSAWHeapsCurrent(p_sard_state, p_saw_data, p_in_model, p_sim_params);
}
コード例 #2
0
ファイル: utils.c プロジェクト: hwp/notGHMM
double gmm_pdf_log(const gmm_t* gmm, const gsl_vector* x) {
  gsl_vector* p = gsl_vector_alloc(gmm->k);
  size_t i;
  for (i = 0; i < p->size; i++) {
    gsl_vector_set(p, i, DEBUG_LOG(gsl_vector_get(gmm->weight, i))
        + gaussian_pdf_log(gmm->comp[i], x));
  }

  double result = log_sum_exp(p);

  gsl_vector_free(p);
  return result;
}
コード例 #3
0
void PathTrie::iterate_to_vec(std::vector<PathTrie*>& output) {
  if (exists_) {
    log_prob_b_prev = log_prob_b_cur;
    log_prob_nb_prev = log_prob_nb_cur;

    log_prob_b_cur = -NUM_FLT_INF;
    log_prob_nb_cur = -NUM_FLT_INF;

    score = log_sum_exp(log_prob_b_prev, log_prob_nb_prev);
    output.push_back(this);
  }
  for (auto child : children_) {
    child.second->iterate_to_vec(output);
  }
}
コード例 #4
0
typename boost::math::tools::promote_args<T_prob>::type
categorical_logit_log(int n,
                      const Eigen::Matrix<T_prob, Eigen::Dynamic, 1>&
                      beta) {
    static const char* function("categorical_logit_log");

    check_bounded(function, "categorical outcome out of support", n,
                  1, beta.size());
    check_finite(function, "log odds parameter", beta);

    if (!include_summand<propto, T_prob>::value)
        return 0.0;

    // FIXME:  wasteful vs. creating term (n-1) if not vectorized
    return beta(n-1) - log_sum_exp(beta);  // == log_softmax(beta)(n-1);
}
コード例 #5
0
/* >>> start tutorial code >>> */
int main( ){

    USING_NAMESPACE_ACADO

    int i;

    // DEFINE VALRIABLES:
    // ---------------------------
    Matrix                 A(3,3);
    Vector                 b(3);
    DifferentialState      x(3);
    DifferentialState      y;
    Function               f[7];

    // DEFINE THE VECTOR AND MATRIX ENTRIES:
    // -------------------------------------
    A.setZero() ;
    A(0,0) = 1.0;  A(1,1) = 2.0;  A(2,2) = 3.0;
    b(0)   = 1.0;  b(1)   = 1.0;  b(2)   = 1.0;


    // DEFINE TEST FUNCTIONS:
    // -----------------------
    f[0] << A*x + b;
    f[1] << y + euclidean_norm(A*x + b);
    f[2] << y*y;
    f[3] << square(y);
    f[4] << 5.0*log_sum_exp( x );
//    f[5] << entropy( y );
    f[6] << -sum_square( x );


    for( i = 0; i < 7; i++ ){
        if( f[i].isConvex() == BT_TRUE )
            printf("f[%d] is convex. \n", i );
        else{
            if( f[i].isConcave() == BT_TRUE )
                  printf("f[%d] is concave. \n", i );
            else
                  printf("f[%d] is neither convex nor concave. \n", i );
        }
    }
    return 0;
}
コード例 #6
0
ファイル: base_quality.cpp プロジェクト: mrG7/HipSTR
std::string BaseQuality::average_base_qualities(std::vector<const std::string*> qualities){
  assert(qualities.size() > 0);

  // Check that all base quality strings are of the same length
  for (unsigned int i = 0; i < qualities.size(); i++){
    if (qualities[i]->size() != qualities[0]->size())
      printErrorAndDie("All base quality strings must be of the same length when averaging probabilities");
  }

  // Average raw error probabilities for each base and convert
  // to the closest quality score
  std::string avg_qualities('N', qualities[0]->size());
  std::vector<double> log_probs(qualities.size());
  for (unsigned int i = 0; i < qualities[0]->size(); i++){
    for (unsigned int j = 0; j < qualities.size(); j++)
      log_probs[j] = log_prob_error(qualities[j]->at(i));
    double log_mean_prob = log_sum_exp(log_probs) - log(qualities.size());
    avg_qualities[i]     = closest_char(log_mean_prob);
  }
  return avg_qualities;
}
コード例 #7
0
ファイル: sampling.c プロジェクト: pmangg/AdaptSAW
// A stupid, basic and crappy linear search-based sampling algorithm.
int sample_from_discrete_log_pd(double *log_pd, int log_pd_size)
{
    double log_x = log(genrand_real2());
    double log_F = log(DBL_MIN);
    int i = 0;

    int sampled_idx = -1;

    for (i = 0; i < log_pd_size; ++i)
    {
        log_F = log_sum_exp(log_F, log_pd[i]);

        if (log_F >= log_x)
        {
            sampled_idx = i;
            break;
        }
    }

    if (sampled_idx < 0)
    {
        if (exp(log_F) < 1)
        {
            // This is due to rounding errors resulting in log_pd not
            // log-exp-summing up to exactly 1.0, so allocate that round-off
            // error to the last element in the multinomial pd.
            return log_pd_size - 1;
        }
        else
        {
            // OMG WTF this is badness.
            // Should really be throwing an exception, but whatevs.
            return -1;
        }
    }

    return sampled_idx;
}
コード例 #8
0
std::vector<double> CanonicalAverager::calculate_weights(const std::vector<double> &energies, const Binner &binner, const DArray &lnG, const BArray &lnG_support, double beta) {
    // Assert the arrays got same shape and are one dimensional
    assert(lnG.same_shape(lnG_support));
    assert(lnG.get_shape().size()==1);

    unsigned int nbins = lnG.get_shape(0);

    // First make a histogram of the energies given as argument
    UArray histogram(nbins);

    for(std::vector<double>::const_iterator it=energies.begin(); it < energies.end(); it++) {
        int bin = binner.calc_bin(*it);

        if (0 <= bin && bin < static_cast<int>(nbins)) {
            histogram(bin)++;
        }
    }

    // Calculate the support for this histogram
    BArray histogram_support = histogram > 0;

    // Calculate the normalization constant for the canonical distribution,
    // by summing over the area with both the histogram and the estimated
    // entropy (lnG) has support. The normalization constant is given by
    //
    // lnZ_beta = log(Z_beta) = log[ sum_E exp( S(E) - beta*E ) ]
    //
    // To calculate this we use the log-sum-exp trick
    BArray support = histogram_support && lnG_support;
    DArray binning = binner.get_binning_centered();

    DArray summands(nbins);

    for (BArray::constwheretrueiterator it = support.get_constwheretrueiterator(); it(); ++it) {
        summands(it) = lnG(it) - beta*binning(it);
    }

    double lnZ_beta = log_sum_exp(summands, support);

    // Now calculate the probability for each bin according to the canonical ensemble
    DArray P_beta(nbins);

    for (BArray::constwheretrueiterator it = support.get_constwheretrueiterator(); it(); ++it) {
        P_beta(it) = exp(-beta*binning(it) + lnG(it) - lnZ_beta);
    }

    // Calculate the weight for each energy in the energy vector
    std::vector<double> weights;

    for(std::vector<double>::const_iterator it=energies.begin(); it < energies.end(); it++) {
        int bin = binner.calc_bin(*it);

        if (0 <= bin && bin < static_cast<int>(nbins)) {
            double weight = 1.0/static_cast<double>(histogram(bin)) * P_beta(bin);
            weights.push_back(weight);
        }
        else {
            weights.push_back(0);
        }
    }

    return weights;
}
コード例 #9
0
ファイル: SARD_unif.c プロジェクト: pmangg/AdaptSAW
void sampleSARD_unif(const SARDInputModel* p_in_model,
                     SARDSimParams* p_sim_params,
                     SARDMCMCDiagnostics* p_out_data,
		     return_sample_fn_t return_sample_fn,
		     double P_LL, double P_HL, size_t strategy)
{
    SAWData* p_saw_data = (SAWData*)malloc(sizeof(SAWData));
    SARDState* p_sard_state = (SARDState*)malloc(sizeof(SARDState));

    initSARDState(p_sard_state, p_in_model, p_sim_params);
    // Set up the heaps, etc. 
    initSAWData(p_saw_data, p_in_model, p_sim_params);

    int nruns_until_next_param_update = 0;

    if (strategy == 1) {
	p_sim_params->SAW_length_min = \
	    p_sim_params->min_SAW_length_bound_min ;
	
	p_sim_params->SAW_length_max = p_sim_params->max_SAW_length_bound_min;

	p_sim_params->P_LL = P_LL;
	p_sim_params->P_LH = 1.0 - P_LL- P_HL;
	p_sim_params->P_HL = P_HL;

	// Pick iters_per_move, gamma_hi and gamma_lo.
	p_sim_params->iters_per_move =
	    p_sim_params->iters_per_move_min;
	p_sim_params->gamma_hi =
	    p_sim_params->gamma_hi_min;
	p_sim_params->gamma_lo =
	    p_sim_params->gamma_lo_min;
	
	    
	int debug = 1;
	if (debug) {
	    printf("iters_per_move: %d\n", (int)round(p_sim_params->iters_per_move));
	    printf("length_min: %d\n", p_sim_params->SAW_length_min);
	    printf("length_max: %d\n", p_sim_params->SAW_length_max);
	    
	    printf("p_sim_params->P_LL: %f\n", p_sim_params->P_LL);
	    printf("p_sim_params->P_LH: %f\n", p_sim_params->P_LH);
	    printf("p_sim_params->P_HL: %f\n", p_sim_params->P_HL);
	    
	    printf("p_sim_params->gamma_hi: %f\n", p_sim_params->gamma_hi);
	    printf("p_sim_params->gamma_lo: %f\n", p_sim_params->gamma_lo);
	}
	resetSAWHeaps(p_saw_data, p_in_model, p_sim_params);
    }
    
    for (int curr_move=0; curr_move<p_sim_params->n_moves; curr_move++)
    {
        if (nruns_until_next_param_update <= 0 && strategy == 0)
        {
            nruns_until_next_param_update = p_sim_params->nruns_per_param_update;
	    	    
            // Pick SAW_length_min and SAW_length_max, satisfying the
            // constraint that SAW_length_max >= SAW_length_min.
            p_sim_params->SAW_length_min = \
                p_sim_params->min_SAW_length_bound_min +
                (int) (genrand_real2() *
                       (p_sim_params->min_SAW_length_bound_max -
                        p_sim_params->min_SAW_length_bound_min + 1));
            int SAW_length_max_lb = \
                max(p_sim_params->SAW_length_min,
                    p_sim_params->max_SAW_length_bound_min);
            p_sim_params->SAW_length_max = \
                SAW_length_max_lb +
                (int) (genrand_real2() *
                       (p_sim_params->max_SAW_length_bound_max -
                        SAW_length_max_lb + 1));

            // Pick P_LL, P_LH and P_HL, satisfying the constraint that
            // P_LL + P_LH + P_HL = 1 by normalizing.
            p_sim_params->P_LL = genrand_real1();
            p_sim_params->P_LH = genrand_real1();
            p_sim_params->P_HL = genrand_real1();

            double mix_Z = \
                exp(log_sum_exp(log_sum_exp(log(p_sim_params->P_LL),
                                            log(p_sim_params->P_HL)),
                                log(p_sim_params->P_LH)));
            p_sim_params->P_LL /= mix_Z;
            p_sim_params->P_LH /= mix_Z;
            p_sim_params->P_HL /= mix_Z;

            // Pick iters_per_move, gamma_hi and gamma_lo.
            p_sim_params->iters_per_move =
                p_sim_params->iters_per_move_min +
                (int) (genrand_real2() *
                       (p_sim_params->iters_per_move_max -
                        p_sim_params->iters_per_move_min + 1));
            p_sim_params->gamma_hi =
                p_sim_params->gamma_hi_min +
                (genrand_real1() *
                 (p_sim_params->gamma_hi_max - p_sim_params->gamma_hi_min));
            p_sim_params->gamma_lo =
                p_sim_params->gamma_lo_min +
                (genrand_real1() *
                 (p_sim_params->gamma_lo_max - p_sim_params->gamma_lo_min));
	    resetSAWHeaps(p_saw_data, p_in_model, p_sim_params);
        }
	
        --nruns_until_next_param_update;

        double log_f_fwd, log_f_rev, MH_ratio;

        double E_initial= p_sard_state->E;

        // Select move type:
        {
            double R=genrand_real2();
            if (R <= p_sim_params->P_LL)
            {
                p_saw_data->move_type=LL;
            }
            else if (R <= p_sim_params->P_LL+p_sim_params->P_LH)
            {
                p_saw_data->move_type=LH;
            }
            else
            {
                p_saw_data->move_type=HL;
            }
        }
        
        
//      printf("Just before SAW_move\n");
        proposeSAWMove(p_saw_data, p_sard_state, p_in_model, p_sim_params);
// 	printf("Just after SAW_move\n");
        evalLogFs(&log_f_fwd, &log_f_rev, p_saw_data, p_sim_params);
	
        MH_ratio = p_sim_params->beta_true*(E_initial - p_sard_state->E);
        MH_ratio += (log_f_rev - log_f_fwd);
        MH_ratio = exp(MH_ratio);
        MH_ratio = MH_ratio >1.0 ? 1.0 : MH_ratio;

	
        double reward = 0.0;

	
        storeDiagnosticsData(p_out_data, p_sim_params, curr_move,
                             E_initial, p_sard_state->E,
                             log_f_fwd, log_f_rev, MH_ratio,
                             (int)p_saw_data->move_type,
                             p_saw_data->sigma_lengths,
                             p_saw_data->rho_lengths,
                             p_sim_params->iters_per_move,
                             p_sim_params->gamma_hi, p_sim_params->gamma_lo,
                             p_sim_params->P_LL, p_sim_params->P_LH,
                             p_sim_params->P_HL, reward);
	
        // If rejection, restore state:
        if (genrand_real2() > MH_ratio)
        { 
            undoSAWMove(p_saw_data, p_sard_state, p_in_model, p_sim_params);
        }
        
	
        return_sample_fn(p_sard_state->S, p_in_model->num_nodes);
        
        if ((curr_move+1)%1000 == 0) {
	    printf("Iteration: %d has finished.\n", curr_move+1);
	}
        
    } // end moves loop

    storeEndState(p_out_data, p_sard_state->S, p_in_model->num_nodes);

    freeSARDState(p_sard_state, p_in_model, p_sim_params);
    freeSAWData(p_saw_data, p_in_model, p_sim_params);  

    free(p_sard_state);
    free(p_saw_data);
}
コード例 #10
0
ファイル: backward_inference.cpp プロジェクト: YpGu/gcoev
void backward() {
  cout << "\tbackward called" << endl;
  double* likelihood_t = new double[T];

  // B-step
  int p_count = 0;
  for (map< int, vector<int> >::iterator map_it = users_time.begin(); map_it != users_time.end(); map_it++) {
//    if (p_count % 10 == 0) cout << p_count << endl;
//    p_count++;
    int old_id = map_it->first;
//    cout << "\n" << old_id << endl;
    vector<int> ts = map_it->second;	// timestamps 
    for (int offset = 0; offset < ts.size(); offset++) {
      int t = ts[ts.size()-1] - offset;	// t should be decreasing 
//      cout << " t = " << t << endl;
      if (t == ts[ts.size()-1]) {   // t = T-1
	int i = G[t].u_map[old_id];
	for (int k = 0; k < K; k++) {
	  double p0 = exp(G[t].log_pt_tik[i][k][0]);
	  double random = rand() / (double)RAND_MAX;
//cout << "i = " << i << ", t = " << t << ", k = " << k << " p0 = " << p0 << endl;
	  if (random < p0) { 
	    G[t].X[i][k] = -1;
	  } else {
	    G[t].X[i][k] = 1;
	  }
	}
      } else {	  // t < T-1
	int i = G[t].u_map[old_id];
	int next_i = G[t+1].u_map[old_id];
	for (int k = 0; k < K; k++) {
	  int cord = ((int)G[t+1].X[next_i][k] == 1) ? 1 : 0;
//	  double log_row_sum = log_sum_exp(G[t+1].log_pt[next_i][k], G[t+1].X[next_i][k], 2*2, 2);
	  double log_col_sum = log_sum_exp(G[t+1].log_pt[next_i][k], cord, 2*2, 2);   // should normalize over column: X(t+1)
	  double p0 = exp(G[t+1].log_pt[next_i][k][2*0+cord] - log_col_sum);
//cout << "i = " << i << ", t = " << t << ", k = " << k << " p0 = " << p0 << endl;
	  double random = rand() / (double)RAND_MAX;
	  if (random < p0) {
	    G[t].X[i][k] = -1;
	  } else {
	    G[t].X[i][k] = 1;
	  }
	}
      }
    }

  }

  // update Sigma
  for (int t = start_T; t < T; t++) {
    int n = G[t].n_users;
    for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) {
      if (i != j) {
	vector<double> xi = G[t].X[i];
	vector<double> xj = G[t].X[j];
	G[t].Sigma[i][j] = sigmoid(xi, xj);
      }
    }
  }

}