Thursday, March 15, 2012

Book Review: Think Stats

I reviewed 'Think Stats' by Allen Downey on Amazon.com

One  line summary: Quickest introduction to Bayesian stats if you know some python programming

Friday, March 9, 2012

Pre budget rally - what pre budget rally?

Media has moved away from UP elections and the talking heads are now talking about budget. I heard this term 'pre budget rally' being tossed around.


Do they actually bother to go and look at the data? I mean in this case, the data is easily available and calculation is trivial. You don't even need Excel, a calculator is sufficient.

Year Budget Date return 1 week before return 1 week after
2000-2001 29-Feb-2000 -4.84 2.90
2001-2002 28-Feb-2001 -1.36 -4.51
2002-2003 28-Feb-2002 -0.68 4.47
2003-2004 28-Feb-2003 -0.26 -4.35
2004-2005 (interim) 3-Feb-2004 -7.12 6.32
2004-2005 8-Jul-2004 -1.24 1.40
2005-2006 28-Feb-2005 2.94 2.70
2006-2007 28-Feb-2006 1.29 3.52
2007-2008 28-Feb-2007 -8.57 -3.16
2008-2009 29-Feb-2008 2.21 -8.65
2009-2010 (interim) 16-Feb-2009 -2.45 -4.02
2009-2010 6-Jul-2009 -5.13 -4.60
2010-2011 26-Feb-2010 1.60 3.38
2011-2012 28-Feb-2011 -3.36 2.44
2012-2013 16-Mar-2012





Average (%)
-1.93 -0.16    

What pre-budget rally? Didn't happen at least in last decade. If anything, the result in the week after budget has been better than the week before.

However, since the number of data points in this case is too small (a grand total of 14 points including the interim budgets), it is hard to say whether the difference between pre-budget week and post-budget week is statistically significant.

I don't prefer to dwell in such 'tiny data' domain. Visualizations can actually mislead you in this case e.g. you might conclude that week before budget looks bad. This is where the classical statistics can help. T-test was designed precisely for such a purpose.

> t.test(df$ret.before, df$ret.after)

 Welch Two Sample t-test

data:  df$ret.before and df$ret.after 
t = -1.1539, df = 24.465, p-value = 0.2597
alternative hypothesis: true difference in means is not equal to 0 
95 percent confidence interval:
 -4.938753  1.394467 
sample estimates:
 mean of x  mean of y 
-1.9264286 -0.1542857 

And, the t-test tells us that difference is not statically significant. So what to do this week? Whatever you do, don't cite the budget week as reason.

Thursday, February 23, 2012

Book review: machine learning for hackers

There is a new book out titled 'Machine learning for hackers' from the publishing house O'Reilly.

I have written a review which is now live on Amazon.

One line summary: 5-stars if you already know some R.

I will be using some of the machine learning techniques in the forthcoming series of posts. Since this is a vast field, there is always that nagging feeling at the back of your mind that maybe what you are doing is naive and not at all what the guru data miners will do. Maybe there is some complex technique that can bypass all the grunt work of data scraping / cleaning / plotting and just transform the data into some n-dimensional space where the problem is trivially solved :)

The book served as a morale boosting benchmark. Authors are well known bloggers in R community. It is reassuring to learn that every traveler to machine learning promised land has to muddle through the messy swamp of unclean data and foggy relationships.

 

Monday, January 30, 2012

Calendar effects in USD-INR exchange rate

In a previous post, i looked at calendar effects in Indian markets using Nifty index data and concluded that end-of-month effect is very much alive in Indian markets.

The beauty of doing this analysis in a higher level language like R is that the work becomes easily reusable. Let's see if we can find some other nails to hit using this hammer. Such as - currencies.


The reference exchange rates between INR and  USD, Euro and JPY are available on RBI site. As of late 2008, retail investors can trade currencies on NSE and MCX-SX through their regular brokerage accounts.

Before we look at the data, let's look at conventional wisdom. Here is a newfeed from last week (26 January 2012) by Reuters which was picked up by ET, Mint and Moneycontrol.  Quoting:

The rupee fell on Wednesday as dollar demand from oil importers for month-end payments offset a rise in the local share market, ...
...
Oil is India's largest import item and oil refiners, the largest buyers of dollar in the local market, step up demand towards the end of every month to meet payment requirements.

Going by this, we would expect that rupee would depreciate toward the end of the month. Time for the evidence.

Let's first look at last 3 years when the currency markets were opened up for retail participation.

The chart below is that of  long Rupee trade i.e. what return you would have made by going long rupee (which is equivalent to short USD) on various calendar dates. Such a strategy would have lost money when rupee depreciated - remember, we are long rupee - and made money when rupee appreciated against dollar.

So, going by conventional wisdom, one would expect this strategy to lose money toward end of the month.




Wow. We find that rupee actually appreciates toward the end of the month.

Maybe market anticipates the oil dollar buying and moves ahead of it.
Or, maybe, this effect is related to end-of-month effect in Nifty i.e. FIIs bring in the money toward the end of the month lifting both the index and the rupee.
Or, maybe there is some other reason. All that is fodder for next few posts.

In any case, let's explore what happened in 2011 when rupee fell all the way from 45 level to 53 level? A long rupee strategy would have killed you, right? Right. Except, if you did it only during the last 3 days of the month and first 3 days of month. These turn-of-month days were still good in such a horrendous year.


