Sunday, September 27, 2009

Best & Worst Positions from Anthony Bolton's Portfolio/Funds


In my previous blog, i listed some advices from Anthony Bolton who worked as fund manager at London, Fidelity for decades. In this blog, i list some of his best and worst bets between 2000 and 2007. I think, these example bets from his portfolio will be useful to understand dynamics of long term investments.


Year

Best

Worst

2000

Autonomy

Merrant

Celltech

British-Borne Oil

Safeway

Compel

Johnson Matthey

Scotia Holdings

Ellis & Everard

Albert Fisher

Gallaher Group

Allied Leisure

Bank Of Ireland

Cookson Group

Wembley

Enodis

Iceland Group

Reed international

2001

ICAP

Railtrack

London Stock Exchange

Carlton Communications

Arcadia

Iceland Group

Inchcape

Novar

Balfour Beatty

Enodis

Safeway

Elementis

George Wimpey

4 Imprint Group

De Beers

SVB Holdings

Gallaher

British Airways

Carillion

Laird Group

2002

Credit Lyonnais

British Energy

Harmony Gold

Cable & Wireless

Galleger Group

SSL Intl

Amlin

Big Food Group

Enterprise Oil

Bulmer HP

MMO2

Cadiz Inc

Bank of Ireland

Royal & Sun Alliance

SOCO international

Carlton Communications

George Wimpey

Oxford Glycosciences

Kiln

Cookson Group

2003

Cable & Wireless

Goshawk Insurance

Big Food group

SOCO Intl

WS Atkins

Tullow Oil

NTL

Kiln

Carlton Communications

Hiscox

William Hill

Wellington U/W

MMO2

Management Consultancy

Mothercare

Beazley Group

Somerfield

De la Rue

Body Shop

Tenon

2004

Cairn Energy

ITV

MMO2

Rank Group

Celltech

Proteome Sciences

London Stock Exchange

BG Group

Carlton Communications

Big Food Group

First Calgary Peteroleum

Aquarius Platinum

Pendragon

Royal & Sun Alliances

Allied Irish Banks

Reuters

Orkla

Shire

Land Securities

NTL

2005

Cairn Energy

GCAP Media

BG Group

ITV

Statoil

William Hill

Amlin

Marconi Corp

British Energy

London Stock Exchange

Roche

NTL

Standard Chartered

Provident Financial

C&C Energy

SMG

P&O Nedlloyd

Minerva

SOCO Intl

Asia Energy

2006

ITV

Sportingbet

Microfocus

Isoft

British Land

Rank Group

Mecom

GCAP Media

Expro International

BSkyB

Shire

Reed Elsevier

BG Group

Highland Gold

Amlin

888 Holdings

Astra Zeneca

SMG

British Energy

Asia Energy

2007

Bayer

Premier Foods

BG Group

Rank Group

Electrcie de France

GCAP Media

Nokia

Premier Farnell

Reuters

SMG

J Sainsbury

BP

Reed Elsevier

British Land

Xansa

Johnson Services Group

Statoil Hydro

Erinaceous Group

Vodafone

ITV

Summary

2000-2007

Autonomy

Sportingbet

ICAP

Rank Group

Gallaher Group

ITV

Cairn Energy

GCAP Media

MMO2

SMG

Amlin

Premier Foods

Balfour Beatty

Isoft

George Wimpey

Cookson Group

BG Group

SSL Intl

Safeway

British-Borneo Oil & Gas


Monday, September 14, 2009

Pricing European Options by Black-Scholes Model

In my previous blogs, price of european call and put options are estimated by Monte Carlo Simulation and Binomial Model. In this blog, these options' prices are estimated by Black-Scholes (BS) model. BS model is more optimal pricing model than previous two methods. Actually, Monte Carlo simulation and Binomal model aims to approximate BS model.

Price of european call and put options is estimated as follows by using BS model:

where
S: Asset price
E:Strike price
D: Divident
r:Interest Rate
Sigma: Variance
t:Current time
T:Strike time
N(x): Normal Cumulative Density Function

Below sample C# and Matlab source code of Black-Scholes Model without any divident contribution is listed.


