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
6ce59d0d
Commit
6ce59d0d
authored
1 year ago
by
Thomas Kennedy
Browse files
Options
Downloads
Patches
Plain Diff
Add numpy to monte carlo integration example
parent
37c09d9b
Branches
master
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
MonteCarloIntegration/Python/monte_carlo_integration.py
+58
-3
58 additions, 3 deletions
MonteCarloIntegration/Python/monte_carlo_integration.py
with
58 additions
and
3 deletions
MonteCarloIntegration/Python/monte_carlo_integration.py
+
58
−
3
View file @
6ce59d0d
...
...
@@ -2,11 +2,31 @@
import
random
import
sys
import
time
from
typing
import
(
Callable
,
Tuple
)
import
numpy
as
np
Point
=
Tuple
[
float
,
float
]
def
simple_timer
(
function
):
"""
This is a simple timer meant to be used as a decorator.
"""
def
wrapper
():
start_time
=
time
.
perf_counter_ns
()
function
()
end_time
=
time
.
perf_counter_ns
()
time_in_sec
=
(
end_time
-
start_time
)
/
10
**
9
print
(
f
"
Total Time:
{
time_in_sec
:
>
4.2
f
}
"
)
return
wrapper
def
generate_random_points
(
f
:
Callable
,
lower_limit
:
float
,
...
...
@@ -90,7 +110,7 @@ def not_so_naive_main():
print
(
f
"
{
integral_result
:
16.8
f
}
"
)
@simple_timer
def
main_without_a_table_flip
():
"""
This main demonstrates the impact of the number of points on Monte Carlo
...
...
@@ -104,12 +124,13 @@ def main_without_a_table_flip():
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
))
point_sequence
=
generate_random_points
(
math_f
,
limit_a
,
limit_b
,
max_num_points
)
all_y_values
=
list
((
y
for
x
,
y
in
point_sequence
))
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
]
)
f_of_x_values
=
all_y_values
[:
num_points
]
integral_result
=
((
limit_b
-
limit_a
)
/
float
(
num_points
)
*
...
...
@@ -118,7 +139,41 @@ def main_without_a_table_flip():
print
(
f
"
|
{
num_points
:
>
16
}
|
{
integral_result
:
^
20.8
f
}
|
"
)
@simple_timer
def
main_with_numpy
():
"""
This main demonstrates the impact of the number of points on Monte Carlo
integration
"""
_
,
limit_a
,
limit_b
,
max_magnitude
=
__parse_cmd_line_args
()
math_f
=
lambda
x
:
x
**
2
print
(
"
| {:^16} | {:^20} |
"
.
format
(
"
# Points
"
,
"
Est. f(x)
"
))
max_num_points
=
2
**
max_magnitude
all_x_values
=
np
.
random
.
uniform
(
low
=
limit_a
,
high
=
limit_b
,
size
=
max_num_points
)
all_y_values
=
math_f
(
all_x_values
)
for
magnitude
in
range
(
0
,
max_magnitude
+
1
):
num_points
=
2
**
magnitude
f_of_x_values
=
all_y_values
[:
num_points
]
integral_result
=
((
limit_b
-
limit_a
)
/
float
(
num_points
)
*
sum
(
f_of_x_values
))
# integral_result = ((limit_b - limit_a) /
# float(num_points) *
# f_of_x_values.sum())
print
(
f
"
|
{
num_points
:
>
16
}
|
{
integral_result
:
^
20.8
f
}
|
"
)
if
__name__
==
"
__main__
"
:
# naive_main()
# not_so_naive_main()
main_without_a_table_flip
()
# main_with_numpy()
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