matteoraso an implementation of the monte carlo method used to find pi 1 draw a 2x2 square centred at 0 0 2 inscribe a circle within the square 3 for each iteration place a dot anywhere in the square a record the number of dots within the circle 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 a local function to see if a dot lands in the circle our circle has a radius of 1 so a distance greater than 1 would land outside the circle the proportion of guesses that landed in the circle the ratio of the area for circle to square is pi4 an implementation of the monte carlo method to find area under a single variable nonnegative realvalued continuous function say fx where x lies within a continuous bounded interval say minvalue maxvalue where minvalue and maxvalue are finite numbers 1 let x be a uniformly distributed random variable between minvalue to maxvalue 2 expected value of fx integrate fx from minvalue to maxvaluemaxvalue minvalue 3 finding expected value of fx a repeatedly draw x from uniform distribution b evaluate fx at each of the drawn x values c expected value average of the function evaluations 4 estimated value of integral expected value maxvalue minvalue 5 returns estimated value checks estimation error for areaundercurveestimator function for fx x where x lies within minvalue to maxvalue 1 calls areaundercurveestimator function 2 compares with the expected value 3 prints estimated expected and error value represents identity function functiontointegratex for x in 2 0 1 0 0 0 1 0 2 0 2 0 1 0 0 0 1 0 2 0 area under curve y sqrt4 x2 where x lies in 0 to 2 is equal to pi represents semicircle with radius 2 functiontointegratex for x in 2 0 0 0 2 0 0 0 2 0 0 0 an implementation of the monte carlo method used to find pi 1 draw a 2x2 square centred at 0 0 2 inscribe a circle within the square 3 for each iteration place a dot anywhere in the square a record the number of dots within the circle 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 a local function to see if a dot lands in the circle our circle has a radius of 1 so a distance greater than 1 would land outside the circle the proportion of guesses that landed in the circle the ratio of the area for circle to square is pi 4 an implementation of the monte carlo method to find area under a single variable non negative real valued continuous function say f x where x lies within a continuous bounded interval say min_value max_value where min_value and max_value are finite numbers 1 let x be a uniformly distributed random variable between min_value to max_value 2 expected value of f x integrate f x from min_value to max_value max_value min_value 3 finding expected value of f x a repeatedly draw x from uniform distribution b evaluate f x at each of the drawn x values c expected value average of the function evaluations 4 estimated value of integral expected value max_value min_value 5 returns estimated value 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 represents identity function function_to_integrate x for x in 2 0 1 0 0 0 1 0 2 0 2 0 1 0 0 0 1 0 2 0 area under curve y sqrt 4 x 2 where x lies in 0 to 2 is equal to pi represents semi circle with radius 2 function_to_integrate x for x in 2 0 0 0 2 0 0 0 2 0 0 0
from collections.abc import Callable
from math import pi, sqrt
from random import uniform
from statistics import mean


def pi_estimator(iterations: int):
    

    
    def is_in_circle(x: float, y: float) -> bool:
        distance_from_centre = sqrt((x**2) + (y**2))
        
        
        return distance_from_centre <= 1

    
    proportion = mean(
        int(is_in_circle(uniform(-1.0, 1.0), uniform(-1.0, 1.0)))
        for _ in range(iterations)
    )
    
    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)}")


def area_under_curve_estimator(
    iterations: int,
    function_to_integrate: Callable[[float], float],
    min_value: float = 0.0,
    max_value: float = 1.0,
) -> float:
    

    return mean(
        function_to_integrate(uniform(min_value, max_value)) for _ in range(iterations)
    ) * (max_value - min_value)


def area_under_line_estimator_check(
    iterations: int, min_value: float = 0.0, max_value: float = 1.0
) -> None:
    

    def identity_function(x: float) -> float:
        
        return x

    estimated_value = area_under_curve_estimator(
        iterations, identity_function, min_value, max_value
    )
    expected_value = (max_value * max_value - min_value * min_value) / 2

    print("******************")
    print(f"Estimating area under y=x where x varies from {min_value} to {max_value}")
    print(f"Estimated value is {estimated_value}")
    print(f"Expected value is {expected_value}")
    print(f"Total error is {abs(estimated_value - expected_value)}")
    print("******************")


def pi_estimator_using_area_under_curve(iterations: int) -> None:
    

    def function_to_integrate(x: float) -> float:
        
        return sqrt(4.0 - x * x)

    estimated_value = area_under_curve_estimator(
        iterations, function_to_integrate, 0.0, 2.0
    )

    print("******************")
    print("Estimating pi using area_under_curve_estimator")
    print(f"Estimated value is {estimated_value}")
    print(f"Expected value is {pi}")
    print(f"Total error is {abs(estimated_value - pi)}")
    print("******************")


if __name__ == "__main__":
    import doctest

    doctest.testmod()