{ "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\n \n \n \n\n\n\n \n \n \n\n\n\n \n \n \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\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\n \n \n \n\n\n\n \n \n \n\n\n\n \n \n \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\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\n\n \n \n \n\n\n\n \n \n \n\n\n\n \n \n \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n \n \n \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n \n \n \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n \n \n \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" }, "execution_count": 103, "metadata": {}, "output_type": "execute_result" } ], "source": [ "function SimulateAvg(T, NSim)\n", " # Create an array with NSim elements and initialize it with the value NaN. This is bc of if we don't put a number in it later, we will receive an error on the next computation\n", " avg = fill(NaN, NSim)\n", " for i = 1:NSim\n", " x_1 = rand(Chisq(2),T) .- 2\n", " avg[i] = mean(x_1)\n", " end\n", " \n", " return avg\n", "end\n", "\n", "NSim = 10_000\n", "\n", "avg_5 = SimulateAvg(5, NSim)\n", "avg_25 = SimulateAvg(25, NSim)\n", "avg_100 = SimulateAvg(100, NSim)\n", "avg_1000 = SimulateAvg(1000, NSim)\n", "\n", "# printmat(avg_5)\n", "\n", "function getHistogram(values)\n", " return histogram(values, \n", " bins = -3:0.1:3,\n", " normalized = true, legend=false )\n", "end\n", "\n", "plot(getHistogram(avg_5), getHistogram(avg_25), getHistogram(avg_100), getHistogram(avg_1000), layout = (2,2), size=(800, 600))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Task 4: Central Limit Theorem\n", "\n", "Multiply each averages (calculated above) with the square root of the sample size (5, 25, 100, or 1000) and divide by $\\sigma$ (to get $\\sqrt{T} \\bar{x} /\\sigma$) and plot histograms again. In each subplot, add the pdf of a N$(0,1)$ variable. Recall that $\\sigma=2$.\n", "\n", "Notice $\\sqrt{T} \\bar{x}$ should converge to a normally distributed variable (with a zero mean since $x$ has). Dividing by $\\sigma$ should make it a standard normal." ] }, { "cell_type": "code", "execution_count": 115, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": "\n\n\n \n \n \n\n\n\n \n \n \n\n\n\n \n \n \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n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\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" }, "execution_count": 115, "metadata": {}, "output_type": "execute_result" } ], "source": [ "σ = 2\n", "pdfN01 = pdf(Normal(0,1), -3:0.1:3)\n", "\n", "function getHistogramWithPdf(values)\n", " p = histogram(values, \n", " bins = -3:0.1:3,\n", " normalized = true,\n", " legend=false \n", " )\n", " plot!(-3:0.1:3, pdfN01, linewidth=2)\n", " return p\n", "end\n", "\n", "plot(getHistogramWithPdf(avg_5 * sqrt(5) / σ),\n", " getHistogramWithPdf(avg_25 * sqrt(25) / σ),\n", " getHistogramWithPdf(avg_100 * sqrt(100) / σ),\n", " getHistogramWithPdf(avg_1000 * sqrt(1000) / σ),\n", " layout = (2,2),\n", " size = (800,600)\n", " )" ] } ], "metadata": { "@webio": { "lastCommId": null, "lastKernelId": null }, "kernelspec": { "display_name": "Julia 1.7.0-rc1", "language": "julia", "name": "julia-1.7" }, "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "1.7.0" } }, "nbformat": 4, "nbformat_minor": 2 }