Skip to content
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 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
111 changes: 111 additions & 0 deletions maths/monte_carlo_integration_univariate.py
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

# importing the modules
import random

import matplotlib.pyplot as plt
import numpy as np


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

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

>>> round(function_to_be_integrated(0))
0
Comment on lines +22 to +23
Copy link
Contributor

Choose a reason for hiding this comment

The 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:

# Doctest
"""
:param lower_limit: int
:param upper_limit: int
:param N: int
:return: float

>>> round(monte_carlo(0, np.pi, 1000))
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)

"""
#--------PLOT SECTION (OPTIONAL)----------#

# 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()


# 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)}.")