Thursday, 12 April 2012

M-O-M vs Y-O-Y inflation rates in India

Some time back I came across this article, by one of our alumni who is currently at NIPFP, which presents some caveats and shortcomings of different measure of inflation in India in a concise manner. Since my master's thesis topic revolves around inflation; I was in a better position to appreciate the author's insightful critique. It might have been the space constrain that held the author from graphically illustrating his point in his article, I'll try and substantiate his arguments using some graphs and R.

There is a recent concern about the rising levels of inflation in India; refer to this post for some numbers that support this claim. However, in this post I wish to illustrate the crucial differences in M-O-M vs Y-O-Y measure for inflation using elementary visualizations in R. From a researchers point of view the choice of variables is something that has to be meticulously addressed and dealt with; especially if one wishes to tread the ambitious path of "inflation forecasting". Before any theoretical mumbo-jumbo one needs to decide whether to work with M-O-M or Y-O-Y rates, or the kind of seasonal adjustment that is required for the data.

Some basic definitions

Inflation rate : Is the percentage change in the consumer price index(CPI).

M-O-M or Month on month inflation rate: Is defined as the monthly percentage change in the CPI series. For eg. January 2010 inflation rate will be the %age change in CPI from December 2009.

Y-O-Y or Year on year inflation rates : Is defined as the yearly percentage change in the CPI series. For eg. January 2010 inflation rate will be the %age change in the CPI from January 2009.

Seasonally adjusted CPI: Any macroeconomic time series, which has a frequency of less than an year, has to be de-seasonalized before being fed into any model. This is basically done to eliminate the seasonal/cyclical effects that might influence the analysis.

The monthly CPI-IW(industrial worker) series was obtained from the RBI handbook of statistics and the data spans from1990M4 : 2011M6. The seasonal adjustment has been done using the X-12 ARIMA filter in EVIEWS.(I am not very proud of this confession, but there were some issues with the "x12" package in R that I could not resolve). 

The below figure shows the Y-O-Y and M-O-M measure of inflation using the seasonally adjusted and unadjusted CPI-IW series.






The difference in the seasonally adjusted and unadjusted series is not evident from the above plots. However, the difference between the M-O-M and Y-O-Y measure for inflation is surely visible by eye balling the plots. To illustrate the difference between the seasonally adjusted and unadjusted measures we plot the same measures with and without the adjustment.





The difference in the two series is not evident in the Y-O-Y rates but we can clearly see that the in M-O-M rates the dynamics gets substantially altered after the seasonal adjustment. This is fairly intuitive since computing the Y-O-Y rate is analogous to passing the series through a (1-B^12) filter(where "B" is the lag operator) which discounts the annual seasonal effect.

Another interesting observation that I made about the 2 series is that Y-O-Y rate is not I(0)(at 5% significance), i.e it has a unit root. The M-O-M rate, on the other hand, comes out clean after being fed into the adf.test() command. Now, I(0)ness is a wonderful property which brings a wry smile to every time series econometricians' face when the p-values drops below 0.01 in the adf.test(). But this apparent conundrum, where the Y-O-Y and M-O-M yield different order of integration, poses a dilemma for the researcher.

The M-O-M inflation captures the short run inflation dynamics, which has less variance and has high forecast ability, that might be helpful in portfolio adjustment for active financial investors. The Y-O-Y rates capture the long run dynamics of inflation, which has high variance and low forecast ability, that might be helpful for the central banker to track.

Here are the codes that I used for the above exercise:


Friday, 23 March 2012

Project Euler: Problem 20



n! means n x (n - 1) x ... x 3 x 2 x 1
For example, 10! = 10 x 9 x ... x 3 x 2 x 1 = 3628800,
and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27.
Find the sum of the digits in the number 100!

The approach to this problem is quite similar to problem 16 which we can see here. We just need to deal with a really large number here. So, GMP again to the rescue.

library(gmp)

x <- factorialZ(100)

sum(as.numeric(unlist(strsplit(as.character(x), split=""))))
# as.character(x) coverts the big number into a string
# strsplit() extracts individual elements of the string. This can be ensured by using the split = "" argument, which splits the string one element at a time
# strsplit() returns a list. To access the elements of this list, use unlist()
# convert the individual characters to numeric using as.numeric()
# sum the elements

