Skip to content

Monte Carlo numerical integration for arbitrary functions over finite range.

License

Notifications You must be signed in to change notification settings

rpowel/monte-carlo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Monte Carlo

Calculate the integral of arbitrary, finite, real functions over a finite interval using a Monte Carlo methodology. This method allows for selection of number of Monte Carlo guesses to use as well as the number of CPU cores to utilize (up to <available_cores>-1).

Installation

Clone git repo:

git clone https://github.com/rpowel/monte-carlo

Install python dependencies:

cd monte-carlo
pip install -r requirements.txt

Usage

from montecarlo.montecarlo import integrate

# Integration limits
LOWER_LIMIT = 0
UPPER_LIMIT = 1

# Monte-Carlo kwargs
NUM_ITERATIONS = 10000
NUM_CORES = 2

# Define your function to integrate
def function_to_integrate(x):
    return x**2

# Calculate area under the curve
area = integrate(
    function_to_integrate,
    LOWER_LIMIT,
    UPPER_LIMIT,
    num_iterations=NUM_ITERATIONS,
    num_cores=NUM_CORES,
)
# Returns ~0.5
# Accuracy depends on NUM_ITERATIONS

Profiling

Test the accuracy and speed curves of integration on your function:

from montecarlo import montecarlo
from montecarlo.profiler import profiler

# Integration limits
LOWER_LIMIT = 0
UPPER_LIMIT = 1

# Profiling kwargs
CORES_LOW = 1  # Lower limit of cores to test
CORES_HIGH = 4  # Upper limit of cores to test
ITER_LOW = 100  # Lower limit of iterations
ITER_HIGH = 1e6  # Upper limit of iterations

# Define your function to integrate
def function_to_integrate(x):
    return x**2

    
def calculate_area(num_iterations=1000, num_cores=1):
    area = montecarlo.integrate(
        function_to_integrate,
        LOWER_LIMIT,
        UPPER_LIMIT,
        num_iterations=num_iterations,
        num_cores=num_cores,
    )
    return area

profiler.profile(
    calculate_area,
    cores_low=CORES_LOW,
    cores_high=CORES_HIGH,
    iter_low=ITER_LOW,
    iter_high=ITER_HIGH,
)

Testing

Unit tests are defined in montecarlo/tests/ and can be run all together from the project root directory with:

python -m unittest discover montecarlo/tests

About

Monte Carlo numerical integration for arbitrary functions over finite range.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages