File size: 4,671 Bytes
bcf16ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
{
 "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",
    "<br>$\\large var(X)=E[(X-E[X])^2]$\n",
    "<br> where $E[X]$ denotes the *expected value* of random variable $X$. We have talked before about the **expected value**.\n",
    "<br>**Hint:** We also call $E[X]$ the **mean** of $X$.\n",
    "<br>\n",
    "<br>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",
    "<hr>\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",
    "<hr>\n",
    "https://github.com/ostad-ai/Machine-Learning\n",
    "<br> 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": [
    "<hr style=\"height:5px;background-color:lightgreen\">"
   ]
  },
  {
   "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
}