Created
August 29, 2018 08:28
-
-
Save csetzkorn/0440cf16f27011609aceda8d052f7877 to your computer and use it in GitHub Desktop.
monte carlo integration - see data camp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Define the sim_integrate function | |
def sim_integrate(func, xmin, xmax, sims): | |
x = np.random.uniform(xmin, xmax, sims) | |
y = np.random.uniform(min(min(func(x)), 0), max(func(x)), sims) | |
area = (max(y) - min(y))*(xmax-xmin) | |
result = area * sum(abs(y) < abs(func(x)))/sims | |
return result | |
# Call the sim_integrate function and print results | |
result = sim_integrate(func = lambda x: x*np.exp(x), xmin = 0, xmax = 1, sims = 50) | |
print("Simulated answer = {}, Actual Answer = 1".format(result)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment