Python meets linear algebra

Python meets linear algebra

Statistics and linear algebra seem like a pair that should always be married to each other. This is hardly the case however. If you are like me and you fancy remembering the equation for say the correlation between two variables without memorizing the formula, then you should probably pick a linear algebra text by Gilbert Strang or head over to his MIT vidoes on youtube which can be found here. Trust me this will be very illuminating.

I will give some examples in case you are still not convinced. We have two variables

x=[123456]y=[789101112]

Pardon my lack of creativity but these two vectors will do. If you wanted to calculate the correlation between these two vectors(variables), from statistics we would need to do Cov(x,y)σxσy​ . Step in linear algebra and the correlation between two vectors(variables) is basically the angle between the two variables given by cosθ=x.y|x||y . Comparing these two equations it becomes apparent that the covariance of two vectors is their dot product whilst their standard deviation(after centering by taking away the mean of the vectors from each component) is basically the length of the vector. This for me is a pretty tidy way to view statistical objects from a linear algebra perspective as I think it is easier to understand what the dot product and length of two vectors are and then relate it to their statistical equivalent.

I think the most interesting of these objects is the regression of y on x. Linear algebra tells us that if y is not in the column space of x then there is no way to arrive at a solution however, we can arrive at an approximation by projecting y orthogonally on x. This is a pretty neat discovery as one can easily imagine two lines x and y, going in different directions, then from the shadow y makes on x, we can create a new vector p that sits on x and is orthogonal to the vector yp . The image this creates in our head can be translated in different dimensions even if we can't represent these dimensions with pen and paper. This connection between statistics and linear algebra is beyond the scope of this article but if I have managed to pique your interest, then I think it is worthwhile to pick up a text like Geometry of multivariate statistics by Thomas D. Wickens and explore this subject further.

I have always liked to make images move around on my screen, however, until I understood better transition matrices and change of basis I never had a good grasp of how things worked under the hood. Do you feel anxious about transition matrices and change of basis? Don't worry they are just fancy names of what happens when you multiply matrices. The matrix multiplication Ax=b is all we probably need to know to understand this article. A is a transition matrix that acts on x to yield b.

I want to explain how to generate a transition matrix, that flips a vector in R2 say x=[12] By 90° in the anticlockwise direction. This should yeild the vector [21]. Firstly I will like to talk about a basis of a space. This is basically the minimal vectors that span a particular space. For example we are in R2 and we want two vectors that span R2 we can easily go with the standard unit vectors [10][01] Or [12][21] with a linear combination of these two vectors(and many more) we can get to any point on the plane that is generated by R2 for R3 we can use the standard unit vectors in R3 -> [100][010][001] the point of this whole discussion is to point out that to get a transition matrix, we only need to observe what happens to for example the standard unit vectors of R2 . This will help us generate our matrix. In the next section, it might be a bit difficult to do this and that is where change of basis comes to play but let us get on with it. When our transition matrix A acts on the vector [10] to get a 90° rotation the x axis becomes 0 and the y axis becomes 1 that is [01] When the transition matrix acts on [01] the cordinates change to -1 and 0 that is [10]. These two vectors give us our transition matrix [0110]. Check for yourself that if you multiply this matrix by [12] that we get [21] .

Lets try to extend this result with a little bit of trigonometry. We know that cos90=0, sin90 = 1 so our transition matrix could possibly look like [cos90sin90sin90cos90] in fact we can generalize this matrix for any angle θ to become [cosθsinθsinθcosθ]. We can implement this in python to verify the results.

If you run the above code you will get two rectangles that look like a head and a body moving around by the angle specified by theta.

This was pretty tidy, however what happens when we want to say for example reflect about the line [12] this is not as straighforward as reflecting about 90° try it. However, what we can do is to switch basis. If you have some knowledge about eigen values and eigen vectors this will come in handy even though I will explain a trick that will help us arrive at the answer. What we need to do is to find a complete set of new eigen vectors that span R2 we already have one. To find the other one we need a vector that is orthogonal to it. That is they form a 90° angle. to achieve this we need a new vector such that if we obtain the dot product of our vector with it, we get the zero vector. This vector is [21] we can easily check that 12+2(1)=0. What we need to do now is use [12] and [21] as our new cordinate system and instead of reflecting about the traditional cordinate system we know, we will reflect about this cordinate system. Imagine a world where the standard cordinates for y and x are the lines c[12] and c[21] where c is any real number as against the lines c[10] and c[01]this takes a bit of a stretch but once you have it, we can then think about what flipping over the y axis which is now the line [12] if we apply our previous mechanics by checking what happens to the vectors [10][01] we would gain an insight on the transition matrix in our new cordinate system. The vector [01] should remain the same since we are flipping about the y axis in our new system. However, the vector [10] will become [10]. We get the transition matrix as [1001]. The numbers -1 and 1 are basically the eigen values of our matrix [1221] try to compute it and the matrix [1001] is basically placing the eigen values in the diagonal of 2 by 2 identity matrix.

Since we humans haven't yet developed the sense of understanding this new cordinate system, if you wanted to show your new results to your friends, you will need to make some adjustments. Linear algebra comes to the rescue as we can switch from this new cordinate system to a more traditional one. There is a pretty neat technique for doing this called diagonilazation of a matrix. The formula is given by A=SES1 where S is the eigen vector matrix and E is a matrix that contains the eigen values in the diagonals. If we plug in our values we get A as [3/54/54/53/5] in standard cordinates. So if we wanted to find the transformed cordinates for say [10] this should yield [3/54/5] . We can check our results with python by inspecting the outcome of the script below

The results can be extended into 3 dimensions with a little more work.

Thanks.

Comments

  1. I read the above article and I got some different kind of information from your article about a mattress. It is a helpful article to enhance our knowledge for us. Thankful to you for sharing an article like this.Abacus For Kids

    ReplyDelete

Post a Comment

Popular posts from this blog

How we processed data of over 100gb with 16gb of ram

AWS networking basics with terraform and a tiny bit of microservices