Exercise Sheet 2 Programming
Exercise Sheet 2 Programming
In [1]:
import numpy
import matplotlib
%matplotlib inline
from matplotlib import pyplot as plt
import utils
X,Y = utils.getdata()
p1,p2 = utils.plotdata(X,Y)
For these two trajetories, that can be understood as two different modalities of the same data, we would like determine under which projections they
appear maximally correlated.
subject to autcorrelation constraints $w_x^\top C_{xx} w_x = 1$ and $w_y^\top C_{yy} w_y = 1$. Using the method of Lagrange multipliers, this
optimization problem can be reduced to finding the first eigenvector of the generalized eigenvalue problem:
$$ \begin{bmatrix}0 & C_{xy}\\C_{yx} & 0\end{bmatrix} \begin{bmatrix}w_x\\w_y\end{bmatrix} = \lambda \begin{bmatrix}C_{xx} & 0\\0 &
C_{yy}\end{bmatrix} \begin{bmatrix}w_x\\w_y\end{bmatrix} $$
Your first task is to write a function that solves the CCA problem in the primal (i.e. that solves the generalized eigenvalue problem above). The function you
need to implement receives two matrices X and Y of size N $\times$ d1 and N $\times$ d2 respectively. It returns two vectors of size d1 and
d2 corresponding to the projections associated to the modalities X and Y . (Hint: Note that the data matrices X and Y have not been centered yet.)
In [2]:
import numpy
def CCAprimal(X,Y):
## -------------------------------
## TODO: replace by your solution
## -------------------------------
import solution
wx,wy = solution.CCAprimal(X,Y)
## -------------------------------
return wx,wy
The function can now be called with our dataset. The learned projection vectors $w_x$ and $w_y$ are plotted as red arrows.
In [3]:
wx,wy = CCAprimal(X,Y)
p1,p2 = utils.plotdata(X,Y)
p1.arrow(0,0,1*wx[0],1*wx[1],color='red',width=0.1)
p2.arrow(0,0,1*wy[0],1*wy[1],color='red',width=0.1)
plt.show()
In each modality, the arrow points in a specific direction (note that the optimal CCA directions are defined up to a sign flip of both $w_x$ and $w_y$).
Furthermore, we can verify CCA has learned a meaningful solution by projecting the data on it.
In [4]:
plt.figure(figsize=(6,2))
plt.plot(numpy.dot(X,wx))
plt.plot(numpy.dot(Y,wy))
plt.show()
In [5]:
X,Y = utils.getHDdata()
utils.plotHDdata(X[0],Y[0])
plt.show()
Several sources can be perceived, however, there is a significant level of noise. Here again, we will use CCA to find subspaces where the two modalities
are maximally correlated. In this example, because there are many more dimensions than there are data points, it is more advantageous to solve CCA in
the dual. Your task is to implement a CCA dual solver that receives two data matrices of size N $\times$ d1 and N $\times$ d2 respectively as input,
and returns the associate CCA directions (two vectors of respective sizes d1 and d2 ).
In [6]:
def CCAdual(X,Y):
## -------------------------------
## TODO: replace by your solution
## -------------------------------
import solution
wx,wy = solution.CCAdual(X,Y)
## -------------------------------
return wx,wy
We now call the function we have implemented with a training sequence of 100 pairs of images. Because the returned solution is of same dimensions as
the inputs, it can be rendered in a similar fashion.
In [7]:
wx,wy = CCAdual(X[:100],Y[:100])
utils.plotHDdata(wx,wy)
plt.show()
Here, we can clearly see a common factor that has been extracted between the two fields, specifically a point source emitting at a particular frequency. A
test sequence of 100 pairs of images can now be projected on these two filters:
In [8]:
plt.figure(figsize=(6,2))
plt.plot(numpy.dot(X[100:],wx))
plt.plot(numpy.dot(Y[100:],wy))
plt.show()
Clearly the two projected signals are correlated and the input noise has been strongly reduced.