Wednesday 22 February 2017

Demonetisation : Benefits and Costs associated with it

On 8th November, 2016 at the stroke of midnight, the government of India demonetised the two largest denomination currency notes in circulation i.e. notes of rupees 1000 and rupees 500. By this step, 86% of the currency in circulation ceased to be a legal tender except for some purposes like payment in hospitals. These notes were to be deposited in the banks by 30 December, 2016. Moreover, restrictions were placed on the cash withdrawals from banks and ATMs.


Aims of this action stated by the government

  1. Curbing corruption
  2. Curbing counterfeiting
  3. Curbing the use of high denomination notes for terrorist activities
  4. To recover black money
When the government realised that it was somewhat slow on the above objectives, its changed the narrative of demonetisation as a step towards cashless economy.

Benefits of demonetisation

  • Tax on Black money
    • Those who has black money had the following choices :
      1. declare their unaccounted wealth and pay taxes at a penalty rate 
      2. continue to hide it, not converting their old notes and thereby suffering a tax rate of 100% 
      3. launder their black money by paying a cost for converting the money into white 
    • As per the Economic Survey, there was active money laundering. But in all the above choices, black money hoarders did suffer. Moreover, bank accounts are still being screened for suspicious transactions, which means that those who engaged in laundering run the risk of punitive taxes and prosecution, in addition to the fees or taxes already paid.
    • The demonetised currency that has not returned decreases the liability of RBI and hence increases its net worth. In this sense, demonetisation has effected a transfer of wealth from holders of illicit black money to the public sector.
  • Move towards cashless economy
    • With large denomination removed, the public began to shift towards digital payment technologies. It increased digital literacy.
    • It would also improve tax compliance since most of the black money is generated because of cash transactions. As per Watal Committee, cash payments account for about 78% of all consumer payments in India.
    • It brought people into the formal economy, thereby increasing financial saving and reducing tax evasion.
    • The government has taken some steps to facilitate digital transactions like launch of BHIM(Bharat Interface for Money) app which has enabled inter-operability of digital transactions and launch of Aadhaar Merchant Pay which allows anyone with just and Aadhaar number and a bank account to make merchant payment using his biometric identification. Government has also reduced Merchant Discount Rate (MDR).

  • Brought money into the formal financial system
    • The money in the form of cash which was lying idle was brought into the financial system. This money can be lent by banks to build the economy or infrastructure.
    • But it needs to be seen if this money is withdrawn once the limits on cash withdrawal are removed. As per RBI, most of this money would be withdrawn once the limits on cash withdrawal are removed.
  • Real Estate
    • Most of the black money in invested in real estate.
    • Real estate prices have fallen since demonetisation. It would lead to affordable housing.

Costs of demonetisation

  • Impact on GDP

    • Since a lot of transactions in India happen in cash, demonetisation affected the small scale industries and informal sector.
    • As workers in the informal sector have been laid off, they have bought fewer goods and hence it has affected formal sector has well.
    • As per the economic survey, economic activity has been affected adversely but temporarily by demonetisation. Therefore real GDP growth in 2017-18 is expected to be in the range of 6.75 - 7.5  percent range.
  • Inconvenience to public
    • There were reports of huge queues outside banks and ATMs.
    • There were cash shortages. People had problem in running their businesses and day to day functions.
  • Costs of printing new notes and recalibrating the ATMs for new notes
  • Loss of bank man power to assist in remonetizing the economy
  • Cost of sterilising the surge in liquidity though Market Stabilisation Scheme.



Costs associated with demonetisation are short term, but it may have long term benefits. Emphasis should be on maximising the benefits and reducing the costs. The windfall gains from unreturned currency should be used in capital expenditures rather than revenue expenditures. The push provided to digitisation must continue.

Monday 13 February 2017

Whether time is ripe for Universal Basic Income in India

Universal Basic Income (UBI) is a paradigm shift in thinking about social justice and social welfare. UBI provides a minimum basic income to all. UBI, by its very name ought to be unconditional and universal. It requires that every person should have a right to a basic income to cover their needs, just by virtue of being citizens. 

