{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Week 4 Lab\n",
"\n",
"# **Simulating Weak Law of Large Numbers**\n",
"\n",
"We are not working with a real data set today. Instead we will be producing our own random samples from a known distribution.\n",
"\n",
"We will work with data that is exponentially distributed. Recall from undergraduate statistics that the pdf of the exponential distribution is\n",
"\n",
"$$\n",
"f(y; \\mu) = \n",
"\\begin{cases}\n",
"\\frac{1}{\\mu} e^{-(y / \\mu)} & \\text{if } y \\geq 0 \\\\\n",
"0 & \\text{if } y < 0\n",
"\\end{cases}\n",
"$$\n",
"\n",
"A random variable with this pdf is called **exponentially distributed**, written $Y \\sim \\text{Exp}(\\mu)$.\n",
"\n",
"Two helpful facts about this distribution are that $E(Y) = \\mu$ and $Var(Y) = \\mu^2$.\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Loading packages for this notebook\n",
"We will be needing the packages `Distributions`, `Random`, and `Plots`."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"using Distributions, Random, Plots"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exercise 1: Plot pdf of exponential distribution\n",
"\n",
"Use the Distributions and Plots packages to plot the pdf of the exponential distribution. Play around with different values of the parameter $\\mu$."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# Exercise 1\n",
"\n",
"\n",
"\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exercise 2: Create one random sample, calculate sample mean/variance, plot histogram\n",
"\n",
"Set the sample size $N=5,000$ and the parameter of the exponential distribution $\\mu = 2$."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# Exercise 2\n",
"\n",
"# parameters\n",
"N = 5000\n",
"μ = 2;\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exercise 3: Visualize the law of large numbers\n",
"\n",
"The law of large number states that, given a random sample $Y_1,\\ldots,Y_N$, $\\bar{Y}_N \\overset{p}{\\to} E(Y_1)$. \n",
"\n",
"(Recall: random sample means, by definition, that $Y_i$ are independently and identically distributed.)\n",
"\n",
"Implement the following pseudo-code in Julia:\n",
"\n",
" # This pseudo code creates a vector Ybar\n",
" # of sample averages, and plots\n",
" \n",
" SET n_max (large number)\n",
" INITIALIZE ybar (vector of size n_max)\n",
" SET mu = 2 (parameter of expon dist)\n",
" CREATE superSample (random sample of size n_max from expon dist)\n",
" FOR n in 1 to n_max\n",
" CREATE subSample (first n values of superSample)\n",
" COMPUTE mean of subSample\n",
" STORE mean in position n of ybar\n",
" END\n",
" PLOT ybar against 1:n_max\n",
"\n",
"(Don't concern yourself with code efficiency when implementing your Julia version.)\n",
"\n",
"How does the pseudo-code illustrate the LLN?\n",
"\n",
"Play around with different values of $\\mu$ and pay attention to how quickly convergence seems to occur as a function of the variance of $Y_1$."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# Exercise 3\n",
"\n",
"# parameters\n",
"n_max = 10^2 # start with a small number, but try higher orders subsequently!\n",
"μ = 2;\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Julia 1.7.2",
"language": "julia",
"name": "julia-1.7"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "1.7.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}