Files
teaching/python-ws2425/Practice Problem - Monte-Carlo Error Propagation.ipynb
T

149 lines
4.8 KiB
Plaintext

{
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python [Root]",
"language": "python",
"name": "Python [Root]"
},
"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"
},
"name": "",
"signature": "sha256:4f36806f83cf7279fc86ca9b836091da47da1fd8d6b5d20a372df321fe52996e"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Practice Problem - Monte-Carlo Error Propagation"
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Part 1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You have likely encountered the concept of propagation of uncertainty before (see [the usual rules here](http://en.wikipedia.org/wiki/Propagation_of_uncertainty#Example_formulas)). The idea is that given measurements with uncertainties, we can find the uncertainty on the final result of an equation.\n",
"\n",
"For example, let us consider the following equation:\n",
"\n",
"$$F = \\frac{G~M_1~M_2}{r^2}$$\n",
"\n",
"which gives the gravitational force between two masses $M_1$ and $M_2$ separated by a distance $r$.\n",
"\n",
"Let us now imagine that we have two masses:\n",
"\n",
"$$M_1=40\\times10^4\\pm0.05\\times10^4\\rm{kg}$$\n",
"\n",
"and\n",
"\n",
"$$M_2=30\\times10^4\\pm0.1\\times10^4\\rm{kg}$$\n",
"\n",
"separated by a distance:\n",
"\n",
"$$r=3.2\\pm0.01~\\rm{m}$$\n",
"\n",
"where the uncertaintes are the standard deviations of Gaussian distributions which could be e.g. measurement errors.\n",
"\n",
"We also know:\n",
"\n",
"$$G = 6.67384\\times10^{-11}~\\rm{m}^3~\\rm{kg}^{-1}~\\rm{s}^{-2}$$\n",
"\n",
"(exact value, no uncertainty)\n",
"\n",
"Use the [standard error propagation rules](http://en.wikipedia.org/wiki/Propagation_of_uncertainty#Example_formulas) to determine the resulting force and uncertainty in your script (you can just derive the equation by hand and implement it in a single line in your code).\n",
"\n",
"Now, we can try using a **Monte-Carlo** technique instead. The idea behind Monte-Carlo techniques is to generate many possible solutions using random numbers and using these to look at the overall results. In the above case, you can propagate uncertainties with a Monte-Carlo method by doing the following:\n",
"\n",
"* randomly sample values of $M_1$, $M_2$, and $r$, 1000000 times, using the means and standard deviations given above\n",
"\n",
"* compute the gravitational force for each set of values\n",
"\n",
"You should do this with Numpy arrays, and **without any loops**. You should then get an array of 1000000 different values for the forces.\n",
"\n",
"Make a plot of the normalized histogram of these values of the force, and then overplot a Gaussian function with the mean and standard deviation derived with the standard error propagation rules. Make sure that you pick the range of x values in the plot wisely, so that the two distributions can be seen. Make sure there are also a sensible number of bins in the histogram so that you can compare the shape of the histogram and the Gaussian function. The two distributions should agree pretty well."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"# Your solution here\n"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": null
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Part 2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now repeat the experiment above with the following values:\n",
"\n",
"$$M_1=40\\times10^4\\pm2\\times10^4\\rm{kg}$$\n",
"$$M_2=30\\times10^4\\pm10\\times10^4\\rm{kg}$$\n",
"$$r=3.2\\pm1.0~\\rm{m}$$\n",
"\n",
"and as above, produce a plot.\n",
"\n",
"In this case, which method do you think is more accurate? Why? What do you think are the advantages of using a Monte-Carlo technique?"
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Solution"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"# your solution here\n"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": null
}
],
"metadata": {}
}
]
}