-
- Notifications
You must be signed in to change notification settings - Fork 48.8k
Create monte_carlo_integration_univariate.py #7215
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 5 commits
Commits
Show all changes
44 commits Select commit Hold shift + click to select a range
94a7fbe
Create monte_carlo_integration_univariate.py
SatyakiMandal f0a5e38
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] a8aa218
Update monte_carlo_integration_univariate.py
SatyakiMandal 6e05ab1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 1943407
Update monte_carlo_integration_univariate.py
SatyakiMandal 474e5ea
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 162c451
Update monte_carlo_integration_univariate.py
SatyakiMandal cfe42a6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 46c2efd
Update monte_carlo_integration_univariate.py
SatyakiMandal da5d6f7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 5993a90
Merge branch 'TheAlgorithms:master' into master
SatyakiMandal e1c5793
Update monte_carlo_integration_univariate.py
SatyakiMandal e57cad9
Update maths/monte_carlo_integration_univariate.py
SatyakiMandal 2f17133
Merge branch 'TheAlgorithms:master' into master
SatyakiMandal 75d6877
Update monte_carlo_integration_univariate.py
SatyakiMandal 6ca0383
Update maths/monte_carlo_integration_univariate.py
SatyakiMandal c1e4653
Update maths/monte_carlo_integration_univariate.py
SatyakiMandal 22b042b
Update maths/monte_carlo_integration_univariate.py
SatyakiMandal 3b41bc6
Merge branch 'TheAlgorithms:master' into master
SatyakiMandal 2f22503
Update monte_carlo_integration_univariate.py
SatyakiMandal 5b6827f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 17a5645
Update monte_carlo_integration_univariate.py
SatyakiMandal 56e3be9
Update monte_carlo_integration_univariate.py
SatyakiMandal 99649da
Update maths/monte_carlo_integration_univariate.py
SatyakiMandal 5afb976
Update maths/monte_carlo_integration_univariate.py
SatyakiMandal 3e18fee
Update maths/monte_carlo_integration_univariate.py
SatyakiMandal 0f413ab
Update maths/monte_carlo_integration_univariate.py
SatyakiMandal fbb93c8
Update maths/monte_carlo_integration_univariate.py
SatyakiMandal 3314e09
More doctests added
SatyakiMandal 3e8f71f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] f8fe5ef
Update maths/monte_carlo_integration_univariate.py
SatyakiMandal 91c9ca0
Merge branch 'TheAlgorithms:master' into master
SatyakiMandal dd6c99c
separate plot function, parameters and doctests
SatyakiMandal c3bff68
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] a26986c
Conflicts corrected
SatyakiMandal f437d36
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 5b61b0a
Update monte_carlo_integration_univariate.py
SatyakiMandal bb1a7ec
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 7ce8310
Update monte_carlo_integration_univariate.py
SatyakiMandal 65b9a92
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 477c361
Update monte_carlo_integration_univariate.py
SatyakiMandal 3852f30
Update monte_carlo_integration_univariate.py
SatyakiMandal a453599
Update monte_carlo_integration_univariate.py
SatyakiMandal 655f519
Update monte_carlo_integration_univariate.py
SatyakiMandal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
""" | ||
This program finds the approximate integration value/area under the curve of | ||
a specified function within specified limits using Monte Carlo integration method. | ||
| ||
Further, a graph of the individal areas under the curve considered for the calculation | ||
is also plotted. (PLOT SECTION -> Optional implementation) | ||
""" | ||
| ||
import doctest | ||
| ||
SatyakiMandal marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
# importing the modules | ||
SatyakiMandal marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
import random | ||
| ||
import matplotlib.pyplot as plt | ||
import numpy as np | ||
| ||
| ||
# function to calculate the sin of a particular value of x | ||
SatyakiMandal marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
# define your function | ||
SatyakiMandal marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
def function_to_be_integrated(x : int) -> float: | ||
SatyakiMandal marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| ||
# Doctest | ||
""" | ||
:param x: int | ||
:return: float | ||
| ||
>>> round(function_to_be_integrated(0)) | ||
0 | ||
Comment on lines +22 to +23 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add some invalid cases here as well. | ||
""" | ||
| ||
return np.sin(x) # example function | ||
| ||
| ||
def monte_carlo(lower_limit : int, upper_limit : int, n : int) -> float: | ||
SatyakiMandal marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| ||
# Doctest | ||
SatyakiMandal marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
""" | ||
:param lower_limit: int | ||
:param upper_limit: int | ||
:param N: int | ||
:return: float | ||
| ||
SatyakiMandal marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
>>> round(monte_carlo(0, np.pi, 1000)) | ||
SatyakiMandal marked this conversation as resolved. Show resolved Hide resolved | ||
2 | ||
""" | ||
| ||
# list to store all the values for plotting | ||
plt_vals = [] | ||
| ||
# array of zeros of length N | ||
ar = np.zeros(n) | ||
| ||
# we iterate through all the values to generate | ||
# multiple results and show whose intensity is | ||
# the most. | ||
for i in range(n): | ||
| ||
# iterating over each Value of ar and filling it | ||
# with a random value between the limits a and b | ||
for i in range(len(ar)): | ||
ar[i] = random.uniform(lower_limit, upper_limit) | ||
| ||
# variable to store sum of the functions of different | ||
# values of x | ||
integral = 0.0 | ||
| ||
# iterates and sums up values of different functions | ||
# of x | ||
for i in ar: | ||
integral += function_to_be_integrated(i) | ||
| ||
# we get the answer by the formula derived adobe | ||
ans = (upper_limit - lower_limit) / float(n) * integral | ||
# appends the solution to a list for plotting the graph | ||
plt_vals.append(ans) | ||
SatyakiMandal marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| ||
""" | ||
#--------PLOT SECTION (OPTIONAL)----------# | ||
SatyakiMandal marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| ||
# details of the plot to be generated | ||
# sets the title of the plot | ||
plt.title("Distributions of areas calculated") | ||
| ||
# 3 parameters (array on which histogram needs | ||
plt.hist(plt_vals, bins=30, ec="black") | ||
| ||
# sets the label of the x-axis of the plot | ||
plt.xlabel("Areas") | ||
plt.show() # shows the plot | ||
| ||
#-----END OF PLOT SECTION (OPTIONAL)------# | ||
""" | ||
| ||
return sum(plt_vals) / N # takinf the average value | ||
| ||
| ||
doctest.testmod() | ||
SatyakiMandal marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| ||
| ||
# define parameters | ||
# limits of integration (specify limits) | ||
# example limits | ||
lower_limit = 0 | ||
upper_limit = np.pi # gets the value of pi | ||
| ||
n = 1000 # Number of individual ares to be considered | ||
| ||
# function call | ||
# the final area under the curve(integration) value is considered as the average | ||
# of all the individual areas calculated | ||
print(f"\nThe value calculated by monte carlo integration is {monte_carlo(lower_limit, upper_limit, n)}.") |
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.