Skip to content

Commit b8ccf5b

Browse files
update background
1 parent 191504b commit b8ccf5b

File tree

1 file changed

+318
-8
lines changed

1 file changed

+318
-8
lines changed

class01/background_materials/basics_math.jl

Lines changed: 318 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ begin
1818
using PlutoTeachingTools
1919
using ShortCodes, MarkdownLiteral
2020
using Random
21+
using Plots
2122
Random.seed!(8803)
2223
end
2324

@@ -66,13 +67,16 @@ f(w) = a^T w
6667
"
6768

6869
# ╔═╡ ac439448-a766-4cb2-b76f-b4733cd14195
69-
question_box(md"What is the partial derivative of $f$ with respect to $w_n$ ?")
70+
question_box(md"What is the partial derivative of $f$ with respect to $w_n$? What is the gradient?")
7071

7172
# ╔═╡ 23ff3286-be75-42b8-8327-46ebb7d8b538
72-
md"Write you answer bellow in place of `missing` as a function of `a` and `n`"
73+
md"Write you answer bellow in place of `missing`"
7374

7475
# ╔═╡ a33c9e20-0139-4e72-a6a1-d7e9641195cb
75-
ans=missing
76+
begin
77+
∂f╱∂ωₙ = missing # should be a function of w, a, n
78+
∇f = missing # should be a function of w, a
79+
end
7680

7781
# ╔═╡ c9afb329-2aa8-48e2-8238-8f6f3977b988
7882
begin
@@ -89,18 +93,19 @@ md" **Lets check our answer (with Symbolic Differentiation):**"
8993
begin
9094
@variables w[1:N]
9195

92-
∂╱∂w = Differential(w[n])
93-
96+
∂╱∂w = Differential(w[n])
9497
f(t) = dot(a, t)
98+
99+
grad_f = Symbolics.gradient(f(w), w)
95100

96101
_ans = expand_derivatives(∂╱∂w(f(w)))
97102
end
98103

99104
# ╔═╡ 570f87c7-f808-442e-b22c-d1aaec61922d
100105
begin
101-
if ismissing(ans)
106+
if ismissing(∂f╱∂ωₙ)
102107
still_missing()
103-
elseif _ans == a[n]
108+
elseif ∂f╱∂ωₙ == a[n] && grad_f == ∇f
104109
correct()
105110
else
106111
keep_working()
@@ -285,6 +290,286 @@ let
285290
end
286291
end
287292

