|
| 1 | +package com.thealgorithms.randomized; |
| 2 | + |
| 3 | +import java.util.Random; |
| 4 | +import java.util.function.Function; |
| 5 | + |
| 6 | +/** |
| 7 | + * A demonstration of the Monte Carlo integration algorithm in Java. |
| 8 | + * |
| 9 | + * <p>This class estimates the value of definite integrals using randomized sampling, |
| 10 | + * also known as the Monte Carlo method. It is particularly effective for: |
| 11 | + * <ul> |
| 12 | + * <li>Functions that are difficult or impossible to integrate analytically</li> |
| 13 | + * <li>High-dimensional integrals where traditional methods are inefficient</li> |
| 14 | + * <li>Simulation and probabilistic analysis tasks</li> |
| 15 | + * </ul> |
| 16 | + * |
| 17 | + * <p>The core idea is to sample random points uniformly from the integration domain, |
| 18 | + * evaluate the function at those points, and compute the scaled average to estimate the integral. |
| 19 | + * |
| 20 | + * <p>For a one-dimensional integral over [a, b], the approximation is the function range (b-a), |
| 21 | + * multiplied by the function average result for a random sample. |
| 22 | + * See more: <a href="https://en.wikipedia.org/wiki/Monte_Carlo_integration">Monte Carlo Integration</a> |
| 23 | + * |
| 24 | + * @author: MuhammadEzzatHBK |
| 25 | + */ |
| 26 | + |
| 27 | +public final class MonteCarloIntegration { |
| 28 | + |
| 29 | + private MonteCarloIntegration() { |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Approximates the definite integral of a given function over a specified |
| 34 | + * interval using the Monte Carlo method with a fixed random seed for |
| 35 | + * reproducibility. |
| 36 | + * |
| 37 | + * @param fx the function to integrate |
| 38 | + * @param a the lower bound of the interval |
| 39 | + * @param b the upper bound of the interval |
| 40 | + * @param n the number of random samples to use |
| 41 | + * @param seed the seed for the random number generator |
| 42 | + * @return the approximate value of the integral |
| 43 | + */ |
| 44 | + public static double approximate(Function<Double, Double> fx, double a, double b, int n, long seed) { |
| 45 | + return doApproximate(fx, a, b, n, new Random(seed)); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Approximates the definite integral of a given function over a specified |
| 50 | + * interval using the Monte Carlo method with a random seed based on the |
| 51 | + * current system time for more randomness. |
| 52 | + * |
| 53 | + * @param fx the function to integrate |
| 54 | + * @param a the lower bound of the interval |
| 55 | + * @param b the upper bound of the interval |
| 56 | + * @param n the number of random samples to use |
| 57 | + * @return the approximate value of the integral |
| 58 | + */ |
| 59 | + public static double approximate(Function<Double, Double> fx, double a, double b, int n) { |
| 60 | + return doApproximate(fx, a, b, n, new Random(System.currentTimeMillis())); |
| 61 | + } |
| 62 | + |
| 63 | + private static double doApproximate(Function<Double, Double> fx, double a, double b, int n, Random generator) { |
| 64 | + double totalArea = 0.0; |
| 65 | + double range = b - a; |
| 66 | + for (int i = 0; i < n; i++) { |
| 67 | + double x = a + generator.nextDouble() * range; |
| 68 | + totalArea += fx.apply(x); |
| 69 | + } |
| 70 | + return range * totalArea / n; |
| 71 | + } |
| 72 | +} |
0 commit comments