# Performing matrix multiplications with a list

I have a question in regards to PseudoInverse. I have $A$, an $n\times 2$ matrix, and when I want to compute $(A^T A)^{-1}A$, by using PseudoInverse, Mathematica is able to give me an answer for this. However If I want to compute $(A^T A)^{-1}$, I am not able to.

Here are the data:

goog = FinancialData["goog", {2012}]; yahoo = FinancialData["yhoo", {2012}];
A = Transpose[{yahoo[[1 ;;, 2]], constant}] (*Where constant is a list of value 1(Intercept)*)
kik = PseudoInverse[A]\[Transpose]*goog[[1 ;;, 2]]
Total[kik[[1 ;;, 1]]] (*gives me the coefficient*)
and Total[kik[[1 ;;, 2]]] (*gives me the intercept.*)


Now I often get mathematical equations which is written as $c^Tc$, lets say that $c$ is a vector list, but Mathematica always gives an error.

where c=RandomReal[100, 100]

And if we look at the data given here as x, if I want to compute $(xx^T)^{-1}$, how is this done?

I am struggling with the understanding of matrices and I therefore need a good understanding of how Mathematica deals with these situations, I am also very sure that this would benefit other Mathematica users, based on the fact that the other students taking the same class as I, are struggling with exactly the same thing in Mathematica.

• what are c and A in your data above? Also, don't include text in the code (and). The answer to your question is that {1,2,3} is different from {{1,2,3}} and {{1},{2},{3}}. The last two can be transposed, but not the first. I'm guessing your c and x are like the first one. If you include your code for c and A, I'll be able to write an answer (I just want to exclude the possibility that you're having some other errors). Also, include the errors in your questio. – rm -rf Dec 10 '13 at 13:41
• I guess you want to use Dot(.), not Times(*), to perform matrix multiplication. – Silvia Dec 10 '13 at 13:49
• I have now updated it, what was called x previously is now changed to A. – ALEXANDER Dec 11 '13 at 1:58
• Alexander -- please place code that works. When I copy/paste your code above, it does not function (for instance, constant is undefined). Most likely you are making a simple syntax error in your attempts to do the computations, but we can't tell because you haven't showed the commands you tried and the code does not run. – bill s Dec 11 '13 at 5:00

## Lists and Matrices in Mathematica

I am not sure if I understand you question right, but I will try to explain general things regarding Mathematica and matrices/vectors/lists. I will start with what you suggested and will try to explain how to fix it (basically rm -rf and Silvia pointed you in that direction, maybe I can help further...)

If you want to multiply two matrices or scalar multiply two vectors you can do this using a dot . like this:

A.B


Lets say we have the two vector $c$ :

c=RandomReal[1, 10]


Lets take a look at the dimensions of those objects:

Dimensions[c]


just returns a {10} - a vector with 10 components to Mathematica. So Mathematica will use the scalar product when multiplying two of them with the dot, and for example wont transpose it properly because it is a vector/list and not a matrix.

You can "tell" Mathematica that it's a $10\times 1$ matrix, that need to be transposed multiplied in a "matrix way", by adding wiggly parenthesis around it.

Dimensions[{c}]


leads to an output of: {1,10}, that means it is a $1\times 10$ matrix to Mathematica so that could be in mathematical notation: $c^T$. To get the $10\times 1$ matrix $c$ you need to write

Transpose[{c}]


Easier would be to take care of that when you generate the list:

c=RandomReal[1,{10,1}]


Now Dimensions[c] leads to the wanted output {10,1}, so it is a $10\times 1$ matrix and transposing and multiplying works just like the common mathematical notation, e.g., $c^T c$ is Transpose[c].c.

## General Problems with inverting and Transposing

Since it is a little long for the comments here a my thoughts about your first problem, since you mentioned your trouble with matrices.

Lets say you have a $n\times m$ matrix $A$ and a $k\times l$ matrix $B$.

• You can only multiply those two in a matrix multiplication way, when the dimensions are right. Lets say we want to calculate $A B$ this can only be done if $m=k$ and the result will be a $n \times l$ matrix. If you want to calculate $B A$ this will only work if $l=n$ and the result is a $k\times m$ matrix.

• If you transpose a matrix the rows and columns are flipped over, so $A^T$ is a $m\times n$ matrix.

• If you want to invert a matrix, you are looking for a matrix $A^{-1}$ that fulfills the equation $A^{-1}A= I$ (where $I$ is the identity matrix). A unique inverse matrix can only be found, if $A$ is a square matrix, so if $n=m$. If that is not the case you might be able find a pseudo inverse that fulfills $A^{-1}A= I$ (the left inverse) or $A A^{-1}= I$ (the right inverse) depending on $n$ and $m$.

If you write something like $(A^T A^{-1}) A$, lets see what we need just in order to be able to calculate the products. $A$ is a $n\times m$ matrix, so $A^T$ is a $m\times n$ matrix and thus $A^{-1}$ needs to be a $n\times n$ matrix just in order to calculate those products. So if $n\neq m$ you run into problems, but maybe that was a typo and you meant $(A^T A)^{-1}$ can be computed and with $(A^T A^{-1}) A$ you have trouble.

• Thank you this is great! The I have tried what you have shown me and it makes sense, however would you be able to show me have to solve this stats.stackexchange.com/questions/5351/…. Using mathematica? In a chat ore something similar? I want the HAC standard errors and I have literally spent a week trying, and I mean several hours each day without being able to get it. – ALEXANDER Dec 12 '13 at 3:01
• In principle I would be fine with it, I just think that we might be in different time zones (so chat would be problematic, besides the usual math typing trouble) and I would have to read about the HAC standard errors before I can give understandable help. – jens_bo Dec 12 '13 at 10:36