Tutorial 3 -  Linear Algebra 1

generic ECE - U. Houston

                                                                                      Han Q. Le (c) copyrighted

page 1              page 2               page 3



2.4 Use App to review matrix dot product

Mathematically, we have two common cases of matrix - vector dot product:

       m × n matrix          n-vector                   m-vector
Tutorial_3_answer_82.png

and this:

       m-vector                   m × n matrix                          n-vector                
Tutorial_3_answer_83.png

Explore the app below:

Out[1]

Consider the vector dot product:

                                vectdot.gif

Next consider the dot product of a vector (which is 1D matrix) with a matrix:

We can also have a "row" vector in front of a matrix:

And finally. we have the general matrix dot product:


By theorem:     Tutorial_3_answer_85.png

Hence, we can use transpose whenever convenient.

2.5 Apply learning above with portfolio_data.csv

Exercise 2.4

Write a block code to do similar things as the example with grocery data file for portfolio_data.csv.
Define variables: dateList, stocksymb, shares, pricedata, with appropriate values.
(follow instructions in class if needed).

In[95]:=

Tutorial_3_answer_86.png

Out[95]=

Tutorial_3_answer_87.png

Answer

We can use exactly the same code for the grocery shopping problem  to get answer. Except the variable names are changed or generic (just to avoid confusion)

Also, we use DateListPlot instead of barchart.

In[127]:=

Tutorial_3_answer_88.gif

Out[133]=

Tutorial_3_answer_89.gif

However, if we want to use the same code over and over, we can define a block code.

In[142]:=

Tutorial_3_answer_90.png

Then, we CAN name variables from the generic data according our interest:

In[143]:=

Tutorial_3_answer_91.gif

We can find portfolio value at just one time point, say at:

In[145]:=

Tutorial_3_answer_92.gif

Out[145]=

Tutorial_3_answer_93.png

Out[146]=

Tutorial_3_answer_94.png

End of exercise

We can do some more for fun:

In[147]:=

Tutorial_3_answer_95.gif

Out[149]=

Tutorial_3_answer_96.gif

Exercise 2.5

Find the portfolio values (name it value) as a function of time (hint: matrix vector product).

Answer

Should we do this?

In[29]:=

Tutorial_3_answer_97.png

Out[29]=

Tutorial_3_answer_98.png

The answer is no. There is a much more elegant, mathematically correct, and code-wise efficient way to do, it is matrix vector product:

Tutorial_3_answer_99.png

Out[103]=

Tutorial_3_answer_100.png

Exercise 2.6

Use DateListPlot to plot portfolio value vs time. You need to pair date (time) with its corresponding portfolio value.

Answer

In[104]:=

Tutorial_3_answer_101.png

Out[104]=

Tutorial_3_answer_102.gif

Below shows some styling approach:

In[134]:=

Tutorial_3_answer_103.png

Out[134]=

Tutorial_3_answer_104.gif

Mathematically, this is what it looks like:

In[135]:=

Tutorial_3_answer_105.gif

This is known as matrix “dot” vector product. In this case, the vector is a column. Each element of a row of the matrix is multiplied with the same index element of the column, and all are summed together to give an element of the output, which is the value of the portfolio.

                         

For display convenience, we have transposed the price matrix so that each row corresponds to a time point, and the column are the prices of the stocks in green above.



Back to:  page 1   Next to:    page 3

Created with the Wolfram Language