Description
1. Exercise 1.1 from FCMA p.35 [0.5 pts]
Figure 1: Reproduction of figure 1.1, Olympic men’s 100m data
By examining Figure 1.1 [from p. 2 of FCMA, reproduced here], estimate (by hand / in your head)
the kind of values we should expect for w0 and w1 (e.g., High? Low? Positive? Negative?).
Solution.
1
NOTE: The following three exercises (2, 3, and 4) review basic linear algebra concepts; we will review these
briefly in Lecture 3.
Notation conventions:
• Script variables, such as xn2 and w1 represent scalar values
• Lowercase bold-face variables, such as xn and w, represent vectors
• Uppercase bold-face variables, such as X, represent n (rows) × m (columns) matrices
• Note that because all indexes in the following are either a value between 0, 1, …, 9, or a scalar,
n, I am representing multiple dimension indexes without a comma, as it is unambiguous; e.g.,
x32 is the element scalar value of X at row 3, column 2. When we have to refer to specific index
values greater than 9, we’ll use commas, such as x32,3 is the scalar value in the 32nd row and 3rd
column.
• ‘>’ in expressions like w> indicates the transpose operator.
2. Exercise 1.3 from FCMA p.35 [1pt]
Show that:
w>X>Xw = w
2
0
X
N
n=1
x
2
n1
!
+ 2w0w1
X
N
n=1
xn1xn2
!
+ w
2
1
X
N
n=1
x
2
n2
!
,
where
w =
”
w0
w1
#
, X =
x11 x12
x21 x22
x31 x32
.
.
.
.
.
.
xN1 xN2
.
(Hint – it’s probably easiest to do the X>X first!)
Solution.
3. Exercise 1.4 from FCMA p.35 [1pt]
Using w and X as defined in the previous exercise, show that (Xw)
> = w>X> by multiplying out
both sides.
Solution.
4. Exercise 1.5 from FCMA p.35 [1pt]
When multiplying a scalar by a vector (or matrix), we multiply each element of the vector (or matrix)
by that scalar. For xn = [xn1, xn2]
>
, t = [t1, …, tN ]
>
, w = [w0, w1]
>
, and
X =
x1
>
x2
>
.
.
.
xN
>
show that
X
n
xntn = X>t
2
and
X
n
xnx
>
n w = X>Xw
Solution.
3
5. Plotting Exercise – three parts
A. [0.5pt] Use the provided python script plotlinear.py and plot three lines (parameters of your
choosing). This requires you have you python environment set up. Place the output graphic
in your pdf submission and provide a descriptive caption that indicates the intercept and slope
values you used to generate the lines. (LATEX users can use the commented code in the HW latex
template for inserting the figure.)
Solution.
B. [0.5pt] Now, try another plot: generate a vector whose entries are the values of sin(x) for x in
the range [0, 10] in steps of 0.01, and plot it. Label the y-axis ‘sin(x)’, the x-axis ’x values’ and
provide a title for the plot, ‘Sine Function for x from 0.0 to 10.0’. Include your plot in the pdf
submission.
Solution.
C. [0.5pt] Finally, put your script code in the pdf file.
For LATEX users: you can use the following python code listing environment. The code currently
listed here is from the plotlinear.py script; replace the code there with the code you wrote for
generating the sin function.
Code Listing 1: Example Code Listing: portion of python plotlinear.py script
import numpy a s np
import m a t pl o tli b . p y pl o t a s p l t
p l t . i o n ( )
## Request u s e r i n p u t
p l t . c l o s e ( ) ;
p l t . f i g u r e ( 1 )
p l t . pl o t ( )
print ( ”\nKeeps p l o t t i n g l i n e s on the c u r r e n t pl o t u n t i l you q ui t ( C t rl −D) o r
e n t e r a non−number” ) ;
while 1 :
i n t e r c e p t = f l o a t ( r aw inpu t ( ” Enter i n t e r c e p t : ” ) )
g r a di e n t = f l o a t ( r aw inpu t ( ” Enter g r a di e n t ( sl o p e ) : ” ) )
p l t . pl o t (x , i n t e r c e p t + g r a di e n t *x )
print ”\ny = ” + s t r ( i n t e r c e p t ) + ” + ” + s t r ( g r a di e n t ) + ” x\n” ;
Solution.
4
6. More Programming in Python Practice
A. [1pt] Write a short script that initializes the random number generator
python: numpy.random.seed(seed=1)
Followed by creating two three-dimensional column vectors using
python: random.rand (used in the context of code to generate the vectors)
Represent the random variables as a and b (be sure you issue the call to set the random seed
immediately before creating these variables). Print them at the terminal and copy-and-paste the
result here. (If using LATEX, use the verbatim environment to display).
Solution.
B. [2pts] Using the values of a and b, compute the following and display the result two ways: (1) copyand-paste the output (from the python interpreter/terminal; again, in LATEX use the verbatim
environment), (2) typeset the output (e.g., using the LATEX math environment).
1. a + b = ?
2. a ◦ b = ? (element-wise multiply; Note: the notation a ◦ b is also known as the Hadamard
product, the entrywise product, or the Schur product.)
3. a
>b = ? (also called the dot-product)
Solution.
Now, set the random seed to 2 and immediately generate a random 3 × 3 matrix X. In your
solution, display the value of X. Using X and the earlier values of a and b, compute the following
in python and typeset the results in two ways, as before.
4. a
>X = ?
5. a
>Xb = ?
6. X−1 = ?
Solution.
5