Even after 70 years of independence, India still has 22% of its population below the poverty line as per Tendulkar poverty line. The basic premise for UBI is that it helps to remove poverty due to unconditional cash transfer, plug leakages due to direct benefit transfer, fix inclusion exclusion errors due to universality and lead to women empowerment by transferring amount in the name of women of the household. Our current social sector schemes like MGNREGA, Food Security Act, Mid Day Meal scheme, Public Distribution System(PDS), fertiliser subsidies suffer from leakages and identification of beneficiaries.


But is UBI coming at the cost of these social sector schemes ? If yes, then I am skeptical about this scheme. Though there are issues in these schemes, UBI cannot replace these schemes. For example, take the case of Mid Day Meal scheme. This scheme has helped us to achieve near universal enrolment and improved child nutrition. Also, if money is directly transferred to the beneficiaries, what is the guarantee that poor people would send their children to schools. Similarly, if PDS is abolished, it can lead to transferred money being spent on alcohol or gambling. Moreover, free rewards can be seeing as harming responsibility and effort.
Second question is whether UBI is feasible ? UBI would have huge costs to the exchequer if it is implemented universally. If you are transferring just Rupees ten thousand per yer, it won’t be of much use to the poor. But given the huge population, even this amount would be difficult to give. On the other hand, if we providing UBI scheme to only a proportion of population, then it is not universal. In that case, it would have problems of beneficiary identification and targeting.
UBI should actually provide minimum basic wage. But the “actual” minimum wage in India is Rs 4,800 per month. Could we expect at least this amount from UBI ? The answer is no. There seems to be a broad consensus around the Tendulkar Committee poverty line, which works out to be around Rs 12,000 - 15,000 per year. But even this modest figure is estimated to cost around 11–12% of GDP, whereas all the current subsidies put together cost around 4–4.5% of GDP. This indicates the non-feasibility of UBI in India, at least in the universal sense.
So, yes I want UBI, but not at the cost of existing social sector schemes at the present moment. Due to out huge population and current fiscal conditions and hence low amount of UBI, UBI can only supplement the income of India’s population which would increase consumption and hence stimulate the economy. 

As a start, we can offer UBI as a choice to beneficiaries to existing schemes. This way, it would have less fiscal costs. It gives people not only greater choice, but also greater negotiating power with the administrators who are currently supposed to be giving them benefits. For example, in case of PDS, a dealer knows if he siphons off ration, he faces the threat of exit from the beneficiaries, who would switch to UBI. Designed in this way, UBI could improve the implementation of existing schemes. The only issue is that this would be administratively cumbersome. 

Another way to start off is that to provide UBI initially to some targeted groups like women or widows or old people etc. These are easily identifiable. 

 So, time is not yet ripe to have UBI as a replacement to the existing social sector schemes. But given the fiscal costs of addition of UBI, we can't approach it universally. To overcome this challenge, we can provide UBI as a choice to existing schemes or have it targeted to some easily identifiable groups of individuals initially. UBI should be approached in a phased manner.

Saturday 14 June 2014

Just In Time (JIT) Compilation in JVM

The Just In Time (JIT) compilation in Java is essentially a process that improves the performance of Java applications at run time. JIT compiler is a component of Java Runtime Environment (JRE) and is enabled by default. JIT is a type of dynamic compilation whereas javac is static compilation. With static compilers, the input code is interpreted once. Unless you make changes to your original source code and recompile the code, the output would result in the same outcome. A dynamic compiler, on the other hand, translates one language to another dynamically, meaning that it happens while program is being executed. Dynamic compilers can do a lot of optimizations at run time which can't be done by a static compiler.

Java source files are compiled by the Java compiler into platform independent bytecode or Java class files. When we start a Java application, the JVM loads the compiled class files at run time and executes the program via Java interpreter which reads the byte code in the loaded class files. Due to this interpretation, Java application performs slowly than a native application. The JIT compiler helps improve the performance of Java applications by compiling byte codes into native machine code at run time. The JIT compiler analyses the application method calls and compiles the byte code into native, more efficient machine code.

