extend estimation of area under curve of y=x using monte carlo simulation to any given lower and upper bound (#1784)

* extend estimation of area under curve of y=x using monte carlo simulation to any given lower and upper bound

* remove doctest
This commit is contained in:
singlav 2020-02-23 04:03:12 +05:30 committed by GitHub
parent 6d7cbdacb1
commit b36e46b9b2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -42,29 +42,34 @@ def area_under_line_estimator(iterations: int,
An implementation of the Monte Carlo method to find area under An implementation of the Monte Carlo method to find area under
y = x where x lies between min_value to max_value 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 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: 3. Finding expected value of x:
a. Repeatedly draw x from uniform distribution a. Repeatedly draw x from uniform distribution
b. Expected value = average of those values 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 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 Checks estimation error for area_under_line_estimator func
1. Calls "area_under_line_estimator" function 1. Calls "area_under_line_estimator" function
2. Compares with the expected value 2. Compares with the expected value
3. Prints estimated, expected and error value 3. Prints estimated, expected and error value
""" """
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("******************")
print("Estimating area under y=x where x varies from 0 to 1") print("Estimating area under y=x where x varies from ",min_value, " to ",max_value)
print("Expected value is ", 0.5) print("Estimated value is ", estimated_value)
print("Estimated value is ", estimate) print("Expected value is ", expected_value)
print("Total error is ", abs(estimate - 0.5)) print("Total error is ", abs(estimated_value - expected_value))
print("******************") print("******************")