Archiv für die Kategorie ‘Mathe’

Random Correlation Matrices

Dienstag, 19. Februar 2008

One of my colleagues has implemented a new method for evaluating the cumulative distribution function of a multivariate normal distribution and wanted to compare the speed of his method with that of randomized quasi-Monte Carlo methods. A few days ago, while we were going to lunch, he asked me how to generate random correlation matrices, because the speed of his method strongly depends on the correlation matrix and he wanted to have some sort of average.

But what is a random correlation matrix?

Let's first give a characterization of correlation matrices.

It is well known that for a matrix $C:=(c_{i,j})_{1\leq i,j\leq n}\in\ensuremath{\mathbb{R}}\xspace ^{n\times n}$ there exist (multivariate normal distributed) random variables $X,Y$ with


if and only if

  1. $-1\leq c_{i,j}\leq 1$ for all $i,j\in\{0,\ldots,n\}$,
  2. $c_{i,i}= 1$ for all $i\in\{0,\ldots,n\}$,
  3. $C$ is symmetric (therefore all eigenvalues $\lambda_1,\ldots,\lambda_n$ of $C$ are real)
  4. and all eigenvalues of $C$ are greater or equal to zero.

But what is the right notion of randomness for these matrices? For example let's look at the orthogonal matrices. In many numerical applications we need uniformly distributed random orthogonal matrices in terms of the Haar measure.

Unfortunately in our case there is no clear, natural notion of randomness. :-(

Method 1 - Try and Error: We generate a matrix fulfilling no. 1, 2 and 3 of the characterization (these matrices are called pseudo correlation matrices) by generating independent pseudo-random numbers uniformly distributed between -1 and 1 for the entries $c_{i,j}=c_{j,i}$, $1\leq i<j\leq n$.

If this random symmetric matrix is positive semidefinite (i.e. all eigenvalues of $C$ are greater or equal to zero), we have the desired result. Otherwise we try again. Here is the corresponding R code:

random.pseudo.correlation.matrix <-
function(n) {
  a <- diag(n)
  for(i in 1:(n-1)) {
    for(j in (i+1):n) {
      a[i,j] <- a[j,i] <- runif(1,-1,1)
    }
  }
  return(a)
}

random.correlation.matrix.try.and.error <-
function(n) {
  repeat {
    a <- random.pseudo.correlation.matrix(n)
    if (min(eigen(a)$values)>=0) return(a)
  }
}

This approach is only reasonable for very small dimensions (try it with $n=6,7,8$).

Method 2 - Lift the Diagonal:

We denote by $I$ the identity matrix. If $C$ has the eigenvalues $\lambda_1\leq\ldots\leq\lambda_n$ then $(C+a\cdot I)$ has the eigenvalues $\lambda_1+a\leq\ldots\leq\lambda_n+a$ since $x$ is a solution of $\det(C-x\cdot I)=0$ if and only if $x+a$ is a solution of $\det(C+a\cdot I-x\cdot I)=\det(C-(x-a)\cdot I)=0$.

So we start again with a pseudo correlation matrix $C$, but instead of retrying if $C$ has negative eigen values, we lift the diagonal by $\lambda_1$ and obtain $C+\lambda_1\cdot I$, which is always positive semidefinite. After dividing by $1+\lambda_1$ we have a correlation matrix which is ``some kind of random''. ;-)

Unfortunately the diagonal is accentuated and the smallest eigen value is always zero. The second problem we could avoid by adding $\lambda_1+b$ where $b$ is some random number, but the first remains.

make.positive.semi.definite <-
function(a, offset=0) {
  (a + (diag(dim(a)[1]) * (abs(min(eigen(a)$values))+offset))) /
    (1+(abs(min(eigen(a)$values))+offset))
}

random.correlation.matrix.lift.diagonal <-
function(n, offset=0) {
  a <- random.pseudo.correlation.matrix(n)
  make.positive.semi.definite(offset)
}

Method 3 - Gramian matrix - my favorite: Holmes [Holmes1991] discusses two principal methods for generating random correlation matrices. One of them is to generate $n$ independent pseudo-random vectors $t_1,\ldots,t_n$ distributed uniformly on the unit sphere $S^{n-1}$ in $\ensuremath{\mathbb{R}}\xspace ^n$ and to use the Gram matrix $T^tT$, where $T:=(t_1,\ldots,t_n)$ has $t_i$ as $i$-th column and $T^t$ is the transpose of $T$.

To create the $t_i$ in R, we load the package mvtnorm, generate $\tau_i\sim\mathcal{N}(0,I)$ and set $t_i:=\tau_i/\vert\vert\tau_i\vert\vert$:

random.correlation.matrix.sphere <-
function(n) {
  require("mvtnorm")
  t <- rmvnorm(n,rep(0,n),diag(n))
  for (i in 1:n) {
    t[i,] <- t[i,]/sqrt(t(t[i,])%*%t[i,])
  }
  t%*%t(t)
}

Conclusion: There are futher methods (like e.g. to generate a random spectrum and then construct the correlation matrix), which are not as easy to implement as the ones described above. But just as the three given methods, they all are unsatisfactory in some way because we don't really know how random correlation matrices should be distributed.

For my colleague an average of calculation time does only make sense when he knows which kind of correlation matrices occurs in the applications. He decided to describe and compare the different cases individually.

But does it perhaps make sense to use random correlation matrices as test cases or are the special cases more important? For example, random correlation matrices generated with method 1 and 3 are only singular with probability zero.

Any critique, comments, suggestions or questions are welcome!

And for the next time: Given a correlation matrix C. How do we generate tuples of pseudo-random numbers following a given multivariate distribution with correlation matrix C?

