Skip to content
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

Lab03 #41

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 113 additions & 0 deletions Lab03/Lab/Lab03-2.2.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "3503da6f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"For convergence criterion 0.01:\n",
"Average number of draws: 228.51\n",
"Standard deviation of draws: 874.1452567508447\n",
"\n",
"For convergence criterion 0.001:\n",
"Average number of draws: 1204.11\n",
"Standard deviation of draws: 8195.205337140736\n",
"\n",
"For convergence criterion 0.0001:\n",
"Average number of draws: 6670.93\n",
"Standard deviation of draws: 29216.754593641985\n",
"\n",
"For convergence criterion 1e-05:\n",
"Average number of draws: 41313.39\n",
"Standard deviation of draws: 218969.87842234815\n",
"\n"
]
}
],
"source": [
"import random\n",
"import math\n",
"import numpy as np\n",
"\n",
"# Define the convergence criteria\n",
"convergence_criteria = [0.01, 0.001, 0.0001, 0.00001]\n",
"\n",
"# Function to estimate Pi given a convergence criterion\n",
"def estimate_pi(convergence_criterion):\n",
" pi = math.pi\n",
" n_draws = []\n",
"\n",
" for _ in range(10): # 10 runs\n",
" n = 0 # number of points falling in the unit circle\n",
" d = 0 # number of points falling in the unit square\n",
" \n",
" while True:\n",
" x = random.random()\n",
" y = random.random()\n",
" if x**2 + y**2 <= 1.0:\n",
" n += 1\n",
" d += 1\n",
" ratio = 4 * n / d\n",
" \n",
" # Check convergence criterion\n",
" if abs(ratio - pi) / pi <= convergence_criterion:\n",
" n_draws.append(d)\n",
" break\n",
"\n",
" return n_draws\n",
"\n",
"# Calculate statistics for each convergence criterion\n",
"for criterion in convergence_criteria:\n",
" all_draws = []\n",
"\n",
" # Perform 10 runs for the current criterion\n",
" for _ in range(10):\n",
" draws = estimate_pi(criterion)\n",
" all_draws.extend(draws)\n",
"\n",
" # Calculate and print statistics\n",
" average_draws = np.mean(all_draws)\n",
" std_draws = np.std(all_draws)\n",
" \n",
" print(f\"For convergence criterion {criterion}:\")\n",
" print(f\"Average number of draws: {average_draws}\")\n",
" print(f\"Standard deviation of draws: {std_draws}\")\n",
" print()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "de54eb99",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
107 changes: 61 additions & 46 deletions Lab03/Lab/simulate_pi.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,14 @@
"cells": [
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"metadata": {},
"source": [
"# Simulating $\\pi$"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"metadata": {},
"source": [
"The area of a circle $C$ is given as $|C| = \\pi r^2$ with $r$ as the radius.\n",
"\n",
Expand All @@ -34,10 +28,7 @@
},
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"metadata": {},
"source": [
"We can use this relationship to simulate the ratio of the areas to generate an estimate of $\\pi$.\n",
"\n",
Expand All @@ -50,9 +41,6 @@
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true,
"jupyter": {
"outputs_hidden": false
}
Expand All @@ -68,9 +56,6 @@
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true,
"jupyter": {
"outputs_hidden": false
}
Expand All @@ -84,9 +69,6 @@
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true,
"jupyter": {
"outputs_hidden": false
}
Expand Down Expand Up @@ -121,9 +103,6 @@
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true,
"jupyter": {
"outputs_hidden": false
}
Expand Down Expand Up @@ -158,9 +137,6 @@
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true,
"jupyter": {
"outputs_hidden": false
}
Expand Down Expand Up @@ -5824,9 +5800,6 @@
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true,
"jupyter": {
"outputs_hidden": false
}
Expand Down Expand Up @@ -5863,9 +5836,6 @@
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true,
"jupyter": {
"outputs_hidden": false
}
Expand Down Expand Up @@ -5897,9 +5867,6 @@
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true,
"jupyter": {
"outputs_hidden": false
}
Expand Down Expand Up @@ -5937,9 +5904,6 @@
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true,
"jupyter": {
"outputs_hidden": false
}
Expand Down Expand Up @@ -5980,9 +5944,6 @@
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true,
"jupyter": {
"outputs_hidden": false
}
Expand Down Expand Up @@ -6018,8 +5979,6 @@
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true,
"jupyter": {
"outputs_hidden": false
}
Expand Down Expand Up @@ -6221,6 +6180,62 @@
"xf"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter the convergence criterion (e.g., 0.0001): 0.00001 \n",
"Enter the sentinel value for the number of draws: 100\n",
"Draw limit was exceeded.\n",
"Pi estimate: 3.28\n",
"Percentage difference: 4.405642668283338\n"
]
}
],
"source": [
"import random\n",
"import math\n",
"\n",
"# Prompt user for convergence criterion\n",
"convergence_criterion = float(input(\"Enter the convergence criterion (e.g., 0.0001): \"))\n",
"\n",
"# Prompt user for sentinel value\n",
"sentinel_value = int(input(\"Enter the sentinel value for the number of draws: \"))\n",
"\n",
"# True value of Pi\n",
"true_pi = math.pi\n",
"\n",
"n = 0 # number of points falling in the unit circle\n",
"d = 0 # number of points falling in the unit square\n",
"simulating = True # use as a sentinel\n",
"\n",
"while simulating:\n",
" x = random.random()\n",
" y = random.random()\n",
" if x**2 + y**2 <= 1.0:\n",
" n += 1\n",
" d += 1\n",
" ratio = 4 * n * 1.0 / d\n",
"\n",
" # Check convergence criterion\n",
" if abs(ratio - true_pi) / true_pi <= convergence_criterion:\n",
" print(\"Convergence criterion met.\")\n",
" print(\"Pi estimate:\", ratio)\n",
" break\n",
"\n",
" # Check sentinel value\n",
" if d >= sentinel_value:\n",
" print(\"Draw limit was exceeded.\")\n",
" print(\"Pi estimate:\", ratio)\n",
" print(\"Percentage difference:\", abs(ratio - true_pi) / true_pi * 100)\n",
" break\n"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -6231,7 +6246,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -6245,7 +6260,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.8"
"version": "3.11.5"
}
},
"nbformat": 4,
Expand Down
Loading