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.