“How can I integrate PHP and R?”
I know I’m not the only one who’s asked this question. After all, with great content management systems like Drupal, it would very cool to be able to drop an R module into some PHP code and instantly have a web app popping out some snazzy looking ggplot graphics. After spending some time Google searching for an easy implementation and finding very little in the PHP + R space, I was able to piece together a method for integrating the two. It uses no JavaScript… no AJAX… just plain old PHP.
Read the rest of this entry »

After a month of on-again, off-again coding, I’ve finally completed a web site geared towards calculating the Value at Risk of the average investor’s portfolio. The site is visualvar.com. The big idea was to combine the statistical and visualization tools of R (especially ggplot2) with the web interface of Drupal. While I’m happy with the results, I think this may only be the tip of the iceberg in mashing up these technologies. As a side note, I took a bit of a shortcut and I don’t actually have R running directly on the web server, which means I had to settle for ‘overnight’ calculations rather than ‘On-Demand’. But I still think it is a good proof of concept for combing Drupal with R.
Read the rest of this entry »
I recently posed a question on stackoverflow on whether anyone knew an efficient way to save an R plot to a MySQL database as a BLOB. My plan was to use my personal desktop to perform R routines and save them to a web server, where they could then be accessed and displayed on a web page using a little PHP magic. After getting numerous responses on what a terrible idea this was, I was able to piece my own solution together. The steps are fairly simple. First, save the plot as a temp file, Second, read it back into R as a binary string. Third, insert the binary text into the database using the RODBC library. The code snippet is below.
Read the rest of this entry »
With the stock market freaking out and all, I figured I should take a look at how volatility was being priced in the option market. The CBOE generously provides snapshots of market data for anyone interested to download. By using this data, we can calculate the markets ‘implied volatility’, or level of ‘freaking out’. For those not familiar with the concept of implied volatility, essentially we can take the prices of options in the market and back out the volatility implied by those prices using the Black-Scholes formula. Its been shown over and over again that the assumptions of the Black-Scholes model don’t hold up to empirical data; but its an easy calculation to perform, and so implied volatility is a widely used metric. Anyway, below is my Black-Scholes option pricing function and the function used to back out implied volatility (written in R of course). Since implied volatility can only be found numerically, I used the Bisection Method to calculate it since it was easy to implement, but there are faster methods out there.
Read the rest of this entry »
I find options fascinating because they deal with the abstract ideas of volatility and correlation, both of which are unobservable and can often seem like wild animal spirits (take the current stock market as an example). Understanding these subtle concepts is never easy, but it is essential in pricing some of the more exotic options which involve multiple underlying stocks. To set the scene, let’s pretend that your neighbor wants to make a bet with you where he will pay you $100 if Google (GOOG) and Apple (APPL) are above 500 and 240 respectively after 1 year, but you have to pay him $25 today. How would we determine if $25 is a good deal or not?
Read the rest of this entry »
The Case-Shiller Home Price Indices measure residential home values for 20 cities in the US, with some indices going all the way back to the 80s. With housing prices all the rage these days, we should perform a quick-and-dirty analysis using R to see what we can glean from this rich dataset. First things first, the data needs to be downloaded from S&P’s website, converted into a CSV format, and then imported into R.
Read the rest of this entry »
A common model used in the financial industry for modelling the short rate (think overnight rate, but actually an infinitesimally short amount of time) is the Vasicek model. Although it is unlikely to perfectly fit the yield curve, it has some nice properties that make it a good model to work with. The dynamics of the Vasicek model are describe below.

Read the rest of this entry »
I am an R user! And I see a whole army of R users, here in defiance of tyranny. You’ve come to use R as free men… and free men you are. What will you do with that freedom? Will you use R? Use R and you may use the command line. Use SAS, and you’ll use a GUI… at least for a while. And dying in your beds, many years from now, would you be willing to trade ALL the GUIs, from this day to that, for one chance, just one chance, to come back here and tell our enemies that they may take our lives, but they’ll never take… OUR COMMAND LINE!
The Next Big Thing

So why should R only be used for ’serious’ stuff? No longer! I’ve written the following code in R which executes a little gravitational physics game. The goal of the game is simple. You supply a velocity and direction to a spaceship with the goal of getting the ship to the winning area without crashing into a planet.
Read the rest of this entry »
I’ve found this standard normal random number generator in a number of places, one of which being from one of Paul Wilmott’s books. The idea is that we can use the Central Limit Theorem (CLT) to easily generate values distributed according to a standard normal distribution by using the sum of 12 uniform random variables and subtracting 6. In Excel, the implementation looks like this:
=RAND()+RAND()+RAND()+RAND()+RAND()+RAND()+RAND()+RAND()+RAND()+RAND()+RAND()+RAND()-6
By doing a simple cut-and-paste, we can stick this formula in an Excel cell and go on with our merry way assuming we have generated values from a standard normal distribution. But what is really going on here, and how good does this generator work?
Read the rest of this entry »