Skip to content

Instantly share code, notes, and snippets.

@tanutarou
Created September 3, 2017 06:32
Show Gist options
  • Select an option

  • Save tanutarou/5e0108f3a862aba17e7877f16bbc72cb to your computer and use it in GitHub Desktop.

Select an option

Save tanutarou/5e0108f3a862aba17e7877f16bbc72cb to your computer and use it in GitHub Desktop.
Calculation of Pi Using the Monte Carlo Method
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"n次元球の体積を用いたモンテカルロ法による$\\pi$の計算"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import numpy as np\n",
"import math"
]
},
{
"cell_type": "code",
"execution_count": 74,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def calc_pi(dim, N):\n",
" \"\"\" \n",
" モンテカルロ法によるpiの計算\n",
" dim : 次元数\n",
" N : シミュレーション回数\n",
" \"\"\"\n",
" cnt = 0 # n次元球に含まれた回数\n",
" for i in range(N):\n",
" r = np.sqrt(sum([np.random.rand()**2 for j in range(dim)]))\n",
" if r <= 1:\n",
" cnt += 1\n",
" return (math.gamma(dim/2 + 1) * 2**dim * (cnt/N)) ** (2/dim)"
]
},
{
"cell_type": "code",
"execution_count": 81,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" n: pi\n",
" 2: 3.1409000000000002\n",
" 3: 3.1414798328376796\n",
" 4: 3.1413364752511876\n",
" 5: 3.138091181495231\n",
" 6: 3.1412505533837036\n",
" 7: 3.134955270430082\n",
" 8: 3.141221866515699\n",
" 9: 3.1557226840558945\n",
"10: 3.1594762405237438\n",
"11: 3.178616355914686\n",
"12: 3.1487253337751135\n",
"13: 3.147298729896722\n",
"14: 2.791857664186204\n",
"15: 1.9255313462211525\n",
"16: 1.817653756143045\n",
"17: 0.0\n",
"18: 0.0\n",
"19: 0.0\n",
"20: 0.0\n"
]
}
],
"source": [
"print(\" n: pi\")\n",
"for i in range(2, 21):\n",
" print(\"{:2d}: {}\".format(i, np.mean([calc_pi(i, 100000) for j in range(10)])))"
]
}
],
"metadata": {
"_draft": {
"nbviewer_url": "https://gist.github.com/896c3401e0ae7c10e3e33627c0064b25"
},
"gist": {
"data": {
"description": "monte_carlo_pi.py.ipynb",
"public": true
},
"id": "896c3401e0ae7c10e3e33627c0064b25"
},
"kernelspec": {
"display_name": "Python 3",
"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.5.2"
},
"toc": {
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"toc_cell": false,
"toc_position": {},
"toc_section_display": "block",
"toc_window_display": false
},
"varInspector": {
"cols": {
"lenName": 16,
"lenType": 16,
"lenVar": 40
},
"kernels_config": {
"python": {
"delete_cmd_postfix": "",
"delete_cmd_prefix": "del ",
"library": "var_list.py",
"varRefreshCmd": "print(var_dic_list())"
},
"r": {
"delete_cmd_postfix": ") ",
"delete_cmd_prefix": "rm(",
"library": "var_list.r",
"varRefreshCmd": "cat(var_dic_list()) "
}
},
"types_to_exclude": [
"module",
"function",
"builtin_function_or_method",
"instance",
"_Feature"
],
"window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment