std::size_t RandomDistribution::sampleIndex() { //const double r1 = randomUniform<double>(m_seed); //const double r2 = randomUniform<double>(m_seed); //std::size_t k = static_cast<std::size_t>(m_weights.size() * r1); return sampleIndex(m_seed); //return r2 < m_probs[k] ? k : m_alias[k]; }
/////////////////////////////////////////////////////////// // given indices for i!=j, sample density j's index /////////////////////////////////////////////////////////// // uses particles, variance, ind // uses newPoints, newIndices, trees, Ndens void gibbs1(unsigned int _Ndens, const BallTreeDensity* _trees, unsigned long Np, unsigned int Niter, double *_pts, BallTree::index *_ind, double *_randU, double* _randN) { unsigned int i,j,l; unsigned long s, maxNp; Ndens = _Ndens; // SET UP GLOBALS trees = _trees; newPoints = _pts; newIndices = _ind; randU = _randU; randN = _randN; Ndim = trees[0].Ndim(); // dimension of densities maxNp = 0; // largest # of particles we deal with for (unsigned int j=0; j<Ndens; j++) // compute Max Np over all densities if (maxNp < trees[j].Npts()) maxNp = trees[j].Npts(); ind = new BallTree::index[Ndens]; // ALLOCATE GLOBALS p = new double[maxNp]; Malmost = new double[Ndim]; Calmost = new double[Ndim]; Nlevels = (unsigned int) (log((double)maxNp)/log((double)2))+1; // how many levels to a balanced binary tree? particles = new double[Ndim*Ndens]; variance = new double[Ndim*Ndens]; dNpts = new unsigned long[Ndens]; levelList = new BallTree::index*[Ndens]; levelListNew = new BallTree::index*[Ndens]; for (j=0;j<Ndens;j++) { levelList[j] = new BallTree::index[maxNp]; levelListNew[j] = new BallTree::index[maxNp]; } for (unsigned long s=0; s<Np; s++) { // (for each sample:) levelInit(); // for (l=0;l<Nlevels;l++) levelDown(); // for (j=0;j<Ndens;j++) printf("%d ",dNpts[j]); initIndices(); // sample initial index values calcIndices(); for (l=0;l<Nlevels;l++) { samplePoint(newPoints); levelDown(); sampleIndices(newPoints); for (i=0;i<Niter;i++) { // perform Gibbs sampling for (j=0;j<Ndens;j++) { sampleIndex(j); } } } for (unsigned int j=0; j<Ndens; j++) // save and newIndices[j] = trees[j].getIndexOf(ind[j])+1; // return particle label samplePoint(newPoints); // draw a sample from that label newIndices += Ndens; // move pointers to next sample newPoints += Ndim; } for (j=0;j<Ndens;j++) { delete[] levelList[j]; delete[] levelListNew[j]; } delete[] levelList; delete[] levelListNew; delete[] dNpts; delete[] ind; delete[] p; delete[] particles; delete[] variance; delete[] Malmost; delete[] Calmost; };
double SpecterDistribution::sampleValue(std::uint64_t seed[2]) const { return m_energies[sampleIndex(seed)]; }
double SpecterDistribution::sampleValue() { return m_energies[sampleIndex()]; }