{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Introduction to mathematical statistics \n", "\n", "Welcome to the lecture 5 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: Probability region" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Sigma = np.array([[1,1],[1,2]])\n", "print(stats.chi2.ppf(0.95,2))\n", "print(\"Sigma = \",Sigma)\n", "print(\"Sigma^-1= \",np.linalg.inv(Sigma))\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Probability region" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "y = np.arange(-np.sqrt(6),np.sqrt(6),0.001)\n", "plt.plot(y,y+np.sqrt(6-y**2))\n", "plt.plot(y,y-np.sqrt(6-y**2))\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Add eigen vectors" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Lambda, V = np.linalg.eig(Sigma)\n", "\n", "print(V)\n", "plt.plot(y,y+np.sqrt(6-y**2),scalex=[-3,3])\n", "plt.plot(y,y-np.sqrt(6-y**2))\n", "plt.plot([0,V[0,0]*np.sqrt(Lambda[0]*6)],[0,V[1,0]*np.sqrt(Lambda[0]*6)])\n", "plt.plot([0,-V[0,1]*np.sqrt(Lambda[1]*6)],[0,-V[1,1]*np.sqrt(Lambda[1]*6)])\n", "plt.axis([-3.5,3.5,-3.5,3.5])\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example: projecton mat" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "A = np.array([[1/2,-1/2],[-1/2,1/2]])\n", "I = np.array([[1,0],[0,1]])\n", "print(I-A)\n", "print(A)\n", "\n", "print(A@A)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Items on a scale" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "X1 = np.array([[1,0],[0,1],[1,1]])\n", "print(np.linalg.inv(X1.T@X1)@X1.T)\n", "\n", "X2 = np.array([[1,0],[1,1],[2,1]])\n", "print(np.linalg.inv(X2.T@X2)@X2.T)\n", "\n", "X3 = np.array([[1,1],[1,-1],[2,0]])\n", "print(np.linalg.inv(X3.T@X3)@X3.T)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Items on a scale orthogonal?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(X1.T@X1)\n", "print(X2.T@X2)\n", "print(X3.T@X3)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Items on a scale" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "H1 = X1 @ np.linalg.inv(X1.T@X1)@X1.T\n", "H2 = X2 @ np.linalg.inv(X2.T@X2)@X2.T\n", "H3 = X3 @ np.linalg.inv(X3.T@X3)@X3.T\n", "\n", "print(np.max(np.abs(H1-H2)))\n", "print(np.max(np.abs(H1-H3)))\n", "H1" ] } ], "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 }