\n",
"## Click here for text recap of video

\n",
"\n",
"The uniform distribution is very simple, and can only be used in some rare cases. If we only had access to this distribution, our statistical toolbox would be very empty. Thankfully we do have some more advanced distributions!\n",
"\n",
"The uniform distribution that we looked at above is an example of a continuous distribution. The value of $X$ that we draw from this distribution can take **any value** between $a$ and $b$.\n",
"\n",
"However, sometimes we want to be able to look at discrete events. Imagine that the rat from before is now placed in a T-maze, with food placed at the end of both arms. Initially, we would expect the rat to be choosing randomly between the two arms, but after learning it should choose more consistently.\n",
"\n",
"A simple way to model such random behaviour is with a single **Bernoulli trial**, that has two outcomes, {$Left, Right$}, with probability $P(Left)=p$ and $P(Right)=1-p$ as the two mutually exclusive possibilities (whether the rat goes down the left or right arm of the maze).\n",
"

\n",
"\n",
"The binomial distribution simulates $n$ number of binary events, such as the $Left, Right$ choices of the random rat in the T-maze. Imagine that you have done an experiment and found that your rat turned left in 7 out of 10 trials. What is the probability of the rat indeed turning left 7 times ($k = 7$)?\n",
"\n",
"This is given by the binomial probability of $k$, given $n$ trials and probability $p$:\n",
"\n",
"\\begin{align}\n",
"P(k|n,p) &= \\left( \\begin{array} \\\\n \\\\ k\\end{array} \\right) p^k (1-p)^{n-k} \\\\\n",
"\\binom{n}{k} &= {\\frac {n!}{k!(n-k)!}}\n",
"\\end{align}\n",
"\n",
"In this formula, $p$ is the probability of turning left, $n$ is the number of binary events, or trials, and $k$ is the number of times the rat turned left. The term $\\binom {n}{k}$ is the binomial coefficient.\n",
"\n",
"This is an example of a *probability mass function*, which specifies the probability that a discrete random variable is equal to each value. In other words, how large a part of the probability space (mass) is placed at each exact discrete value. We require that all probability adds up to 1, i.e. that\n",
"\n",
"\\begin{equation}\n",
"\\sum_k P(k|n,p)=1.\n",
"\\end{equation}\n",
"\n",
"Essentially, if $k$ can only be one of 10 values, the probabilities of $k$ being equal to each possible value have to sum up to 1 because there is a probability of 1 it will equal one of those 10 values (no other options exist).\n",
"\n",
"If we assume an equal chance of turning left or right, then $p=0.5$. Note that if we only have a single trial $n=1$ this is equivalent to a single Bernoulli trial (feel free to do the math!)."
]
},
{
"cell_type": "markdown",
"metadata": {
"execution": {}
},
"source": [
"### Think! 2.1: Binomial distribution sampling\n",
"\n",
"We will draw a desired number of random samples from a binomial distribution, with $n = 10$ and $p = 0.5$. Each sample returns the number of trials, $k$, a rat turns left out of $n$ trials.\n",
"\n",
"We will draw 1000 samples of this (so it is as if we are observing 10 trials of the rat, 1000 different times). We can do this using numpy: `np.random.binomial(n, p, size = (n_samples,))`\n",
"\n",
"See below to visualize a histogram of the different values of $k$, or the number of times the rat turned left in each of the 1000 samples. In a histogram all the data is placed into bins and the contents of each bin is counted, to give a visualisation of the distribution of data. Discuss the following questions.\n",
"\n",
"1. What are the x-axis limits of the histogram and why?\n",
"2. What is the shape of the histogram?\n",
"3. Looking at the histogram, how would you interpret the outcome of the simulation if you didn't know what p was? Would you have guessed p = 0.5?\n",
"3. What do you think the histogram would look like if the probability of turning left is 0.8 ($p = 0.8$)?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" Execute this cell to see visualization\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cellView": "form",
"execution": {},
"tags": [
"hide-input"
]
},
"outputs": [],
"source": [
"# @markdown Execute this cell to see visualization\n",
"\n",
"# Select parameters for conducting binomial trials\n",
"n = 10\n",
"p = 0.5\n",
"n_samples = 1000\n",
"\n",
"# Set random seed\n",
"np.random.seed(1)\n",
"\n",
"# Now draw 1000 samples by calling the function again\n",
"left_turn_samples_1000 = np.random.binomial(n, p, size = (n_samples,))\n",
"\n",
"# Visualize\n",
"count, bins = plot_hist(left_turn_samples_1000, 'Number of left turns in sample')"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"execution": {}
},
"source": [
"[*Click for solution*](https://github.com/NeuromatchAcademy/precourse/tree/main/tutorials/W0D5_Statistics/solutions/W0D5_Tutorial1_Solution_1ed83ff5.py)\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Submit your feedback\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cellView": "form",
"execution": {},
"tags": [
"hide-input"
]
},
"outputs": [],
"source": [
"# @title Submit your feedback\n",
"content_review(f\"{feedback_prefix}_Binomial_distribution_Sampling_Discussion\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"execution": {}
},
"source": [
"When working with the Bernoulli and binomial distributions, there are only 2 possible outcomes (in this case, turn left or turn right). In the more general case where there are $n$ possible outcomes (our rat is an n-armed maze) each with their own associated probability $p_1, p_2, p_3, p_4, ...$ , we use a **categorical distribution**. Draws from this distribution are a simple extension of the Bernoulli trial: we now have a probability for each outcome and draw based on those probabilities. We have to make sure that the probabilities sum to one:\n",
"\n",
"\\begin{equation}\n",
"\\sum_i P(x=i)=\\sum_i p_i =1\n",
"\\end{equation}\n",
"\n",
"If we sample from this distribution multiple times, we can then describe the distribution of outcomes from each sample as the **multinomial distribution**. Essentially, the categorical distribution is the multiple outcome extension of the Bernoulli, and the multinomial distribution is the multiple outcome extension of the binomial distribution. We'll see a bit more about this in the next tutorial when we look at Markov chains."
]
},
{
"cell_type": "markdown",
"metadata": {
"execution": {}
},
"source": [
"## Section 2.2: Poisson distribution"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Video 4: Poisson distribution\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cellView": "form",
"execution": {},
"tags": [
"remove-input"
]
},
"outputs": [],
"source": [
"# @title Video 4: Poisson distribution\n",
"from ipywidgets import widgets\n",
"from IPython.display import YouTubeVideo\n",
"from IPython.display import IFrame\n",
"from IPython.display import display\n",
"\n",
"\n",
"class PlayVideo(IFrame):\n",
" def __init__(self, id, source, page=1, width=400, height=300, **kwargs):\n",
" self.id = id\n",
" if source == 'Bilibili':\n",
" src = f'https://player.bilibili.com/player.html?bvid={id}&page={page}'\n",
" elif source == 'Osf':\n",
" src = f'https://mfr.ca-1.osf.io/render?url=https://osf.io/download/{id}/?direct%26mode=render'\n",
" super(PlayVideo, self).__init__(src, width, height, **kwargs)\n",
"\n",
"\n",
"def display_videos(video_ids, W=400, H=300, fs=1):\n",
" tab_contents = []\n",
" for i, video_id in enumerate(video_ids):\n",
" out = widgets.Output()\n",
" with out:\n",
" if video_ids[i][0] == 'Youtube':\n",
" video = YouTubeVideo(id=video_ids[i][1], width=W,\n",
" height=H, fs=fs, rel=0)\n",
" print(f'Video available at https://youtube.com/watch?v={video.id}')\n",
" else:\n",
" video = PlayVideo(id=video_ids[i][1], source=video_ids[i][0], width=W,\n",
" height=H, fs=fs, autoplay=False)\n",
" if video_ids[i][0] == 'Bilibili':\n",
" print(f'Video available at https://www.bilibili.com/video/{video.id}')\n",
" elif video_ids[i][0] == 'Osf':\n",
" print(f'Video available at https://osf.io/{video.id}')\n",
" display(video)\n",
" tab_contents.append(out)\n",
" return tab_contents\n",
"\n",
"\n",
"video_ids = [('Youtube', 'E_nvNb596DY'), ('Bilibili', 'BV1wV411x7P6')]\n",
"tab_contents = display_videos(video_ids, W=730, H=410)\n",
"tabs = widgets.Tab()\n",
"tabs.children = tab_contents\n",
"for i in range(len(tab_contents)):\n",
" tabs.set_title(i, video_ids[i][0])\n",
"display(tabs)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Submit your feedback\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cellView": "form",
"execution": {},
"tags": [
"hide-input"
]
},
"outputs": [],
"source": [
"# @title Submit your feedback\n",
"content_review(f\"{feedback_prefix}_Poisson_distribution_Video\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"execution": {}
},
"source": [
"This video covers the Poisson distribution and how it can be used to describe neural spiking.\n",
"\n",
"\n",
"## Click here for text recap of video

\n",
"\n",
"For some phenomena there may not be a natural limit on the maximum number of possible events or outcomes.\n",
"\n",
"The Poisson distribution is a '**point-process**', meaning that it determines the number of discrete 'point', or binary, events that happen within a fixed space or time, allowing for the occurence of a potentially infinite number of events. The Poisson distribution is specified by a single parameter $\\lambda$ that encapsulates the mean number of events that can occur in a single time or space interval (there will be more on this concept of the 'mean' later!).\n",
"\n",
"Relevant to us, we can model the number of times a neuron spikes within a time interval using a Poisson distribution. In fact, neuroscientists often do! As an example, if we are recording from a neuron that tends to fire at an average rate of 4 spikes per second, then the Poisson distribution specifies the distribution of recorded spikes over one second, where $\\lambda=4$.\n",
"\n",
"

\n",
"\n",
"The formula for a Poisson distribution on $x$ is:\n",
"\n",
"\\begin{equation}\n",
"P(x)=\\frac{\\lambda^x e^{-\\lambda}}{x!}\n",
"\\end{equation}\n",
"\n",
"where $\\lambda$ is a parameter corresponding to the average outcome of $x$."
]
},
{
"cell_type": "markdown",
"metadata": {
"execution": {}
},
"source": [
"### Coding Exercise 2.2: Poisson distribution sampling\n",
"\n",
"In the exercise below we will draw some samples from the Poisson distribution and see what the histogram looks.\n",
"\n",
"In the code, fill in the missing line so we draw 5 samples from a Poisson distribution with $\\lambda = 4$. Use `np.random.poisson`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"execution": {}
},
"outputs": [],
"source": [
"# Set random seed\n",
"np.random.seed(0)\n",
"\n",
"# Draw 5 samples from a Poisson distribution with lambda = 4\n",
"sampled_spike_counts = ...\n",
"\n",
"# Print the counts\n",
"print(\"The samples drawn from the Poisson distribution are \" +\n",
" str(sampled_spike_counts))"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"execution": {}
},
"source": [
"[*Click for solution*](https://github.com/NeuromatchAcademy/precourse/tree/main/tutorials/W0D5_Statistics/solutions/W0D5_Tutorial1_Solution_90422623.py)\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"execution": {}
},
"source": [
"You should see that the neuron spiked 6 times, 7 times, 1 time, 8 times, and 4 times in 5 different intervals."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Submit your feedback\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cellView": "form",
"execution": {},
"tags": [
"hide-input"
]
},
"outputs": [],
"source": [
"# @title Submit your feedback\n",
"content_review(f\"{feedback_prefix}_Poisson_distribution_sampling_Exercise\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"execution": {}
},
"source": [
"### Interactive Demo 2.2: Varying parameters of Poisson distribution\n",
"\n",
"Use the interactive demo below to vary $\\lambda$ and the number of samples, and then visualize the resulting histogram.\n",
"\n",
"1. What effect does increasing the number of samples have? \n",
"2. What effect does changing $\\lambda$ have?\n",
"3. With a small lambda, why is the distribution asymmetric?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" Make sure you execute this cell to enable the widget!\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cellView": "form",
"execution": {},
"tags": [
"hide-input"
]
},
"outputs": [],
"source": [
"# @markdown Make sure you execute this cell to enable the widget!\n",
"\n",
"@widgets.interact(lambda_value = widgets.FloatSlider(value=4, min=0.1, max=10, step=0.1),\n",
" n_samples = widgets.IntSlider(value=5, min=5, max=500, step=1))\n",
"\n",
"def gen_and_plot_possion_samples(lambda_value, n_samples):\n",
" sampled_spike_counts = np.random.poisson(lambda_value, n_samples)\n",
" count, bins = plot_hist(sampled_spike_counts, 'Recorded spikes per second')"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"execution": {}
},
"source": [
"[*Click for solution*](https://github.com/NeuromatchAcademy/precourse/tree/main/tutorials/W0D5_Statistics/solutions/W0D5_Tutorial1_Solution_b5d0c82a.py)\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Submit your feedback\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cellView": "form",
"execution": {},
"tags": [
"hide-input"
]
},
"outputs": [],
"source": [
"# @title Submit your feedback\n",
"content_review(f\"{feedback_prefix}_Varying_parameters_of_Poisson_distribution_Interactive_Demo\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"execution": {}
},
"source": [
"---\n",
"# Section 3: Continuous distributions"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Video 5: Continuous distributions\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cellView": "form",
"execution": {},
"tags": [
"remove-input"
]
},
"outputs": [],
"source": [
"# @title Video 5: Continuous distributions\n",
"from ipywidgets import widgets\n",
"from IPython.display import YouTubeVideo\n",
"from IPython.display import IFrame\n",
"from IPython.display import display\n",
"\n",
"\n",
"class PlayVideo(IFrame):\n",
" def __init__(self, id, source, page=1, width=400, height=300, **kwargs):\n",
" self.id = id\n",
" if source == 'Bilibili':\n",
" src = f'https://player.bilibili.com/player.html?bvid={id}&page={page}'\n",
" elif source == 'Osf':\n",
" src = f'https://mfr.ca-1.osf.io/render?url=https://osf.io/download/{id}/?direct%26mode=render'\n",
" super(PlayVideo, self).__init__(src, width, height, **kwargs)\n",
"\n",
"\n",
"def display_videos(video_ids, W=400, H=300, fs=1):\n",
" tab_contents = []\n",
" for i, video_id in enumerate(video_ids):\n",
" out = widgets.Output()\n",
" with out:\n",
" if video_ids[i][0] == 'Youtube':\n",
" video = YouTubeVideo(id=video_ids[i][1], width=W,\n",
" height=H, fs=fs, rel=0)\n",
" print(f'Video available at https://youtube.com/watch?v={video.id}')\n",
" else:\n",
" video = PlayVideo(id=video_ids[i][1], source=video_ids[i][0], width=W,\n",
" height=H, fs=fs, autoplay=False)\n",
" if video_ids[i][0] == 'Bilibili':\n",
" print(f'Video available at https://www.bilibili.com/video/{video.id}')\n",
" elif video_ids[i][0] == 'Osf':\n",
" print(f'Video available at https://osf.io/{video.id}')\n",
" display(video)\n",
" tab_contents.append(out)\n",
" return tab_contents\n",
"\n",
"\n",
"video_ids = [('Youtube', 'LJ4Zdokb6lc'), ('Bilibili', 'BV1dq4y1L7eC')]\n",
"tab_contents = display_videos(video_ids, W=730, H=410)\n",
"tabs = widgets.Tab()\n",
"tabs.children = tab_contents\n",
"for i in range(len(tab_contents)):\n",
" tabs.set_title(i, video_ids[i][0])\n",
"display(tabs)"
]
},
{
"cell_type": "markdown",
"metadata": {
"execution": {}
},
"source": [
"**Note:** There is a typo in the vido ~3.40, where the product of Gaussian distributions should be $\\mathcal{N}(\\mu_1, \\sigma_1^2) \\cdot \\mathcal{N}(\\mu_2, \\sigma_2^2)$."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Submit your feedback\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cellView": "form",
"execution": {},
"tags": [
"hide-input"
]
},
"outputs": [],
"source": [
"# @title Submit your feedback\n",
"content_review(f\"{feedback_prefix}_Continuous_distributions_Video\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"execution": {}
},
"source": [
"We do not have to restrict ourselves to only probabilistic models of discrete events. While some events in neuroscience are discrete (e.g., number of spikes by a neuron), many others are continuous (e.g., neuroimaging signals in EEG or fMRI, distance traveled by an animal, human pointing in the direction of a stimulus).\n",
"\n",
"While for discrete outcomes, we can ask about the probability of a specific event (\"What is the probability this neuron will fire 4 times in the next second\"), this is not defined for a continuous distribution (\"What is the probability of the BOLD signal being exactly 4.000120141...\"). Hence we need to focus on intervals when calculating probabilities from a continuous distribution.\n",
"\n",
"If we want to make predictions about possible outcomes (\"I believe the BOLD signal from the area will be in the range $x_1$ to $ x_2 $\"), we can use the integral $\\int_{x_1}^{x_2} P(x)$.\n",
"$P(x)$ is now a **probability density function**, sometimes written as $f(x)$ to distinguish it from the probability mass functions.\n",
"\n",
"With continuous distributions, we have to replace the normalizing sum\n",
"\n",
"\\begin{equation}\n",
"\\sum_i P(x=p_i) = 1\n",
"\\end{equation}\n",
"\n",
"over all possible events, with an integral\n",
"\n",
"\\begin{equation}\n",
"\\int_a^b P(x) = 1\n",
"\\end{equation}\n",
"\n",
"where a and b are the limits of the random variable $x$ (often $-\\infty$ and $\\infty$)."
]
},
{
"cell_type": "markdown",
"metadata": {
"execution": {}
},
"source": [
"## Section 3.1: Gaussian Distribution\n",
"\n",
"The most widely used continuous distribution is probably the Gaussian (also known as Normal) distribution. It is extremely common across all kinds of statistical analyses. Because of the central limit theorem, many quantities are Gaussian distributed. Gaussians also have some nice mathematical properties that permit simple closed-form solutions to several important problems.\n",
"\n",
"As a working example, imagine that a human participant is asked to point in the direction where they perceived a sound coming from. As an approximation, we can assume that the variability in the direction/orientation they point towards is Gaussian distributed."
]
},
{
"cell_type": "markdown",
"metadata": {
"execution": {}
},
"source": [
"### Coding Exercise 3.1A: Gaussian Distribution\n",
"\n",
"In this exercise, you will implement a Gaussian by filling in the missing portions of code for the function `my_gaussian` below. Gaussians have two parameters. The **mean** $\\mu$, which sets the location of its center, and its \"scale\" or spread is controlled by its **standard deviation** $\\sigma$, or **variance** $\\sigma^2$ (i.e. the square of standard deviation). **Be careful not to use one when the other is required.**\n",
"\n",
"The equation for a Gaussian probability density function is:\n",
"\n",
"\\begin{equation}\n",
"f(x;\\mu,\\sigma^2) = \\mathcal{N}(\\mu,\\sigma^2) = \\frac{1}{\\sqrt{2\\pi\\sigma^2}}\\exp\\left(\\frac{-(x-\\mu)^2}{2\\sigma^2}\\right)\n",
"\\end{equation}\n",
"\n",
"In Python $\\pi$ and $e$ can be written as `np.pi` and `np.exp` respectively.\n",
"\n",
"As a probability distribution this has an integral of one when integrated from $-\\infty$ to $\\infty$, however in the following your numerical Gaussian will only be computed over a finite number of points (for the cell below we will sample from -8 to 9 in step sizes of 0.1). You therefore need to explicitly normalize it to sum to one yourself.\n",
"\n",
"Test out your implementation with a $\\mu = -1$ and $\\sigma = 1$. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"execution": {}
},
"outputs": [],
"source": [
"def my_gaussian(x_points, mu, sigma):\n",
" \"\"\" Returns normalized Gaussian estimated at points `x_points`, with\n",
" parameters: mean `mu` and standard deviation `sigma`\n",
"\n",
" Args:\n",
" x_points (ndarray of floats): points at which the gaussian is evaluated\n",
" mu (scalar): mean of the Gaussian\n",
" sigma (scalar): standard deviation of the gaussian\n",
"\n",
" Returns:\n",
" (numpy array of floats) : normalized Gaussian evaluated at `x`\n",
" \"\"\"\n",
"\n",
" ###################################################################\n",
" ## TODO for students: Implement the formula for a Gaussian\n",
" ## Add code to calculate the gaussian px as a function of mu and sigma,\n",
" ## for every x in x_points\n",
" ## Function Hints: exp -> np.exp()\n",
" ## power -> z**2\n",
" ##\n",
" ## Fill out the following then remove\n",
" raise NotImplementedError(\"Student exercise: need to implement Gaussian\")\n",
" ###################################################################\n",
" px = ...\n",
"\n",
" # as we are doing numerical integration we have to remember to normalise\n",
" # taking into account the stepsize (0.1)\n",
" px = px/(0.1*sum(px))\n",
" return px\n",
"\n",
"x = np.arange(-8, 9, 0.1)\n",
"\n",
"# Generate Gaussian\n",
"px = my_gaussian(x, -1, 1)\n",
"\n",
"# Visualize\n",
"my_plot_single(x, px)"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"execution": {}
},
"source": [
"[*Click for solution*](https://github.com/NeuromatchAcademy/precourse/tree/main/tutorials/W0D5_Statistics/solutions/W0D5_Tutorial1_Solution_2730515e.py)\n",
"\n",
"*Example output:*\n",
"\n",
"\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Submit your feedback\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cellView": "form",
"execution": {},
"tags": [
"hide-input"
]
},
"outputs": [],
"source": [
"# @title Submit your feedback\n",
"content_review(f\"{feedback_prefix}_Gaussian_Distribution_Exercise\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"execution": {}
},
"source": [
"### Interactive Demo 3.1: Sampling from a Gaussian distribution\n",
"\n",
"Now that we have gained a bit of intuition about the shape of the Gaussian, let's imagine that a human participant is asked to point in the direction of a sound source, which we then measure in horizontal degrees. To simulate that we draw samples from a Normal distribution:\n",
"\n",
"\\begin{equation}\n",
"x \\sim \\mathcal{N}(\\mu,\\sigma)\n",
"\\end{equation}\n",
"\n",
"We can sample from a Gaussian with mean $\\mu$ and standard deviation $\\sigma$ using `np.random.normal(mu, sigma, size = (n_samples,))`.\n",
"\n",
"In the demo below, you can change the mean and standard deviation of the Gaussian, and the number of samples, we can compare the histogram of the samples to the true analytical distribution (in red).\n",
"\n",
"1. With what number of samples would you say that the full distribution (in red) is well approximated by the histogram?\n",
"2. What if you just wanted to approximate the variables that defined the distribution, i.e., mean and variance?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" Make sure you execute this cell to enable the widget!\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cellView": "form",
"execution": {},
"tags": [
"hide-input"
]
},
"outputs": [],
"source": [
"#@markdown Make sure you execute this cell to enable the widget!\n",
"\n",
"\n",
"@widgets.interact(mean = widgets.FloatSlider(value=0, min=-5, max=5, step=0.5),\n",
" standard_dev = widgets.FloatSlider(value=0.5, min=0, max=10, step=0.1),\n",
" n_samples = widgets.IntSlider(value=5, min=1, max=300, step=1))\n",
"def gen_and_plot_normal_samples(mean, standard_dev, n_samples):\n",
" x = np.random.normal(mean, standard_dev, size = (n_samples,))\n",
" xspace = np.linspace(-20, 20, 100)\n",
" plot_gaussian_samples_true(x, xspace, mean, standard_dev,\n",
" 'orientation (degrees)', 'probability')"
]
},
{
"cell_type": "markdown",
"metadata": {
"execution": {}
},
"source": [
"**Main course preview:** Gaussian distriutions are everywhere and are critical for filtering, [linear systems](https://compneuro.neuromatch.io/tutorials/W2D2_LinearSystems/chapter_title.html), [optimal control](https://compneuro.neuromatch.io/tutorials/W3D3_OptimalControl/chapter_title.html) and almost any statistical model of continuous data."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Submit your feedback\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cellView": "form",
"execution": {},
"tags": [
"hide-input"
]
},
"outputs": [],
"source": [
"# @title Submit your feedback\n",
"content_review(f\"{feedback_prefix}_Sampling_from_a_Gaussian_distribution_Interactive_Demo\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"execution": {}
},
"source": [
"---\n",
"# Summary\n",
"\n",
"Across the different exercises you should now:\n",
"* have gotten some intuition about how stochastic randomly generated data can be\n",
"* understand how to model data using simple distributions\n",
"* understand the difference between discrete and continuous distributions\n",
"* be able to plot a Gaussian distribution\n",
"\n",
"For more reading on these topics see just about any statistics textbook, or take a look at the [online resources](https://github.com/NeuromatchAcademy/precourse/blob/main/resources.md)."
]
}
],
"metadata": {
"colab": {
"collapsed_sections": [],
"include_colab_link": true,
"name": "W0D5_Tutorial1",
"provenance": [],
"toc_visible": true
},
"kernel": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"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.9.17"
},
"toc-autonumbering": true
},
"nbformat": 4,
"nbformat_minor": 0
}