Skip to content
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

fix input, add some error tests and add note about random #10021

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
43 changes: 40 additions & 3 deletions maths/monte_carlo.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,32 @@ def pi_estimator(iterations: int):
4. After all the dots are placed, divide the dots in the circle by the total.
5. Multiply this value by 4 to get your estimate of pi.
6. Print the estimated and numpy value of pi

Note:
this function relies on a random method. So consistent testing is
impossible!

>>> pi_estimator("a")
Traceback (most recent call last):
...
ValueError: iterations must be a integer!

>>> pi_estimator(-1)
Traceback (most recent call last):
...
ValueError: iterations must be at least 1!

>>> pi_estimator(0)
Traceback (most recent call last):
...
ValueError: iterations must be at least 1!
"""
try:
iterations = int(iterations)
except ValueError as e:
raise ValueError("iterations must be a integer!") from e
if iterations < 1:
raise ValueError("iterations must be at least 1!")

# A local function to see if a dot lands in the circle.
def is_in_circle(x: float, y: float) -> bool:
Expand All @@ -33,9 +58,8 @@ def is_in_circle(x: float, y: float) -> bool:
)
# The ratio of the area for circle to square is pi/4.
pi_estimate = proportion * 4
print(f"The estimated value of pi is {pi_estimate}")
print(f"The numpy value of pi is {pi}")
print(f"The total error is {abs(pi - pi_estimate)}")
error_value = abs(pi - pi_estimate)
return pi_estimate, error_value


def area_under_curve_estimator(
Expand All @@ -60,6 +84,11 @@ def area_under_curve_estimator(
c. Expected value = average of the function evaluations
4. Estimated value of integral = Expected value * (max_value - min_value)
5. Returns estimated value

Note:
this function relies on a random method. So consistent testing is
impossible!

"""

return mean(
Expand All @@ -76,6 +105,10 @@ def area_under_line_estimator_check(
1. Calls "area_under_curve_estimator" function
2. Compares with the expected value
3. Prints estimated, expected and error value

Note:
this function relies on a random method. So consistent testing is
impossible!
"""

def identity_function(x: float) -> float:
Expand All @@ -102,6 +135,10 @@ def identity_function(x: float) -> float:
def 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

Note:
this function relies on a random method. So consistent testing is
impossible!
"""

def function_to_integrate(x: float) -> float:
Expand Down