Ans: 648

Thursday, 15 March 2012

Project Euler: Problem 16


215 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.
What is the sum of the digits of the number 21000?

Handling large numbers or rather, very large numbers, can be a pain at times. But have no fear, for GMP is here.

GMP  makes the solution quite simple.

library(gmp)

x <- as.bigz(2^1000)
# as.bigz() is a function in the gmp library that helps dealing with large numbers quite easily 

This is the approach we take
  1. Convert the number to a string so that we can extract the individual elements of the number
  2. Extract the individual elements or digits
  3. Create a vector of these digits
  4. Sum the vector
  5. Rejoice

sum(as.numeric(unlist(strsplit(as.character(x), split=""))))
# as.character(x) coverts the big number into a string
# strsplit() extracts individual elements of the string. This can be ensured by using the split = "" argument, which splits the string one element at a time
# strsplit() returns a list. To access the elements of this list, use unlist()
# convert the individual characters to numeric using as.numeric()
# sum the elements

Ans: 1366

Saturday, 10 March 2012

"Fear of floating exchange rate" or "fear of losing international reserves".

We were recently required to do an assignment for the International Finance course where we had to investigate the policy that the emerging economies adopt towards holding international reserves. A recent research paper at the NBER by Joshua Aizenman and Yi Sun demonstrated that many emerging economies are averse to the idea of losing international reserves in times to crisis in order to maintain their exchange rates (against $). They have termed this phenomenon as "fear of losing international reserves". Let me test my understand on this subject (which is limited) and try illustrating in simple language what we did in our assignment. Readers familiar with international economics might relate to it easily, but I would try to make it decipherable for the novice economists too.

Foreign exchange reserves and Foreign exchange market:
In the globalized world that we live, trade relation between countries have become very strong and the quantum of trade has boomed by a considerable amount in the past few decades. In the historical gold-era the trading countries made the respective payments for imports/exports in gold, but with the increasing volume of trade and the Bretton Woods system, countries moved to US $ as the major trading and reserve currency. To put it simply, post the 1960's when the $ became the official reserve currency countries started trading with $ as the universal currency and also started keeping reserves mostly in the form of US currency.

International reserves are nothing but foreign currency deposits and bonds that are held by the central bank or the monetary authority of the country. Its important for countries as they use these reserves to pay for their import. Countries accumulate reserves when they export goods (as it results in inflows of foreign currency) and use up their reserves to pay for imports. So we see why it is important for countries to maintain their reserve levels. But interestingly this is not the only function that the reserves perform. Since we have a foreign exchange market where different currencies are traded, these reserves are also used by the countries' central bank in order to avoid excess volatility in exchange rates. For eg. if the demand for a currency increases its value appreciates and if its demand falls its value depreciates, both these movements are not very desirable if in large magnitude ("fear of floating exchange rate"). So in case of these excess movements the central bank can kick in with a huge buy/sell order and stabilize this movement in the currency exchange rate. This kind of an exchange rate policy is called a "managed float" since the exchange rate is not completely market determined but instead is managed in a band by the central bank.

"Fear of losing reserve":
The recent global financial crisis of 2008 provided some evidence that suggested that the countries are now less worried about their exchange rate volatility and are more concerned about the foreign reserve levels. This is precisely what Joshua Aizenman and Yi Sun investigated in their research paper for 21 emerging economies and found that many economies refrained from using up their reserves during the 2008 crisis. For the ones that did, the loss was not substantial and even for the ones that lost their reserves by a considerable amount they quickly regained it back. This phenomenon was termed as "fear of losing reserves". More than its currency depreciation/appreciation a country might be bothered about the signalling effect that a depleting level of reserves might create. Losing reserves could increase the countries' probability of defaulting on foreign debt which might hamper its trading relation with other nations, or it could lead to a balance of payment crisis. Some prominent figures in the academics field, for instance Dr Ajay Shah, argue that given the large trading volume in the foreign exchange market, it has become a highly liquid market, and as a result the magnitude of orders needed to move the currency rates in your favor are tremendously large. We can see this as one possible reason for emerging economies not to intervene too frequently in the FX markets in times of crisis, but more research is needed to establish whether these explanations hold water.

