Monte Carlo Integration Library 1.0
High-performance Monte Carlo methods for numerical integration and optimization
uniformProposal.tpp
Go to the documentation of this file.
1#ifndef MONTECARLO_1_UNIFORM_PROPOSAL_TPP
2#define MONTECARLO_1_UNIFORM_PROPOSAL_TPP
3
4namespace mc {
5namespace proposals {
6
7/**
8 * @brief Construct a uniform proposal over a domain's bounding box.
9 * @tparam dim Dimensionality parameter.
10 * @param d Integration domain to sample from.
11 *
12 * @details Initializes uniform distributions for each dimension spanning
13 * the domain's bounding box. Precomputes the box volume for PDF evaluation.
14 */
15template <size_t dim>
16UniformProposal<dim>::UniformProposal(const mc::domains::IntegrationDomain<dim>& d)
17 : domain(d), vol_box(d.getBoxVolume())
18{
19 auto bounds = domain.getBounds();
20 for (size_t i = 0; i < dim; ++i) {
21 dist[i] = std::uniform_real_distribution<double>(
22 bounds[i].first, bounds[i].second
23 );
24 }
25}
26
27/**
28 * @brief Sample a uniform point from the domain's bounding box.
29 * @tparam dim Dimensionality parameter.
30 * @param rng Mersenne Twister random generator.
31 * @return Point uniformly distributed in the bounding box.
32 *
33 * @details Generates independent uniform samples along each dimension.
34 * Time complexity: O(dim).
35 */
36template <size_t dim>
37mc::geom::Point<dim> UniformProposal<dim>::sample(std::mt19937& rng) const
38{
39 mc::geom::Point<dim> x;
40 for (size_t i = 0; i < dim; ++i) {
41 x[i] = dist[i](rng);
42 }
43 return x;
44}
45
46/**
47 * @brief Evaluate the uniform probability density.
48 * @tparam dim Dimensionality parameter.
49 * @param x Query point (ignored for uniform distribution).
50 * @return 1 / V where V is the bounding box volume.
51 *
52 * @details Constant probability density over the entire bounding box.
53 * This PDF is well-defined only if x is inside the bounding box;
54 * outside the box, the density is technically undefined (but we return
55 * the constant value anyway for API consistency).
56 */
57template <size_t dim>
58double UniformProposal<dim>::pdf(const mc::geom::Point<dim>&) const
59{
60 return 1.0 / vol_box;
61}
62
63} // namespace proposals
64} // namespace mc
65
66#endif // MONTECARLO_1_UNIFORM_PROPOSAL_TPP