In practice, the methods are not compiled the first time they are called. For each method, JVM maintains a call count, which is incremented every time a method is called. JVM would keep on interpreting a method until its call count exceeds a JIT compilation threshold. Therefore, methods which are used often are compiled soon after the JVM is started, and less used methods are compiled much later or not at all. After a method is compiled, the JMV resets its call count to zero. Subsequent method calls keep on incrementing the call count. If it reaches JIT compilation threshold for the second time, the JIT compiler compiles the method for a second time, but this time it applies much more optimizations. This process is repeated until maximum optimization threshold is reached. This is the reason, why Java applications are warmed up before benchmarking. This also explains why Java application have long start up time.

Here are some of the optimizations done by JIT in Hotspot JVM :
  1. Instead of calling methods on an instance of an object, it copies the method to the caller code. This is called inlining. The hot methods (methods used more often) should be located as close to the caller as possible to prevent any overhead.
  2. Delay memory writes for non-volatile variables.
  3. Replace interface with direct method calls for method implemented only once to eliminate calling of virtual function overhead.
  4. Eliminate dead code. Dead code is a code in the source code which is executed, but whose result is never used in any other computation.
  5. Rearrange the code. JIT analyzes the flow of control within a method and rearrange code paths to improve the efficiency.


The HotSpot JVM currently supports two JIT compilers : Client and Server. The HotSpot VM's Client JIT compiler targets applications desiring rapid startup time and quick compilation so as not to introduce jitter in responsiveness such as client GUI applications. The HotSpot VM Server JIT compiler targets peak performance and high throughput for Java applications, so its design tends to focus on using the most powerful optimizations it can. This often means that compiles can require much more space or time than an equivalent compile by the Client JIT complier. It tends to aggressively inline as well, which often leads to large methods, and larger methods take longer to compile. It also has an extensive set of optimizations covering a large number of corner cases, which is needed to generate optimal code for any bytecodes it might see.

Saturday 3 May 2014

Largest Rectangular Area in a Histogram

Problem :

Find the largest rectangular area possible in a given histogram where the largest rectangle can be made of a number of contiguous bars. All bars have same width and the width is 1 unit.
For example, consider the following histogram with 7 bars of heights {6, 2, 5, 4, 5, 2, 6}. The largest possible rectangle possible is 12 (see the below figure, the max area rectangle is highlighted in red)

histogram

Solution:

A straightforward answer is to go for each bar in the histogram and find the maximum possible area in histogram for it. Finally find the maximum of these values. This will require O(n^2) time. We would solve this problem in O(n) time complexity.

There are a few invariants that we can use for this problem :

  1. If we include bar i, maximum possible height of rectangle including that bar will be h(i), height of that bar.
  2. If we include bar i, maximum possible width of rectangle including that bar will be L + R + 1, where :
    1. L is the number of adjacent bars to the left of ith bar and height greater than or equal to h(i)
    2. R is the number of adjacent bars to the right of ith bar and height greater than or equal to h(i)

Now our task remains as follows:
1) Get Li
2) Get Ri
3) Get area of rectangle for bar i : A(i) = h(i) * (Li + Ri + 1)
4) Fid maximum value of A(i)

Here is the Java code :

Saturday 19 April 2014

Fail-Safe Vs Fail-Fast iterators in Java

Fail-Fast iterators :

Fail-fast iterators fail as soon as they realize that the structure of the collection has changed since the iteration has begun. By changing the structure of the collection, we mean adding, removing or updating an element in the collection. It is implemented by keeping a modification count and if the iterating thread realizes the change in modification count, it throws ConcurrentModificationException. Most of the collections in Java are fail-fast. This is not a guaranteed behavior. So, we should not rely on this behavior.

