I recently needed to generate multivariate normal values in a web browser. Since I was unable to find a simple implementation, I wrote the following JavaScript code. This code is not the fastest, and it also makes use of the Math.random() method which I can’t vouch for, but it seems to do a decent job. The first two functions are needed to generate normal values via the Box-Muller method. This implementation uses the basic trig functions of the JavaScript Math object. The third function performs a Cholesky decomposition, which is need to decompose a correlation matrix. Finally, the last function combines these functions to generate iid normals and then transform them to correlated normals.
So how can a drunkard walking down the street lead to such a scary sounding thing like ‘Stochastic Calculus‘? Under certain conditions and taking the continuous limit, the Drunkard’s Walk becomes Brownian Motion. I’ll try to show you how this all happens using a little bit of JavaScript. In the Drunkard’s Walk, we assumed that with each step the drunkard takes he moves a distance of 1 foot to either the left or right. Its natural to wonder if something interesting would happen if we scaled this distance and took many more steps. As a matter of fact, something interesting does happen!
Read the rest of this entry »