Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
cs417-lecture-examples
Manage
Activity
Members
Labels
Plan
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Terraform modules
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Thomas Kennedy
cs417-lecture-examples
Commits
1f077a48
Commit
1f077a48
authored
4 years ago
by
Thomas Kennedy
Browse files
Options
Downloads
Patches
Plain Diff
Add Monte Carlo integration example
parent
46dac02d
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
MonteCarloIntegration/monte_carlo_integration.py
+115
-0
115 additions, 0 deletions
MonteCarloIntegration/monte_carlo_integration.py
with
115 additions
and
0 deletions
MonteCarloIntegration/monte_carlo_integration.py
0 → 100644
+
115
−
0
View file @
1f077a48
#! /usr/bin/env python3
import
random
import
sys
from
typing
import
(
Callable
,
Tuple
)
Point
=
Tuple
[
float
,
float
]
def
generate_random_points
(
f
:
Callable
,
lower_limit
:
float
,
upper_limit
:
float
,
n
:
int
)
->
Point
:
"""
Generate a sequence of random x values and plug them into f(x).
Args:
f: mathematical function
lower_limit:
'
a
'
the lower bound
upper_bound:
'
b
'
the upper bound
n: number of points to generate
Yields:
A sequence of points in the form (x, f(x))
"""
for
_
in
range
(
0
,
n
):
x
=
random
.
uniform
(
lower_limit
,
upper_limit
)
y
=
f
(
x
)
yield
(
x
,
y
)
def
naive_main
():
"""
This is a
"
naive
"
main function used to demonstrate the basic premise
behind Monte Carlo integration.
"""
num_points
=
int
(
sys
.
argv
[
1
])
limit_a
=
float
(
sys
.
argv
[
2
])
limit_b
=
float
(
sys
.
argv
[
3
])
math_f
=
lambda
x
:
x
**
2
# math_f = lambda x: cos(x)
print
(
"
{:-^80}
"
.
format
(
"
Points
"
),
file
=
sys
.
stderr
)
temp_sum
=
0
for
i
,
point
in
enumerate
(
generate_random_points
(
math_f
,
limit_a
,
limit_b
,
num_points
)):
print
(
f
"
{
i
:
5
d
}
- (
{
point
[
0
]
:
>
12.8
f
}
,
{
point
[
1
]
:
>
12.8
f
}
)
"
,
file
=
sys
.
stderr
)
temp_sum
+=
point
[
1
]
integral_result
=
(
limit_b
-
limit_a
)
/
float
(
num_points
)
*
temp_sum
print
(
f
"
{
integral_result
:
16.8
f
}
"
)
def
not_so_naive_main
():
"""
This main function demonstrates the more
"
Pythonic
"
approach
"""
num_points
=
int
(
sys
.
argv
[
1
])
limit_a
=
float
(
sys
.
argv
[
2
])
limit_b
=
float
(
sys
.
argv
[
3
])
math_f
=
lambda
x
:
x
**
2
# math_f = lambda x: cos(x)
point_sequence
=
generate_random_points
(
math_f
,
limit_a
,
limit_b
,
num_points
)
f_of_x_values
=
(
y
for
x
,
y
in
point_sequence
)
integral_result
=
((
limit_b
-
limit_a
)
/
float
(
num_points
)
*
sum
(
f_of_x_values
))
print
(
f
"
{
integral_result
:
16.8
f
}
"
)
def
main_without_a_table_flip
():
"""
This main demonstrates the impact of the number of points on Monte Carlo
integration
"""
num_points
=
int
(
sys
.
argv
[
1
])
limit_a
=
float
(
sys
.
argv
[
2
])
limit_b
=
float
(
sys
.
argv
[
3
])
max_magnitude
=
int
(
sys
.
argv
[
4
])
math_f
=
lambda
x
:
x
**
2
print
(
"
| {:^16} | {:^20} |
"
.
format
(
"
# Points
"
,
"
Est. f(x)
"
))
max_num_points
=
2
**
max_magnitude
point_sequence
=
list
(
generate_random_points
(
math_f
,
limit_a
,
limit_b
,
max_num_points
))
for
magnitude
in
range
(
0
,
max_magnitude
+
1
):
num_points
=
2
**
magnitude
f_of_x_values
=
(
y
for
x
,
y
in
point_sequence
[:
num_points
])
integral_result
=
((
limit_b
-
limit_a
)
/
float
(
num_points
)
*
sum
(
f_of_x_values
))
print
(
f
"
|
{
num_points
:
>
16
}
|
{
integral_result
:
^
20.8
f
}
|
"
)
if
__name__
==
"
__main__
"
:
# not_so_naive_main()
main_without_a_table_flip
()
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment