From 7781d5b746eda31947b81e66355ade3a59494bff Mon Sep 17 00:00:00 2001 From: Varun <1990.singlav@gmail.com> Date: Sun, 23 Feb 2020 00:19:23 +0530 Subject: [PATCH 1/2] extend estimation of area under curve of y=x using monte carlo simulation to any given lower and upper bound --- maths/monte_carlo.py | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/maths/monte_carlo.py b/maths/monte_carlo.py index 4980c5c55c8c..6e1084c96d4a 100644 --- a/maths/monte_carlo.py +++ b/maths/monte_carlo.py @@ -42,29 +42,37 @@ def area_under_line_estimator(iterations: int, An implementation of the Monte Carlo method to find area under y = x where x lies between min_value to max_value 1. Let x be a uniformly distributed random variable between min_value to max_value - 2. Expected value of x = integration of x from min_value to max_value + 2. Expected value of x = (integration of x from min_value to max_value) / (max_value - min_value) 3. Finding expected value of x: a. Repeatedly draw x from uniform distribution b. Expected value = average of those values - 4. Actual value = 1/2 + 4. Actual value = (max_value^2 - min_value^2) / 2 5. Returns estimated value """ - return mean(uniform(min_value, max_value) for _ in range(iterations)) + return mean(uniform(min_value, max_value) for _ in range(iterations)) * (max_value - min_value) -def area_under_line_estimator_check(iterations: int) -> None: +def area_under_line_estimator_check(iterations: int, + min_value: float=0.0, + max_value: float=1.0) -> None: """ Checks estimation error for area_under_line_estimator func 1. Calls "area_under_line_estimator" function 2. Compares with the expected value 3. Prints estimated, expected and error value + + >>> area_under_line_estimator_check(100, 0, 2) + >>> area_under_line_estimator_check(100) """ - estimate = area_under_line_estimator(iterations) + + estimated_value = area_under_line_estimator(iterations, min_value, max_value) + expected_value = (max_value*max_value - min_value*min_value) / 2 + print("******************") - print("Estimating area under y=x where x varies from 0 to 1") - print("Expected value is ", 0.5) - print("Estimated value is ", estimate) - print("Total error is ", abs(estimate - 0.5)) + print("Estimating area under y=x where x varies from ",min_value, " to ",max_value) + print("Estimated value is ", estimated_value) + print("Expected value is ", expected_value) + print("Total error is ", abs(estimated_value - expected_value)) print("******************") From 2bf88a373fcb48d029c1a3bf490e075bf1820804 Mon Sep 17 00:00:00 2001 From: Varun <1990.singlav@gmail.com> Date: Sun, 23 Feb 2020 00:32:23 +0530 Subject: [PATCH 2/2] remove doctest --- maths/monte_carlo.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/maths/monte_carlo.py b/maths/monte_carlo.py index 6e1084c96d4a..6a407e98badd 100644 --- a/maths/monte_carlo.py +++ b/maths/monte_carlo.py @@ -60,9 +60,6 @@ def area_under_line_estimator_check(iterations: int, 1. Calls "area_under_line_estimator" function 2. Compares with the expected value 3. Prints estimated, expected and error value - - >>> area_under_line_estimator_check(100, 0, 2) - >>> area_under_line_estimator_check(100) """ estimated_value = area_under_line_estimator(iterations, min_value, max_value)