maths.monte_carlo¶
@author: MatteoRaso
Functions¶
|
An implementation of the Monte Carlo method to find area under |
|
Checks estimation error for area_under_curve_estimator function |
|
An implementation of the Monte Carlo method used to find pi. |
Area under curve y = sqrt(4 - x^2) where x lies in 0 to 2 is equal to pi |
Module Contents¶
- maths.monte_carlo.area_under_curve_estimator(iterations: int, function_to_integrate: collections.abc.Callable[[float], float], min_value: float = 0.0, max_value: float = 1.0) float ¶
- An implementation of the Monte Carlo method to find area under
a single variable non-negative real-valued continuous function, say f(x), where x lies within a continuous bounded interval, say [min_value, max_value], where min_value and max_value are finite numbers
Let x be a uniformly distributed random variable between min_value to max_value
Expected value of f(x) = (integrate f(x) from min_value to max_value)/(max_value - min_value)
- Finding expected value of f(x):
Repeatedly draw x from uniform distribution
Evaluate f(x) at each of the drawn x values
Expected value = average of the function evaluations
Estimated value of integral = Expected value * (max_value - min_value)
Returns estimated value
- maths.monte_carlo.area_under_line_estimator_check(iterations: int, min_value: float = 0.0, max_value: float = 1.0) None ¶
Checks estimation error for area_under_curve_estimator function for f(x) = x where x lies within min_value to max_value 1. Calls “area_under_curve_estimator” function 2. Compares with the expected value 3. Prints estimated, expected and error value
- maths.monte_carlo.pi_estimator(iterations: int)¶
An implementation of the Monte Carlo method used to find pi. 1. Draw a 2x2 square centred at (0,0). 2. Inscribe a circle within the square. 3. For each iteration, place a dot anywhere in the square.
Record the number of dots within the circle.
After all the dots are placed, divide the dots in the circle by the total.
Multiply this value by 4 to get your estimate of pi.
Print the estimated and numpy value of pi
- maths.monte_carlo.pi_estimator_using_area_under_curve(iterations: int) None ¶
Area under curve y = sqrt(4 - x^2) where x lies in 0 to 2 is equal to pi