Skip to content

Commit 74893e1

Browse files
#6219: Add Monte Carlo Integral Approximation
1 parent 164f328 commit 74893e1

2 files changed

Lines changed: 131 additions & 0 deletions

File tree

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.thealgorithms.randomized;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import java.util.function.Function;
6+
import org.junit.jupiter.api.Test;
7+
8+
class MonteCarloIntegrationTest {
9+
10+
private static final double EPSILON = 0.03; // Allow 3% error margin
11+
12+
@Test
13+
void testConstantFunction() {
14+
// Integral of f(x) = 2 from 0 to 1 is 2
15+
Function<Double, Double> constant = x -> 2.0;
16+
double result = MonteCarloIntegration.approximate(constant, 0, 1, 10000);
17+
assertEquals(2.0, result, EPSILON);
18+
}
19+
20+
@Test
21+
void testLinearFunction() {
22+
// Integral of f(x) = x from 0 to 1 is 0.5
23+
Function<Double, Double> linear = x -> x;
24+
double result = MonteCarloIntegration.approximate(linear, 0, 1, 10000);
25+
assertEquals(0.5, result, EPSILON);
26+
}
27+
28+
@Test
29+
void testQuadraticFunction() {
30+
// Integral of f(x) = x^2 from 0 to 1 is 1/3
31+
Function<Double, Double> quadratic = x -> x * x;
32+
double result = MonteCarloIntegration.approximate(quadratic, 0, 1, 10000);
33+
assertEquals(1.0 / 3.0, result, EPSILON);
34+
}
35+
36+
@Test
37+
void testLargeSampleSize() {
38+
// Integral of f(x) = x^2 from 0 to 1 is 1/3
39+
Function<Double, Double> quadratic = x -> x * x;
40+
double result = MonteCarloIntegration.approximate(quadratic, 0, 1, 50000000);
41+
assertEquals(1.0 / 3.0, result, EPSILON / 2); // Larger sample size, smaller error margin
42+
}
43+
44+
@Test
45+
void testReproducibility() {
46+
Function<Double, Double> linear = x -> x;
47+
double result1 = MonteCarloIntegration.approximate(linear, 0, 1, 10000, 42L);
48+
double result2 = MonteCarloIntegration.approximate(linear, 0, 1, 10000, 42L);
49+
assertEquals(result1, result2, 0.0); // Exactly equal
50+
}
51+
52+
@Test
53+
void testNegativeInterval() {
54+
// Integral of f(x) = x from -1 to 1 is 0
55+
Function<Double, Double> linear = x -> x;
56+
double result = MonteCarloIntegration.approximate(linear, -1, 1, 10000);
57+
assertEquals(0.0, result, EPSILON);
58+
}
59+
}

0 commit comments

Comments
 (0)