Skip to content

Commit 7434119

Browse files
committed
Minor changes
1 parent 8b4e767 commit 7434119

16 files changed

+3405
-2642
lines changed

Diff for: DataSets.ipynb

+1-1
Original file line numberDiff line numberDiff line change
@@ -5269,7 +5269,7 @@
52695269
"\n",
52705270
"### http://mldata.org/repository/data/viewslug/well-log/\n",
52715271
"\n",
5272-
"Consists of 4050 nuclear magnetic resonance measurement taken from drill while drilling a well\n",
5272+
"Consists of 4050 nuclear magnetic resonance measurements taken from drill while drilling a well\n",
52735273
"\n"
52745274
]
52755275
},

Diff for: DecisionTree.ipynb

+240
Large diffs are not rendered by default.

Diff for: DrawGraphs.ipynb

+20-16
Large diffs are not rendered by default.

Diff for: DynamicalSystems.ipynb

+985
Large diffs are not rendered by default.

Diff for: HiddenMarkovModel.ipynb

+71-131
Large diffs are not rendered by default.

Diff for: KNN.ipynb

+19-2
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,13 @@
1414
"\n",
1515
"Just store the dataset and for a new observed point $x$, find it's nearest neighbor $i^*$ and report $c_{i^*}$ \n",
1616
"\n",
17-
"\n",
1817
"$$\n",
1918
"i^* = \\arg\\min_{i=1\\dots N} D(x_i, x)\n",
20-
"$$\n"
19+
"$$\n",
20+
"\n",
21+
"## KNN: K nearest neighbors\n",
22+
"\n",
23+
"Find the $k$ nearest neighbors and do a majority voting.\n"
2124
]
2225
},
2326
{
@@ -655,6 +658,13 @@
655658
"print c[:5]"
656659
]
657660
},
661+
{
662+
"cell_type": "markdown",
663+
"metadata": {},
664+
"source": [
665+
"The choice of the distance function (divergence) can be important. In practice, a popular choice is the Euclidian distance but this is by no means the only one. "
666+
]
667+
},
658668
{
659669
"cell_type": "code",
660670
"execution_count": 42,
@@ -688,6 +698,13 @@
688698
"Divergence([0,0],[1,1],p=W)\n"
689699
]
690700
},
701+
{
702+
"cell_type": "markdown",
703+
"metadata": {},
704+
"source": [
705+
"Equal distance contours"
706+
]
707+
},
691708
{
692709
"cell_type": "code",
693710
"execution_count": 27,

Diff for: KalmanFilter.ipynb

+132-81
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,82 @@
44
"cell_type": "markdown",
55
"metadata": {},
66
"source": [
7-
"### Derivation of the Kalman Filter\n",
7+
"# The Linear Dynamical System and the Kalman Filter\n",
88
"\n",
9+
"The Kalman filter is a recursive algorithm for estimating the latent state of a linear dynamical system given the observations.\n",
910
"\n",
10-
"Implement the Kalman filter with parameters $\\mu, P, A, C, Q, R$. \n",
11+
"The linear dynamical system is a key model and understanding it as a generative model is key in understanding the principles and limitations of the Kalman filter.\n",
12+
"\n"
13+
]
14+
},
15+
{
16+
"cell_type": "markdown",
17+
"metadata": {},
18+
"source": [
19+
"## The Linear Dynamical System (LDS)\n",
20+
"\n",
21+
"A (discrete time) linear dynamical system describes the evolution of the state of a system and\n",
22+
"the observations that can be obtained from the state.\n",
23+
"\n",
24+
"The system is described at times $t=1,2,\\dots$. At each time $t$, the state is denoted by an $N \\times 1$ state vector $x_t$. We don't directly observe the states but obtain observations that are related to the states. The observations are denoted by $M \\times 1$ observation vector $y_t$. The mathematical model is \n",
1125
"\n",
1226
"\\begin{eqnarray}\n",
1327
"x_0 & \\sim & {\\mathcal N}(\\mu, P) \\\\\n",
14-
"x_{t} & \\sim & {\\mathcal N}(A x_{t-1}, Q) \\\\\n",
15-
"y_{t} & \\sim & {\\mathcal N}(C x_{t}, R) \n",
28+
"x_{t} & = & A x_{t-1} + \\epsilon_t \\\\\n",
29+
"y_{t} & = & C x_{t} + \\nu_t \n",
1630
"\\end{eqnarray}\n",
1731
"\n",
32+
"here, $\\epsilon_t$ and $\\nu_t$ are assumed to be zero mean Gaussian random variables with variance $Q$ and $P$. \n",
33+
"\n",
34+
"Equivalently, we can express the system as a hierarchical generative model\n",
35+
"\\begin{eqnarray}\n",
36+
"x_0 & \\sim & {\\mathcal N}(\\mu, P) \\\\\n",
37+
"x_{t}|x_{t-1} & \\sim & {\\mathcal N}(A x_{t-1}, Q) \\\\\n",
38+
"y_{t}|x_{t} & \\sim & {\\mathcal N}(C x_{t}, R) \n",
39+
"\\end{eqnarray}\n",
1840
"for $t=1\\dots T$.\n",
19-
"The known Parameters:\n",
2041
"\n",
21-
"$\\mu$ is $N \\times 1$ \n",
42+
"Both formulations are equivalent; where the former is more often used in engineering and the latter in statistics. Later, we will describe a few small extensions to the model.\n",
43+
"\n",
44+
"We can try to understand the meaning of each parameter to understand qualitatively what the LDS is doing. The parameters reflect our knowledge about \n",
2245
"\n",
23-
"$P$ is $N \\times N$ and diagonal, positive semidefinite\n",
46+
"* the initial state: $\\mu, P$\n",
47+
"* the inner working of the system dynamics, the state transition model: $A, Q$\n",
48+
"* how the states are observed, the observation model $C, R$ \n",
2449
"\n",
25-
"$A$ is $N \\times N$\n",
50+
"The initial state mean $\\mu$ is a $N \\times 1$ vector, and the initial state covariance $P$ is an $N \\times N$ positive semidefinite matrix.\n",
51+
"These two parameters reflect our prior knowledge about the initial state of the process: the expected state and the uncertainty around it as characterized by the covariance matrix.\n",
2652
"\n",
27-
"$C$ is $M \\times N$\n",
53+
"Think of the pair $(\\mu, \\Sigma)$ defining an ellipse and the initial state $x_0$ is most likely located somewhere in this ellipse.\n",
54+
"\n",
55+
"The state transition matrix $A$ is $N \\times N$, and the state transition noise covariance matrix $Q$ is $N \\times N$, diagonal, positive semidefinite. The state transition matrix $A$ describes the dynamic behaviour of the linear dynamic system: the system would simply undergo a linear transformation if there was no uncertainty about the dynamics\n",
56+
"$$\n",
57+
"x_t = A x_{t-1}\n",
58+
"$$\n",
59+
"The linear dynamic system model also includes an additive noise term that enables us to model random deviations from an exactly deterministic linear transformation. The deviations are assumed to be zero mean and have the covariance matrix $Q$.\n",
2860
"\n",
29-
"$Q$ is $N \\times N$ and diagonal, positive semidefinite\n",
61+
"Given the previous state $x_{t-1}$ at time $t-1$, think of the pair $(A x_{t-1}, Q)$ defining an ellipse and the current state $x_t$ is most likely located somewhere in this ellipse.\n",
3062
"\n",
31-
"$R$ is $M \\times M$ and diagonal, positive semidefinite\n",
63+
"Finally, the observation matrix \n",
3264
"\n",
65+
"$C$ is $M \\times N$, observation matrix\n",
66+
"$R$ is $M \\times M$ and diagonal, positive semidefinite, observation noise covariance"
67+
]
68+
},
69+
{
70+
"cell_type": "markdown",
71+
"metadata": {},
72+
"source": [
73+
"Example: A point object moving with (almost) constant velocity.\n",
74+
"\n",
75+
"\n",
76+
"\n"
77+
]
78+
},
79+
{
80+
"cell_type": "markdown",
81+
"metadata": {},
82+
"source": [
3383
"The input to the algorithm are the parameters and a sequence of \n",
3484
"observations $y_t$ for $t=1 \\dots T$\n",
3585
"\n",
@@ -49,9 +99,10 @@
4999
"\n",
50100
"-- Covariances: $\\Sigma_{t|t}$\n",
51101
"\n",
52-
"-- A sequence of loglikelihoods $l_k$\n",
102+
"-- A sequence of loglikelihoods $l_k = p(y_k| y_{1:k-1})$\n",
103+
"\n",
53104
"\n",
54-
"## Kalman Filter\n",
105+
"## The algorithm\n",
55106
"\n",
56107
"The Kalman filtering algorithm is as follows:\n",
57108
"\n",
@@ -86,6 +137,74 @@
86137
"End For"
87138
]
88139
},
140+
{
141+
"cell_type": "code",
142+
"execution_count": 24,
143+
"metadata": {
144+
"collapsed": false,
145+
"scrolled": false
146+
},
147+
"outputs": [
148+
{
149+
"name": "stdout",
150+
"output_type": "stream",
151+
"text": [
152+
"mu=\n",
153+
" [[ 10.]\n",
154+
" [ 10.]]\n",
155+
"P=\n",
156+
" [[ 100. 0.]\n",
157+
" [ 0. 100.]]\n",
158+
"A=\n",
159+
" [[ 12. 4.]\n",
160+
" [ 1. -3.]]\n",
161+
"C=\n",
162+
" [[-3. 5.]\n",
163+
" [-4. 2.]\n",
164+
" [ 4. -6.]]\n",
165+
"Q=\n",
166+
" [[ 0.1 0. ]\n",
167+
" [ 0. 0.1]]\n",
168+
"R=\n",
169+
" [[ 2. 0. 0.]\n",
170+
" [ 0. 2. 0.]\n",
171+
" [ 0. 0. 2.]]\n",
172+
"observations=\n",
173+
" [[-1. -5. 6.]\n",
174+
" [ 3. -0. -5.]\n",
175+
" [ 1. -1. -8.]]\n"
176+
]
177+
}
178+
],
179+
"source": [
180+
"import numpy as np\n",
181+
"\n",
182+
"N = 2\n",
183+
"M = 3\n",
184+
"\n",
185+
"A = np.matrix(np.ceil(5*np.random.randn(N,N)))\n",
186+
"C = np.matrix(np.ceil(5*np.random.randn(M,N)))\n",
187+
"R = np.matrix(2*np.eye(M))\n",
188+
"Q = np.matrix(0.1*np.eye(N))\n",
189+
"\n",
190+
"mu = np.matrix(10*np.ones((N,1)))\n",
191+
"P = np.matrix(100*np.eye(N))\n",
192+
"\n",
193+
"print('mu=\\n',mu)\n",
194+
"print('P=\\n',P)\n",
195+
"\n",
196+
"print('A=\\n',A)\n",
197+
"print('C=\\n',C)\n",
198+
"print('Q=\\n',Q)\n",
199+
"print('R=\\n',R)\n",
200+
"\n",
201+
"T = 3;\n",
202+
"\n",
203+
"y = np.matrix(np.ceil(5*np.random.randn(M,T)))\n",
204+
"\n",
205+
"print('observations=\\n',y)"
206+
]
207+
},
89208
{
90209
"cell_type": "markdown",
91210
"metadata": {},
@@ -200,74 +319,6 @@
200319
"All the parameters will be specified when constructing the object and observations will be provided to the filter as a $M \\times T$ matrix."
201320
]
202321
},
203-
{
204-
"cell_type": "code",
205-
"execution_count": 24,
206-
"metadata": {
207-
"collapsed": false,
208-
"scrolled": false
209-
},
210-
"outputs": [
211-
{
212-
"name": "stdout",
213-
"output_type": "stream",
214-
"text": [
215-
"mu=\n",
216-
" [[ 10.]\n",
217-
" [ 10.]]\n",
218-
"P=\n",
219-
" [[ 100. 0.]\n",
220-
" [ 0. 100.]]\n",
221-
"A=\n",
222-
" [[ 12. 4.]\n",
223-
" [ 1. -3.]]\n",
224-
"C=\n",
225-
" [[-3. 5.]\n",
226-
" [-4. 2.]\n",
227-
" [ 4. -6.]]\n",
228-
"Q=\n",
229-
" [[ 0.1 0. ]\n",
230-
" [ 0. 0.1]]\n",
231-
"R=\n",
232-
" [[ 2. 0. 0.]\n",
233-
" [ 0. 2. 0.]\n",
234-
" [ 0. 0. 2.]]\n",
235-
"observations=\n",
236-
" [[-1. -5. 6.]\n",
237-
" [ 3. -0. -5.]\n",
238-
" [ 1. -1. -8.]]\n"
239-
]
240-
}
241-
],
242-
"source": [
243-
"import numpy as np\n",
244-
"\n",
245-
"N = 2\n",
246-
"M = 3\n",
247-
"\n",
248-
"A = np.matrix(np.ceil(5*np.random.randn(N,N)))\n",
249-
"C = np.matrix(np.ceil(5*np.random.randn(M,N)))\n",
250-
"R = np.matrix(2*np.eye(M))\n",
251-
"Q = np.matrix(0.1*np.eye(N))\n",
252-
"\n",
253-
"mu = np.matrix(10*np.ones((N,1)))\n",
254-
"P = np.matrix(100*np.eye(N))\n",
255-
"\n",
256-
"print('mu=\\n',mu)\n",
257-
"print('P=\\n',P)\n",
258-
"\n",
259-
"print('A=\\n',A)\n",
260-
"print('C=\\n',C)\n",
261-
"print('Q=\\n',Q)\n",
262-
"print('R=\\n',R)\n",
263-
"\n",
264-
"T = 3;\n",
265-
"\n",
266-
"y = np.matrix(np.ceil(5*np.random.randn(M,T)))\n",
267-
"\n",
268-
"print('observations=\\n',y)"
269-
]
270-
},
271322
{
272323
"cell_type": "code",
273324
"execution_count": 18,

Diff for: MixtureModels.ipynb

+163
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)