MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / area_under_line_estimator_check

Function area_under_line_estimator_check

maths/monte_carlo.py:71–100  ·  view source on GitHub ↗

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

(
    iterations: int, min_value: float = 0.0, max_value: float = 1.0
)

Source from the content-addressed store, hash-verified

69
70
71def area_under_line_estimator_check(
72 iterations: int, min_value: float = 0.0, max_value: float = 1.0
73) -> None:
74 """
75 Checks estimation error for area_under_curve_estimator function
76 for f(x) = x where x lies within min_value to max_value
77 1. Calls "area_under_curve_estimator" function
78 2. Compares with the expected value
79 3. Prints estimated, expected and error value
80 """
81
82 def identity_function(x: float) -> float:
83 """
84 Represents identity function
85 >>> [function_to_integrate(x) for x in [-2.0, -1.0, 0.0, 1.0, 2.0]]
86 [-2.0, -1.0, 0.0, 1.0, 2.0]
87 """
88 return x
89
90 estimated_value = area_under_curve_estimator(
91 iterations, identity_function, min_value, max_value
92 )
93 expected_value = (max_value * max_value - min_value * min_value) / 2
94
95 print("******************")
96 print(f"Estimating area under y=x where x varies from {min_value} to {max_value}")
97 print(f"Estimated value is {estimated_value}")
98 print(f"Expected value is {expected_value}")
99 print(f"Total error is {abs(estimated_value - expected_value)}")
100 print("******************")
101
102
103def pi_estimator_using_area_under_curve(iterations: int) -> None:

Callers

nothing calls this directly

Calls 1

Tested by

no test coverage detected