Stop abusing spring….please :)

Dependency Injection is magic.  Once you understand the trick, it is insanely simple.

Let’s all agree that if we are going to use dependency injection, we will first learn the trick.  It keeps the magic alive.

I highly recommend studying the Guice framework tutorials before you try to jump into spring (you may even decide to just use Guice instead).

To summarize using an excellent analogy from the people behind Guice, dependency injection is like the “Hollywood principle,” which is “don’t call us, we’ll call you.”  Think of it as a way to decouple your code such that testing small portions becomes trivial.  Certainly other ways to achieve the same means, but dependency injection does a lot of the work for you by using a simple annotation (in spring think  @autowired, in Guice think @Inject ).

Planet of the API

Several weeks back I was discussing with a co-worker what it means to work in the data field.  The old-school answer, which is still probably the most common, is that you will be in some way involved with databases.  While that is unlikely to change there is a shift towards working with APIs instead of directly with the database (providing a level of abstraction from the underlying database).  I, for one, am a strong fan of this approach since it often makes my life easier.

Of course that means that someone has to build the APIs, which has gotten a whole lot easier.  The question from my co-worker was, “Can you build an API in R?”  Valid question.  My off-the-cuff answer was of course, you can do anything in R, and if you can’t do it in native R you use Rcpp and do it in C++ and call it from R (EDIT: After thinking about this a bit, it is probably not wise to do this in R given the fact that R is an interpreted language and doesn’t really “run” a stand-alone application in the traditional sense).

While I didn’t take the time to develop an API in R,  I did write a simple Java program that uses Maven, Spring MVC, and MyBatis to publish an API.  I learned this technique from some highly seasoned developers; many thanks to them.

It is really quite simple once you get all the pieces hooked together.

Check it out on GitHub.

Another (probably redundant) post on Black-Scholes


Black-Scholes Framework

We can construct a portfolio such that we have an amount in stock and an amount in a money market account, such that 1.  It can be shown that using replication and no arbitrage, it is possible to derive an equation for the value function [V(t,s)] for the price of a derivative.  This equation was labeled the Black-Scholes PDE and is listed here for reference:


For the purposes of this post, it is important to note that daily log returns are modeled as geometric Brownian motion, such that 3 where 4 , a is the drift, and b is the dispersion parameter (all assumed to be positive and constant).  Furthermore,  with .



Through a series of transformations, the Black-Scholes PDE can be mapped to the heat equation and solved for certain payoffs (European options).

For reference, the solution to the Black-Scholes PDE for European Calls is listed below:


In order to study these assumptions empirically, data from the London Stock Exchange was used. Two companies were selected based on the largest volume across multiple option contracts.

The exchange reports Settlement price  as “Weighted average of traded prices in the last 30 seconds of trading. If there is no trading in the contract month the analyst will look at the last bid & offer prices and take the average of these – in this case the contract can settle outside the high/low range.”[1]  Several contracts had no daily volume, so it is noted here.

Daily stock returns were computed for the date range of 4/4/2013-4/16/2013 (the maximum available free data).  For volatility, the previous year of monthly and daily stock returns was used to derive an estimate (4/4/2012-4/4/2013).  A list of the companies selected is in the table below.


Company Name Ticker Symbol
BHP Billiton plc BLT
British Petroleum BP


Empirical observations – Volatility

Figure 1 shows settlement prices from the date range 4/4/2013-4/16/2013 for call options on BLT expiring on 9/13/2013 with strike price £1,800 (in-the-money).  The blue points (market price) represent the market price at close on the date listed, while the 95% CI lines represent the Black-Scholes price computed using the upper and lower bounds of volatility computed on monthly returns.  In this example the market price falls within the 95% confidence interval 44% of the time.




Using daily returns to estimate volatility is even worse, as the market price falls within the confidence interval 10% of the time overall (compared to 74% for monthly returns).


Figure 2 shows similar results for symbol BP on an out-of-the-money put option (strike=£440 expiration=4/13/2013)



Lastly, figure 3 shows a near-the-money call option on BP (strike=£450, expiration=6/13/2013)


BP displayed similar patterns as BLT, with the market price falling within the confidence interval 1% of the time overall (on daily returns, compared to 66% for monthly returns).

Empirical observations – Log-normal returns

As noted above, another major assumption of the Black-Scholes model is log-normal stock returns.  This assumption can be empirically tested using a distance test such as the Kolmogorov-Smirnov test.

Figure 4 shows a histogram of daily returns for BP


The maximum difference between the cumulative distributions, D, is: 0.3544 with a corresponding P of: 0.086.  The K-S test says it is unlikely this data is normally distributed: P= 0.00 where the normal distribution has mean= 5.0232E-04 and stdev= 1.4525E-02

Testing monthly returns, K-S test finds the data is consistent with a normal distribution: P= 0.66 where the normal distribution has mean= 6.0153E-03 and stdev= 6.9000E-02.