1818 using PlutoTeachingTools
1919 using ShortCodes, MarkdownLiteral
2020 using Random
21+ using Plots
2122 Random. seed! (8803 )
2223end
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
7882begin
@@ -89,18 +93,19 @@ md" **Lets check our answer (with Symbolic Differentiation):**"
8993begin
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)))
97102end
98103
99104# ╔═╡ 570f87c7-f808-442e-b22c-d1aaec61922d
100105begin
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
286291end
287292
293+ # ╔═╡ a6fbd265-f78a-4914-b9ec-f0596db10a4f
294+ md """
295+ #### g) Least--Squares
296+
297+ The least-squares problem is for $A_{M\t imes N}$
298+
299+ ```math
300+ \m in_{x \i n \m athbb{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+ \m in_{x \i n \m athbb{R}^{N}} \; \|\, y - Ax\,\| _2^{2} \; +\; \d elta\,\|\, 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+ \b egin{aligned}
334+ \m in_{x \i n \m athbb{R}^{N}} \; &\; \|\, x\,\| _2^{2} \\ [4pt]
335+ \t ext{s.t.}\; &\; A^{\m athsf T} A\, x \; =\; A^{\m athsf T} y.
336+ \e nd{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)=\f rac{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), \q quad t_m = -1 + \f rac{2m}{M}, \q uad m = 0,1,\d ots,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^{\t ext{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=\p m 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)=\s um_{k=0}^{4}\a lpha_k\, b_2(t-k),
482+ ```
483+
484+ where the basis function $$b_2(t)$$ is
485+
486+ ```math
487+ b_2(t)=
488+ \b egin{cases}
489+ \d frac{(t+\t frac32)^2}{2}, & -\t frac32\l e t\l e -\t frac12,\\ [6pt]
490+ -t^{2}+\d frac34, & -\t frac12\l e t\l e \t frac12,\\ [6pt]
491+ \d frac{(t-\t frac32)^2}{2}, & \t frac12\l e t\l e \t frac32,\\ [6pt]
492+ 0, & |t|\g e \t frac32.
493+ \e nd{cases}
494+ ```
495+
496+ "
497+
498+ # ╔═╡ 9a170bfa-5ce5-46a5-9024-16394f51289b
499+ begin
500+ question_box (md """
501+
502+ Find the corresponding coefficients $$\a lpha_k$$ so that:
503+
504+ ```math
505+ f(0)=-1,\q uad f(1)=-1,\q uad f(2)=2,\q uad f(3)=5,\q uad 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)=\s um_{n=0}^{N-1}\a lpha_n\, b_2(t-n)
537+ ```
538+ be a superposition of $$N$$ quadratic B--splines.
539+
540+ Describe how to construct the $$N\t imes N$$ matrix $$A$$ that maps the coefficient vector $$\b oldsymbol{\a lpha}$$ to the sample vector
541+
542+ ```math
543+ \b egin{bmatrix}
544+ f(0)\\
545+ f(1)\\
546+ \v dots\\
547+ f(N-1)
548+ \e nd{bmatrix}
549+ =
550+ A
551+ \b egin{bmatrix}
552+ \a lpha_0\\
553+ \a lpha_1\\
554+ \v dots\\
555+ \a lpha_{N-1}
556+ \e nd{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\m athbf{x}=0$$ only when $$\m athbf{x}=0$$.
563+ Write $$A$$ as $$I+G$$ for a matrix $$G$$ you can evaluate, and show
564+
565+ ```math
566+ \| A\m athbf{x}\| _2=\|\m athbf{x}+G\m athbf{x}\| _2\g e \|\m athbf{x}\| _2-\| G\m athbf{x}\| _2.
567+ ```
568+
569+ Demonstrate that $$|G\m athbf{x}|_2<|\m athbf{x}|_2$$ by proving $$G\m athbf{x}$$ is the sum of two vectors whose norms are each strictly less than $$|\m athbf{x}|_2/2$$.
570+ """ )
571+ )
572+
288573# ╔═╡ 4634c856-9553-11ea-008d-3539195970ea
289574md " ## 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