Files
Jupyter-Notebooks/MonteCarlo Function.ipynb
2024-06-06 16:30:11 +02:00

174 lines
4.3 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "b5867788",
"metadata": {},
"outputs": [],
"source": [
"import random\n",
"import time\n",
"import numpy as np\n",
"\n",
"from scipy.integrate import quad"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "ba84224d",
"metadata": {},
"outputs": [],
"source": [
"class Point:\n",
" x: float\n",
" y: float\n",
"\n",
" def __init__(self, point: (float, float)):\n",
" self.x, self.y = point\n",
"\n",
"\n",
"class LinearBounds:\n",
" lower: float\n",
" higher: float\n",
"\n",
" def __init__(self, lower: float, higher: float):\n",
" self.lower = lower\n",
" self.higher = higher\n",
"\n",
" def length(self) -> float:\n",
" return self.higher - self.lower\n",
"\n",
" def get_random_value(self) -> float:\n",
" return random.uniform(self.lower, self.higher)\n",
"\n",
"\n",
"class Bounds:\n",
" x: LinearBounds\n",
" y: LinearBounds\n",
"\n",
" def __init__(self, x: LinearBounds, y: LinearBounds):\n",
" self.x = x\n",
" self.y = y\n",
"\n",
" def area(self) -> float:\n",
" return self.x.length() * self.y.length()\n",
"\n",
" def get_random_point(self) -> Point:\n",
" return Point((self.x.get_random_value(), self.y.get_random_value()))"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "2120dbad",
"metadata": {},
"outputs": [],
"source": [
"def getRealIntegral(f, bounds: LinearBounds) -> float:\n",
" return quad(f, bounds.lower, bounds.higher)[0]"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "bdc2240a",
"metadata": {},
"outputs": [],
"source": [
"def getIsInside(f, p: Point) -> bool:\n",
" y_0: float = f(p.x)\n",
" if abs(y_0) > abs(p.y) and y_0*p.y >= 0:\n",
" return True\n",
"\n",
" return False"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "d3f58a1f",
"metadata": {},
"outputs": [],
"source": [
"def function(x: float) -> float:\n",
" return x**2-3\n",
"\n",
"bounds = Bounds(LinearBounds(-1, 4), LinearBounds(-4, 15))"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "93672263",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The approximated Integral of the function is: 6.67280\n",
"The real Integral of the function is: 6.66667\n",
"That's an error of 0.00613 or 0.09200% \n",
"And the whole thing took 1.37645 Seconds for 1000000 samples\n"
]
}
],
"source": [
"startTime = time.time()\n",
"pointsInside: int = 0\n",
"samples: int = 10**6\n",
"\n",
"for i in range(samples):\n",
" toTestPoint = bounds.get_random_point()\n",
" if getIsInside(function, toTestPoint):\n",
" if toTestPoint.y >= 0:\n",
" pointsInside += 1\n",
" else:\n",
" pointsInside -= 1\n",
"\n",
"integral = (pointsInside / samples) * bounds.area()\n",
"print(f\"The approximated Integral of the function is: {integral:.5f}\")\n",
"\n",
"real_value: float = getRealIntegral(function, bounds.x)\n",
"print(f\"The real Integral of the function is: {real_value:.5f}\")\n",
"\n",
"error: float = abs(real_value-integral)\n",
"print(f\"That's an error of {error:.5f} or {(error/real_value)*100:.5f}% \")\n",
"\n",
"print(f\"And the whole thing took {time.time()-startTime:.5f} Seconds for {samples} samples\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ce2f249e",
"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.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}