{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "The Binomial distribution\n", "visualize the Binomial distribution X~B(n,p)\n", "think of n coin tosses, sum number of \"face\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "options(repr.plot.width=10, repr.plot.height=6.5) # this command just formats the size of the figures. Adapt to view them nicely\n", " # in your browser." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "n <- 50\n", "p <- 0.80\n", "\n", "k <- seq(0,n,by=1) #Binomial: x can take any value k between 0 and n.\n", "f_k <- dbinom(x = k,size = n,prob = p) # compute pdf of B(n,k) with dbinom\n", "plot(k,f_k, ylab = \"PDF\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Show numerical pdf\n", "\n", "f_k" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Normal distribution\n", "\n", "mean <- 5 # mu in literature\n", "sd <- 0.8 # sigma\n", "\n", "x <- seq(-5,10,length=1000)\n", "y <- dnorm(x,mean,sd) #dnorm : \"D\"ensity of \"NORM\"al distribution. This gives the PDF.\n", "\n", "plot(x, y, type=\"n\", xlab=\"Parameter X\", ylab=\"\",\n", " main=\"Normal Distribution\", axes=FALSE)\n", "axis(1, at=seq(0, 10, 2), pos=0)\n", "lines(x, y)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Normal distribution\n", "mean=100\n", "sd=15\n", "lb=80 # lower bound of X\n", "ub=120 # upper bound of x \n", "#lb=mean-1*sd; ub=mean+1*sd\n", "# This will compute the probability for lb < X < ub\n", "\n", "\n", "x <- seq(-4,4,length=100)*sd + mean\n", "hx <- dnorm(x,mean,sd) #dnorm : \"D\"ensity of \"NORM\"al distribution\n", "\n", "plot(x, hx, type=\"n\", xlab=\"Parameter X\", ylab=\"\",\n", " main=\"Normal Distribution\", axes=FALSE)\n", "\n", "i <- x >= lb & x <= ub\n", "lines(x, hx)\n", "polygon(c(lb,x[i],ub), c(0,hx[i],0), col=\"red\")\n", "\n", "area <- pnorm(ub, mean, sd) - pnorm(lb, mean, sd) #pnorm : \"P\"robability of \"NORM\" (=Phi(Z))\n", "result <- paste(\"P(\",lb,\"< X <\",ub,\") =\",\n", " signif(area, digits=4))\n", "mtext(result,3)\n", "axis(1, at=seq(40, 160, 20), pos=0)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# The Whiskey case\n", "\n", "1-pnorm(q = 69.5,mean = 67.5,sd = 2.5) " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "R", "language": "R", "name": "ir" }, "language_info": { "codemirror_mode": "r", "file_extension": ".r", "mimetype": "text/x-r-source", "name": "R", "pygments_lexer": "r", "version": "3.6.2" } }, "nbformat": 4, "nbformat_minor": 4 }