SHARE
TWEET

Untitled

a guest Oct 13th, 2019 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import math
  2. import random
  3.  
  4. class MonteCarlo:
  5.  
  6.     def __init__(self, function, iterations):
  7.         """
  8.         Initializes the MonteCarlo integrator.
  9.  
  10.         :param function: Inject the function that the user whishes to integrate.
  11.         :param iterations: Give the number of iterations the MonteCarlo integrator is allowed to perform.
  12.         :return self: Return the object (the __init__() method does this by default)
  13.         """
  14.         self.__setFunction(function)
  15.         self.__setIterations(iterations)
  16.  
  17.     def setBoundaries(self, lowerBoundary, upperBoundary):
  18.         """
  19.         Set the integration boundaries and return the object.
  20.  
  21.         :param lowerBoundary: Define the lower boundary.
  22.         :param upperBoundary: Define the upper boundary.
  23.         :return: Return the object such that we can run other methods on this instance.
  24.         """
  25.         self.lowerBoundary = lowerBoundary
  26.         self.upperBoundary = upperBoundary
  27.  
  28.         return self
  29.  
  30.     def setBoundaryCondition(self, requirement):
  31.         """
  32.         Set a boundary condition by injecting a anonymous (lambda) function.
  33.  
  34.         :param requirement: A anonymous function which implements a boundary condition, let's call it a requirement.
  35.         :return: Return the object such that we can run other methods on this instance.
  36.         """
  37.         self.requirement = requirement
  38.  
  39.         return self
  40.  
  41.     def integrate(self):
  42.         """
  43.         Perform the actual integration once all the required parameters are defined/injected in this instance.
  44.  
  45.         :return: Return the integration sum divided by the number of iterations.
  46.         """
  47.         sum = 0
  48.  
  49.         for i in range(self.iterations):
  50.             randomNumberForX = self.__generatePseudoRandomNumber(self.lowerBoundary, self.upperBoundary)
  51.             randomNumberForY = self.__generatePseudoRandomNumber(self.lowerBoundary, self.upperBoundary)
  52.             if self.__validateBoundaryCondition(randomNumberForX, randomNumberForY):
  53.                 continue
  54.             sum += self.function(randomNumberForX, randomNumberForY)
  55.  
  56.         return (sum / self.iterations)
  57.  
  58.     def __validateBoundaryCondition(self, x, y) -> bool:
  59.         """
  60.         For a randomly generated x and y value, check whether they fulfill the condition or requirement.
  61.  
  62.         :param x: x value.
  63.         :param y: y value.
  64.         :return: Return a boolean that indicated whether the condition or requirement has been fulfilled.
  65.         """
  66.         return self.requirement(x, y)
  67.  
  68.     def __setFunction(self, function):
  69.         """
  70.         Set the integrand.
  71.  
  72.         :param function: Anonymous function that represents the integrand.
  73.         :return: None.
  74.         """
  75.         self.function = function
  76.  
  77.     def __setIterations(self, iterations):
  78.         """
  79.         Define the number of iterations that the Monte Carlo integrator is allowed to perform.
  80.  
  81.         :param iterations: Set the number of iterations.
  82.         :return: None
  83.         """
  84.         self.iterations = iterations
  85.  
  86.     def __generatePseudoRandomNumber(self, lowerBoundary, upperBoundary):
  87.         """
  88.         Generate a pseudorandom number required for the Monte Carlo technique.
  89.  
  90.         :param lowerBoundary: The lower boundary for the random number.
  91.         :param upperBoundary: The upper boundary for the random number.
  92.         :return: Random float.
  93.         """
  94.         return random.uniform(lowerBoundary, upperBoundary)
  95.  
  96. """
  97.     Create an instance of the MonteCarlo class and define the integrand and the number of iterations.
  98. """
  99. integrator = MonteCarlo(
  100.     lambda x, y: 1,
  101.     1000000
  102. )
  103.  
  104. """
  105.     After we created an instance, saved in the var 'integrator', define the boundaries and inject an anonymous function
  106.     that represents the integration boundary condition. If that is all done, call the integrate() method to perform
  107.     the actual integration.
  108. """
  109. result = integrator.setBoundaries(0, 1).setBoundaryCondition(
  110.     lambda x, y: False if math.pow(x, 2) + math.pow(y, 2) <= 1 else True
  111. ).integrate();
  112.  
  113. """
  114.     Simply print the result and multiply it by four since we only performed the integration for one quadrant of the
  115.     unit circle. Therefore its boundary [0,1]. The latter should return an approximation of the value of pi.
  116. """
  117. print(result)
  118. print(result * 4)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top