{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Introduction to mathematical statistics \n", "\n", "Welcome to lecture 3 in 02403\n", "\n", "During the lectures we will present both slides and notebooks. \n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import matplotlib.pyplot as plt\n", "import scipy.stats as stats" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Example: June 2024, Lotka-Volterra" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "k = 10000\n", "alpha = 2/3\n", "beta = 4/3\n", "gamma = delta = 1\n", "sig_x = 1/8\n", "sig_y = 1/64\n", "mu_x = 1\n", "mu_y = 1/2\n", "x = stats.norm.rvs(size= k, loc = mu_x, scale = sig_x)\n", "y = stats.norm.rvs(size= k, loc = mu_y, scale = sig_y)\n", "K = y**alpha * np.exp(-beta * y) * x**gamma * np.exp(-delta * x)\n", "print(\"mu=\",np.mean(K))\n", "print(\"sigma^2=\",np.var(K, ddof = 1))\n", "print(\"sigma=\",np.std(K,ddof = 1))\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.hist(K,density=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## June 2024 part two" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mu_x = np.arange(0.5,2,0.01)\n", "K = mu_y**alpha * np.exp(-beta * mu_y) * mu_x**gamma * np.exp(-delta * mu_x)\n", "Kx = K * (gamma / mu_x - delta)\n", "Ky = K * (alpha / mu_y - beta)\n", "Vk = Kx**2 * sig_x**2 +Ky**2 * sig_y**2\n", "fig, ax =plt.subplots(2,1)\n", "ax[0].plot(mu_x,K) ## mean\n", "ax[1].plot(mu_x,Vk) ## mean\n", "plt.tight_layout()\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Standard scale" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "A = np.array([[1/2,-1/2],[-1/2,1/2]])\n", "print(A)\n", "print(A @ A)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "At = np.array([[1/2,-1/2],[-1/2,1/2],[1/2,1/2]])\n", "print(At)\n", "print(At @ At.T)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Variance of normal sample" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "n = 5\n", "k = 10000\n", "y = stats.norm.rvs(loc=10, scale=np.sqrt(2), size=(k,n))\n", "print(y)\n", "S2 = y.var(axis=1,ddof=1)\n", "plt.hist(S2,bins=20,density=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example: Meen and median" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "n = 5\n", "k = 10000\n", "y = stats.norm.rvs(loc=10, scale=np.sqrt(2), size=(k,n))\n", "y_bar = y.mean(axis=1)\n", "y_tilde = np.median(y,axis=1)\n", "print(\"E[y_bar]\", np.mean(y_bar))\n", "print(\"E[y_tilde]\", np.median(y_bar))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(\"V[y_bar]\", np.var(y_bar,ddof=1))\n", "print(\"V[y_tilde]\", np.var(y_tilde,ddof=1))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example P(Q>10)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "1-stats.chi2.cdf(10,df=10)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.10.16" } }, "nbformat": 4, "nbformat_minor": 2 }