We can get ConcurrentModificationException if one thread is iterating over a collection and at the same time that collection is modified by some other thread. Please note that we can get ConcurrentModificationException even in single threaded applications. The following code snippet would throw ConcurrentModificationException :
Whereas, the following code snippet won't throw ConcurrentModificationException :

So, if we use Iterator's remove method, we won't get ConcurrentModificationException. To understand why the second approach doesn't throw ConcurrentModificationException, we need to understand how fail-fast iterators are implemented in Java. The fail-fast iterators are typically implemented using a volatile counter on the list object. When the list is updated, the counter is updated. When an iterator is created, the current value of the counter is embedded in the Iterator object. When an iterator operation is performed, the iterator method compares the list counter with the Iterator counter, and throws ConcurrentModificationException if they differ. That explains why the first code snippet above throws ConcurentModificationException. On the contrary, if we use Iterator's remove method (second code snippet above), the Iterator counter is also decremented. So, in the next iteration, the counter check succeeds. Hence, the second code snippet above doesn't throw ConcurrentModificationException.

Fail-Safe iterators :

Contrary to fail-fast iterators, fail safe iterators don't throw ConcurrentModificationException. This is because they work on clone of collection rather than the original collection. Fail-safe iterators typically operate over a snapshot and allow concurrent modification. But the fail fast iterators may not reflect the updates on collection after the iterator was created. What this implies is that Fail-Fast iterator is guaranteed to list elements as they existed upon iterator construction, and may reflect any modification subsequent to construction of the iterator(without guarantee).  Iterator of CopyOnWriteArrayList and iterators of java.util.concurrent package was fail-fast iterators. Since fail-safe iterators work on cloned objects, they have extra memory overhead.

Saturday 15 March 2014

Java 8 : Difference between List.sort and Collections.sort

One of the most anticipated programming language updates in many years is here. Java 8 would be officially released on March 18, 2014. It offers a lot of new features which are worth exploring. One of them that we would be discussing here is List.sort. The addition of List.sort(Comparator) in Java 8 is fantastic. List.sort unsurprisingly allows you to sort a list, but the neat thing about List.sort is that for most of the common list implementations (such as ArrayList), it would sort much more quickly than Collections.sort. To understand this, first we need to know how Collections.sort works internally.

Collections.sort is a stable sort which means that equal elements won't be reordered after sorting. So, quicksort and heapsort are out of question since they are not stable. The most efficient stable sorting algorithm is merge sort. So, Java internally uses a modified version of merge sort. The exact algorithm was derived from Tim Sort which was developed by Tim Peter for Python. Tim sort is a combination of insertion sort and merge sort. If the number of elements is less than a certain value, insertion sort is used. This is because in case of smaller lists, the overhead of making recursive calls in case of merge sort is greater than the performance benefit obtained by using merge sort. Hence, it makes sense to use insertion sort for smaller lists.

Collections.sort implementation first dumps the specified list into a primitive array. Then it sorts this primitive array and puts back the sorted array elements back into the list in sorted order. Now the question is why do we need to copy the list into a new primitive array ? This is because Collections can be backed up by a primitive array (as in ArrayList) or can be backed up by a linked list (as in LinkedList). Copying into a new primitive array is done to avoid n^2 * log(n) performance that would result if we try to sort linked list in place.

But if we see carefully, this copying into a new primitive array is not required in case of ArrayList and some other list implementations since these list implementations are already backed up by a primitive array. This is exactly what List.sort takes advantage of. The default implementation of List.sort still does what Collections.sort does, but concrete implementing classes are free to optimize. For example, ArrayList.sort invokes Arrays.sort on the ArrayList's internal array. Hence the overhead of allocating and deallocating a new primitive array is gone. Also overhead of copying back the sorted elements from the array is gone. This is a big performance gain. Needless to say, we are saving space as well since we are not allocating space for a new primitive array.

Performance isn't the only potential gain from these new methods. Sorting a Collections.synchronizedList is an atomic operation using List.sort. You can iterate over all the elements of a List as an atomic operation using List.forEach. This was not possible in Java 7.

Saturday 8 March 2014

Bonds

