Skip to content
Closed
Changes from 1 commit
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
94a7fbe
Create monte_carlo_integration_univariate.py
SatyakiMandal Oct 15, 2022
f0a5e38
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 15, 2022
a8aa218
Update monte_carlo_integration_univariate.py
SatyakiMandal Oct 15, 2022
6e05ab1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 15, 2022
1943407
Update monte_carlo_integration_univariate.py
SatyakiMandal Oct 15, 2022
474e5ea
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 15, 2022
162c451
Update monte_carlo_integration_univariate.py
SatyakiMandal Oct 15, 2022
cfe42a6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 15, 2022
46c2efd
Update monte_carlo_integration_univariate.py
SatyakiMandal Oct 15, 2022
da5d6f7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 15, 2022
5993a90
Merge branch 'TheAlgorithms:master' into master
SatyakiMandal Oct 15, 2022
e1c5793
Update monte_carlo_integration_univariate.py
SatyakiMandal Oct 15, 2022
e57cad9
Update maths/monte_carlo_integration_univariate.py
SatyakiMandal Oct 16, 2022
2f17133
Merge branch 'TheAlgorithms:master' into master
SatyakiMandal Oct 16, 2022
75d6877
Update monte_carlo_integration_univariate.py
SatyakiMandal Oct 16, 2022
6ca0383
Update maths/monte_carlo_integration_univariate.py
SatyakiMandal Oct 18, 2022
c1e4653
Update maths/monte_carlo_integration_univariate.py
SatyakiMandal Oct 18, 2022
22b042b
Update maths/monte_carlo_integration_univariate.py
SatyakiMandal Oct 18, 2022
3b41bc6
Merge branch 'TheAlgorithms:master' into master
SatyakiMandal Oct 18, 2022
2f22503
Update monte_carlo_integration_univariate.py
SatyakiMandal Oct 18, 2022
5b6827f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 18, 2022
17a5645
Update monte_carlo_integration_univariate.py
SatyakiMandal Oct 18, 2022
56e3be9
Update monte_carlo_integration_univariate.py
SatyakiMandal Oct 18, 2022
99649da
Update maths/monte_carlo_integration_univariate.py
SatyakiMandal Oct 18, 2022
5afb976
Update maths/monte_carlo_integration_univariate.py
SatyakiMandal Oct 18, 2022
3e18fee
Update maths/monte_carlo_integration_univariate.py
SatyakiMandal Oct 18, 2022
0f413ab
Update maths/monte_carlo_integration_univariate.py
SatyakiMandal Oct 18, 2022
fbb93c8
Update maths/monte_carlo_integration_univariate.py
SatyakiMandal Oct 18, 2022
3314e09
More doctests added
SatyakiMandal Oct 18, 2022
3e8f71f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 18, 2022
f8fe5ef
Update maths/monte_carlo_integration_univariate.py
SatyakiMandal Oct 18, 2022
91c9ca0
Merge branch 'TheAlgorithms:master' into master
SatyakiMandal Oct 18, 2022
dd6c99c
separate plot function, parameters and doctests
SatyakiMandal Oct 18, 2022
c3bff68
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 18, 2022
a26986c
Conflicts corrected
SatyakiMandal Oct 18, 2022
f437d36
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 18, 2022
5b61b0a
Update monte_carlo_integration_univariate.py
SatyakiMandal Oct 18, 2022
bb1a7ec
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 18, 2022
7ce8310
Update monte_carlo_integration_univariate.py
SatyakiMandal Oct 18, 2022
65b9a92
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 18, 2022
477c361
Update monte_carlo_integration_univariate.py
SatyakiMandal Oct 18, 2022
3852f30
Update monte_carlo_integration_univariate.py
SatyakiMandal Oct 18, 2022
a453599
Update monte_carlo_integration_univariate.py
SatyakiMandal Oct 18, 2022
655f519
Update monte_carlo_integration_univariate.py
SatyakiMandal Oct 18, 2022
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
Prev Previous commit
Next Next commit
Update monte_carlo_integration_univariate.py
  • Loading branch information
SatyakiMandal authored Oct 15, 2022
commit 162c4510348356388f78d30fcb1a4dd95e41cbf2
26 changes: 12 additions & 14 deletions maths/monte_carlo_integration_univariate.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@

# function to calculate the sin of a particular value of x
# define your function
def function_to_be_integrated(x: int) -> float:
def function_to_be_integrated(univariate_variable: int) -> float:

# Doctest
"""
:param x: int
:param univariate_variable: int
:return: float

>>> round(function_to_be_integrated(0))
0
"""

return np.sin(x) # example function
return np.sin(univariate_variable) # example function


def monte_carlo(lower_limit: int, upper_limit: int, n: int) -> float:
def monte_carlo(lower_limit: int, upper_limit: int, number_of_sections: int) -> float:

# Doctest
"""
Expand All @@ -48,29 +48,29 @@ def monte_carlo(lower_limit: int, upper_limit: int, n: int) -> float:
plt_vals = []

# array of zeros of length N
ar = np.zeros(n)
ar = np.zeros(number_of_sections)

# we iterate through all the values to generate
# multiple results and show whose intensity is
# the most.
for i in range(n):
for i in range(number_of_sections):

# 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
# values of univariate_variable
integral = 0.0

# iterates and sums up values of different functions
# of x
# of univariate_variable
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
ans = (upper_limit - lower_limit) / float(number_of_sections) * integral
# appends the solution to a list for plotting the graph
plt_vals.append(ans)

Expand All @@ -91,7 +91,7 @@ def monte_carlo(lower_limit: int, upper_limit: int, n: int) -> float:
#-----END OF PLOT SECTION (OPTIONAL)------#
"""

return sum(plt_vals) / N # takinf the average value
return sum(plt_vals) / number_of_sections # takinf the average value


doctest.testmod()
Expand All @@ -103,11 +103,9 @@ def monte_carlo(lower_limit: int, upper_limit: int, n: int) -> float:
lower_limit = 0
upper_limit = np.pi # gets the value of pi

n = 1000 # Number of individual ares to be considered
number_of_sections = 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)}."
)
print(f"The value calculated by monte carlo integration is {monte_carlo(lower_limit, upper_limit, number_of_sections)}.")