core::MonteCarloMoverResult WeightMover::do_propose() { IMP_OBJECT_LOG; // store the old weights in case of reject oldweights_ = w_.get_weights(); // Draw weights from a uniform distribution of variables that sum to one // taken from http://stats.stackexchange.com/questions/14059/ // generate-uniformly-distributed-weights-that-sum-to-unity // get the old x Floats x; x.push_back(oldweights_[0]); for (unsigned i = 1; i < oldweights_.get_dimension() - 1; ++i) { x.push_back(oldweights_[i] + x[i - 1]); } // zero vector in D dimension algebra::VectorKD zero = algebra::get_zero_vector_kd(x.size()); // generate random perturbation of old components algebra::VectorKD dX = algebra::get_random_vector_in(algebra::SphereKD(zero, radius_)); // add perturbation to x and apply reflective boundaries for (unsigned i = 0; i < x.size(); ++i) { if (x[i] + dX[i] > 1.0) x[i] = 2.0 - x[i] - dX[i]; else if (x[i] + dX[i] < 0.0) x[i] = -x[i] - dX[i]; else x[i] += dX[i]; } // sort the new x here std::sort(x.begin(), x.end(), std::less<double>()); // get the new weights algebra::VectorKD newweights = algebra::get_zero_vector_kd(oldweights_.get_dimension()); newweights[0] = x[0]; for (unsigned i = 1; i < newweights.get_dimension() - 1; ++i) { newweights[i] = x[i] - x[i - 1]; } newweights[newweights.get_dimension() - 1] = 1.0 - x[x.size() - 1]; // set the new weights w_.set_weights(newweights); return core::MonteCarloMoverResult( ParticleIndexes(1, w_.get_particle()->get_index()), 1.0); }
IMPCORE_BEGIN_NAMESPACE OpenCubicSpline::OpenCubicSpline(const Floats &values, Float minrange, Float spacing, bool extend) : spacing_(spacing), inverse_spacing_(1.0/spacing_), spline_(values, spacing_, inverse_spacing_), minrange_(minrange), maxrange_(minrange_ + spacing_ * (values.size() - 1)), extend_(extend) { IMP_USAGE_CHECK(spacing >0, "The spacing between values must be positive."); IMP_USAGE_CHECK(values.size() >=1, "You must provide at least one value."); }
Floats CrossLinkData::get_nonmarginal_elements(double sigmai, Floats dists) const { Floats probs; if (bias_) { for (unsigned n = 0; n < dists.size(); n++) { probs.push_back(get_biased_element(dists[n], sigmai)); } } if (!bias_) { for (unsigned n = 0; n < dists.size(); n++) { probs.push_back(get_unbiased_element(dists[n], sigmai)); } } return probs; }
Floats CysteineCrossLinkData::get_omegas(Floats fmods, double omega0) const { Floats omegas; for (unsigned n = 0; n < fmods.size(); ++n) { double cumul = 0; double cumul2 = 0; for (unsigned j = 1; j < omega_grid_.size(); ++j) { double omj = omega_grid_[j]; double omjm1 = omega_grid_[j - 1]; double dom = omj - omjm1; double priorj = get_omega_prior(omj, omega0); double priorjm1 = get_omega_prior(omjm1, omega0); double pj = get_element(fexp_, fmods[n], omj) * priorj; double pjm1 = get_element(fexp_, fmods[n], omjm1) * priorjm1; double pj2 = get_element(fexp_, fmods[n], omj) * priorj * omj; double pjm12 = get_element(fexp_, fmods[n], omjm1) * priorjm1 * omjm1; cumul += (pj + pjm1) / 2.0 * dom; cumul2 += (pj2 + pjm12) / 2.0 * dom; } omegas.push_back(cumul2 / cumul); } return omegas; }
IMPSCOREFUNCTOR_BEGIN_INTERNAL_NAMESPACE RawOpenCubicSpline::RawOpenCubicSpline(const Floats &values, double spacing, double inverse_spacing) : values_(values) { IMP_USAGE_CHECK(spacing > 0, "The spacing between values must be positive."); IMP_USAGE_CHECK(values.size() >= 1, "You must provide at least one value."); int npoints = values_.size(); // Precalculate second derivatives for a natural cubic spline (open) by // inversion of the tridiagonal matrix (Thomas algorithm) second_derivs_.resize(npoints); Floats tmp(npoints); // Forward elimination phase second_derivs_[0] = 0.; tmp[0] = 0.; double inverse_doublespacing = 1.0 / (spacing + spacing); for (int i = 1; i < npoints - 1; ++i) { Float m = 0.5 * second_derivs_[i - 1] + 2.; second_derivs_[i] = -0.5 / m; tmp[i] = (6. * ((values_[i + 1] - values_[i]) * inverse_spacing - (values_[i] - values_[i - 1]) * inverse_spacing) * inverse_doublespacing - 0.5 * tmp[i - 1]) / m; } // Backward substitution phase second_derivs_[npoints - 1] = 0.; for (int i = npoints - 2; i >= 0; --i) { second_derivs_[i] = second_derivs_[i] * second_derivs_[i + 1] + tmp[i]; } /*IMP_LOG_TERSE( "Initialized spline with " << values.size() << " values " << "with spacing " << spacing << std::endl);*/ }
Floats FStudentT::evaluate_derivative_FX(const Floats FXs) const { Floats dervs; double c = (N_ + nu_) / IMP::square(sigma_) / (nu_ + t2_); for (unsigned int i = 0; i < FXs.size(); i++) { dervs.push_back(c * (FXs[i] - FM_)); } return dervs; }
IMPCORE_BEGIN_NAMESPACE RigidBodyUmbrella::RigidBodyUmbrella(kernel::Model *m, kernel::ParticleIndex pi, kernel::ParticleIndex ref, Floats x0, double alpha, double k, std::string name) : Restraint(m, name), pi_(pi), ref_(ref), x0_(x0), alpha_(alpha), k_(k) { IMP_USAGE_CHECK(x0.size() == 7, "Wrong size for x0, should be 7"); }
void FStudentT::do_update_sufficient_statistics(Floats FXs) { N_ = FXs.size(); sumFX_ = FXs[0]; sumFX2_ = IMP::square(FXs[0]); LogJX_ = 0.; for (unsigned int i = 1; i < N_; ++i) { sumFX_ += FXs[i]; sumFX2_ += IMP::square(FXs[i]); } }
Floats CrossLinkData::get_marginal_elements(double sigma, Floats dists) const { Floats probs; unsigned is = get_closest(sigma_grid_, sigma); for (unsigned n = 0; n < dists.size(); n++) { unsigned id = get_closest(dist_grid_, dists[n]); probs.push_back(grid_[is][id]); } return probs; }
Floats CysteineCrossLinkData::get_nonmarginal_elements(double fexp, Floats fmods, double omega) const { Floats probs; for (unsigned n = 0; n < fmods.size(); n++) { probs.push_back(get_element(fexp, fmods[n], omega)); } return probs; }
IMPISD_BEGIN_NAMESPACE GaussianProcessInterpolation::GaussianProcessInterpolation( FloatsList x, Floats sample_mean, Floats sample_std, unsigned n_obs, UnivariateFunction *mean_function, BivariateFunction *covariance_function, Particle *sigma, double sparse_cutoff) : Object("GaussianProcessInterpolation%1%"), x_(x), n_obs_(n_obs), mean_function_(mean_function), covariance_function_(covariance_function), sigma_(sigma), cutoff_(sparse_cutoff) { // O(M) // store dimensions M_ = x.size(); N_ = x[0].size(); sigma_val_ = Scale(sigma_).get_nuisance(); // basic checks IMP_USAGE_CHECK(sample_mean.size() == M_, "sample_mean should have the same size as x"); IMP_USAGE_CHECK(sample_std.size() == M_, "sample_std should have the same size as x"); IMP_USAGE_CHECK(mean_function->get_ndims_x() == N_, "mean_function should have " << N_ << " input dimensions"); IMP_USAGE_CHECK(mean_function->get_ndims_y() == 1, "mean_function should have 1 output dimension"); IMP_USAGE_CHECK(covariance_function->get_ndims_x1() == N_, "covariance_function should have " << N_ << " input dimensions for first vector"); IMP_USAGE_CHECK(covariance_function->get_ndims_x2() == N_, "covariance_function should have " << N_ << " input dimensions for second vector"); IMP_USAGE_CHECK(covariance_function->get_ndims_y() == 1, "covariance_function should have 1 output dimension"); // set all flags to false = need update. force_mean_update(); force_covariance_update(); // compute needed matrices compute_I(sample_mean); compute_S(sample_std); }
Floats CysteineCrossLinkData::get_marginal_elements(Floats fmods, double omega0) const { Floats probs; unsigned is = get_closest(omega0_grid_, omega0); for (unsigned n = 0; n < fmods.size(); n++) { unsigned id = get_closest(fmod_grid_, fmods[n]); probs.push_back(grid_[is][id]); } return probs; }
algebra::Vector3Ds ProbabilisticAnchorGraph::get_particle_anchors( Particle *p, float min_prob) const { Floats probs = get_particle_probabilities(p); algebra::Vector3Ds anchors; for (unsigned int i = 0; i < probs.size(); i++) { if (probs[i] >= min_prob) { anchors.push_back(positions_[i]); } } return anchors; }
void ChiSquareMetric::add_configuration(Floats data, Floats stddev, double weight) { // store weight weight_.push_back(weight); // if the constructor for normal chi2 is used, store non-normalized data if (constr_type_ == 0) { datas_.push_back(data); // standard deviation stddev_.push_back(stddev); // else normalize vector (data-data_exp)/stddev } else { double norm2 = 0.; for (unsigned i = 0; i < data.size(); ++i) { norm2 += pow((data[i] - data_exp_[i]) / stddev[i], 2); } norm_.push_back(sqrt(norm2)); for (unsigned i = 0; i < data.size(); ++i) { data[i] = (data[i] - data_exp_[i]) / stddev[i]; } datas_.push_back(data); } return; }
internal::Coord RigidBodyUmbrella::interpolate(double lambda, Floats x1, Floats x2) const { //checks IMP_USAGE_CHECK(x1.size() == 7, "Wrong size for x1, should be 7"); IMP_USAGE_CHECK(x2.size() == 7, "Wrong size for x2, should be 7"); IMP_USAGE_CHECK(lambda >= 0, "lambda should be >=0"); IMP_USAGE_CHECK(lambda <= 1, "lambda should be <=1"); //centroid IMP_Eigen::Vector3d cx1,cx2,cx0; cx1 << x1[0], x1[1], x1[2]; cx2 << x2[0], x2[1], x2[2]; cx0 = (1-lambda)*cx1 + lambda*cx2; //quaternion IMP_Eigen::Quaterniond qx1(x1[3],x1[4],x1[5],x1[6]); IMP_Eigen::Quaterniond qx2(x2[3],x2[4],x2[5],x2[6]); IMP_Eigen::Quaterniond qx0(qx1.slerp(lambda,qx2)); qx0.normalize(); //return it internal::Coord x0; x0.coms.push_back(cx0); x0.quats.push_back(qx0); return x0; }
Coord::Coord(Floats fl) { IMP_USAGE_CHECK(fl.size() % 7 == 0, "coordinates must be multiple of 7"); unsigned nrbs = fl.size()/7; for (unsigned i = 0; i < nrbs; i++) { Eigen::Vector3d com; com << fl[3 * i], fl[3 * i + 1], fl[3 * i + 2]; coms.push_back(com); Eigen::Quaterniond quat(fl[3 * nrbs + 4 * i], fl[3 * nrbs + 4 * i + 1], fl[3 * nrbs + 4 * i + 2], fl[3 * nrbs + 4 * i + 3]); quat.normalize(); quats.push_back(quat); } }
IMPALGEBRA_BEGIN_NAMESPACE LinearFit2D::LinearFit2D(const algebra::Vector2Ds& data, const Floats &errors) { // check that there are at least 3 points IMP_USAGE_CHECK(data.size() > 1, "At least 2 points are required for LinearFit2D " << data.size() << " given"); IMP_USAGE_CHECK(errors.empty() || errors.size()==data.size(), "Either there must be no error bars given or one per" << " point."); find_regression(data, errors); evaluate_error(data, errors); }
Floats CrossLinkData::get_omegas(double sigma, Floats dists) const { Floats omegas; for (unsigned n = 0; n < dists.size(); ++n) { double cumul = 0; double cumul2 = 0; if (!bias_) { for (unsigned j = 1; j < omega_grid_.size(); ++j) { double omj = omega_grid_[j]; double omjm1 = omega_grid_[j - 1]; double dom = omj - omjm1; double pj = get_unbiased_element(dists[n], omj * sigma) / omj; double pjm1 = get_unbiased_element(dists[n], omjm1 * sigma) / omjm1; double pj2 = get_unbiased_element(dists[n], omj * sigma); double pjm12 = get_unbiased_element(dists[n], omjm1 * sigma); cumul += (pj + pjm1) / 2.0 * dom; cumul2 += (pj2 + pjm12) / 2.0 * dom; } } if (bias_) { for (unsigned j = 1; j < omega_grid_.size(); ++j) { double omj = omega_grid_[j]; double omjm1 = omega_grid_[j - 1]; double dom = omj - omjm1; double pj = get_biased_element(dists[n], omj * sigma) / omj; double pjm1 = get_biased_element(dists[n], omjm1 * sigma) / omjm1; double pj2 = get_biased_element(dists[n], omj * sigma); double pjm12 = get_biased_element(dists[n], omjm1 * sigma); cumul += (pj + pjm1) / 2.0 * dom; cumul2 += (pj2 + pjm12) / 2.0 * dom; } } omegas.push_back(cumul2 / cumul); } return omegas; }
void EM2DRestraint::init_grid(const Floats& sigma_grid) { for (unsigned i = 0; i < fmod_grid_.size(); ++i) { double fmod = fmod_grid_[i]; double cumul = 0; for (unsigned j = 1; j < sigma_grid.size(); ++j) { double sj = sigma_grid[j]; double sjm1 = sigma_grid[j - 1]; double ds = sj - sjm1; double pj = get_element(fmod, sj) / sj; double pjm1 = get_element(fmod, sjm1) / sjm1; cumul += (pj + pjm1) / 2.0 * ds; } // add marginal element grid_.push_back(cumul); } }
void FretData::init_grids (const Floats& d_grid_int, Float R0, Float Rmin, Float Rmax, bool do_limit) { // grid on distance between termini for(unsigned l=0; l<d_term_.size(); ++l){ // grid on sigma for(unsigned i=0; i<s_grid_.size(); ++i){ // grid on distance between center of GMM for(unsigned j=0; j<d_center_.size(); ++j){ Float marg=0.; Float norm=0.; unsigned kmin=0; unsigned kmax=d_grid_int.size(); // find boundaries for marginalization if(do_limit){ kmin=get_closest(d_grid_int,std::max(Rmin,d_term_[l]-Rmax)); kmax=get_closest(d_grid_int,d_term_[l]+Rmax); } // do the marginalization for(unsigned k=kmin+1; k<kmax; ++k){ Float dx = d_grid_int[k] - d_grid_int[k-1]; Float prob = get_probability(d_grid_int[k], d_center_[j], s_grid_[i]); Float probm1 = get_probability(d_grid_int[k-1], d_center_[j], s_grid_[i]); Float kernel = get_kernel( d_grid_int[k], R0 ); Float kernelm1 = get_kernel( d_grid_int[k-1], R0 ); marg += ( kernel * prob + kernelm1 * probm1 ) / 2.0 * dx; norm += ( prob + probm1 ) / 2.0 * dx; } // store in grid_ and norm_ grid_.push_back(marg); norm_.push_back(norm); } } } }
Floats _pass_floats(const Floats &in) { for (unsigned int i = 0; i < in.size(); ++i) { std::cout << in[i] << " "; } return in; }
CrossLinkData::CrossLinkData(Floats dist_grid, Floats omega_grid, Floats sigma_grid, Floats pot_x_grid, Floats pot_value_grid, double don, double doff, int prior_type) : Object("Data Structure for CrossLinkMSRestraint %1%") { prior_type_ = prior_type; bias_ = true; // this constructor calculates the marginal likelihood using a biasing // potential // obtained from Free energy calculations. // Store dist grid dist_grid_ = dist_grid; // Store omega grid omega_grid_ = omega_grid; // Store sigma grid sigma_grid_ = sigma_grid; // Store the potential x coordinates pot_x_grid_ = pot_x_grid; // Store the normalized potential double cumul = 0; for (unsigned j = 1; j < pot_value_grid.size(); ++j) { double xj = pot_x_grid_[j]; double xjm1 = pot_x_grid_[j - 1]; double dx = xj - xjm1; double pj = pot_value_grid[j]; double pjm1 = pot_value_grid[j - 1]; cumul += (pj + pjm1) / 2.0 * dx; } for (unsigned i = 0; i < pot_value_grid.size(); ++i) { pot_value_grid_.push_back(pot_value_grid[i] / cumul); } for (unsigned k = 0; k < sigma_grid_.size(); ++k) { double sigma = sigma_grid_[k]; Floats grid; for (unsigned i = 0; i < dist_grid_.size(); ++i) { double dist = dist_grid_[i]; double cumul = 0; for (unsigned j = 1; j < omega_grid_.size(); ++j) { double omj = omega_grid_[j]; double omjm1 = omega_grid_[j - 1]; double dom = omj - omjm1; double pj; double pjm1; if (prior_type_ == 0) { pj = get_biased_element(dist, omj * sigma) / omj; pjm1 = get_biased_element(dist, omjm1 * sigma) / omjm1; } else { pj = get_biased_element(dist, omj) * get_omega_prior(omj, sigma); pjm1 = get_biased_element(dist, omjm1) * get_omega_prior(omjm1, sigma); } cumul += (pj + pjm1) / 2.0 * dom; } // switching cumul to zero between don and doff if (dist > doff) { cumul = 0.; } if (dist > don && dist <= doff) { cumul *= pow(doff * doff - dist * dist, 2) * (doff * doff + 2. * dist * dist - 3. * don * don) / pow(doff * doff - don * don, 3); } grid.push_back(cumul); } grid_.push_back(grid); } }