{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Scientific Programming using Anaconda\n", "## David Mertz\n", "### dmertz@continuum.io\n", "### 2016-04-04" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Table of Contents\n", "* [Exercise (Monte Carlo approximation)](#Exercise-%28Monte-Carlo-approximation%29)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Exercise (Monte Carlo approximation)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can use the Python random module to perform Monte Carlo simulations. As a fairly simple application, write a function that returns $\\pi$ after approximating it in the manner of the below illustration.\n", "\n", "As a bonus, you can choose to examine when convergence of the value occurs after guessing largest numbers of points (perhaps make the function parameterized by such rough convergence and/or by number of guessed points).\n", "\n", "\n", "\n", "Monte Carlo method applied to approximating the value of π. After placing 30000 random points, the estimate for π is within 0.07% of the actual value. This happens with an approximate probability of 20%." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false, "scrolled": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on built-in function random:\n", "\n", "random(...) method of random.Random instance\n", " random() -> x in the interval [0, 1).\n", "\n" ] } ], "source": [ "import random\n", "help(random.random)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello Cuba!\n" ] } ], "source": [ "print(\"Hello Cuba!\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "continuum": { "depends": [ "ip_functions", "ip_essentials", "ip_flowcontrol", "ip_datatypes" ], "requires": [ "img/Pi_30K.gif" ], "tag": "ip_ex_montecarlo" }, "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.1" } }, "nbformat": 4, "nbformat_minor": 0 }