Last active
December 11, 2015 16:28
-
-
Save herrfz/4627922 to your computer and use it in GitHub Desktop.
Coursera Data Analysis -- in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"metadata": { | |
"name": "simulation" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "heading", | |
"level": 1, | |
"metadata": {}, | |
"source": [ | |
"Simulation Basics -- equivalent in Python" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"import numpy as np\n", | |
"import scipy.stats as s" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 1 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# normal distribution\n", | |
"# normal(mean, stdev, size)\n", | |
"heights = np.random.normal(188, 3, 10)\n", | |
"print heights" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"[ 190.15026626 187.20390227 187.95763008 187.25140845 191.62472211\n", | |
" 190.08039721 186.44935175 184.17754141 192.08413107 190.014875 ]\n" | |
] | |
} | |
], | |
"prompt_number": 2 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# binomial distribution\n", | |
"# binomial(n, p, size)\n", | |
"coinFlips = np.random.binomial(10, 0.5, 10)\n", | |
"print coinFlips" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"[4 2 4 4 5 5 7 6 7 7]\n" | |
] | |
} | |
], | |
"prompt_number": 3 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# normal density\n", | |
"x = np.linspace(-5, 5, num=10)\n", | |
"normalDensity = s.norm.pdf(x, 0, 1)\n", | |
"print np.around(normalDensity, decimals=2)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"[ 0. 0. 0.01 0.1 0.34 0.34 0.1 0.01 0. 0. ]\n" | |
] | |
} | |
], | |
"prompt_number": 4 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# binomial density\n", | |
"x = np.arange(0, 11, 1) # note that it's (0, 11, 1) instead of (0, 10, 1)\n", | |
"binomialDensity = s.binom.pmf(x, 10, 0.5)\n", | |
"print np.around(binomialDensity, decimals=2)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"[ 0. 0.01 0.04 0.12 0.21 0.25 0.21 0.12 0.04 0.01 0. ]\n" | |
] | |
} | |
], | |
"prompt_number": 5 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"## 'sample' draws a random sample with and without replacement --> numpy.random.choice in numpy 1.7\n", | |
"## not available in my installed version of numpy\n", | |
"#\n", | |
"# heights = np.random.normal(188, 3, 10)\n", | |
"#\n", | |
"## with replacement\n", | |
"# np.random.choice(heights, size=10, replace=True, p=None)\n", | |
"#\n", | |
"## without replacement\n", | |
"# r.choice(heights, size=10, replace=False, p=None)\n", | |
"#\n", | |
"## sample according to a set of probability\n", | |
"# probs = [0.4, 0.3, 0.2, 0.1, 0, 0, 0, 0, 0, 0]\n", | |
"# sum(probs)\n", | |
"# np.random.choice(heights, size=10, replace=True, p=probs)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 6 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# setting a seed\n", | |
"np.random.seed(12345)\n", | |
"print np.random.normal(0, 1, 5)\n", | |
"print '-------------------------------------------------------------'\n", | |
"\n", | |
"np.random.seed(12345)\n", | |
"print np.random.normal(0, 1, 5)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"[-0.20470766 0.47894334 -0.51943872 -0.5557303 1.96578057]\n", | |
"-------------------------------------------------------------\n", | |
"[-0.20470766 0.47894334 -0.51943872 -0.5557303 1.96578057]\n" | |
] | |
} | |
], | |
"prompt_number": 8 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment