{ "cells": [ { "cell_type": "markdown", "id": "2e10d47e", "metadata": {}, "source": [ "# 02402 Week 3\n", "\n", "Welcome to week 3 of 02402 Statistics (PF)\n", "\n", "Today we will talk about continous probability distributions, including the normal distribution, using the scipy.stats library" ] }, { "cell_type": "code", "execution_count": 13, "id": "01b8f412", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import matplotlib.pyplot as plt\n", "import pandas as pd\n", "import scipy.stats as stats" ] }, { "cell_type": "markdown", "id": "8821a33c", "metadata": {}, "source": [ "## Part 1: Calculations in the exponential distribution" ] }, { "cell_type": "code", "execution_count": null, "id": "dc75573f", "metadata": {}, "outputs": [], "source": [ "# Lets define our parameter lambda \n", "# OBS: do not name it \"lambda\", since \"lambda\" is a special name for so-called lambda-functions in Python. \n", "# So we just call it lamb\n", "lamb = 3" ] }, { "cell_type": "code", "execution_count": null, "id": "ebb78e6d", "metadata": {}, "outputs": [], "source": [ "# Get values from the pdf (f(x)) directly from the stats.expon.pdf function: \n", "\n", "print(stats.expon.pdf(x=0.0, scale=1/lamb)) # the value of f(x) at x = 0.0\n", "print(stats.expon.pdf(x=0.5, scale=1/lamb)) # the value of f(x) at x = 0.5\n", "print(stats.expon.pdf(x=1.0, scale=1/lamb)) # the value of f(x) at x = 1.0\n", "print(stats.expon.pdf(x=1.5, scale=1/lamb)) # the value of f(x) at x = 1.5" ] }, { "cell_type": "code", "execution_count": null, "id": "1a9246dc", "metadata": {}, "outputs": [], "source": [ "# we can also plot the pdf (f(x)) for a range of x-values:\n", "x = np.arange(0,2,0.01)\n", "plt.plot(x, stats.expon.pdf(x, scale=1/lamb))\n", "plt.show()\n" ] }, { "cell_type": "code", "execution_count": null, "id": "520e9746", "metadata": {}, "outputs": [], "source": [ "# Get values from the cdf (F(x) = P(X < x)) directly from the stats.expon.cdf function: \n", "\n", "print(stats.expon.cdf(x=0.0, scale=1/lamb)) # the value of F(x) at x = 0.0\n", "print(stats.expon.cdf(x=0.5, scale=1/lamb)) # the value of F(x) at x = 0.5\n", "print(stats.expon.cdf(x=1.0, scale=1/lamb)) # the value of F(x) at x = 1.0\n", "print(stats.expon.cdf(x=1.5, scale=1/lamb)) # the value of F(x) at x = 1.5" ] }, { "cell_type": "markdown", "id": "30fdd93a", "metadata": {}, "source": [ "How do these values relate to the plot above? (think about how you would sketch and interpret the probabilities)" ] }, { "cell_type": "code", "execution_count": null, "id": "8f31e45c", "metadata": {}, "outputs": [], "source": [ "# we can also plot the cdf (F(x)) for a range of x-values:\n", "x = np.arange(0,2,0.01)\n", "plt.plot(x, stats.expon.cdf(x, scale=1/lamb))\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "id": "bacb83c3", "metadata": {}, "outputs": [], "source": [ "# get the 0.90-quantile:\n", "print(stats.expon.ppf(0.90, scale=1/lamb)) # ppf is the inverse of cdf" ] }, { "cell_type": "markdown", "id": "74a1bdd9", "metadata": {}, "source": [ "How does this value relate to the plot above? Does the value fit with your visual inspection?" ] }, { "cell_type": "code", "execution_count": null, "id": "cb9110f5", "metadata": {}, "outputs": [], "source": [ "# calculate the mean and variance:\n", "print(stats.expon.mean(scale=1/lamb))\n", "print(stats.expon.var(scale=1/lamb))" ] }, { "cell_type": "code", "execution_count": null, "id": "d3ac80d0", "metadata": {}, "outputs": [], "source": [ "# compare with theoretical values (theorem 2.49):\n", "print(1/lamb)\n", "print(1/lamb**2)" ] }, { "cell_type": "markdown", "id": "4cfbf8e2", "metadata": {}, "source": [ "## Part 2: Examples from slides" ] }, { "cell_type": "markdown", "id": "52792145", "metadata": {}, "source": [ "### Example with Exponential distribution" ] }, { "cell_type": "code", "execution_count": null, "id": "053d69b0", "metadata": {}, "outputs": [], "source": [ "# Example from slide: calculate 1-P(X<10) for Exp(lambda=0.3)\n", "1-stats.expon.cdf(10, scale=1/0.3)" ] }, { "cell_type": "code", "execution_count": null, "id": "3781b1ee", "metadata": {}, "outputs": [], "source": [ "# same question could have been answered formulating a random variable that follow a Poisson distribrution. \n", "# Can you explain WHY this result is the same as the one calculated above? (what probability is calculated here?)\n", "stats.poisson.pmf(0, mu=0.3*10)" ] }, { "cell_type": "markdown", "id": "46508c38", "metadata": {}, "source": [ "### Example with Normal distribution" ] }, { "cell_type": "code", "execution_count": null, "id": "0963d60a", "metadata": {}, "outputs": [], "source": [ "# Example from slide: calculate P(X > 300) in normal distribution\n", "1 - stats.norm.cdf(300, loc=290, scale=4)" ] }, { "cell_type": "code", "execution_count": null, "id": "a53c8c03", "metadata": {}, "outputs": [], "source": [ "# Find lower limit in interval:\n", "stats.norm.ppf(0.025, loc=290, scale=4)" ] }, { "cell_type": "code", "execution_count": null, "id": "7a0659b1", "metadata": {}, "outputs": [], "source": [ "# Find upper limit in interval:\n", "stats.norm.ppf(0.975, loc=290, scale=4)" ] }, { "cell_type": "code", "execution_count": null, "id": "00bf31ed", "metadata": {}, "outputs": [], "source": [ "# calculate lower and upper limit in interval using \"z-quantile\"\n", "print(290 - 1.96*4)\n", "print(290 + 1.96*4)" ] }, { "cell_type": "markdown", "id": "44c14692", "metadata": {}, "source": [ "For exam june 2020, ex III:" ] }, { "cell_type": "code", "execution_count": null, "id": "56c71393", "metadata": {}, "outputs": [], "source": [ "1-stats.binom.cdf(5,n=120, p=0.07)" ] }, { "cell_type": "code", "execution_count": null, "id": "3bbf6e48", "metadata": {}, "outputs": [], "source": [ "15*120*0.07" ] }, { "cell_type": "code", "execution_count": null, "id": "7d5dd082", "metadata": {}, "outputs": [], "source": [ "15*120*0.07*(1-0.07)" ] } ], "metadata": { "kernelspec": { "display_name": "pernille", "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.11.5" } }, "nbformat": 4, "nbformat_minor": 5 }