-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path05-VectorCalculus.Rmd
More file actions
1209 lines (833 loc) · 30 KB
/
05-VectorCalculus.Rmd
File metadata and controls
1209 lines (833 loc) · 30 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Vector Calculus
Vector calculus plays a central role in machine learning, as many algorithms involve optimizing an objective function with respect to model parameters that determine how well the model explains the data. These optimization problems often rely on **gradient-based methods** to find the best parameters.
Examples include:
- **Linear regression** — optimizing linear weights to maximize likelihood (Chapter 9).
- **Neural network autoencoders** — minimizing reconstruction error using the chain rule for derivatives.
- **Gaussian mixture models** — optimizing location and shape parameters to best fit data distributions (Chapter 11).
<p align="center">
<img src="Figure5.2MML.png" alt="A mind map of the concepts introduced in this chapter, along with when they are used in other parts of the book." width="400">
</p>
The core concept in this chapter is that of a **function**, which maps inputs \( \mathbf{x} \in \mathbb{R}^D \) to real-valued outputs \( f(\mathbf{x}) \in \mathbb{R} \).
A function assigns each input exactly one output:
\[
f : \mathbb{R}^D \to \mathbb{R}, \quad \mathbf{x} \mapsto f(\mathbf{x})
\]
Gradients are essential because they point in the **direction of steepest ascent**, which allows optimization algorithms to improve model performance efficiently. Thus, **vector calculus** provides a fundamental mathematical foundation for many learning and optimization techniques used in machine learning.
Most functions in this context are assumed to be **differentiable**, though extensions to sub-differentials can handle non-differentiable cases. Optimization under constraints is discussed further in **Chapter 7**.
## Differentiation of Univariate Functions
Differentiation of univariate functions is fundamental in understanding how functions change with respect to their input. A **derivative** measures the instantaneous rate of change or slope of the tangent line to a function at a point.
One can compute the slope of the secant line through two points on \( f(x) \) using the difference quotient.
<div class="definition">
**Difference Quotient**
\[
\frac{\delta y}{\delta x} = \frac{f(x + \delta x) - f(x)}{\delta x}
\]
</div>
As \( \delta x \to 0 \), the secant approaches the tangent line. We call the value of the difference quotient as $\delta x \rightarrow 0$ the derivative and use the notation $\frac{df}{dx}$ to denote the derivative.
<div class="definition">
**Derivative**
\[
\frac{df}{dx} = \lim_{h \to 0} \frac{f(x + h) - f(x)}{h}
\]
</div>
The derivative represents the slope of the tangent to \( f \) at point \( x \). It points in the **direction of steepest ascent** of \( f \).
<div class="example">
For \( f(x) = x^n \):
\[
\frac{df}{dx} = \lim_{h \to 0} \frac{(x + h)^n - x^n}{h} = nx^{n-1}
\]
</div>
---
### Taylor Series and Polynomial Approximation
<div class="definition">
A **Taylor series** approximates a function \( f(x) \) around a point \( x_0 \):
\[
T_\infty(x) = \sum_{k=0}^{\infty} \frac{f^{(k)}(x_0)}{k!}(x - x_0)^k
\]
</div>
If \( x_0 = 0 \), the Taylor series is known as the **Maclaurin series**. If \( f(x) = T_\infty(x) \), then \( f \) is **analytic**.
<div class="example">
Taylor Polynomial for \( f(x) = x^4 \)
At \( x_0 = 1 \):
\[
T_6(x) = 1 + 4(x - 1) + 6(x - 1)^2 + 4(x - 1)^3 + (x - 1)^4
\]
Expanding gives \( T_6(x) = x^4 = f(x) \).
</div>
<div class="example">
Taylor Series of \( f(x) = \sin(x) + \cos(x) \)
At \( x_0 = 0 \):
\[
T_\infty(x) = \cos(x) + \sin(x)
\]
Derived using power series:
\[
\cos(x) = \sum_{k=0}^{\infty} \frac{(-1)^k x^{2k}}{(2k)!}, \quad
\sin(x) = \sum_{k=0}^{\infty} \frac{(-1)^k x^{2k+1}}{(2k+1)!}
\]
</div>
---
### Differentiation Rules
<div class="theorem">
Let $f$ and $g$ be continuous functions. Then:
1. **Product Rule**
\[
(f(x)g(x))' = f'(x)g(x) + f(x)g'(x)
\]
2. **Quotient Rule**
\[
\left( \frac{f(x)}{g(x)} \right)' = \frac{f'(x)g(x) - f(x)g'(x)}{[g(x)]^2}
\]
3. **Sum Rule**
\[
(f(x) + g(x))' = f'(x) + g'(x)
\]
4. **Chain Rule**
For \( h(x) = g(f(x)) \):
\[
h'(x) = g'(f(x)) \cdot f'(x)
\]
</div>
<div class="example">
Given \( f(x) = 2x + 1 \) and \( g(x) = x^4 \), if $h(x) = g(f(x)) = (2x+1)^4$, then
\[
h'(x) = g'(f) \cdot f'(x) = 4(2x + 1)^3 \cdot 2 = 8(2x + 1)^3
\]
</div>
---
### Exercises {.unnumbered .unlisted}
<div class="exercise">
Differentiate $f(x) = e^x \sin(x) + \cos(\ln(x))$.
<div style="text-align: right;">
[Solution]( )
</div>
</div>
<div class="exercise">
Differentiate $f(x) = \dfrac{\ln(x)}{x^2 + x + 5}$.
<div style="text-align: right;">
[Solution]( )
</div>
</div>
<div class="exercise">
Differentiate $f(x) = \frac{\sin(x)\cos(x)}{x^2e^x}$. Hint, take logs on both sides.
<div style="text-align: right;">
[Solution]( )
</div>
</div>
<div class="exercise">
Differentiate $g(x) = e^{\sin(x)e^x}$. Hint, take logs on both sides.
<div style="text-align: right;">
[Solution]( )
</div>
</div>
<div class="exercise">
Find the Taylor series for $f(x) = x^4$ around $x_0 = 1$.
<div style="text-align: right;">
[Solution]( )
</div>
</div>
<div class="exercise">
Find the Taylor series for $f(x) = \cos(x)$ around $x_0 = 0$.
<div style="text-align: right;">
[Solution]( )
</div>
</div>
<div class="exercise">
Find the Taylor series for $f(x) = e^x$ around $x_0 = 0$.
<div style="text-align: right;">
[Solution]( )
</div>
</div>
## Partial Differentiation and Gradients
Partial derivatives extends differentiation to functions of multiple variables \( f(x_1, x_2, \ldots, x_n) = f(\mathbf{x})\). The **gradient** generalizes the concept of the derivative for multivariate functions. Each component of the gradient is a **partial derivative**, found by differentiating with respect to one variable while keeping others constant.
<div class="definition">
For a function \( f : \mathbb{R}^n \to \mathbb{R} \), the **partial derivative** with respect to each variable \( x_i \) is:
\[
\frac{\partial f}{\partial x_i} = \lim_{h \to 0} \frac{f(x_1, \ldots, x_i + h, \ldots, x_n) - f(x)}{h}
\]
</div>
Collecting all partial derivatives gives the **gradient (Jacobian)**:
<div class="definition">
The **Gradient** of a function of $n$ variables is the vector of first partial derivatives with respect to each variable.
\[
\nabla_x f = \text{grad} f = \frac{df}{d\mathbf{x}} =
\begin{bmatrix}
\frac{\partial f}{\partial x_1} &
\frac{\partial f}{\partial x_2} &
\cdots &
\frac{\partial f}{\partial x_n}
\end{bmatrix}
\in \mathbb{R}^{1 \times n}
\]
</div>
The gradient is defined here as a **row vector** for consistency with later generalizations.
<div class="example">
Given \( f(x, y) = (x + 2y^3)^2 \), we have
\[
\frac{\partial f}{\partial x} = 2(x + 2y^3)
\]
and
\[
\frac{\partial f}{\partial y} = 2(x + 2y^3) \cdot 6y^2 = 12y^2(x + 2y^3).
\]
So, the gradient for $f$ is given by
\[
\nabla_x f = \text{grad} f = \frac{df}{d\mathbf{x}} =\left[2(x + 2y^3), \;\; 12y^2(x + 2y^3) \right].\]
</div>
Although gradients are often represented as column vectors, this text defines them as row vectors. By doing so, we have the following advantages:
1. Allows consistent generalization to **vector-valued functions** \( f: \mathbb{R}^n \to \mathbb{R}^m \).
2. Enables a compact **matrix form of the multivariate chain rule**.
<div class="example">
For \( f(x_1, x_2) = x_1^2x_2 + x_1x_2^3 \):
\[
\frac{\partial f}{\partial x_1} = 2x_1x_2 + x_2^3
\]
\[
\frac{\partial f}{\partial x_2} = x_1^2 + 3x_1x_2^2
\]
Hence, the gradient is:
\[
\frac{df}{d\mathbf{x} } =
\begin{bmatrix}
2x_1x_2 + x_2^3 & x_1^2 + 3x_1x_2^2
\end{bmatrix}
\in \mathbb{R}^{1 \times 2}
\]
</div>
---
### Basic Rules of Partial Differentiation
The basic differentiation rules extend naturally to multivariate functions.
<div class="theorem">
Let $f$ and $g$ be continuous functions.
- **Product Rule:**
\[
(f(\mathbf{x})g(\mathbf{x}))' = f'(\mathbf{x})g(\mathbf{x}) + f(\mathbf{x})g'(\mathbf{x})
\]
- **Sum Rule:**
\[
(f(\mathbf{x}) + g(\mathbf{x}))' = f'(\mathbf{x}) + g'(\mathbf{x})
\]
- **Chain Rule:**
\[
(g \circ f)'(\mathbf{x}) = g'(f(\mathbf{x})) f'(\mathbf{x})
\]
</div>
---
### Multivariate Chain Rule (Matrix Form)
<div class="definition">
For functions of several variables, the **chain rule** can be written compactly as a matrix multiplication:
\[
\frac{df}{d(s, t)} =
\frac{\partial f}{\partial x}
\frac{\partial x}{\partial (s, t)}
\]
\[
=
\begin{bmatrix}
\frac{\partial f}{\partial x_1} & \frac{\partial f}{\partial x_2}
\end{bmatrix}
\begin{bmatrix}
\frac{\partial x_1}{\partial s} & \frac{\partial x_1}{\partial t} \\
\frac{\partial x_2}{\partial s} & \frac{\partial x_2}{\partial t}
\end{bmatrix}
\]
</div>
This form only works directly if the gradient is defined as a row vector.
<div class="example">
Let
\[
f(x, y) = x^2 y + \sin(y)
\]
where \(x\) and \(y\) are functions of a single variable \(t\) given by
\[
x(t) = t^2, \qquad y(t) = e^{t}.
\]
Compute
\[
\frac{d}{dt} \, f(x(t), y(t)).
\]
By the multivariable chain rule,
\[
\frac{d}{dt} f(x(t), y(t))
= \frac{\partial f}{\partial x}\frac{dx}{dt}
+ \frac{\partial f}{\partial y}\frac{dy}{dt}.
\]
First compute the partial derivatives:
\[
\frac{\partial f}{\partial x} = 2xy, \qquad
\frac{\partial f}{\partial y} = x^2 + \cos(y).
\]
Next compute the derivatives of \(x(t)\) and \(y(t)\):
\[
\frac{dx}{dt} = 2t, \qquad
\frac{dy}{dt} = e^{t}.
\]
Substitute \(x(t) = t^2\) and \(y(t) = e^t\):
\[
\frac{d}{dt} f(x(t), y(t))
= (2 t^2 e^t)(2t) + (t^4 + \cos(e^t)) e^t= 4 t^3 e^t + t^4 e^t + e^t \cos(e^t)
\]
</div>
---
### Exercises {.unnumbered .unlisted}
<div class="exercise">
Find the gradient of $f\left( {x,y,z} \right) = {x^2}z + {y^3}{z^2} - xyz$.
<div style="text-align: right;">
[Solution]( )
</div>
</div>
<div class="exercise">
Find the gradient of $f(x,y) = xe^{xy}$.
<div style="text-align: right;">
[Solution]( )
</div>
</div>
<div class="exercise">
Find the gradient of $f\left( {x,y} \right) = x\cos \left( y \right)$.
<div style="text-align: right;">
[Solution]( )
</div>
</div>
<div class="exercise">
Find the gradient of $f\left( {x,y,z} \right) = \sin \left( {yz} \right) + \ln \left( {{x^2}} \right)$.
<div style="text-align: right;">
[Solution]( )
</div>
</div>
<div class="exercise">
The rate of change of $f(x,y,z)$ in the direction of a unit vector $\mathbf{u}$ is called the directional derivative and is denoted by $D_{\mathbf{u}} f(x,y,z)$. We can show that \[D_u f(x,y,z) = \langle f_x, f_y, f_z \rangle \cdot \langle a,b,c \rangle,\] with $\mathbf{u} = <a,b,c>$. Prove that the maximum value of $D_{\mathbf{u}} f(x,y,z)$ (and hence the maximum rate of change of $f(x,y,z)$) is given by $||\nabla f(x,y,z)||$ will occur in the direction given by $\nabla f(x,y,z)$.
<div style="text-align: right;">
[Solution]( )
</div>
</div>
<div class="exercise">
Prove that the gradient vector $\nabla f(x_0,y_0)$ is orthogonal to the level curve $f(x,y) = k$ at the point $(x(t_0),y(t_0)) = (x_0,y_0)$.
<div style="text-align: right;">
[Solution]( )
</div>
</div>
<div class="exercise">
Find $\frac{dz}{dt}$ for \[z = xe^{xy}, \;\;\;\; x = t^2, \;\;\;\; y = t^{-1}.\]
<div style="text-align: right;">
[Solution]( )
</div>
</div>
<div class="exercise">
Find $\frac{dz}{dt}$ for \[z = {x^2}{y^3} + y\cos x, \;\;\;\; x = \ln(t^2), \;\;\;\; y = \sin(4t).\]
<div style="text-align: right;">
[Solution]( )
</div>
</div>
<div class="exercise">
Find $\frac{dz}{dx}$ for \[z = x\ln \left( {xy} \right) + {y^3}, \;\;\;\; y=\cos(x^2+1).\]
<div style="text-align: right;">
[Solution]( )
</div>
</div>
## Gradients of Vector-Valued Functions
In previous sections, we studied gradients of scalar functions \( f: \mathbb{R}^n \to \mathbb{R} \). We now generalize to **vector-valued functions** \( \mathbf{f}: \mathbb{R}^n \to \mathbb{R}^m \), where \( m > 1 \).
<div class="definition">
A **vector valued function** $\mathbf{f}:\mathbb{R}^n \rightarrow \mathbb{R}^m$ can be written as:
\[
\mathbf{f}(\mathbf{x}) =
\begin{bmatrix}
f_1(\mathbf{x}) \\ \vdots \\ f_m(\mathbf{x})
\end{bmatrix}
\in \mathbb{R}^m
\]
where each \( f_i: \mathbb{R}^n \to \mathbb{R} \).
</div>
<div class="example">
Define the function
\[
\mathbf{r}(t) =
\begin{bmatrix}
\cos t \\
\sin t
\end{bmatrix},
\quad t \in \mathbb{R}.
\]
Each input \(t\) produces a 2D vector. This function traces out the unit circle in \(\mathbb{R}^2\).
</div>
<div class="example">
Define
\[
\mathbf{r}(t) =
\begin{bmatrix}
t \\
t^2 \\
e^t
\end{bmatrix}.
\] The input is a scalar \(t\). The output is a vector in \(\mathbb{R}^3\). Each component is an ordinary real-valued function.
</div>
<div class="definition">
- The **Jacobian** collects all first-order partial derivatives of \( \mathbf{f} \):
\[
\mathbf{J} = \nabla_x \mathbf{f} = \frac{d\mathbf{f}(\mathbf{x})}{d\mathbf{x}} =
\begin{bmatrix}
\frac{\partial f_1}{\partial x_1} & \cdots & \frac{\partial f_1}{\partial x_n} \\
\vdots & \ddots & \vdots \\
\frac{\partial f_m}{\partial x_1} & \cdots & \frac{\partial f_m}{\partial x_n}
\end{bmatrix}
\in \mathbb{R}^{m \times n}
\]
</div>
Each element \( \mathbf{J}(i, j) = \frac{\partial f_i}{\partial x_j} \) gives the rate of change of the \( i^{th} \) output with respect to the \( j^{th} \) input.
<div class="example">
Consider the vector-valued function
\[
\mathbf{f} : \mathbb{R}^2 \to \mathbb{R}^3
\]
defined by
\[
\mathbf{f}(x,y) =
\begin{bmatrix}
x^2 + y \\
xy \\
\sin x
\end{bmatrix}.
\]
The partial derivatives are:
- \( f_1(x,y) = x^2 + y \)
\[
\frac{\partial f_1}{\partial x} = 2x,
\quad
\frac{\partial f_1}{\partial y} = 1
\]
- \( f_2(x,y) = xy \)
\[
\frac{\partial f_2}{\partial x} = y,
\quad
\frac{\partial f_2}{\partial y} = x
\]
- \( f_3(x,y) = \sin x \)
\[
\frac{\partial f_3}{\partial x} = \cos x,
\quad
\frac{\partial f_3}{\partial y} = 0
\]
Thus, the Jacobian for $\mathbf{f}$ is
\[
\mathbf{J}_{\mathbf{f}}(x,y) =
\begin{bmatrix}
2x & 1 \\
y & x \\
\cos x & 0
\end{bmatrix}.
\]
</div>
<div class="note">
This book adopts the **numerator layout**:
The derivative \( \frac{d\mathbf{f}}{d\mathbf{x}} \) is an \( m \times n \) matrix — rows correspond to function outputs, columns to input variables.
</div>
The **Jacobian determinant** \( |\det(\mathbf{J})| \) represents how a transformation scales areas or volumes. For example, a mapping with \( |\det(\mathbf{J})| = 3 \) triples the area (or volume) of a region. This property becomes important in probability (e.g., change of variables in Section 6.7).
For **linear mappings**, such as \( y = \mathbf{\mathbf{J}}\mathbf{x} \), the Jacobian is simply the transformation matrix \( \mathbf{J} \). For **nonlinear mappings**, the Jacobian provides a **local linear approximation** around a point.
<div class="example">
Given \(\mathbf{f}(\mathbf{x}) = \mathbf{A}\mathbf{x} \), where \( \mathbf{A} \in \mathbb{R}^{M \times N} \) and \( \mathbf{x} \in \mathbb{R}^N \):
\[
\frac{d\mathbf{f}}{d\mathbf{x}} = \mathbf{A}
\]
since each element \( \frac{\partial f_i}{\partial x_j} = A_{ij} \).
</div>
<div class="definition">
For compositions \( h(t) = f(\mathbf{g}(t)) \) with \( f: \mathbb{R}^2 \to \mathbb{R} \) and \( \mathbf{g}: \mathbb{R} \to \mathbb{R}^2 \),
the chain rule generalizes to:
\[
\frac{dh}{dt} = \frac{\partial f}{\partial \mathbf{g}} \frac{\partial \mathbf{g}}{\partial t}
\]
where matrix multiplication replaces scalar multiplication in the standard chain rule.
</div>
<div class="example">
Let
\[
\mathbf{g} : \mathbb{R} \to \mathbb{R}^2
\quad \text{and} \quad
f : \mathbb{R}^2 \to \mathbb{R}
\]
be defined by
\[
\mathbf{g}(t) =
\begin{bmatrix}
t^2 \\
\sin t
\end{bmatrix},
\qquad
f(x,y) = x y.
\]
Define the composition
\[
h(t) = f(\mathbf{g}(t)).
\]
**Step 1: Write the Composition Explicitly**
Substitute \( \mathbf{g}(t) \) into \( f \):
\[
h(t) = f(t^2, \sin t) = t^2 \sin t.
\]
**Step 2: Apply the Chain Rule**
The multivariable chain rule states:
\[
h'(t) = \nabla f(\mathbf{g}(t)) \cdot \mathbf{g}'(t).
\]
**Step 3: Compute Each Component**
- Gradient of \( f \):
\[
\nabla f(x,y) =
\begin{bmatrix}
\frac{\partial f}{\partial x} \\
\frac{\partial f}{\partial y}
\end{bmatrix}
=
\begin{bmatrix}
y \\
x
\end{bmatrix}.
\]
Evaluate at \( \mathbf{g}(t) = (t^2, \sin t) \):
\[
\nabla f(\mathbf{g}(t)) =
\begin{bmatrix}
\sin t \\
t^2
\end{bmatrix}.
\]
- Derivative of \( \mathbf{g}(t) \):
\[
\mathbf{g}'(t) =
\begin{bmatrix}
2t \\
\cos t
\end{bmatrix}.
\]
**Step 4: Compute \( h'(t) \)**
\[
h'(t) = \nabla f(\mathbf{g}(t)) \cdot \mathbf{g}'(t)
=
\begin{bmatrix}
\sin t & t^2
\end{bmatrix}
\begin{bmatrix}
2t \\
\cos t
\end{bmatrix}
=
2t \sin t + t^2 \cos t.
\]
To Check, we can differentiate directly:
\[
h(t) = t^2 \sin t
\quad \Rightarrow \quad
h'(t) = 2t \sin t + t^2 \cos t,
\]
which agrees with the chain rule result.
</div>
<div class="example">
For a linear model \( \mathbf{y} = \mathbf{\Phi} \mathbf{\theta} \) with residuals \( \mathbf{e}(\mathbf{\theta}) = \mathbf{y} - \mathbf{\Phi} \mathbf{\theta} \),
the least-squares loss is:
\[
L( \mathbf{e} ) = \| \mathbf{e} \|^2 = \mathbf{e}^\top \mathbf{e}
\]
Applying the chain rule gives:
\begin{align*}
\frac{\partial L}{\partial \mathbf{\theta}}
&= \frac{\partial L}{\partial \mathbf{e}}\frac{\partial \mathbf{e}}{\partial \mathbf{\theta}} \\
&= -2 \mathbf{e}^\top \mathbf{\Phi}\\
&= -2 (\mathbf{y}^\top - \mathbf{\theta}^\top \mathbf{\Phi}^\top) \mathbf{\Phi}.
\end{align*}
This result forms the basis for optimization in linear regression (explored further in Chapter 9).
</div>
---
### Dimensional Summary of Derivatives
| Function Type | Gradient Dimension |
|----------------------|---------------------|
| \( f: \mathbb{R} \to \mathbb{R} \) | Scalar (1 × 1) |
| \( f: \mathbb{R}^D \to \mathbb{R} \) | Row vector (1 × D) |
| \( f: \mathbb{R} \to \mathbb{R}^E \) | Column vector (E × 1) |
| \( f: \mathbb{R}^D \to \mathbb{R}^E \) | Matrix (E × D) |
---
### Exercises {.unnumbered .unlisted}
<div class="exercise">
Compute the Jacobian for $x = 4u-3v^2$ and $y = u^2-6v$.
<div style="text-align: right;">
[Solution]( )
</div>
</div>
<div class="exercise">
Compute the Jacobian for $x = \sqrt{u}$ and $y = 10u + v$.
<div style="text-align: right;">
[Solution]( )
</div>
</div>
<div class="exercise">
Compute the Jacobian for $x = v^3u$ and $y = u^2/v$.
<div style="text-align: right;">
[Solution]( )
</div>
</div>
<div class="exercise">
Compute the Jacobian for $x = u^2v^3$ and $y = 4-2\sqrt{u}$.
<div style="text-align: right;">
[Solution]( )
</div>
</div>
<div class="exercise">
Define a matrix $\mathbf{A}$ to be $3 \times 3$ and a vector $\mathbf{x}$ to be length 3. Define $\mathbf{f}(\mathbf{x}) = \mathbf{A} \mathbf{x}$. Compute $d \mathbf{f}/d \mathbf{x}$.
<div style="text-align: right;">
[Solution]( )
</div>
</div>
<div class="exercise">
Let $r(t) = \left[t^2 + 1, 3-t, t^3 \right]$. Find the unit tangent vector in the direction of $r$.
<div style="text-align: right;">
[Solution]( )
</div>
</div>
<div class="exercise">
Prove each of the following rules
\begin{array}{lrcll} \mathrm{i.} & \dfrac{d}{\,dt}[c\mathbf{r}(t)] & = & c\mathbf{r}′(t) & \text{Scalar multiple} \nonumber\\
\mathrm{ii.} & \dfrac{d}{\,dt}[\mathbf{r}(t)±\mathbf{u}(t)] & = & \mathbf{r}′(t)±\mathbf{u}′(t) & \text{Sum and difference} \nonumber\\
\mathrm{iii.} & \dfrac{d}{\,dt}[f(t)\mathbf{u}(t)] & = & f′(t)\mathbf{u}(t)+f(t)\mathbf{u}′(t) & \text{Scalar product} \nonumber\\
\mathrm{iv.} & \dfrac{d}{\,dt}[\mathbf{r}(t)⋅\mathbf{u}(t)] & = & \mathbf{r}′(t)⋅\mathbf{u}(t)+\mathbf{r}(t)⋅\mathbf{u}′(t) & \text{Dot product} \nonumber\\
\mathrm{v.} & \dfrac{d}{\,dt}[\mathbf{r}(f(t))] & = & \mathbf{r}′(f(t))⋅f′(t) & \text{Chain rule} \nonumber\\
\mathrm{vi.} & \text{If} \; \mathbf{r}(t)·\mathbf{r}(t) & = & c, \text{then} \; \mathbf{r}(t)⋅\mathbf{r}′(t) \; =0 \; . & \mathrm{} \nonumber \end{array} \nonumber
<div style="text-align: right;">
[Solution]( )
</div>
</div>
## Gradients of Matrices
In many machine learning applications, we need to compute **gradients of matrices** with respect to vectors or other matrices. The result is typically a **multidimensional tensor**, which can be viewed as an array of partial derivatives.
---
### Gradients as Tensors
<div class="definition">
If \( \mathbf{A} \in \mathbb{R}^{m \times n} \) and \( \mathbf{B} \in \mathbb{R}^{p \times q} \), then the Jacobian of \( \mathbf{A} \) with respect to \( \mathbf{B} \) is a tensor of shape:
\[
\mathbf{J} \in \mathbb{R}^{(m \times n) \times (p \times q)}
\]
with entries:
\[
\mathbf{J}_{ijkl} = \frac{\partial \mathbf{A}_{ij}}{\partial \mathbf{B}_{kl}}
\]
</div>
Because matrices represent linear mappings, there exists a vector-space isomorphism between \( \mathbb{R}^{m \times n} \) and \( \mathbb{R}^{mn} \). This means matrices can be **flattened** (by stacking columns) into vectors, allowing gradients to be represented as **matrices** instead of tensors.
Working with flattened matrices simplifies computation: the chain rule becomes standard matrix multiplication, whereas tensor gradients require summing across multiple dimensions.
<div class="example">
Given:
\[
f = \mathbf{A} \mathbf{x}, \quad \mathbf{f} \in \mathbb{R}^M, \; \mathbf{A} \in \mathbb{R}^{M \times N}, \; \mathbf{x} \in \mathbb{R}^N
\]
we seek \( \frac{d\mathbf{f}}{d\mathbf{A}} \).
- The gradient has dimension:
\[
\frac{d \mathbf{f}}{d\mathbf{A}} \in \mathbb{R}^{M \times (M \times N)}
\]
- For each component:
\[
f_i = \sum_{j=1}^{N} \mathbf{A}_{ij} x_j
\Rightarrow
\frac{\partial f_i}{\partial \mathbf{A}_{iq}} = x_q
\]
- The partial derivatives with respect to each row of \( \mathbf{A} \) are:
\[
\frac{\partial f_i}{\partial \mathbf{A}_{i,:}} = x^\top, \quad
\frac{\partial f_i}{\partial \mathbf{A}_{k \neq i,:}} = 0^\top
\]
- Stacking these gives the full gradient tensor:
\[
\frac{\partial f_i}{\partial \mathbf{A}} =
\begin{bmatrix}
0^\top \\ \vdots \\ 0^\top \\ x^\top \\ 0^\top \\ \vdots \\ 0^\top
\end{bmatrix}
\in \mathbb{\mathbf{R}}^{1 \times (M \times N)}
\]
</div>
<div class="example">
Let:
\[
\mathbf{f}(\mathbf{R}) = \mathbf{R}^\top \mathbf{R} = \mathbf{K}, \quad \mathbf{R} \in \mathbb{\mathbf{R}}^{M \times N}, \; \mathbf{K} \in \mathbb{\mathbf{R}}^{N \times N}
\]
We want \( \frac{d\mathbf{K}}{d\mathbf{R}} \).
- The gradient tensor has dimensions:
\[
\frac{d\mathbf{K}}{d\mathbf{R}} \in \mathbb{\mathbf{R}}^{(N \times N) \times (M \times N)}
\]
- Each entry of \( \mathbf{K} \) is defined as:
\[
K_{pq} = r_p^\top r_q = \sum_{m=1}^{M} R_{mp} R_{mq}
\]
- Differentiating gives:
\[
\frac{\partial K_{pq}}{\partial R_{ij}} =
\begin{cases}
R_{iq}, & \text{if } j = p, \; p \neq q \\
R_{ip}, & \text{if } j = q, \; p \neq q \\
2R_{iq}, & \text{if } j = p, \; p = q \\
0, & \text{otherwise}
\end{cases}
\]
- Therefore, every element of the gradient tensor \( \frac{d\mathbf{K}}{d\mathbf{R}} \) is determined by how each matrix element \( R_{ij} \) contributes to every element \( K_{pq} \) of \( \mathbf{K} \).
</div>
---
### Exercises {.unnumbered .unlisted}
<div class="exercise">
The following rules describe matrix derivatives in a general setting. Each rule can be proved using small-dimensional examples (e.g., \(2\times2\) or \(3\times3\) matrices) by differentiating entrywise. Prove each result below. Throughout, let:
- \( \mathbf{A}(t), \mathbf{B}(t) \) be matrix-valued functions of compatible sizes
- \( c \in \mathbb{R} \) be a constant
- \( \mathbf{x}(t) \) be a vector-valued function
1. Entrywise Differentiation: Matrix derivatives are defined componentwise:
\[
\frac{d}{dt}\mathbf{A}(t)
=
\left[ \frac{d}{dt} a_{ij}(t) \right].
\]