Just in case you are interested, here is data for all of last 10 years: from 2002 to 2011.


Long live the calendar effects!

Wednesday, January 25, 2012

(OT) Tools of the trade

I posted on my other blog about the free online valuation courses being offered by Prof. Ashwath Damodaran.

Prof Damodaran is using an online system (again, free) called coursekit and i decided to give it a go for the 'introduction to R' course i am conducting. 

Coursekit has facebook like look and feel. Students love it.  The interaction among them has gone up by a magnitude compared to the old system of using google-groups - which was mainly email oriented. 

I am totally impressed with coursekit. 




Tuesday, January 17, 2012

A technical market timing method for Nifty

Technical investor wants to be in sync with the market. Go with the flow is their motto.

On the other hand, fundamental investor wants to zag when the market zigs. Be Fearful when others are greedy and be greedy when others are fearful, says Warren Buffett. Invest when there is blood in the streets, says Sir Templeton.

Who is right? I have a hunch that maybe both are right - at different time scales. Anyhow, in the spirit of this blog, let's test the hunches with the data. Let's see what data has to say.

In this post, i will look at  one of the most common technical method for market timing viz. moving average method. (Fundamental based market timing methods are a topic for another post)

A Moving average is commonly used with time series data to smooth out short-term fluctuations and highlight longer-term trends. Traders most commonly use moving averages of 50-period, 100-period and 200-period.

The strategy is very simple:
i) If today's closing price for Nifty is above N days moving average, then stay in the market.
ii) If today's closing price falls below N days moving average, sell out and stay out of the market till condition i signals you to get back in again.

How did this strategy do in Indian markets over last 10 years? The chart below shows the results of 100 Rupees invested according to 50 day / 100 day and 200 day simple moving average (SMA) strategy. The start date is 1st January 2002 and end date is 31 Dec 2011.

The benchmark is the buy-and-hold strategy i.e. you just invested 100 Rupees in an index fund and stayed invested through thick and thin. This is shown by the third line from the top in the graph.


Click on the graph to see bigger picture.

Both 50 and 100 day SMA strategies did better than buy and hold. They did this primarily by stepping out of the market for a time in 2008. However, the really good part about them is that they allowed one to participate fully during the bull markets. Participate in the upside, minimize damage during downside ... what more can one ask for ;)

Several caveats are in order:
i) Look at 200 day SMA. That strategy didn't work in Indian markets in last 10 years. You left way too much money on the table during bull runs. You were better off just buying and holding through thick and thin.

Note that 200 day strategy is most well known. See Mebane Faber's paper on SSRN where he sings eulogies of 10-month moving average (essentially 200 day). This paper is the 2nd most downloaded paper on SSRN.

With the benefit of hindsight, one can come up with arguments such as 200-day is too slow for today's markets and 50 or 100 days is better. But you didn't know that back then.

ii) Even with a 100 day SMA strategy, there were 6 signals on average per year. That means you had to switch in and out of the markets 6 times a year. Imagine churning your entire portfolio that many times. (There is a workaround which will get you most of the benefits without churning your portfolio - see post on my other blog)

iii) It wasn't very effective in 2011. By the way, i consider that as good news.

So there it is.

As usual, all the nifty index data is from NSE and i have posted the code to github. I have also posted an Excel file so that people who are not familiar with R could replicate my work in Excel and come up with even better timing models.

Tuesday, January 10, 2012

Looking at calendar effects in Indian Markets


What is 'too good to be true' is generally so - especially so, in stock markets.

You come across something like Calendar Effects and your first reaction is: nice try. Maybe it used to work but cannot work anymore. After all, markets are efficient and if everybody knows something to work, lo and behold, it will stop working.

In this post, let's look at the so called 'turn of the month effect'. For an accessible commentary, see Larry Connors 'Do Stocks show a bullish bias at month end'? Larry concludes: 'We show that over the past 11 3/4 years, stocks have been much stronger near months-end than they have been any other time of the month ... The fund managers like to buy near months-end, ...'.

So here we have:
a) an effect which has been documented in other markets &
b) there is a plausible reason why it works and might continue to work

Let's test it in Indian markets.

I split the data into 10 groups so that group 1 contains dates 1st - 3rd, group 2 contains dates 4th - 6th and so on.

If there is any turn of the month effect, we should be able to see it when we plot average returns for each group. If there is turn of month effect then the group 10 (dates 28 to 31 of month) should have higher average returns. See the graphs for yourself.

Conclusion: Yes Virginia, there is a turn of the month effect in Indian equity markets. Last 3 days of month have been unusually good historically.


This is the graph for last 11 years: 2001 to 2011


Does it still work? Maybe the average is driven by earlier period?
Let's plot data for last 3 years i.e. 2009, 2010 and 2011.



It continued to work in last 3 years.
How about the really bad 2008 - when nothing worked?


Cool. Even in the horrendous 2008, you actually made money if you were in the market only for last calendar week.

Notes:
(1) Data: S&P Nifty index data for last 11 years viz. from 1st January 2001 to 31 Dec 2011. You can download it from NSE. The data is already clean and in CSV format.
(2)  For the geek reader, the code in R is hosted here