@@ -26,7 +26,8 @@ def in_circle(x: float, y: float) -> bool:
26
26
27
27
# The proportion of guesses that landed in the circle
28
28
proportion = mean (
29
- int (in_circle (uniform (- 1.0 , 1.0 ), uniform (- 1.0 , 1.0 ))) for _ in range (iterations )
29
+ int (in_circle (uniform (- 1.0 , 1.0 ), uniform (- 1.0 , 1.0 )))
30
+ for _ in range (iterations )
30
31
)
31
32
# The ratio of the area for circle to square is pi/4.
32
33
pi_estimate = proportion * 4
@@ -35,9 +36,9 @@ def in_circle(x: float, y: float) -> bool:
35
36
print ("The total error is " , abs (pi - pi_estimate ))
36
37
37
38
38
- def area_under_line_estimator (iterations : int ,
39
- min_value : float = 0.0 ,
40
- max_value : float = 1.0 ) -> float :
39
+ def area_under_line_estimator (
40
+ iterations : int , min_value : float = 0.0 , max_value : float = 1.0
41
+ ) -> float :
41
42
"""
42
43
An implementation of the Monte Carlo method to find area under
43
44
y = x where x lies between min_value to max_value
@@ -49,24 +50,28 @@ def area_under_line_estimator(iterations: int,
49
50
4. Actual value = (max_value^2 - min_value^2) / 2
50
51
5. Returns estimated value
51
52
"""
52
- return mean (uniform (min_value , max_value ) for _ in range (iterations )) * (max_value - min_value )
53
+ return mean (uniform (min_value , max_value ) for _ in range (iterations )) * (
54
+ max_value - min_value
55
+ )
53
56
54
57
55
- def area_under_line_estimator_check (iterations : int ,
56
- min_value : float = 0.0 ,
57
- max_value : float = 1.0 ) -> None :
58
+ def area_under_line_estimator_check (
59
+ iterations : int , min_value : float = 0.0 , max_value : float = 1.0
60
+ ) -> None :
58
61
"""
59
62
Checks estimation error for area_under_line_estimator func
60
63
1. Calls "area_under_line_estimator" function
61
64
2. Compares with the expected value
62
65
3. Prints estimated, expected and error value
63
66
"""
64
-
67
+
65
68
estimated_value = area_under_line_estimator (iterations , min_value , max_value )
66
- expected_value = (max_value * max_value - min_value * min_value ) / 2
67
-
69
+ expected_value = (max_value * max_value - min_value * min_value ) / 2
70
+
68
71
print ("******************" )
69
- print ("Estimating area under y=x where x varies from " ,min_value , " to " ,max_value )
72
+ print (
73
+ "Estimating area under y=x where x varies from " , min_value , " to " , max_value
74
+ )
70
75
print ("Estimated value is " , estimated_value )
71
76
print ("Expected value is " , expected_value )
72
77
print ("Total error is " , abs (estimated_value - expected_value ))
0 commit comments