Skip to content

Commit

Permalink
Lecture 7/8
Browse files Browse the repository at this point in the history
  • Loading branch information
afarbin committed Feb 12, 2024
1 parent 2c5b9f4 commit 496b205
Show file tree
Hide file tree
Showing 2 changed files with 1,136 additions and 80 deletions.
164 changes: 84 additions & 80 deletions Lectures/Lecture.7/Lecture.7.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,34 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## Mean/Variance\n",
"\n",
"Also for lab 3, remember the equations for mean/variance. If you have a data sample ${x_1, x_2, ..., x_N}$ the mean is:\n",
"\n",
"$$ \n",
"\\bar{x} = \\frac{1}{N}\\sum_{i=1}^{N} x_i\n",
"$$\n",
"\n",
"and the variance is:\n",
"\n",
"$$\n",
"<x^2> = \\frac{1}{N-1} \\sum_{i=1}^{N} (x_i - \\bar{x})^2\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Random Number Generator\n",
"\n",
"We have learned about probability distributions and how data is generally a collection of random variables drawn from probability distributions. \n",
"\n",
"We also have discussed that analysis of a dataset is often really just trying to characterize the probability distributions of the random variables. Once we understand those probability distributions, we can then make predictions about future data.\n",
"\n",
"But we can also create (e.g. simulate) new data from the probability distributions. \n",
"\n",
"## Random Number Generator"
"So how do we get a computer to generate data (e.g. random variables) from probability distributions? First, think about generating random numbers in a computer."
]
},
{
Expand All @@ -27,53 +53,6 @@
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['In',\n",
" 'Out',\n",
" '_',\n",
" '__',\n",
" '___',\n",
" '__builtin__',\n",
" '__builtins__',\n",
" '__doc__',\n",
" '__loader__',\n",
" '__name__',\n",
" '__package__',\n",
" '__spec__',\n",
" '_dh',\n",
" '_i',\n",
" '_i1',\n",
" '_i2',\n",
" '_ih',\n",
" '_ii',\n",
" '_iii',\n",
" '_oh',\n",
" 'exit',\n",
" 'get_ipython',\n",
" 'np',\n",
" 'open',\n",
" 'plt',\n",
" 'quit',\n",
" 'x']"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x=5\n",
"dir()"
]
},
{
"cell_type": "code",
"execution_count": 3,
Expand Down Expand Up @@ -271,25 +250,6 @@
"In your solution, you'll most likely generate $x_0$ one by one, compute $x$, and store $x$ into a list to be returned from your function.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Mean/Variance\n",
"\n",
"Also for lab 3, remember the equations for mean/variance. If you have a data sample ${x_1, x_2, ..., x_N}$ the mean is:\n",
"\n",
"$$ \n",
"\\bar{x} = \\frac{1}{N}\\sum_{i=1}^{N} x_i\n",
"$$\n",
"\n",
"and the variance is:\n",
"\n",
"$$\n",
"<x^2> = \\frac{1}{N-1} \\sum_{i=1}^{N} (x_i - \\bar{x})^2\n",
"$$\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -810,6 +770,17 @@
"3 ways to do the same thing:"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [],
"source": [
"y_vals = list()\n",
"for x in x_vals:\n",
" y_vals.append(a_function(x))"
]
},
{
"cell_type": "code",
"execution_count": 29,
Expand Down Expand Up @@ -837,17 +808,6 @@
"print(y_vals)"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [],
"source": [
"y_vals = list()\n",
"for x in x_vals:\n",
" y_vals.append(a_function(x))"
]
},
{
"cell_type": "code",
"execution_count": 32,
Expand Down Expand Up @@ -1105,7 +1065,51 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Note that `find_min` can be rewritten as a single evaluation:"
"Now lets write `find_min` in a more function way. Here is the original again:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def find_min_0(f,x_min,x_max,steps=10):\n",
" \n",
" step_size=(x_max-x_min)/steps\n",
" x=x_min\n",
" y_min=f(x_min)\n",
" x_min_val=x_min\n",
"\n",
" for i in range(steps):\n",
" y=f(x)\n",
" if y<y_min:\n",
" x_min_val=x\n",
" y_min=y\n",
" x+=step_size\n",
" \n",
" return x_min_val"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This function:\n",
"* Loops in \"step\", where in each step\n",
" * Considers a specific value of `x`, starting with `x_min`.\n",
" * Calculates `y=f(x)`.\n",
" * Keeps track of the minimum value of `y` found so far, and the associated `x`.\n",
" * Increments `x`.\n",
"* Returns the value of `x` where `y` was the lowest.\n",
"\n",
"Lets think through the steps of implementing `find_min` functionally:\n",
"* Create a list of all values of `x`.\n",
"* Use that list to create all values of `y`.\n",
"* Find the `arg_min` of lowest value of `y`. \n",
"* Return the `x` value for lowest value of `y`.\n",
"\n",
"Here is the implementation:"
]
},
{
Expand Down Expand Up @@ -1149,7 +1153,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"We could have implemented `a_range` and `arg_min` the same way, but instead of while loops use recursion:"
"As a side, lets work out how to implement `a_range` recursively:"
]
},
{
Expand Down
Loading

0 comments on commit 496b205

Please sign in to comment.