Evidence from 4 South Asian and 2 Non-oil exporting Middle East economies:
For our assignment, we had to check whether a similar trend in foreign exchange reserve is seen during the Euro-zone debt crisis that started in mid-2010. The countries that we used for our study were India, Pakistan, Sri Lanka, Bangladesh, Jordan and Egypt. We simply plotted a few graphs and visually commented on the trends that we saw. First we plotted the exchange rates of the respective countries' currencies to see if there was some excess volatility in the rates during the time of crisis and subsequently we checked if this volatility was reflected in the foreign reserves levels.

Exchange rates w.r.t US $ of South Asian and middle east emerging economies over time
In order to make these graphs comparable we plotted the [exchange rates/max(exchange rates)] which normalized the plots to values between 0 and 1. The time series has been plotted with the starting month as January 2002 and end month as September 2011. The fi rst vertical line (at Months = 79) indicates the global financial crisis of 2008. Similarly the second dotted vertical line (at Months = 102) indicates the advent of the Euro-zone crisis.


Apart from Jordan, which has its exchange rates pegged to the US $, we see that the exchange rates have been fairly stable since 2002, and that the volatility has increased for most of them in the 2008 crisis. This volatility has been very drastic for India, Pakistan and Sri Lanka in particular. Let us now see whether their trend of international reserves corroborate to this excess volatility in exchange rates.

The below graph shows how these emerging economies have accumulated reserves over the past decade.


Foreign reserves of South Asian and middle east emerging economies over time


The codes to replicate the graphs are fairly simple:


You can do a similar thing with the exchange rates.


The reserve accumulation for all the economies has seen a steady rise from 2002 to 2008 (till the  financial crisis) but post the crisis there has been a decline in the reserves for all these economies except Jordan, which has its exchange rate pegged to the US $. The decline has been marginal for Bangladesh and Egypt, but India, Sri Lanka and Pakistan lost a substantial proportion of their reserves in the 2008 crisis, which corroborates to our earlier fi nding of excess volatility in these countries. Despite this loss of international reserves, all the countries bounced back soon within 10 months time to an accelerating trend of reserve accumulation. This idea seems to be consistent with the hypothesis that countries were adamant not to lose their reserves at times of crisis, instead they chose to play safe by shedding some reserves but quickly regaining them back. The depletion of reserves is viewed by the countries as a sign of vulnerability, and hence they refrain from depleting them in a rush.


When we look at the trend of international reserves post the advent of 2010 Euro-zone crisis the trend seems to be on a rising for most of the countries and stable for some. There has been no depletion of reserves per se, except in Egypt. One reason that can be attributed to this is that the volatility of these countries respective exchange rates w.r.t US $ has not be comparable to the 2008 levels. Srinivasan et al. (2009) have argued in the context of Indian central bank that there is a zone or band of exchange rate in which the central bank does not intervene and its only when the rate exceeds the band does the CB intervene in the forex market. Using a similar argument we can say that since the movement of exchange rates has been within that zone of "no-action" for most of these countries post the Euro-zone crisis the CBs have refrained from intervening in the forex market.

Apart from the above visualization we also plotted these countries trading relation (import and export) with the European union and found a steady decline. (I am not posting those graphs here for sake of precision but the ones interested can view the entire document from here.)

Conclusion:
In this study we find evidence of "fear of depleting reserves" for most of the above discussed economies in the context of the 2008 global financial crisis which is consistent with the Joshua Aizenman & Yi Sun (2009) findings. However, the same cannot be concluded for the Euro-zone debt crisis yet. Since the crisis is still unfolding and its full repercussions is unclear, we will have to wait for the crisis to unfold to see what are the adverse e ects it has on the emerging economies. The evidences so far suggest that the trading relation of these emerging economies with the EU nations has declined post the 2010 crisis, but the international reserve position has not seen a substantial decline yet.

P.S: The data for the above exercise has been extracted from the IFS database. Feedback are welcome.

Thursday, 9 February 2012