293+
# ╔═╡ a6fbd265-f78a-4914-b9ec-f0596db10a4f
294+
md"""
295+
#### g) Least--Squares
296+
297+
The least-squares problem is for $A_{M\times N}$
298+
299+
```math
300+
\min_{x \in \mathbb{R}^{N}} \; \|\,y - Ax\,\|_2^{2},
301+
```
302+
"""
303+
304+
# ╔═╡ ef54ff3d-f713-46c3-a758-b36166f9e898
305+
question_box(md"How many solutions when $rank(A)=N$? Closed form?
306+
307+
How many solutions when $rank(A)<N$? What can you say about Null$(A)$?
308+
309+
How many solutions when $rank(A)=M$? What is the objective value?
310+
")
311+
312+
# ╔═╡ 53b889b3-edf4-4eb2-ac4f-478b51117bf5
313+
md"""
314+
#### i) Regularized Least--Squares
315+
316+
The regularized least-squares problem is
317+
318+
```math
319+
\min_{x \in \mathbb{R}^{N}} \; \|\,y - Ax\,\|_2^{2} \;+\; \delta\,\|\,x\,\|_2^{2},
320+
```
321+
"""
322+
323+
# ╔═╡ 92eda57b-a791-4507-bd30-5e81dcdf8946
324+
question_box(md"For which $Rank(A)$ this is useful?")
325+
326+
# ╔═╡ 890ac713-4636-4ee3-9d3f-fb661f944576
327+
question_box(md"Closed form solution?")
328+
329+
# ╔═╡ 0e3331b5-f7fc-4666-a617-d1b5f99aa162
330+
question_box(md"""
331+
Show that, as δ → 0, the regularized solution converges to the minimum--norm solution of the original problem:
332+
```math
333+
\begin{aligned}
334+
\min_{x \in \mathbb{R}^{N}} \;&\; \|\,x\,\|_2^{2} \\[4pt]
335+
\text{s.t.}\;&\; A^{\mathsf T} A\,x \;=\; A^{\mathsf T} y.
336+
\end{aligned}
337+
```
338+
""")
339+
340+
# ╔═╡ 7a0dc457-c4b4-4893-a36f-366cac98c349
341+
begin
342+
md"""
343+
## 2) Interpolation
344+
345+
Polynomial interpolation can be unstable, even when the samples can be interpolated by a very smooth function. Consider
346+
347+
```math
348+
f(t)=\frac{1}{1+25t^{2}}.
349+
```
350+
351+
Suppose that we sample this function at $M+1$ equally–spaced locations on the interval $[-1,1]$; we take
352+
353+
```math
354+
y_m = f(t_m), \qquad t_m = -1 + \frac{2m}{M}, \quad m = 0,1,\dots,M.
355+
```
356+
"""
357+
end
358+
359+
# ╔═╡ 9afd68f1-0205-40cd-bf33-20fba8069055
360+
begin
361+
f_q1(t) = 1 / (1 + 25 * t * t)
362+
363+
t_q1(M) = [-1 + (2 * i / M) for i in range(0, M)]
364+
365+
ts = -1:0.001:1
366+
plt_q1 = plot(ts, f_q1, xlabel="t", label="f(t) = 1 / (1 + 25 * t²)")
367+
end
368+
369+
# ╔═╡ 7a49dd41-dca3-4a88-84c5-0bb00016bb8e
370+
md"#### a) $M_{th}$ order polynomial"
371+
372+
# ╔═╡ f7e30484-c36c-4624-a38e-1ca0338dc735
373+
begin
374+
question_box(md"""
375+
Find an $M^{\text{th}}$--order polynomial that interpolates the data for
376+
377+
```math
378+
M = 3,\,5,\,7,\,9,\,11,\,15.
379+
```
380+
381+
Plot each interpolant separately, with $f(t)$ overlaid.
382+
""")
383+
end
384+
385+
# ╔═╡ 78558f34-fd3a-4a72-ae30-6966d13a8990
386+
"""
387+
polynomial_fit(tm::Vector{Real}, ym::Vector{Real}, M::Int)::Function
388+
389+
Function to fit a polynomial of order `M` to sample points `(t,y)` using least-squares.
390+
"""
391+
function polynomial_fit(tm::Vector{T}, ym::Vector{T}, M::Int)::Function where {T<:Real}
392+
# Write your answer here in place if missing
393+
return (t) -> missing
394+
end
395+
396+
# ╔═╡ 5db09226-55de-4fe6-b630-0d9748500579
397+
begin
398+
range_M = [3, 5, 7, 9, 11, 15]
399+
polinomials = Vector{Function}(undef, length(range_M))
400+
for (i,m) in enumerate(range_M)
401+
tm = t_q1(m); ym = f_q1.(tm)
402+
polinomials[i] = polynomial_fit(tm, ym, m)
403+
end
404+
end
405+
406+
# ╔═╡ 1a27508f-fd91-4a40-82d0-74753d3d1acd
407+
begin
408+
# Plot here
409+
end
410+
411+
# ╔═╡ f8ca0eaa-6fc4-40eb-9723-f8445cd145dd
412+
function l2_distance_approx(f, g, a, b, num_points=1000)
413+
delta_x = (b - a) / num_points
414+
sum_sq_diff = 0.0
415+
for i in 0:(num_points-1)
416+
x_mid = a + (i + 0.5) * delta_x
417+
sum_sq_diff += (f(x_mid) - g(x_mid))^2
418+
end
419+
return sqrt(sum_sq_diff * delta_x)
420+
end
421+
422+
# ╔═╡ 42210cd5-73bb-4d38-ac37-1896ed61327d
423+
begin
424+
if ismissing(polinomials[1](0.0))
425+
still_missing()
426+
else
427+
l2_dist = l2_distance_approx.(f_q1, polinomials, -1, 1)
428+
_check = [0.344869;0.206622;0.145602;0.154995;0.232231;0.713119]
429+
if norm(_check - l2_dist) <= 1e-6
430+
correct()
431+
else
432+
keep_working()
433+
end
434+
end
435+
end
436+
437+
# ╔═╡ 8253c6cb-ec11-473c-a24f-5be4d645b0cd
438+
md"#### b) Cubic spline interpolation"
439+
440+
# ╔═╡ 590847e9-35a0-4efd-9963-7db770715192
441+
begin
442+
question_box(md"""
443+
For $M+1 = 9$ equally–spaced points, find a cubic spline interpolation.
444+
445+
Impose the endpoint derivative conditions $f'(t=\pm 1)=0$; this yields a
446+
$32×32$ linear system to solve (e.g. with `LinearAlgebra.inv` or `\`).
447+
448+
Plot the resulting cubic spline with $f(t)$ overlaid.
449+
""")
450+
end
451+
452+
# ╔═╡ 3b4e9c20-44fd-4bcc-ab61-231cab22f6ff
453+
# Implement and replace here
454+
cobic_spline = (t) -> missing
455+
456+
# ╔═╡ 8155e2d3-5a82-42d4-8450-297f88da190a
457+
begin
458+
# Plot here
459+
end
460+
461+
# ╔═╡ 002cc5c1-094e-4133-9108-451ff74e8f2f
462+
let
463+
global cobic_spline
464+
if ismissing(cobic_spline(0.0))
465+
still_missing()
466+
else
467+
l2_dist = l2_distance_approx(f_q1, cobic_spline, -1, 1)
468+
if l2_dist < 0.145602
469+
correct()
470+
else
471+
keep_working()
472+
end
473+
end
474+
end
475+
476+
# ╔═╡ 33a6337a-2d74-49a7-bc3a-8f92b78ff16d
477+
md"#### c) Quadratic B-spline Superposition
478+
Suppose that $$f(t)$$ is a second-order spline formed from five shifted quadratic B--splines:
479+
480+
```math
481+
f(t)=\sum_{k=0}^{4}\alpha_k\, b_2(t-k),
482+
```
483+
484+
where the basis function $$b_2(t)$$ is
485+
486+
```math
487+
b_2(t)=
488+
\begin{cases}
489+
\dfrac{(t+\tfrac32)^2}{2}, & -\tfrac32\le t\le -\tfrac12,\\[6pt]
490+
-t^{2}+\dfrac34, & -\tfrac12\le t\le \tfrac12,\\[6pt]
491+
\dfrac{(t-\tfrac32)^2}{2}, & \tfrac12\le t\le \tfrac32,\\[6pt]
492+
0, & |t|\ge \tfrac32.
493+
\end{cases}
494+
```
495+
496+
"
497+
498+
# ╔═╡ 9a170bfa-5ce5-46a5-9024-16394f51289b
499+
begin
500+
question_box(md"""
501+
502+
Find the corresponding coefficients $$\alpha_k$$ so that:
503+
504+
```math
505+
f(0)=-1,\quad f(1)=-1,\quad f(2)=2,\quad f(3)=5,\quad f(4)=1.
506+
```
507+
""")
508+
end
509+
510+
# ╔═╡ 6b006a1e-e53c-490e-ab61-772a168f0064
511+
begin
512+
# Write your answer here
513+
alpha = missing # This should a vector of floats
514+
end
515+
516+
# ╔═╡ 4ffc2c46-7c78-4afa-9ea3-888b1790b291
517+
let
518+
global alpha
519+
if ismissing(alpha)
520+
still_missing()
521+
else
522+
_check = [-1.08975469, -1.46147186, 1.85858586, 6.30995671, 0.28167388]
523+
if norm(_check - alpha) <= 1e-6
524+
correct()
525+
else
526+
keep_working()
527+
end
528+
end
529+
end
530+
531+
# ╔═╡ ad73cc63-2872-431f-803d-8a986993fce2
532+
Columns(question_box(md"""
533+
Now let
534+
535+
```math
536+
f(t)=\sum_{n=0}^{N-1}\alpha_n\, b_2(t-n)
537+
```
538+
be a superposition of $$N$$ quadratic B--splines.
539+
540+
Describe how to construct the $$N\times N$$ matrix $$A$$ that maps the coefficient vector $$\boldsymbol{\alpha}$$ to the sample vector
541+
542+
```math
543+
\begin{bmatrix}
544+
f(0)\\
545+
f(1)\\
546+
\vdots\\
547+
f(N-1)
548+
\end{bmatrix}
549+
=
550+
A
551+
\begin{bmatrix}
552+
\alpha_0\\
553+
\alpha_1\\
554+
\vdots\\
555+
\alpha_{N-1}
556+
\end{bmatrix}.
557+
```
558+
559+
And Show that the matrix $$A$$ is invertible for every $$N$$.
560+
"""),
561+
tip(md"""
562+
$$A$$ is invertible iff $$A\mathbf{x}=0$$ only when $$\mathbf{x}=0$$.
563+
Write $$A$$ as $$I+G$$ for a matrix $$G$$ you can evaluate, and show
564+
565+
```math
566+
\|A\mathbf{x}\|_2=\|\mathbf{x}+G\mathbf{x}\|_2\ge \|\mathbf{x}\|_2-\|G\mathbf{x}\|_2.
567+
```
568+
569+
Demonstrate that $$|G\mathbf{x}|_2<|\mathbf{x}|_2$$ by proving $$G\mathbf{x}$$ is the sum of two vectors whose norms are each strictly less than $$|\mathbf{x}|_2/2$$.
570+
""")
571+
)
572+
288573
# ╔═╡ 4634c856-9553-11ea-008d-3539195970ea
289574
md"## Final notes"
290575

@@ -320,7 +605,7 @@ And others that are useful for Machine Learning:
320605
# ╟─ac439448-a766-4cb2-b76f-b4733cd14195
321606
# ╟─23ff3286-be75-42b8-8327-46ebb7d8b538
322607
# ╠═a33c9e20-0139-4e72-a6a1-d7e9641195cb
323-
# ╟─c9afb329-2aa8-48e2-8238-8f6f3977b988
608+
# ╠═c9afb329-2aa8-48e2-8238-8f6f3977b988
324609
# ╟─529c2464-4611-405b-8ff7-b9de8158aa2d
325610
# ╠═a6bd5fd0-f856-4f1e-8e06-4e16639d436c
326611
# ╠═ebd908a2-98c5-4abb-84cf-9a286779a736
@@ -348,6 +633,31 @@ And others that are useful for Machine Learning:
348633
# ╠═4eaf1b5e-7114-4b1a-bd13-6991f0c596bb
349634
# ╠═17a2fa3a-4dfc-45df-955a-068bbd1c225c
350635
# ╟─1fec2e0a-c108-43a1-a4f3-557af9e215ab
636+
# ╟─a6fbd265-f78a-4914-b9ec-f0596db10a4f
637+
# ╟─ef54ff3d-f713-46c3-a758-b36166f9e898
638+
# ╟─53b889b3-edf4-4eb2-ac4f-478b51117bf5
639+
# ╟─92eda57b-a791-4507-bd30-5e81dcdf8946
640+
# ╟─890ac713-4636-4ee3-9d3f-fb661f944576
641+
# ╟─0e3331b5-f7fc-4666-a617-d1b5f99aa162
642+
# ╟─7a0dc457-c4b4-4893-a36f-366cac98c349
643+
# ╠═9afd68f1-0205-40cd-bf33-20fba8069055
644+
# ╟─7a49dd41-dca3-4a88-84c5-0bb00016bb8e
645+
# ╟─f7e30484-c36c-4624-a38e-1ca0338dc735
646+
# ╠═78558f34-fd3a-4a72-ae30-6966d13a8990
647+
# ╠═5db09226-55de-4fe6-b630-0d9748500579
648+
# ╠═1a27508f-fd91-4a40-82d0-74753d3d1acd
649+
# ╟─f8ca0eaa-6fc4-40eb-9723-f8445cd145dd
650+
# ╟─42210cd5-73bb-4d38-ac37-1896ed61327d
651+
# ╟─8253c6cb-ec11-473c-a24f-5be4d645b0cd
652+
# ╟─590847e9-35a0-4efd-9963-7db770715192
653+
# ╠═3b4e9c20-44fd-4bcc-ab61-231cab22f6ff
654+
# ╠═8155e2d3-5a82-42d4-8450-297f88da190a
655+
# ╟─002cc5c1-094e-4133-9108-451ff74e8f2f
656+
# ╟─33a6337a-2d74-49a7-bc3a-8f92b78ff16d
657+
# ╟─9a170bfa-5ce5-46a5-9024-16394f51289b
658+
# ╠═6b006a1e-e53c-490e-ab61-772a168f0064
659+
# ╟─4ffc2c46-7c78-4afa-9ea3-888b1790b291
660+
# ╟─ad73cc63-2872-431f-803d-8a986993fce2
351661
# ╟─4634c856-9553-11ea-008d-3539195970ea
352662
# ╟─4d0ebb46-9553-11ea-3431-2d203f594815
353663
# ╟─d736e096-9553-11ea-3ba5-277afde1afe7

0 commit comments

Comments
 (0)