A bond is a fixed income security i.e. the return from a bond is known when it is issued. The essential difference between a bond and a stock can be summed up in one phrase : "Debt Vs Equity". That is, bonds represent debt and stocks represent equity ownership. This difference brings us to the first main advantage of bonds : In general, investing in bonds is safer than investing in stocks. The reason for this is because of the priority that debt holders have over share holders. But not all bonds are safe. There are also very risky bonds which are known as junk bonds. If a company goes bankrupt, debt holders are ahead of shareholders in the line to be paid.

Bond Issuance :

Bonds are issued by public authorities, credit institutions, governments and companies in the primary market. The bonds are mostly issued through a process called underwriting. On the other hand, government bonds are mostly issued in an auction. After the issue, the bonds can be traded in the secondary market just like other instruments.

Important Bond terms:

Principal value or Par value or face value :

This is the amount on which the issuer pays the interest. In most of the bonds, this amount has to be paid at the maturity of the bond. In some bonds, the amount which is paid at maturity is different from the principal amount.

Maturity :

This the date on which the bond expires. The issuer of the bond has to pay back the nominal amount to the holder of the bond on this date. Most of the bonds have term upto 10 to 30 years. But there are bonds with terms upto 100 years. And there are bonds with terms upto 1 year.

Coupon :

This is the interest rate that the issuer of the bond pays to the holder of the bond. Usually it is fixed, but it can also be variable.

Yield:

Yield is the rate of interest received from investing in the bond. It is one of the most important factors that investors care about while investing in bonds. We would talk more about it later.

Credit Rating:

This is the rating given by an agency which tells us the probability that the bond issuer would pay back the promised amount at maturity.

Some major types of bonds:

1) Fixed rate bonds : As the name suggests the coupon rate of these bonds is fixed.
2) Floating rate bonds :  The coupon rate of these bonds is not fixed.
3) Zero coupon bonds : These bonds don't pay any coupon.
4) High yield bonds :  These are also known as junk bonds. These bonds are usually start up companies which need capital to expand but don't have good credit rating. So, to lure the investors these companies issue high-yield bonds which have very high coupon rate. But this comes at the cost of risk associated with these bonds.
5) Convertible bonds : These types of bonds allows the owner of a bond to exchange it for some specific number of shares.

Bond Valuation:

Bond valuation refers to valuing the present value of a bond. The fundamental principle of bond valuation is that the bond's value is equal to the present value of its expected (future) cash flows. While valuing bonds, we consider time value of money concepts. Let's see how to calculate the price of a bond. First we need to compute the present value (PV) of the bond's future cash flows.The PV is the amount that would have to be invested today to generate that future cash flow. To find the value or the price of a bond, the PV of each individual cash flow should be added.

PV at time T = (Expected cash flow in period T) /( (1 + I) to the power T)
where I is the discount rate.

Value of bond = PV @ T1 + PV @ T2 +.........+ PV @ Tn

How does the value of a bond change:

As we can see, discount rate is the only variable factor in bond price. Rest everything is fixed.

  • As the rate increases of decreases, the discount rate that is used also changes. Higher the discount rate, the lower the value of a bond; the lower the discount rate, the higher the value of the bond. 
  • If the discount rate is higher than the coupon rate, the PV will be less than par. If the discount rate is lower than the coupon rate, the PV will be higher than par value.
  • As the bond moves closer to its maturity, it's price will closer to par. At par, market price is equal to par value and yield to maturity is equal to the coupon rate. This is clear since the expected rate of return in this case would be equal to the coupon rate. When a bond is selling at a discount, it's market price is less than the par value. So, it's yield to maturity is greater than the coupon rate. On the other hand, if a bond is selling at a premium, it's market price is greater than it's par value. So, it's yield to maturity is less than the coupon rate.
  • If a bond's market prices increases, then it's yield must increase; conversely if a bond's market price decreases, then it's yield must increase.
  • If a bond's yield does not change over it's life, then the size of it's discount or premium will decrease at an increasing rate as it's life gets shorter.