Skip to content

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

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
merged 2 commits into from
Feb 22, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions maths/monte_carlo.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,29 +42,34 @@ 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
"""
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("******************")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned in the previous PR, instead of printing the results it makes more sense to return them

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is OK for the _check() function to print because it is not the algorithm.

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("******************")


Expand Down