-
Notifications
You must be signed in to change notification settings - Fork 19.9k
Adding Monte Carlo's Integral Approximation #6235
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
siriak
merged 11 commits into
TheAlgorithms:master
from
MuhammadEzzatHBK:add-monte-carlo
May 9, 2025
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
445c26b
Adding Karger Minimum Graph Cut Algorithm to the randomized module.
MuhammadEzzatHBK bd04a7d
Updating javadocs.
MuhammadEzzatHBK fbde45f
clang formatting.
MuhammadEzzatHBK 164f328
Removing redundant access modifiers.
MuhammadEzzatHBK 74893e1
#6219: Add Monte Carlo Integral Approximation
MuhammadEzzatHBK b8b5b29
Merge branch 'master' into add-monte-carlo
MuhammadEzzatHBK 1109024
#6219: Removing package imports
MuhammadEzzatHBK 9034842
Merge remote-tracking branch 'origin/add-monte-carlo' into add-monte-…
MuhammadEzzatHBK 785c8af
#6219: Using Function.identity() instead of an identity lambda.
MuhammadEzzatHBK 33e3d8f
#6219: Adding input validation
MuhammadEzzatHBK 9dba2d8
Merge branch 'master' into add-monte-carlo
siriak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
82 changes: 82 additions & 0 deletions
82
src/main/java/com/thealgorithms/randomized/MonteCarloIntegration.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package com.thealgorithms.randomized; | ||
|
||
import java.util.Random; | ||
import java.util.function.Function; | ||
|
||
/** | ||
* A demonstration of the Monte Carlo integration algorithm in Java. | ||
* | ||
* <p>This class estimates the value of definite integrals using randomized sampling, | ||
* also known as the Monte Carlo method. It is particularly effective for: | ||
* <ul> | ||
* <li>Functions that are difficult or impossible to integrate analytically</li> | ||
* <li>High-dimensional integrals where traditional methods are inefficient</li> | ||
* <li>Simulation and probabilistic analysis tasks</li> | ||
* </ul> | ||
* | ||
* <p>The core idea is to sample random points uniformly from the integration domain, | ||
* evaluate the function at those points, and compute the scaled average to estimate the integral. | ||
* | ||
* <p>For a one-dimensional integral over [a, b], the approximation is the function range (b-a), | ||
* multiplied by the function average result for a random sample. | ||
* See more: <a href="https://en.wikipedia.org/wiki/Monte_Carlo_integration">Monte Carlo Integration</a> | ||
* | ||
* @author: MuhammadEzzatHBK | ||
*/ | ||
|
||
public final class MonteCarloIntegration { | ||
|
||
private MonteCarloIntegration() { | ||
} | ||
|
||
/** | ||
* Approximates the definite integral of a given function over a specified | ||
* interval using the Monte Carlo method with a fixed random seed for | ||
* reproducibility. | ||
* | ||
* @param fx the function to integrate | ||
* @param a the lower bound of the interval | ||
* @param b the upper bound of the interval | ||
* @param n the number of random samples to use | ||
* @param seed the seed for the random number generator | ||
* @return the approximate value of the integral | ||
*/ | ||
public static double approximate(Function<Double, Double> fx, double a, double b, int n, long seed) { | ||
return doApproximate(fx, a, b, n, new Random(seed)); | ||
} | ||
|
||
/** | ||
* Approximates the definite integral of a given function over a specified | ||
* interval using the Monte Carlo method with a random seed based on the | ||
* current system time for more randomness. | ||
* | ||
* @param fx the function to integrate | ||
* @param a the lower bound of the interval | ||
* @param b the upper bound of the interval | ||
* @param n the number of random samples to use | ||
* @return the approximate value of the integral | ||
*/ | ||
public static double approximate(Function<Double, Double> fx, double a, double b, int n) { | ||
return doApproximate(fx, a, b, n, new Random(System.currentTimeMillis())); | ||
} | ||
|
||
private static double doApproximate(Function<Double, Double> fx, double a, double b, int n, Random generator) { | ||
if (!validate(fx, a, b, n)) { | ||
throw new IllegalArgumentException("Invalid input parameters"); | ||
} | ||
double totalArea = 0.0; | ||
double interval = b - a; | ||
for (int i = 0; i < n; i++) { | ||
double x = a + generator.nextDouble() * interval; | ||
totalArea += fx.apply(x); | ||
} | ||
return interval * totalArea / n; | ||
} | ||
|
||
private static boolean validate(Function<Double, Double> fx, double a, double b, int n) { | ||
boolean isFunctionValid = fx != null; | ||
boolean isIntervalValid = a < b; | ||
boolean isSampleSizeValid = n > 0; | ||
return isFunctionValid && isIntervalValid && isSampleSizeValid; | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
src/test/java/com/thealgorithms/randomized/MonteCarloIntegrationTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package com.thealgorithms.randomized; | ||
|
||
import static com.thealgorithms.randomized.MonteCarloIntegration.approximate; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import java.util.function.Function; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class MonteCarloIntegrationTest { | ||
|
||
private static final double EPSILON = 0.03; // Allow 3% error margin | ||
|
||
@Test | ||
void testConstantFunction() { | ||
// Integral of f(x) = 2 from 0 to 1 is 2 | ||
Function<Double, Double> constant = x -> 2.0; | ||
double result = approximate(constant, 0, 1, 10000); | ||
assertEquals(2.0, result, EPSILON); | ||
} | ||
|
||
@Test | ||
void testLinearFunction() { | ||
// Integral of f(x) = x from 0 to 1 is 0.5 | ||
Function<Double, Double> linear = Function.identity(); | ||
double result = approximate(linear, 0, 1, 10000); | ||
assertEquals(0.5, result, EPSILON); | ||
} | ||
|
||
@Test | ||
void testQuadraticFunction() { | ||
// Integral of f(x) = x^2 from 0 to 1 is 1/3 | ||
Function<Double, Double> quadratic = x -> x * x; | ||
double result = approximate(quadratic, 0, 1, 10000); | ||
assertEquals(1.0 / 3.0, result, EPSILON); | ||
} | ||
|
||
@Test | ||
void testLargeSampleSize() { | ||
// Integral of f(x) = x^2 from 0 to 1 is 1/3 | ||
Function<Double, Double> quadratic = x -> x * x; | ||
double result = approximate(quadratic, 0, 1, 50000000); | ||
assertEquals(1.0 / 3.0, result, EPSILON / 2); // Larger sample size, smaller error margin | ||
} | ||
|
||
@Test | ||
void testReproducibility() { | ||
Function<Double, Double> linear = Function.identity(); | ||
double result1 = approximate(linear, 0, 1, 10000, 42L); | ||
double result2 = approximate(linear, 0, 1, 10000, 42L); | ||
assertEquals(result1, result2, 0.0); // Exactly equal | ||
} | ||
|
||
@Test | ||
void testNegativeInterval() { | ||
// Integral of f(x) = x from -1 to 1 is 0 | ||
Function<Double, Double> linear = Function.identity(); | ||
double result = approximate(linear, -1, 1, 10000); | ||
assertEquals(0.0, result, EPSILON); | ||
} | ||
|
||
@Test | ||
void testNullFunction() { | ||
Exception exception = assertThrows(IllegalArgumentException.class, () -> approximate(null, 0, 1, 1000)); | ||
assertNotNull(exception); | ||
} | ||
|
||
@Test | ||
void testInvalidInterval() { | ||
Function<Double, Double> linear = Function.identity(); | ||
Exception exception = assertThrows(IllegalArgumentException.class, () -> { | ||
approximate(linear, 2, 1, 1000); // b <= a | ||
}); | ||
assertNotNull(exception); | ||
} | ||
|
||
@Test | ||
void testZeroSampleSize() { | ||
Function<Double, Double> linear = Function.identity(); | ||
Exception exception = assertThrows(IllegalArgumentException.class, () -> approximate(linear, 0, 1, 0)); | ||
assertNotNull(exception); | ||
} | ||
|
||
@Test | ||
void testNegativeSampleSize() { | ||
Function<Double, Double> linear = Function.identity(); | ||
Exception exception = assertThrows(IllegalArgumentException.class, () -> approximate(linear, 0, 1, -100)); | ||
assertNotNull(exception); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.