{
"cells": [
{
"cell_type": "code",
"execution_count": 99,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"printyellow (generic function with 1 method)"
]
},
"execution_count": 99,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"using Printf, Statistics, Random, Distributions\n",
"include(\"jlFiles/printmat.jl\")"
]
},
{
"cell_type": "code",
"execution_count": 100,
"metadata": {},
"outputs": [],
"source": [
"using Plots\n",
"\n",
"gr(size=(480,320))\n",
"default(fmt = :svg)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Task 1 (done)\n",
"\n",
"Generate 10000 values of $x=z-2$ where $z$ has a `Chisq(2)` distribution. Show a histogram (with bins from at least -3 to 6) and comment on whether the distribution looks normal. Add the pdf of $x$. \n",
"\n",
"Notice that the (population) mean and standard deviation of a `Chisq(n)` variable are $\\mu=n$ and $\\sigma=\\sqrt{2n}$."
]
},
{
"cell_type": "code",
"execution_count": 101,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": "\n\n"
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"estimated mean and std: -0.002 1.950\n",
"\n",
"\u001b[31m\u001b[1mstrongly non-normal\u001b[22m\u001b[39m\n"
]
}
],
"source": [
"Random.seed!(123) #so the same random numbers are drawn \n",
" #each time you run this cell\n",
"x = rand(Chisq(2),10_000) .- 2\n",
"\n",
"printlnPs(\"estimated mean and std: \",mean(x),\" \",std(x))\n",
"\n",
"zGrid = range(0,8,length=101)\n",
"\n",
"p1 = histogram( x,bins = -3:0.1:6,\n",
" normalized = true, \n",
" legend=false,\n",
" title = \"Histogram of x\",\n",
" xlabel = \"x\" )\n",
"plot!(zGrid.-2,pdf(Chisq(2),zGrid),linewidth=2) #pdf(x) value equals pdf(z) value, where x=z-2\n",
"display(p1)\n",
"\n",
"printred(\"\\nstrongly non-normal\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Task 2: Law of Large Numbers\n",
"\n",
"\n",
"Use the $x$ series above and calculate the average of the first $s$ observations. Do this for $s=1$, $s=2$, ..., $s=2000$. Plot the averages (on the vertical axis) against $s$ (on the horizontal axis).\n",
"\n",
"Hint: `cumsum()` or a loop over `s`."
]
},
{
"cell_type": "code",
"execution_count": 102,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": "\n\n"
},
"execution_count": 102,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"avg_s = cumsum(x)./(1:10_000)\n",
"\n",
"plot(1:2000, avg_s[1:2000], xlabel=\"s\", title=\"sample average over observation 1 to s\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Task 3\n",
"\n",
"(a) Code up a function `SimulateAvg(T,NSim)` which calculates `NSim` different sample averages from `NSim` different samples of length `T` where the data is drawn from $x=z-2$ where $z$ has a `Chisq(2)` distribution. \n",
"\n",
"(b) Simulate 10000 samples of length 5, and estimate the average of x in each sample. Plot a histogram over the averages, using the bins `-3:0.1:3`. Repeat for sample lengths of 25, 100 and 1000."
]
},
{
"cell_type": "code",
"execution_count": 103,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": "\n