Monte Carlo Integration Library 1.0
High-performance Monte Carlo methods for numerical integration and optimization
ISintegrator.tpp
Go to the documentation of this file.
1/**
2 * @file ISintegrator.tpp
3 * @brief ISMontecarloIntegrator template implementation.
4 * @details Contains inline implementations for importance sampling integration
5 * with custom proposal distributions.
6 */
7#include "integrator.hpp"
8#include "../geometry.hpp"
9#include <vector>
10#include <fstream>
11#include <iostream>
12#include <omp.h>
13
14namespace mc::integrators {
15
16/**
17 * @brief Construct an importance sampling integrator.
18 * @tparam dim Dimensionality parameter.
19 * @param d Reference to the integration domain.
20 */
21template <size_t dim>
22ISMontecarloIntegrator<dim>::ISMontecarloIntegrator(const mc::domains::IntegrationDomain<dim> &d)
23 : Integrator<dim>(d) {}
24
25/**
26 * @brief Compute the integral using importance sampling.
27 * @tparam dim Dimensionality parameter.
28 * @param f Integrand function: ℝⁿ → ℝ.
29 * @param n_samples Number of samples drawn from the proposal.
30 * @param proposal Custom sampling distribution q(x) (should approximate f for efficiency).
31 * @param seed Random seed for reproducibility.
32 * @return Estimated integral ∫_Ω f(x) dx.
33 *
34 * @details Algorithm:
35 * 1. Uses ISMeanEstimator to sample from proposal q(x)
36 * 2. Computes weighted mean: μ̂ = (1/N) ∑ [f(xᵢ)/q(xᵢ)]
37 * 3. Returns mean directly (no volume factor for importance sampling)
38 *
39 * **Variance reduction**: When q(x) ≈ f(x), the weights f/q are nearly constant,
40 * reducing variance compared to uniform sampling.
41 */
42template <size_t dim>
43double ISMontecarloIntegrator<dim>::integrate(
44 const std::function<double(const mc::geom::Point<dim>&)>& f,
45 int n_samples,
46 const mc::proposals::Proposal<dim>& proposal,
47 std::uint32_t seed)
48{
49 mc::estimators::ISMeanEstimator<dim> mean_estimator;
50 mc::estimators::ImportanceEstimate<dim> mean_estimate = mean_estimator.estimate(this->domain, seed, n_samples, proposal, f);
51 return mean_estimate.mean;
52}
53
54} // namespace mc::integrators