Friday, August 27, 2010

Karhunen-Loeve transform in java

Background knowledge and matlab code here.
The matlab code below is taken from the previous paper.
%number of examples
N=size(X,2);
%dimension of each example
M=size(X,1);
%mean
meanX=mean(X,2);
%centering the data
Xm=X-meanX*ones(1,N);
%covariance matrix
C=(Xm*Xm')/N;
%computing the eigenspace:
[U D]=eig(C);
%projecting the centered data
over the eigenspace
P=U'*Xm;

Helpful lecture video
Download JAMA package
Java code:
import Jama.*;

public class KLTransform {

//Matrix x[i,j]= jth feature in ith example
public Matrix k_lTransform(Matrix x)
{
 
   x = x.transpose();
     
   int nExample = x.getColumnDimension();

  //calculate mean
   Matrix mean = getMean(x);
  
   double[][] oneD = new double[1][nExample];
   for(int i = 0; i < nExample; i++)
       oneD[0][i] = 1;
   Matrix ones = new Matrix(oneD);
  
   //center the data
   Matrix xm = x.minus(mean.times(ones)); 

   //Calculate covariance matrix
   Matrix cov  = xm.times(xm.transpose());
  
   /*
   In the matlab code, the covariance matrix is divided with N (nExample). 
   Now cov and cov/nExample have the same eigenvectors but different eigenvalues. 
   In this code, the division doesn't make any difference as we are only 
   considering the eigenvectors. 
   But there are some cases, like in Kaiser-Guttman stopping rule 
   where only the eigenvectors with eigenvalue > 1 are chosen, 
   division might make a difference.
   */
   cov = cov.times(1.0/nExample);

   //compute eigen vectors
   Matrix eigenVectors = cov.eig().getV();

  //compute pca
   Matrix pca = eigenVectors.transpose().times(xm);
   return pca;
}
 
public Matrix getMean(Matrix x) {
 int nExample = x.getColumnDimension();
 int nFeature = x.getRowDimension();
  
 double[][] meanD = new double[nFeature][1];
 Matrix mean = new Matrix(meanD);
  
 for(int i = 0; i < nFeature; i++)
 {
   double avg = 0.0;
   for(int j = 0; j < nExample; j++)
   {
     avg+=x.get( i,j);
   }
   mean.set(i, 0, avg/nExample);
   
 }
 return mean;
}
//test 
public static void main(String[] args)
{
  KLTransform kl = new KLTransform();

  double [][] d = new double[][]{{1, 2, 3},{4,5,6}};
  Matrix x = new Matrix(d);
  Matrix pc = kl.k_lTransform(x);

  pc.print(pc.getRowDimension(), 2);

 }
}

Note that in the matlab code, number of examples is the number of columns that is each column is an example. But the java code assumes each row is an example. So the matrix X in matlab is the transpose of matrix X in java.
To test the java and matlab code:
run java with :
X=
[1 2 3]
[4 5 6]

run matlab with: X = [1, 4; 2, 5; 3, 6]

syntax highlighting tutorial

Friday, August 20, 2010

If I were

I am tagged by Mahmud in the "if i were" thing.
If I wait and try to think about the "correct" answers of these questions, i'll never have time to answer them, so I decided to do it in NOW, it's now or never :P


If I were a month, i'd be April, my Birth month :D, and this month is like me - sunny and bright !
If I were a day of the week, I’d be tuesday- not monday when everyone is grumpy, not weekends when everybody feels relaxed and I don't know why tuesdays are lucky for me.
If I were a time of day, I’d be 5 am- awesome start of a super awesome day. 
If I were a season, I’d be summer, colorful and  cheerful
If I were a planet, I’d be earth - green and full of people :D
If I were a sea animal, I'd be a mermaid I guess and always want to be a human.
If I were a direction, I am always "right"
If I were a piece of furniture, I’d be a rocking chair -always in motion
If I were a liquid, I’d be orange juice- tangy in taste but kills the bacteria :D
If I were a tree, I’d be a daffodil - I am totally narcisist
If I were a tool, I’d be a knife, really handy and versatile :P 
If I were an element, I could never be just one element, i'm too complex :D.
If I were a gemstone, I’d be ruby- I love red
If I were a musical instrument, I’d be a flute - subtle but unique.
If I were a color, I’d be RED
If I were an emotion, I’d be ":P"
If I were a fruit, I’d be orange - with the shape of earth and color of sun.
If I were a sound, I’d be the sound of laughter.
If I were a car, I’d be the cheapest but useful one :D
If I were food, I’d be khichuri & omelet - easy to make and you can never have too much.
If I were a taste, I’d be a mixed taste of a various of spices- a little bit of chili, a tinge of cinnamon, some cardamoms, some lime juice may be and a handful of cilantro :D
If I were a scent, I’d be the smell of lemon - fresh!!
If I were a pair of shoes, I’d be flip-flop -  down to earth and unpretentious
And if I were a bird, I’d be a rooster - I have a voice and sometimes I scream relentlessly even if no one is there to listen.
And If I were to tag people, If i could tag ANYONE then I'd tag the dead ones - to know what they feel about their earthy life now.

Saturday, August 07, 2010

find a file and delete it


Command:
find <where-to-look> -name <name-of-the-file> | xargs /bin/rm -f
Example:
I want to delete .svn folder from every directory
then the command would be
find <main-directory-name> -name .svn | xargs /bin/rm -rf

Source: Unix "find" command

Friday, August 06, 2010

You got to dance with the one that brought you
Stay with the one that wants you
The one who's gonna love you when all of the others go home
Don't let the green grass fool ya
Don't let the moon get to ya
Dance with the one that brought you and you can't go wrong

It was the best of times, it was the worst of times; 
it was the age of wisdom, it was the age of foolishness; 
it was the epoch of belief, it was the epoch of incredulity;
it was the season of Light, it was the season of Darkness; 
it was the spring of hope, it was the winter of despair;
we had everything before us, we had nothing before us

Wednesday, August 04, 2010

Awesome documentary: My architect


                 Louis Kahn




Its not everyday that I get to watch a documentary like this. To me, "documentary" has a synonym - Boring. So I tend to avoid documentaries as much I can. But this documentary made me captivated and changed my view on documentaries!

Its about Architect Louis Kahn's life, made by his son Nathaniel Kahn, who was 11 when Louis died at the New York Penn station. He had such an unusual life! I think EVERY person has an unusual life; different and unique life than everyone else. It's sad that we get to know only a few lives. There should be a rule that everyone has to make a movie about his/her life, so that other people can see and appreciate the extraordinary experiences and phenomenal circumstances that he/she might have witnessed.

Anyway, as long as I'm not the one making rules of life and there isn't one movie per person, at least watch the ones that are already here.

This is the ending of the movie in Bangladesh featuring Louis's biggest work Jatiyo Shonshod Bhaban
and TEDTalk by Nathaniel