GARCH estimation using maximum likelihood

In my previous post I presented my findings from my finance project under the guidance of Dr Susan Thomas. The results in my paper suggested that there are macroeconomic variables, particularly the INR/USD exchange rates, that help us understand the dynamics of stock returns. Although the results that I obtained were significant at 5% level, the weight of my assertion needs some more robust check before we consider the matter closed. One possible source of discrepancy we identified was that the error terms could be heteroskedastic. Meaning that one of our assumption of classical linear regression model (CLRM) estimation viz. homoskedasticity is violated. The resultant coefficient estimated, in case of heteroskedasticity of the error terms, can have underestimated standard errors, which in turn might lead to false acceptance/rejection of our null hypothesis. There are several parametric as well as non-parametric ways to remove the effect of heteroskedasticity in the error terms on the coefficient estimates. However, the method that I adopted to correct for this effect in my model was the path breaking conditional heteroskedasticity modelling propagated by Robert Engle for which he was awarded the Nobel prize in 2003.

The idea behind auto regressive conditional heteroskedasticity (ARCH) model is quite simple and straightforward. One needs to model the mean equation (this is your regression model) and along with that there has to be a specification for the modelling the squares of the errors. Suppose we have a simple AR regression:
     
zt = φ*zt-1 + et        (Mean equation)

Now to model the conditional variance of the error terms all we need to do is a simultaneous estimation of the squares of the error terms in the following manner:


et = ѵtht1/2 ,  where ѵt  ~ NID(0,1)
ht = α0 + α1et-12      (Variance equation)

The above specification of the mean and the variance equations is termed as a AR(1) ARCH(1) specification. This simultaneous estimation takes into account the particular form of heteroskedasticity (ARCH(1) in this case) and estimates the 'φ' coefficient accordingly. Now the above simple specification tends to pose another problem of lag selection, what lag should be considered for the variance equation. To deal with this and several other shortcomings of the simple ARCH model, Bollerslev(1986)  proposed a generalized ARCH model (GARCH). The only difference being that the variance equation now becomes:

ht = α0 + α1et-12 + βht-1

Which is nothing but a GARCH(1,1) model. The beauty of this specification is that a GARCH(1,1) model can be expressed as an ARCH(∞) model. For those who are interested in learning more about ARCH and GARCH processes and the mathematics behind them here are Dr Krishnan's notes that provide an in-depth understanding on the matter. The reason why the ARCH and GARCH models rose to such prominence was because they offered us a method of not just correcting the standard errors of the estimates (as like other robust correction methods) but also provided a functional form for modelling the variance. This idea of modelling variance was heavily adopted by financial researchers. Considering the standard deviation (or variance) of asset prices as a crude estimate of volatility, financial researchers used ARCH and GARCH models to model asset price and/or other high frequency index volatility.   

Now that the idea behind the GARCH modelling is clear let us plunge right into writing the conditional maximum likelihood function for a GARCH process.



Utkarsh was the generous one who provided me with the basic structure of the codes that I then customized to solve this problem at hand. Well now let me confess that a similar result or in fact a more accurate estimates of the coefficients can be obtained using unconditional maximum likelihood estimates that are offered by any of the high priced computer packages like EVIEWS, RATS, SAS etc but my motivation (apart from learning ofcourse) was to figure out a way that could give me the flexibility of modelling any functional form of heteroskedasticity. The above codes have been presented for the estimation of a GARCH(1,1) model but I could do a simple manipulation in the definition of hto fit any arbitrary functional form. 

P.S Sincere thanks to Utkarsh for letting me share the codes. Any feedback/suggestions are welcome.

Friday, 6 January 2012

Revisiting basic macroeconomics : Illustrations with R

Prologue
After 3 semesters of studying economics at IGIDR, the basics of macroeconomics still elude me. What policy shifts what curve? What determines the slope of IS-LM and AD-AS curves? What exactly was Keynes contribution to Economics? How do all these curves move in tandem? Well, I would not be surprised if you are a graduate student in economics and face similar doubts with these basic fundamentals. So in an attempt to demystify or rather simplify these macro fundamentals for econ (as well an non econ) students Isha and I have attempted this brave move of presenting the fundamentals of macroeconomics in a concise manner. We will try to be as explicit as we can but we might end up assuming some prior knowledge of economics. In such cases, we would encourage the readers active participation in improving the quality of the post wherever he/she thinks and idea is expressed with inadequate explanation.

