{ "metadata": { "name": "", "signature": "sha256:d2f72deed8c75c78e56cd49ca438da24eaef281bf80b3a3b92de09745a57c0ac" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Multiplexing Exercise - Monte Carlo \u03c0\n", "\n", "A simple toy problem to get a handle on multiple engines is a Monte\n", "Carlo approximation of \u03c0.\n", "\n", "Let's say we have a dartboard with a round target inscribed on a square\n", "board. If you threw darts randomly, and they land evenly distributed on\n", "the square board, how many darts would you expect to hit the target?\n", "\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\frac{A_c}{A_{sq}} = \\frac{\\pi r^2}{(2r)^2} = \\frac{\\pi}{4}\n", "$$" ] }, { "cell_type": "code", "collapsed": false, "input": [ "from __future__ import print_function\n", "\n", "from random import random\n", "from math import pi\n", "\n", "def mcpi(nsamples):\n", " s = 0\n", " for i in range(nsamples):\n", " x = random()\n", " y = random()\n", " if x*x + y*y <= 1:\n", " s+=1\n", " return 4.*s/nsamples" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "for n in [10, 100, 1000, 10000, 100000, 1000000]:\n", " print(\"%8i\" % n, end=' ')\n", " for i in range(3):\n", " print(\"%.5f\" % mcpi(n), end=' ')\n", " print()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ " 10 3.60000 2.40000 2.80000 \n", " 100 2.72000 3.20000 3.20000 \n", " 1000 3.14800 3.10400 3.16000 \n", " 10000 3.15720 3.13480 3.16320 \n", " 100000 3.13800" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " 3.14412 3.13400" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " \n", " 1000000 3.14120" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " 3.14144" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " 3.14244" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " \n" ] } ], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "%timeit mcpi(1000000)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1 loops, best of 3: 426 ms per loop\n" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "It takes a lot of samples to get a good approximation. Can you write a\n", "function that will use your engines to break up the work?\n", "\n", "```python\n", "def multi_mcpi(dview, nsamples):\n", " raise NotImplementedError(\"you write this\")\n", "```" ] }, { "cell_type": "code", "collapsed": false, "input": [ "from IPython import parallel\n", "rc = parallel.Client()\n", "\n", "view = rc[:]" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 6 }, { "cell_type": "code", "collapsed": true, "input": [ "%load ../soln/mcpi.py" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 4 }, { "cell_type": "code", "collapsed": false, "input": [ "multi_mcpi(view, 10000000)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 10, "text": [ "3.1411995999999998" ] } ], "prompt_number": 10 } ], "metadata": {} } ] }