@@ -42,29 +42,34 @@ def area_under_line_estimator(iterations: int,
42
42
An implementation of the Monte Carlo method to find area under
43
43
y = x where x lies between min_value to max_value
44
44
1. Let x be a uniformly distributed random variable between min_value to max_value
45
- 2. Expected value of x = integration of x from min_value to max_value
45
+ 2. Expected value of x = ( integration of x from min_value to max_value) / (max_value - min_value)
46
46
3. Finding expected value of x:
47
47
a. Repeatedly draw x from uniform distribution
48
48
b. Expected value = average of those values
49
- 4. Actual value = 1/ 2
49
+ 4. Actual value = (max_value^2 - min_value^2) / 2
50
50
5. Returns estimated value
51
51
"""
52
- return mean (uniform (min_value , max_value ) for _ in range (iterations ))
52
+ return mean (uniform (min_value , max_value ) for _ in range (iterations )) * ( max_value - min_value )
53
53
54
54
55
- def area_under_line_estimator_check (iterations : int ) -> None :
55
+ def area_under_line_estimator_check (iterations : int ,
56
+ min_value : float = 0.0 ,
57
+ max_value : float = 1.0 ) -> None :
56
58
"""
57
59
Checks estimation error for area_under_line_estimator func
58
60
1. Calls "area_under_line_estimator" function
59
61
2. Compares with the expected value
60
62
3. Prints estimated, expected and error value
61
63
"""
62
- estimate = area_under_line_estimator (iterations )
64
+
65
+ estimated_value = area_under_line_estimator (iterations , min_value , max_value )
66
+ expected_value = (max_value * max_value - min_value * min_value ) / 2
67
+
63
68
print ("******************" )
64
- print ("Estimating area under y=x where x varies from 0 to 1" )
65
- print ("Expected value is " , 0.5 )
66
- print ("Estimated value is " , estimate )
67
- print ("Total error is " , abs (estimate - 0.5 ))
69
+ print ("Estimating area under y=x where x varies from " , min_value , " to " , max_value )
70
+ print ("Estimated value is " , estimated_value )
71
+ print ("Expected value is " , expected_value )
72
+ print ("Total error is " , abs (estimated_value - expected_value ))
68
73
print ("******************" )
69
74
70
75
0 commit comments