1:  % Matlab Source Code  
2: % Estimatation of European Call and Put Option by Black-Scholes Model
3: % denizstij (http://denizstij.blogspot.com/),
4: % Sep,2009
5: asset=230; %S
6: strike=210;%X
7: volatility=0.25; %sigma
8: r=0.04545;
9: time=0.5;
10: d1=(log(asset/strike)+(r+(volatility^2)/2)*time)/volatility/(sqrt(time));
11: %d2=(log(asset/strike)+(r-(volatility^2)/2)*time)/volatility/(sqrt(time));
12: d2=d1-volatility*(sqrt(time))
13: eu_call_option_price= asset*normcdf(d1,0,1)-strike*exp(-r*time)*normcdf(d2,0,1)
14: eu_put_option_price= -asset*normcdf(-d1,0,1)+strike*exp(-r*time)*normcdf(-d2,0,1)


1:  using System;  
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Text;
5: using CenterSpace.Free;
6: namespace Denizstij.Finance.Pricing.EuropeanOptionPricingByBlackScholes
7: {
8: /// <summary>
9: /// Estimates European Call and Put options by using Black-Scholes model
10: /// denizstij (http://denizstij.blogspot.com/)
11: /// Sep-2009
12: /// </summary>
13: class EuropeanOptionPricingByBlackScholes
14: {
15: // CenterSpace.Free.NormalDist to estimate normal cumulative density function
16: // by CenterSpace Software (http://www.centerspace.net/resources.php)
17: private NormalDist normDist = new NormalDist(0, 1);
18: // Price estimator
19: public List<double> estimatePrice(double asset, double strike, double volatility, double intRate, double time)
20: {
21: double d1 = (Math.Log(asset / strike) + (intRate + Math.Pow(volatility, 2) / 2) * time) / volatility / (Math.Sqrt(time));
22: double d2 = d1 - volatility * (Math.Sqrt(time));
23: double eu_call_option_price = asset * normDist.CDF(d1) - strike * Math.Exp(-intRate * time) * normDist.CDF(d2);
24: double eu_put_option_price = -asset * normDist.CDF(-d1) + strike * Math.Exp(-intRate * time) * normDist.CDF(-d2);
25: List<double> prices = new List<double>();
26: prices.Add(eu_call_option_price);
27: prices.Add(eu_put_option_price);
28: return prices;
29: }
30: static void Main(string[] args)
31: {
32: EuropeanOptionPricingByBlackScholes pricing = new EuropeanOptionPricingByBlackScholes();
33: List<double> prices = pricing.estimatePrice(230,
34: 210,
35: 0.25,
36: 0.04545,
37: 0.5 // In terms of years
38: );
39: System.Console.WriteLine("Call Option Price:" + prices.ElementAt(0)); // £30.741
40: System.Console.WriteLine("Put Option Price:" + prices.ElementAt(1)); // £6.023
41: }
42: }
43: }

Wednesday, September 09, 2009

Lessons -- Investing Against the Tide by Anthony Bolton

Followings are lifelong lessons’ of Anthony Bolton who delivered record level return on investment funds at Fidelity for decades. These are elaborated in more detail in his latest book, “Investing Against the Tide: Lessons from a Life Running Money”

Companies
  • Start by evaluating the quality of the finance
  • Will it be here in ten years’ time and be more valuable?
  • Is the company in control of its own destiny?
  • Is the business model easy to understand?
  • Does the business generate cash?
  • Remember, mean reversion is one of the great truism of capitalism
  • Beware company guidance
  • Use part of a company meeting to talk about other companies
  • If you have any doubt about a company, follow the cash
What to look for in management
  • Integrity and openness are most important
  • If you have any question on company or trustworthiness, avoid the company
  • Do they have a detailed knowledge of the business strategically, operationally and financially?
  • Are the objectives and incentives of managements aligned with shareholders?
  • Do the management’s trades in the stock conflicts or confirm their statements?
  • Remember, people rarely change, invest in managers you trust
Shares
  • Every stock you own should have an investment thesis
  • Test this regularly and if no longer valid sell
  • Look at a share the same way as if you were buying the whole business at the price
  • Forget the price you paid for shares
  • Keep an open mind and know the ‘counter’ thesis
  • Think in terms of levels of conviction rather than price targets
  • Don’t try to make it back the way you lost it
  • Consider six factors before you buy a share
    v The quality of the business franchise
    v The management
    v The financials
    v Technical analysis of the share price history
    v The valuation against history
    v Prospect for a takeover of the company
Sentiment
  • Rate perception as important as reality
  • Successful investment is a blend of standing your own ground while listening to the market
  • Short term, the stock market is a voting machine, rather than a weighting machine
  • Sentiment extremes, regardless of the underlying attraction of a share, can suggest major opportunity or risk.
Constructing a portfolio
  • Position size should reflect conviction
  • Don’t spend too much time on past performance attribution
  • Your portfolio should as nearly as possible reflect a ‘start from scratch’ portfolio
  • Don’t pay too much attention to index weights
  • Make incremental rather than large moves
  • Never become emotionally attached to a holding
  • Investment is about making mistakes; win by not losing too often
  • Sell if the investment thesis is broken, if a stock reaches your valuation target or if you find something better
  • If in doubt about a holding or a possible new holding compare it directly against the most comparative stock that you own.
  • Keep a balance between being on top of what you own and spending enough time looking for new ideas.
Risks
  • My biggest mistakes have nearly always been companies with poor balance sheet
  • One loses the most money on highly geared companies when business conditions deteriorate
  • Remember that bad news doesn’t travel well
  • Look at a share differently if it has performed well for several years; stocks with big unrealised profits in them are vulnerable in set backs
  • Avoid ‘pass the parcel’ stocks – overvalued stocks with momentum - where investors hope there is more to go and they can sell them before the music stops
Financials
  • Always read a company’s announcements and information in the original – don’t rely on a broker’s summary
  • Carefully read the notes that accompany accounts – key information can be hidden in the notes
Looking at valuations
  • Don’t look at one valuation measure, especially just a PE multiple
  • Buying cheap shares gives you a margin of safety
  • Valuation anomalies are more likely in medium-sized and small companies
  • Look at today’s valuation in the context of at least twenty-year historical valuations
  • Buying when valuations are low against history substantially increases your chance of making money
  • Never forget absolute valuations
  • Remember that as a bull market progresses, valuation methods typically get less conservative and vice versa
Takeovers and takeover targets
  • Buy companies that have a M&A angle
  • Big companies are less likely to be taken over
  • The shareholder list can often carry clues about potential takeover candidates
  • Be sceptical of being able to predict very short term M&A targets
Favourite shares
  • At the heart if my approach is buying cheaply valued recovery shares
  • Favour unpopular shares
  • Does a targeted company have a new management team with a clear and detained recovery plan that you can track
  • You may have to buy a recovery stock before you have all the information
  • Some of my best calls were in stocks that felt uncomfortable to buy
  • Look for stocks with asymmetric pay-offs where you may make a lot of money but your downside is limited
  • Value stocks outperform growth stocks in the long term
How to trade
  • Delegate to a skilful trader and give them reasonable autonomy
  • I only set tight limits on a minority of my trades
  • Know when to be aggressive and know when to let the market come to you
  • Avoid giving round number limits – this is what most other portfolio managers do
  • Be patient - most stocks give you a second chance
  • A block is normally the cheapest way to deal in size
Technical analysis
  • The first thing I look at is the share chart
  • Use technical analysis as a cross-check to your fundamental views
  • Find an approach that works for you and then stick to it
  • More useful for larger stocks
  • Run profits and cut losses
Market timing
  • Consistently successful market calls are very difficult to make
  • If you’re a private investor, take a long-term view. Don’t put money in the stock market that you will need in the next three years
  • Never underestimate the fact that the market is an excellent discounter of the future
  • Don’t be afraid to go against the general mode of the market
  • Markets will react to expected positive or negative events in anticipation of those events.
  • Consider what is being assumed in share prices, rather than what the outlook is like
  • In the mature stages of a bull market, prune back your holdings of more risky stocks
  • Be most on your guard after a long upward move of four to five years

Friday, August 21, 2009

Pricing European Options by using Monte Carlo Simulation


In my previous article, price of european option (put and call) is estimated by using binomial model (Cox-Ross-Rubinstein (CRR) approach [1]). In this article, i provide java and Scilab (similar to Matlab)
source code to estimate these option prices by Monte Carlo simulation.

In binomial model, intrinsic value of an asset (S_T) at expiry t
ime (T) is estimated with a sequence of discrete time steps, at each step, stock price is estimated with a probability (either down or up probability. In monte carlo simulation, intrinsic value of an asset (S_T) at expiry time (T) is obtained from a normal distribution such as [2]:
where, r is annual interest rate S_t asset price at time t and sigma is volatility and x is normal distribution variable.

Having estimated S_T, option's payoff can be estimated easily (max (0, X-S_T) or max (0, S_T-X) for call and put options respectively, where X is options strike, exercise price). Based on expected values of pay off functions which generated with monte carlo simulation, option prices can be calculated as follows for european call option:


Source code of this estimation in java and
Scilab is listed


1:  // SciLab (Matlab) Source code
2: // Estimate European Option price by Monte Carlo Simulation
3: // denizstij (http://denizstij.blogspot.com/),
4: // Aug-2009
5: asset=230; //S
6: strike=210;//X
7: volatility=0.25; //sigma
8: r=0.04545;
9: time=0.5;
10: num_sims=10000;
11: R= (r-0.5*volatility^2)*time;
12: SD=volatility*sqrt(time);
13: sum_call_payoffs=0.0;
14: sum_put_payoffs=0.0;
15: for i=0:num_sims,
16: S_T= asset*exp(R+SD*rand(1,'normal'));
17: sum_call_payoffs=sum_call_payoffs+max([0,S_T-strike]);
18: sum_put_payoffs=sum_put_payoffs+max([0,strike-S_T]);
19: end
20: eu_call_option_price= exp(-r*time)*(sum_call_payoffs/double(num_sims))
21: eu_put_option_price= exp(-r*time)*(sum_put_payoffs/double(num_sims))


1:  package com.denizstij.finance.pricing;
2: import java.util.ArrayList;
3: import java.util.List;
4: import java.util.Random;
5: /**
6: *
7: * Estimate European Option price by Monte Carlo Simulation
8: * @author denizstij (http://denizstij.blogspot.com/)
9: * Aug-2009
10: *
11: */
12: public class EuropeanOptionPricingByMonteCarlo {
13: /**
14: * Estimate European Option price by Monte Carlo Simulation
15: *
16: * @param asset Current Asset Price
17: * @param strike Exercise Price
18: * @param volatility Annual volatility
19: * @param intRate Annual interest rate
20: * @param expiry: Time to maturity (in terms of year)
21: * @param num_sim : Number of simulations
22: * @return Put and call price of european options based on
23: * Monte Carlo Simulation
24: */
25: public strictfp List<Double> estimatePrice(double asset,
26: double strike,
27: double volatility,
28: double intRate,
29: double time,
30: int num_sim) {
31: List<Double> results = new ArrayList<Double>();
32: double R = (intRate - 0.5 *Math.pow(volatility,2))*time;
33: double SD = volatility * Math.sqrt(time);
34: double dF = Math.exp(-intRate*time); // discount Factor
35: double sumCallPayoffs=0.0;
36: double sumPutPayoffs=0.0;
37: Random random= new Random();
38: for (int i = 0; i <= num_sim; i++) {
39: double nextGaussian = random.nextGaussian();
40: double S_T= asset*Math.exp(R+SD*nextGaussian);
41: sumCallPayoffs+=callPayOff(S_T,strike);
42: sumPutPayoffs+=putPayOff(S_T,strike);
43: }
44: double callOptionPrices= dF*sumCallPayoffs/num_sim;
45: double putOptionPrices= dF*sumPutPayoffs/num_sim;
46: results.add(callOptionPrices);
47: results.add(putOptionPrices);
48: return results;
49: }
50: // Pay off method for put options
51: private double putPayOff(double stockPrice, double strike) {
52: return Math.max(strike - stockPrice, 0);
53: }
54: // Pay off method for call options
55: private double callPayOff(double stockPrice, double strike) {
56: return Math.max(stockPrice - strike, 0);
57: }
58: public static void main(String args[]) {
59: EuropeanOptionPricingByMonteCarlo euOptionPricing = new EuropeanOptionPricingByMonteCarlo();
60: List<Double> results = euOptionPricing.estimatePrice(
61: 230,
62: 210,
63: 0.25,
64: 0.04545,
65: 0.5, // In terms of year
66: 10000);
67: Double callOptionPrice = results.get(0);
68: Double putOptionPrice = results.get(1);
69: System.out.println("call Option Price:" + callOptionPrice);
70: System.out.println("put Option Price:" + putOptionPrice);
71: }
72: }

Wednesday, August 19, 2009

Pricing European Options by using Binomial Model

Binomial Model is one of the simplest pricing model for European options. Below you can find an implementation of Cox-Ross-Rubinstein (CRR) approach [1] in java for pricing of put and call European options.

With following parameters, sensitivity of the binomial pricing model as function of number of time steps can be seen in figure 1. With high number of time steps, binomial model with CRR approach converges with optimal Black-Scholes formula (e.x: 30.741 for call option)

Figure 1: Option price as a function of number of time steps.

1:  package com.denizstij.finance.pricing;
2:
3: import java.util.ArrayList;
4: import java.util.List;
5:
6: /**
7: *
8: * Estimate European Option price based on Cox, Ross and Rubinstein model
9: * @author denizstij (http://denizstij.blogspot.com/)
10: *
11: */
12: public strictfp class EuropeanOptionPricingByBinomial {
13:
14: /**
15: * Estimate European Option price based on Cox, Ross and Rubinstein model
16: *
17: * @param asset Current Asset Price
18: * @param strike Exercise Price
19: * @param volatility Annual volatility
20: * @param intRate Annual interest rate
21: * @param expiry: Time to maturity (in terms of year)
22: * @param steps : Number of steps
23: * @return Put and call price of european options based on Cox, Ross and Rubinstein model
24: */
25: public List<Double> estimatePrice(double asset,
26: double strike,
27: double volatility,
28: double intRate,
29: double expiry,
30: int steps) {
31: List<Double> results = new ArrayList<Double>();
32:
33: List<Double> stockPrices = new ArrayList<Double>();
34: List<Double> callOptionPrices = new ArrayList<Double>();
35: List<Double> putOptionPrices = new ArrayList<Double>();
36:
37: double time_step = (expiry) / steps;
38: double R = Math.exp(intRate * time_step);
39: double dF = 1 / R; // discount Factor
40:
41: double u = Math.exp(volatility * Math.sqrt(time_step)); // up boundary
42: double d = 1 / u; // down boundary (Cox, Ross and Rubinstein constraint)
43: // at leaf node, price difference factor between each node
44: double uu = u * u; // (u*d)
45: double p_up = (R - d) / (u - d); // up probability
46: double p_down = 1 - p_up; // down probability
47:
48: // initiliaze stock prices
49: for (int i = 0; i <= steps; i++) {
50: stockPrices.add(i, 0.0d);
51: }
52:
53: double sDown = asset * Math.pow(d, steps);
54: stockPrices.set(0, sDown);
55:
56: // Estimate stock prices in leaf nodes
57: for (int i = 1; i <= steps; i++) {
58: double sD = uu * stockPrices.get(i - 1);
59: stockPrices.set(i, sD);
60: }
61:
62: // estimate option's intrinsic values at leaf nodes
63: for (int i = 0; i <= steps; i++) {
64: double callOptionPrice = callPayOff(stockPrices.get(i), strike);
65: callOptionPrices.add(i, callOptionPrice);
66: double putOptionPrice = putPayOff(stockPrices.get(i), strike);
67: putOptionPrices.add(i, putOptionPrice);
68: }
69:
70: // and lets estimate option prices
71: for (int i = steps; i > 0; i--) {
72: for (int j = 0; j <= i - 1; j++) {
73: double callV = dF*(p_up * callOptionPrices.get(j + 1) +
74: p_down* callOptionPrices.get(j));
75: callOptionPrices.set(j, callV);
76: double putV = dF*(p_up * putOptionPrices.get(j + 1) +
77: p_down * putOptionPrices.get(j));
78: putOptionPrices.set(j, putV);
79: }
80: }
81:
82: // first elements holds option's price
83: results.add(callOptionPrices.get(0));
84: results.add(putOptionPrices.get(0));
85: return results;
86: }
87:
88: // Pay off method for put options
89: private double putPayOff(double stockPrice, double strike) {
90: return Math.max(strike - stockPrice, 0);
91: }
92:
93: // Pay off method for call options
94: private double callPayOff(double stockPrice, double strike) {
95: return Math.max(stockPrice - strike, 0);
96: }
97:
98: public static void main(String args[]) {
99:
100: EuropeanOptionPricingByBinomial euOptionPricing = new EuropeanOptionPricingByBinomial();
101: List<Double> results = euOptionPricing.estimatePrice(
102: 230,
103: 210,
104: 0.25,
105: 0.04545,
106: 0.5, // In terms of year
107: 10);
108: Double callOptionPrice = results.get(0);
109: Double putOptionPrice = results.get(1);
110: System.out.println("call Option Price:" + callOptionPrice);
111: System.out.println("put Option Price:" + putOptionPrice);
112: }
113: }
114:

Tuesday, August 11, 2009

Volume Weighted Average Price (VWAP)

Recently i was skimming through an ebook about electronic trading (Electronic and Algorithmic Trading Technology) after reading this news article. This ebook outlines algorithmic trading in a concise way. According to this book and wikipedia entry, more than half of the orders in London Stock Exchange in the last year were entered by algo traders.

According to the ebook, one of the fundamental and basic algo trading is Volume Weighted Average Price (VWAP). VWAP is the ratio of value traded to total quantity of trades in a time period. It is average price of an security in terms of quantity. It is calculated as follows:



VWAP algorithm implies that at a given time if VWAP is greater than the price of share, then the share is under valued and it is a good candidate for buying. But if VWAP is lesser than the price of share, then it should be considered for selling. Even though, it is a very simple algorithm, it and its variations are commonly used in electronic trading.

I have implemented a sample java application (algo trading) which estimates VWAP of an equity by fetching real time (15 min delayed) financial quotes from Yahoo Finance, UK. Equity quotes (15 min delated) can be downloaded free of charge from yahoo finance pages in CSV format. The sample application periodically downloads predetermined quotes (for example VODAFONE GRP --VOD.L) in CSV format and estimates VWAP. Unfortonately, since downloaded data do not consists of trade quantity values (CSV files has following data: symbol, mid trade, time, date, change, lowest trade, highest trade and volume), quantity value for each quote is estimated based on the volume and mid price.

The sample application has multi layered architecture with observer design pattern. It uses Apache HTTPClient to download CVS files periodically from yahoo finance pages. After quotes are extracted from CSV files, they are appended to a linkedlist based time series and then observers (VWAP estimator and then VWAP clients(UI layer)) are notified for the latest updates.

Click here to download source code of the sample application (algo trading) in java.

Friday, July 31, 2009

Customized ClassPath Contributor- An Eclipse Plug-in

When i first started using java (JDK 1.1), classpath management was an issue for me. Setting up a classpath in command line and then running a java program was so error-prone. Luckily, IDE's, such such as eclipse and netbeans, make a developer's life is more happier. Eclipse (actually eclipse framework) provides various functionalities for fast, productive, efficent, and happy programming, not just for Java, but also many other langues (C/C++, Php, Flex, ...)

Classpath management for java in eclipse , of course, is more easer than bare command line. But it still lacks of some functionalities. Profile based shared repository is one of these missing functionalities. So often in my current work, in eclipse, i find myself arranging classpaths after SVN branching or merging or changing workspace or checking out from SVN. My personal settings during these operations conflicts with other developers settings. There is not any profile (indiviual) based mechanism for settings and classpath ( I am awera that maven provides profile and central reposisty based but our projects has legacy ant scripts)

Therefore, We decided to create a central setting project (Settings) wh
ich contributes automatically and independently to our projects (for example, Beee & Booo) based on developer's profiles. Figure 1 depicts a sample workspace which contains two sample projects (Beee & Booo) and a setting project (Settings) which contains profiles (/Settings/profiles) and central shared libraries (/Settings/libs)


Figure 1: Settings projects and Sample projects (Beee & Booo ) in workspace

Let me talk a bit about Settings project. Setting project is a centralized configuration project and holds shared and indivual settings and libraries for each developer. Therefore, structure of the settings project would vary. But a setting project has following main directories:
  • Settings/libs: Contains common, share libraries
  • Settings/profiles: Holds indivual profiles.
Each profile (ex: deniz.turan) directory under "Settings/profiles" directory holds developer specific settings. Name of profiles directories are same as user's system (windows or linux) login names in order to discover automaticly profiles for developer.

Each profile has following subdirectories which would have specific or custom sub directories (such ants, libs)

  • Settings/profiles/deniz.turan/ants: Holds ant script related indivual properties and custom build scrtips: There are two important file here:
  • Settings/profiles/deniz.turan/libs: Holds profile based libraries. User can create sub library directories (with prefiex "lib_", ex: lib_1, lib_2, lib_3 ....) to arrange class path orders. Library class path order is natural order of String Class such as : lib_1 >lib_2> lib_3> ... > Settings/profiles/deniz.turan/libs (See figure 4 and 5)

Imprtant note: Profiles' libraries are in higher order in class path as demonstrated in below

Having a central setting project is not enough alone, unless the content of profiles (/Settings/profiles) and central common libraries (/Settings/libs) are automatically contributed to main projects for each developer independently. Therefore, i decided to develop an eclipse plug-in to contribute to classpath of a project from a setting project and profile, with a automatic manner. Of course, it is possible to add libraries or folders to project's build path in eclipse, but as i pointed out above, it causes conflicts during merges, branches or in new workspace or on other machines.

To develop plug-in, following two extension points from eclipse rcp framework are used:
  • org.eclipse.jdt.core.classpathContainerInitializer : Initialize and manage class path container (ex: "Deniz's Classpath", in sample source project, Figure 2 , 4 and 5)
  • org.eclipse.jdt.ui.classpathContainerPage : Configuration page for classpath container (Figure 3)
For an overview of JDT in eclipse, please click here

Below you can see some screen shots from Classpath Manager project and a sample workspace.

Figure 2: Classpath Container ("Deniz's Classpath Manager")


Figure 3: Classpath Container Page


Figure 4: After Classpath Manager contributes to a project's build path


Figure 5: Final workspace after Classpath Manager contributes resources from Settings project and profile (deniz.turan)


As figure 5 illustrates, content of deniz.turan profile in Settings project (/Settings/profiles/deniz.turan) and common libraries (/Settings/libs) are contributed to sample "Booo" project. As explained above, order of contributed libraries in classpath are based on first profile sub libraries (/Settings/profiles/deniz.turan/libs/lib_1), and then profiles libraray (/Settings/profile/deniz.turan/libs) and finally common libraries (/Settings/libs)

You can download source code of Classpath Manager eclipse plugin project with sample setting project. The source code is a prototype and implements basic idea of the classpathContainerInitializer and classpathContainerPage extensions. It is tested with eclipse 3.4 and 3.5.

Click here to download ource code.


Saturday, July 25, 2009

Fooled by Randomness, Nassim Nicholas Taleb

Think a book, whose author offends, mocks and bores its readers while he pretends he is the most clever, smart, intellectual and scientific guy in the world in each page of the book. It is not only written in a chaotic structure-- chapters, sections are not related to each other or very mixed up in terms of concepts and structure, the author also uses lots of buzzwords, redundant and wrong examples.

I am talking about
"Fooled by Randomness, The hidden Role of Chance in Life and in the Market", by Nassim Nicholas Taleb. Have a look at following quote from the book:

"What has more value? (a) a contract that pays you $1 million if the stock market goes down 10% on any given day in the next year; (b)a contract that pays you $1 million if the stock market goes down 10% on any given day in the next year due to a terrorist act. "

What is your answer? a or b ? Taleb claims:"I expect most people to select (b)." I am not sure IQ level of people around Taleb, but i reckon, most people would go option a. (I know that, in a normal distrubuted financial world, %10 changes in stock market is so low probability -- once every 73 to 603 trillion billion years-- , but in last 80 years, that incident happened over 2 times and five sigma deviation is over 73 times, which happens once in 7000 years if finance data is normally distrubuted. More info is here).

Taleb is a trader and scholar, works in a fixed income (bonds) financial company. He has background in science (PhD) and like many other PhD graduate, he bored in academia after some time and started to work in finance sector. In his book, he claims that probability theory is not a natural or trivial concepts for many people to comprehend. I do agree with him in this claim. But he takes his claims further and try to establish a theory and life style based on randomness (rare events) . He claims that he is expert on random events and he takes advantages of these random events in his life and business.

He exemplifies his ideas with "fictitious" characters who works as traders in finance (fixed income or equity markets). These characters are generally quite extreme and opposite of each other. For example, he kicks off the book with stock and bond market traders. The stock market trader, Steve, does not have any sound education and a risk taker and get successful so quickly. On the other hand, the bond market trader, Bob, has a degree in probability and he does not take much risk in his business (to be honest, there is not much risk in fixed income market compare to stock market). Because of calculated and not risk taking style, Bob is not rich as much as Steve.

Taleb claims that the success of the stock market trader, Steve, is based on randomness, in another words just "luck". He claims that even if you put 10.000 monkeys in stock market as trader, and monkeys trade randomly, by the end of 5 years, there will be at least a rich monkey. He also claims that after some lengthy time (10 years), all of these monkeys will disappear (The clever one would run away when he has some money, but most of them lose all of their money before they are kicked out). Taleb has a point in analogy, i think. Similar to many aspect of life, randomness also has a part in stock market. But i think, his claims that without proper analysis, research and hard work in stock market, someone would get rich randomly is just ridiculous. He dismisses that the participant of stock markets are intelligent, agile and very adaptive people. Stock market has chaotic aspects, but not totally random. It responses a deterministic way to some events (for example, if a small company merges or bought by a big company, it's share will surge). And the job of traders is to predict (or to speculate) these events in order to make profit.

I found many so-called scientific or intellectual ideas of Talebs, especially in probability theory is very mixed up. For example, he ignores the main reason of filters in statistics or engineering (filtering outliners and noise). While he claims that he takes advantages of these outliners (randomness, noise) in his real life and business, he complaints about the source of the noise for example, media and journalist (He has issue with media, TV, papers too). He contradicts himself, and gives mixed messages in different part of the book.

In brief, in his book, Taleb comes cross as an arrogant, geek person who claims 'if you disagree with me, you're an idiot and I will ignore and laugh at you.' He wrote the book for just sake of writing a book, and before clearing and organising the ideas in his mind. His writing style and personality kills the some of his nice ideas. By the end of book, i felt big disappointment. It was worst ever book i read in a long time. Luckily, the current book in my hand ("The Ascent of Money: A Financial History of the World") , is making me to forget yucky taste of this book.

Saturday, July 18, 2009

Better Annotation in Java Around a Testing Framework

Recently in a pet project, i got involved extensively with annotation in java. I wanted to create a testing framework in which unit tests are created based on given methods level annotations. The testing concept is similar to EasyMock, but rather than creating mock objects, i wanted to capture real objects during "runtime" and then create tests cases based on annotation and output/input parameters. By the way, with "Runtime" i don't mean production environment. When a QA or developer run (tests) the product, tests cases and its inputs are created and stored.

After clearing the concept in my mind, i decided to do a prototype. In the first prototype, i wanted to implement @AssertNotNull unit test case for method's input parameters and output value.

By deploying method level java byte code injection with javassist and a simple annotation (@AssertNotNull), unit tests are create for methods and inputs and outputs of methods are serialized. Once i catch the input objects (parameters of the method) with injected byte code at the beginning and end of methods, i serialized them in XML format with XStream . Injected byte codes also call a TestGenerator service to generate unit tests based on the defined annotation and serialized input/outputs. In unit test phrase, I was planning to deserialize these input objects and uses as input to automatically created tests. First prototype was successful. Unit tests (@AssertNotNull) are created for output object and input parameters of a given annotated method. It was a big step to create unit tests automatically with real object rather than mock objects like EasyMock.

As a next step, i decided to implement a prototype for more complex test cases which has two inputs, for example @AssertEquals. When i was prototyping @AssertEquals annotation, i noticed that annotation in java are really so basic. They do not provide so much functionalities for advance usage such as:

  • Hierarchy: One of the main concept in OO is missing in annotation creation.You are not allowed to extend an annotation. For example, i would like to create a base annotation type (let say @Test) and many sub annotation types (@AssertTrue, @AssertNotNull, @AssertEquals) which extend @Tests. This approach, Hierarchy in annotation, would save lots of computation and coding time if reflection is used to probe which methods are annotated with a type of annotation (@Test) rather than a specific annotation (@AssertTrue). Of course, as a solution to lack of Hierarchy in annotation, methods or classes can be annotated both with super type and specific type annotation (both for example, @AssertNotNull and @Test). But isn't it an ugly code ?
  • Multiple annotation: A method or class can be annotated only once with a specific annotation. Even it is not much common in real life, as in my test framework, it should be possible to annotate a method with different parameters. For example, i would like to annotate a method with multiple @AssertNotEqual each of which uses different parameters. People who comes cross that issue, generally creates a container annotation For example in JPA, If you would like to annotate an entity with multiple @NamedQuery, you have to declare these annotations in @NamedQueries.
  • Simple Type : Types of fields in annotation are so restricted. Only primitive, String and Class types are allowed.
I think annotation in java should be revised. Similar to enums they should to be enhanced for a versatile and advanced usage. I am aware that these issues can be resolved with some other ways as i mentioned above, and it is not trivial to implement comprehensive annotation, but i still think that java deserves better annotation design.

By the way, i am still working on this pet project, test framework! Once i finished it, i will create a blog here with more detail.