Bibliography

Holmes1991
Holmes, R. B. 1991.
On random correlation matrices.
Siam J. Matrix Anal. Appl., Vol. 12 No. 2: 239-272.

Jahr der Mathematik

Sonntag, 10. Februar 2008

Seit 2000 widmet das Bundesministerium für Bildung und Forschung jedes neue Jahr einer besonderen Wissenschaftsdisziplin.

So ist 2008 das Jahr der Mathematik.

Wie in vielen anderen Städten gibt es in Oldenburg daher eine Reihe von Aktionen zur Mathematik, die auf der Seite

http://www.math.uni-oldenburg.de/jdm/

gesammelt werden.

Den Start zur Reihe “Mathematik am Freitagnachmittag” macht so z.B. der Vortrag “Was Schuler und Lehrer in Mathe wissen: Erkenntnisse aus PISA und Co” von Herrn Prof. Dr. Neubrand:

Jahr der Mathematik - Was Schüler und Lehrer in Mathe wissen.

Ganz untätig will ich selbst im Jahr der Mathematik übrigens auch nicht bleiben – aber dazu erst später mehr.

Zeit…

Montag, 14. Januar 2008

Ist doch dumm...


Nein, keine Sorge, dies wird nicht Yet Another Emo Blog – davon bin ich weit entfernt. Der Grund für den Comic ist zum einen, dass ich mein Stifttablett mal ausprobieren wollte (und wie man sieht kann ich dadurch trotzdem nicht besser zeichnen – von akzeptablen Comics bin ich weit entfernt).

Zum anderen ist’s aber irgendwie auch wahr. Selbst in Spezialgebieten muss man sich heutzutage spezialisieren. War es klug Mathe, Informatik. Physik und Biologie zu studieren und dabei noch in genug andere Bereiche zu schauen – immer in der Hoffnung auf den großen SYNERGIE-Effekt und ohne den Willen etwas ganz auszulassen?

Warum fehlt mir die Zeit für mehr Musik, Elektrotechnik, genetische Computer-Picasso-Algorithmen, Politik, … ?


Vorgestern und gestern habe ich daher das gebührenfreie Einstellen bei eBay nicht mal für meine Unmengen an Magic-Karten genutzt – mir fehlt die Zeit… (Zumindest die Library of Alexandria wird aber noch verkauft… der Preis geht ja gar nicht.) Nur das RAM und zwei DVD-Laufwerke suchen Käufer…


Immerhin haben sie den Kaffeepreis von 15 Cent auf 10 Cent bei der Arbeit gesenkt – weniger Schlaf => mehr Zeit. :-)


Also wenn nicht mehr soviel von mir zu hören ist, liegt’s einfach an der Zeit, die ich nicht finde.

P.S.: Die Orconectes sind jetzt auf dem Balkon – mal sehen wie kalt es dies Jahr noch wird.

Tag der Mathematik und gleitende Arbeitszeit

Freitag, 16. November 2007

Gestern war an der Carl von Ossietzky Universität der siebte Tag der Mathematik und ich bin dafür mal nach Oldenburg gefahren.

Es war schön ehemalige Kommilitonen wiederzusehen und die Halbleiterphysikerin, von der ich meine erste Ratte Zoe bekommen habe, all die Professoren (und zu hören, dass meine Diplomarbeit schon nützlich war), die Fachschaft (wobei ich mich frage, wie all die neuen Erstis zur aktiven Gremienarbeit überredet wurden – großes Lob!), die lieben Mitarbeiter der Geschäftstelle und die Doktoranden – selbst bei … … aus! – so nostalgisch bin ich nicht. ;-) Es war trotzdem schön.


Was mir an meiner momentanen Arbeit gut gefällt ist die gleitende Arbeitszeit durch die ich gestern ja auch nach Oldenburg fahren konnte. Prinzipiell könnte ich momentan sogar die 10 Werktage-Woche praktizieren, die mir damals im Zivildienst auf Station so gefallen hat: 10 Tage arbeiten, 4 Tage Wochenende – die Zahl der Arbeitstage direkt nach dem Wochenende werden so halbiert und man kann an viertägigen Wochenenden auch mal größere Dinge unternehmen. Wie Lena die Idee gefällt, habe ich sie noch nicht gefragt – wahrscheinlich bleibe ich doch lieber bei der 7-Tage-Periode.

Lernen beim Spielen – Mathinvaders

Sonntag, 04. November 2007

Man lernt am besten beim Spielen. Hätten die RPGs früher in der echten Welt gespielt, wüsste ich weniger über Thorwal, Waterdeep und Corrinis, dafür vielleicht ein paar mehr Details zur echten Geschichte.

Das ist eigentlich ein spannendes Thema für sich, dass ich noch mal irgendwann aufgreifen will, aber eigentlich wollte ich heute nur einen Link posten:

Online-Games on MathPlayGround – Wer die Geschwindigkeiten seiner Grundrechenfähigkeiten aufbessern will, sollte hier mal vorbeischauen.

Funktionen-Zahlen-Inverter

Dienstag, 05. Juni 2007

Ein Link aus dem Netz:
Plouffe’s Inverter

Gegeben eine Zahl – zum Beispiel das heutige Datum 05062007 – woher kommt diese Zahl?

So unsinnig diese Frage für das Datum auch sein mag, die Ergebnisse mögen für andere Zahlen durchaus interessant sein, wenn man auf der Suche nach Zusammenhängen zwischen Zahlen ist.

Die Ziffern 05062007 sind übrigens laut der Seite die ersten Ziffern der Zahl

05062007

und auch des natürlichen Logarithmus des Primzahlquotienten 14813/8929. :-)