The story
Any theory/idea, be it in pure science or economics, emanates with a hypothesis that is supported by some assumptions. As the study on the subject progresses researchers try and play with the assumptions and see if they can make it better in terms of explaining the real world as we see it. Macroeconomics is no exception, classical macroeconomics dates back to the 19th century with major proponents being Adam Smith, David Ricardo, Malthus, John Stuart Mill, J.B Say, etc. They believed in the functioning of free markets, that is a decentralized mechanism to ensure demand meets supply would result in efficient allocation of resources. Any counter-cyclical measure by a centralized government in order to smoothen the fluctuations in the business cycles would be self defeating. The centralized government, according to these classicals, was primarily to keep the budget deficit (difference between the govt. spending and tax revenue) in check. Meaning that in times of recession, when there is less revenue being generated by taxes, the government should cut spending in order to keep the deficit in check. The reason for them to propagate this idea was the if govt. has to increase spending, in times of recession, it would lead to a rise in the interest rates (since it has to borrow from the citizens) and this increase in interest rates would crowd out private investment (which is what we don't want at times of recession). This ideology was called into question during the great depression of 1929 in the US. This is when the "Say's law", which was at the heart of classical economics, failed to hold. There was mass unemployment, which was invoulantary (as opposed to the classicals view of only voluntary unemployment), insufficient demand for the produced goods, in short "Great" was an adequate adjective to describe the depressed economic scenario during the 1930's.

The Great depression of 1929 provided researchers with the much needed opportunity to revise/update the assumption that they were playing with. J.M Keynes made a break through contribution in revising the assumptions and ideologies of the classicals. He argued that market left completely to themselves might not lead to efficient allocation of resources. This was a paradigm shift in the way policy makers and researchers thought about macroeconomics. He argued that in times of recession the govt. should boost spending in order to revive the economy, contrary to the idea that the classicals propagated. Government spending and private investment were seen as complements and not substitutes. Private spending was determined not only by the interest rates in the economy but also by the expectation of future profitability (famously called the "animal spirit"). Another set of assumptions that he relaxed from the classical framework was the full flexibility of wages and prices. Classical economists believed that the market mechanism ensured that the prices of commodities adjusted instantaneously and fully to make the supply = demand. For eg. if there are 10 apples being produced and the demand turns out for only 8 apples, classical economists argues that the price of the apples would adjust in a manner (fall in this case) so that the demand rises instantaneously and exactly by 2 apples and the equilibrium is maintained. Keynes on the other hand argued that due to rigidities in the market (sticky prices, labor unions, menu costs, etc.) the price adjustment would not be instantaneous and hence the additional demand, in the short run will have to be created by the govt. by spending more. Either it can pitch in and buy the additional 2 apples or it can expedite investment in an avenue that absorbs the labor retrenched due to insufficient demand in the apple market.

Enough of literature, now let us get our hands dirty with some basic maths and visualization of some economic concept that would help us appreciate the above ideologies. We would start by illustrating the difference between the 2 idelogies using the IS-LM  framework (also called the Investment Savings/Liquidity preference Money supply). The basic national income identity finds mention in the first chapters of most of the introductory Macroeconomics textbooks hence we shall start by the same identity:

Derivation


For complete and comprehensive proofs of the above equations you can refer to a text book by William Branson or another textbook by Dornbusch and Fischer.

IS curve: The points on the IS curve represent the combinations of rate of interest (i) and output (Y) for which the goods market are in equilibrium. Meaning, at these combinations of "I" and "Y", the aggregate supply of goods equals aggregate demand for goods in the economy.

LM curve: The points on the LM curve represent the combinations of rate of interest (i) and output for which the money market is in equilibrium. Meaning, at these combinations of "I" and "Y", the aggregate demand for money equals aggregate supply of money in the economy. (Note that we have the prices as exogenously given and fixed)

R codes

Simple IS-LM framework with simulated data

Equilibrium: The point of intersection of the IS and LM curve is the combination of "I" and "Y" for which both the goods and the money market are in equilibrium.

Effects of fiscal policy (or increase in government expenditure)
An increase in the government spending (fiscal expansion) results in the rightward shift of the IS curve. This happens because the autonomous component of the aggregate demand ("A" in the above derivation) increases. Fiscal policy (with exogenous price level) leads to increase in both output and interest rates.
Effect of fiscal policy
Effect of monetary policy (change in money supply)
An increase in the money supply by the central bank (monetary policy) results in the rightward shift in the LM curve. This happens because of the increase in nominal money supply (MS), and since the prices are exogenous the entire curve shifts to the right. Monetary policy (with exogenous prices) leads to fall in interest rates and rise in the output.
Effect of monetary policy

Mixture of monetary and fiscal policy
Any desired level of output and interest rates can be achieved by using both these policies in tandem, as illustrated below.
Effect of both fiscal and monetary policy

Aggregate demand curve (AD curve)
Now if we introduce prices in the picture we can trace out different combinations of "P"(prices) and "Y" for which both the goods and the money market are in equilibrium. In order to achieve this we have endogenised the computation of prices (P) (by taking ms = (nominal money)/(prices))  This represents an important relationship between aggregate price level and output for the economy. The downward sloping AD curve should not be confused with the demand curve for a good (as in microeconomics). Although both are downward sloping, the reasons for the negative relation between the prices and output demanded are different in the 2 cases. In microeconomics if the price of a good increases less of it is demanded, ceteris paribus. However in the case of AD curve, this negative relation is established by the interplay between the goods and the money market that ensures that markets clear.




This completes the story from the demand side, as to how we arrived at the aggregate demand curve for the economy. Note that we have taken a simplified version of the equations to make our point clear and to make it useful for non-econ students too. We could introduce government taxes to make the equations more realistic (and complicated) but the math looks cleaner this way and anyways the intuition remains the same even after incorporating taxes.

The next thing that we need to do is to arrive at the aggregate supply curve (labor side) for the economy, which we shall take up in the next post. Once we have presented both the ideas of aggregate demand and aggregate supply, we would be in a position to better understand the above discussion about the classical and Keynesian school of thought.

Wednesday, 4 January 2012

Memoization in R : Illustrative example

I came across a nice problem at project euler that gave me sense of satisfaction that was unusual, I think that because I don't usually get the solutions right the first time as I did in this case. Anyhow, I shall try and decode the R codes that I used in simple English language and Mathematics.



Let me first illustrate the brute force method, that is usually the method used by novice coders like myself. The idea here is to find the largest number below 1 million that gives the maximum number of the above mentioned iterations.



So what I have done above is simply performed the iteration for each and every integer from 1 to 1 million and using a counter variable kept a track of which number gave me the largest number of iterations and recorded the corresponding number, which is what we needed in the end. The idea was straight forward the only challenge was to come up with that cute function (which we now see is not that challenging after all).

Well, now that the novice part is done lets get to what Utkarsh (my pro bro) had to say about this. My codes took ~ 701.75 seconds to run (on my Sony vaio VPCCW12EN), this was completely fine by me. Utkarsh shrugged in his usual nonchalant manner at my codes and came up with an awesome algorithm to optimize the above calculation and saving some precious time (which I think he referred to as Memoization). The idea that he worked on was that since in many cases we would already have computed the number of iterations there was no need to keep computing then again. Suppose in the example in the question we see that 13 -> 40 -> 20 -> 10 -> 5 -> 16 -> 8 -> 4 -> 2 -> 1. Now in the computation of 13 if say we already have that 10 will further iterate say 6 times we would not have to go all the way to 1. Similarly even for 10 if we know that 5 further iterates 5 times we don't need to go all the way back till 1. This would be more clear when we take a look at the codes.



The above codes, courtesy Utkarsh, took ~ 50 seconds. As it turns out I was 1,390% inefficient as compared to this optimal algorithm. I would glad to know if there is any other optimization technique (apart from using super computers) that might reduce the computational time, please share if you can find a better way of coding this in R.