{ "cells": [ { "cell_type": "markdown", "id": "20fce00f", "metadata": {}, "source": [ "# ML, Data Analysis\n", "### Probability: variance \n", "\n", "The **variance** measures the spread of the given random variable, which is defined by the following formula:\n", "
$\\large var(X)=E[(X-E[X])^2]$\n", "
where $E[X]$ denotes the *expected value* of random variable $X$. We have talked before about the **expected value**.\n", "
**Hint:** We also call $E[X]$ the **mean** of $X$.\n", "
\n", "
Some properties of variance:\n", "1. **Non-negativity:** For any random varibale $X$: $var(X)>=0$\n", "2. **Linear transformation:** For constants $a$ and $b$, we have: $var(aX+b)=a^2var(X)$\n", "3. **Sum of variances:** Having *uncorrelated* random variables $X$ and $Y$ ($E[XY]=E[X]E[Y]$): we have: $var(X+Y)=var(X)+var(Y)$\n", "
\n", "\n", "**Contents:**\n", " - Computing the mean and variance for a discrete random variable.\n", " - Computing the mean and variance for a continuous random variable.\n", "
\n", "https://github.com/ostad-ai/Machine-Learning\n", "
Explanation: https://www.pinterest.com/HamedShahHosseini/Machine-Learning/background-knowledge" ] }, { "cell_type": "code", "execution_count": 1, "id": "f0c89242", "metadata": {}, "outputs": [], "source": [ "# Import the required function for integration\n", "from scipy.integrate import quad" ] }, { "cell_type": "code", "execution_count": 2, "id": "d4430b1d", "metadata": {}, "outputs": [], "source": [ "# Functions for discerete random variables\n", "def mean(data,probs=None):\n", " n=len(data)\n", " if probs is None:\n", " probs=[1/n]*n\n", " return sum(x*p for x,p in zip(data,probs))\n", "\n", "def variance(data,probs=None):\n", " mu = mean(data,probs)\n", " n=len(data)\n", " if probs is None:\n", " probs=[1/n]*n\n", " squared_diff = [p*(x - mu) ** 2 for x,p in zip(data,probs)]\n", " return sum(squared_diff)" ] }, { "cell_type": "code", "execution_count": 7, "id": "eb2a6b01", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The outcomes: [1, 2, 3, 4, 5, 6]\n", "The mean: 3.5\n", "The variance: 2.9166666666666665\n" ] } ], "source": [ "# Example: discrete random variable\n", "outcomes = [1, 2, 3, 4, 5, 6]\n", "print(f'The outcomes: {outcomes}')\n", "print(f'The mean: {mean(outcomes)}')\n", "print(f'The variance: {variance(outcomes)}')" ] }, { "cell_type": "markdown", "id": "1c730379", "metadata": {}, "source": [ "
" ] }, { "cell_type": "code", "execution_count": 4, "id": "e97de675", "metadata": {}, "outputs": [], "source": [ "# Functions for continuous random variable\n", "# Define the probability (density) function (for short, PDF)\n", "def pdf(x):\n", " return 2. * x if 0 <= x <= 1 else 0.\n", "\n", "def mean_c():\n", " integrand=lambda x: x*pdf(x)\n", " mean, _ = quad(integrand, 0,1)\n", " return mean\n", "\n", "def variance_c():\n", " mu=mean_c()\n", " integrand=lambda x: (x-mu)**2*pdf(x)\n", " var,_=quad(integrand, 0,1)\n", " return var" ] }, { "cell_type": "code", "execution_count": 6, "id": "d47e820d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The pdf is a ramp defined between zero and one\n", "The mean: 0.6666666666666667\n", "The variance: 0.05555555555555555\n" ] } ], "source": [ "# Example: continuous random variable\n", "print('The pdf is a ramp defined between zero and one')\n", "print(f'The mean: {mean_c()}')\n", "print(f'The variance: {variance_c()}')" ] }, { "cell_type": "code", "execution_count": null, "id": "e4203044", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.1" } }, "nbformat": 4, "nbformat_minor": 5 }