{ "metadata": { "name": "00-Introduction-and-demo" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n",
"\n",
" $ which ipython\n",
" /Users/ivan/dev/anaconda/bin/ipython\n",
"\n",
" $ cd ~/dev/proj/pyconsg2013-tut/ipynb\n",
"\n",
" $ pwd\n",
" /Users/ivan/dev/proj/pyconsg2013-tut/ipynb\n",
"\n",
" $ ipython notebook\n",
" [NotebookApp] Using existing profile dir: u'/Users/ivan/.ipython/profile_default'\n",
" [NotebookApp] Serving notebooks from /Users/ivan/dev/proj/pyconsg2013-tut/ipynb\n",
" [NotebookApp] The IPython Notebook is running at: http://127.0.0.1:8888/\n",
" [NotebookApp] Use Control-C to stop this server and shut down all kernels.\n",
" [NotebookApp] Using MathJax from CDN: http://cdn.mathjax.org/mathjax/latest/MathJax.js\n",
" [NotebookApp] Kernel started: 8843b5b0-be77-4b41-9e90-ed5af6bafbbb\n",
" [NotebookApp] Connecting to: tcp://127.0.0.1:52020\n",
" [IPKernelApp] To connect another client to this kernel, use:\n",
" [IPKernelApp] --existing kernel-8843b5b0-be77-4b41-9e90-ed5af6bafbbb.json\n",
" [NotebookApp] Connecting to: tcp://127.0.0.1:52021\n",
" [NotebookApp] Connecting to: tcp://127.0.0.1:52023\n",
"\n",
"\n",
"\n",
"\n",
"## Download tutorial notebooks\n",
"\n",
"Git:\n",
"\n",
" $ git clone https://github.com/vanzaj/pyconsg2013-tut.git\n",
"\n",
"or download from https://github.com/vanzaj/pyconsg2013-tut/archive/master.zip\n"
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Quick demo"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Computing $\\pi$ using Monte Carlo method\n",
"\n",
"\n",
"Consider a circle inside a square\n",
"\n",
"\n",
"\n",
"([image source](http://learntofish.wordpress.com/2010/10/13/calculating-pi-with-the-monte-carlo-method/))\n",
"\n",
"$$\n",
"\\frac{A_{circle}}{A_{square}} = \\frac{\\pi r^2}{(2r)^2} = \\frac{\\pi}{4}\n",
"$$\n",
"\n",
"The value of $\\pi$ can be found from the ratio of the areas times 4. If we don't know the areas, we can still estimate the ratio by considering a set of random points inside the square and the fraction of points inside the circle (thinks (a few) games of darts).\n"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%pylab inline"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Welcome to pylab, a matplotlib-based Python environment [backend: module://IPython.zmq.pylab.backend_inline].\n",
"For more information, type 'help(pylab)'.\n"
]
}
],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"nb_pts = 5000\n",
"px = rand(nb_pts) # x coordinates\n",
"py = rand(nb_pts) # y coordiantes\n",
"inside = find((px**2 + py**2) <= 1.0)\n",
"nb_inside = len(inside)\n",
"ratio = float(nb_inside)/nb_pts\n",
"print(\"pi estimate: %5.3f\" % (ratio * 4))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"pi estimate: 3.170\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"A bit of visualisation"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"axis('scaled')\n",
"plot(px, py, '.')\n",
"plot(px[inside], py[inside], 'r.')\n",
"\n",
"# quarter of a circle\n",
"cx = linspace(0,1, 100)\n",
"cy = sqrt(1 - cx**2)\n",
"plot(cx, cy, 'k-', linewidth=2)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 3,
"text": [
"[