Monday, February 23, 2009

An Oscar for Globalization!

Slumdog millionaire swept the Oscars with 8 awards last night. I read a headline on CNN that said - "India erupts in a celebration for Slumdog". But was it an Indian movie? Was it a British movie? Which country did Slumdog belong to? I think the answer is none! As far as I know it was the first truly global movie to win so many oscars. The producers and directors of the movie were British, the story and setting was quintessential Indian and it was an American distributor who showed faith in it before the British distributors touched it. The American audience liked the movie much more before the world noticed it in a big way. In my opinion, globalization of the movies won at the Oscars last night.

Globalization perpetuates itself. Slumdog winning Oscars has an impact on the both sides. First of all rural India who knew A. R. Rehman very well but not necessarily know about the Oscars now knows about the Oscars. I am sure that more number of Indians saw this Oscars on the television than ever before. At the same time Indian musicians, cinema technicians and actors/actresses are getting global recognition. This will increase cooperation between Hollywood and Bollywood and spur projects of a similar nature - having the global setting. Globalization in the movie industry has truly come into light last night. The process of globalization of the movies is only going to accelerate here onwards.

I keep telling the critics of globalization in India who fear that western culture will destroy the Indian culture - Globalization is not one way. As you accept western influences and culture, the West will also be exposed to your culture. I keep telling people here too that what you see now is just the beginning - the beginning of the mixing of cultures, movies, people, languages.... It's in everybody's benefit to succumb to it and go with the flow.

Thursday, January 29, 2009

How to divide an amount into a large number of pieces

If somebody asked you to divide $10 among 3 people, how will you divide the money? 3.33, 3.33 and 3.33? The three amounts do not add back to $10. To accurately (but not necessary fairly) distribute $10 you will have to give $3.34 to one person. Thus when you add $3.33, $3.33 and $3.34 it adds up to $10.

How will you do this programatically in java? We faced this problem few months ago. It wasn't a trivial problem to solve. But Martin Fowler came to our rescue. His Money class does the magic. Let's look at it's divide method:

public Money[] divide(int denominator) {
BigInteger bigDenominator = BigInteger.valueOf(denominator);
Money[] result = new Money[denominator];
BigInteger simpleResult = amount.divide(bigDenominator);
for (int i = 0; i < denominator ; i++) {
result[i] = new Money(simpleResult, currency, true);
}
int remainder = amount.subtract(simpleResult.multiply(bigDenominator)).intValue();
for (int i=0; i < remainder; i++) {
result[i] = result[i].add(new Money(BigInteger.valueOf(1), currency, true));
}
return result;
}
The method above divides the given amount ($10) by a denominator - 3. All it's doing is dividing 10 / 3 and storing the result into an array - {3.33, 3.33, 3.33}. Then it finds out how many pennies are remaining by doing 10 - (3.33 * 3) = 0.01 - that is 1 penny. It starts distributing remaining pennies to the elements of the previously resulted array. As only one penny is remaining we just add this penny to the first element. Thus now we have {3.34, 3.33, 3.33}.

The algorithm ensures the if you add the distributed amount back, you will get the original amount back - 3.34 + 3.33 + 3.33 = 10. But the algorithm hogs memory if your denominator is a very big number. In our case, we had to divide an amount into 12 million pieces. It's very inefficient as it created array of 12 million elements! I changed the method in the following way to make the division memory efficient:


def divideEfficiently(int denominator) {
BigInteger bigDenominator = BigInteger.valueOf(denominator);
BigInteger simpleResult = amount.divide(bigDenominator);

def map = [:]
def dividedMoney = new Money(simpleResult)
for (int i = 0; i < denominator ; i++) {
map[dividedMoney] = map.get(dividedMoney, 0) + 1
}
int remainder = amount.subtract(simpleResult.multiply(bigDenominator)).intValue();
def Money one = new Money(BigInteger.valueOf(1))
def addedMoney = dividedMoney.add(one)
for (int i=0; i < remainder; i++) {
map[dividedMoney] = map.get(dividedMoney) - 1
map[addedMoney] = map.get(addedMoney, 0) + 1
}
return map;
}

The above method is similar but instead of giving an array of 3 elements, it gives a map of two elements [3.34 : 1, 3.33 : 2]. The map indicates that you have two shares of 3.33 and 1 share of 3.33. Your 12 million shares can be simply mentioned by two elements in a map!

Let me explain this with one more example. If you are trying to divide 11 in 13 pieces the original algorithm will give you following array : {0.85, 0.85, 0.85, .85, 0.85, 0.85, 0.85, .85, 0.84, 0.84, 0.84, .84, .84}. You can notice that there are only two unique elements in above array - .85 and .84. Thus the array can be expressed in the following map - [0.85 : 8, 0.84 : 5]. Even if your denominator is 12 million, you will always get two unique elements by Fowler's algorithm and hence the result can always be expressed as a map of two simple elements there by saving huge amount of memory.

Saturday, December 13, 2008

Two lessons US can learn from India

India is by no means close to being a super power. It is still battling poverty, health care and education issues. Its per capita income is still very low - not even close to any developed nation. But there are two areas in which US can learn from India's meteoric rise in the recent past.

First lesson - Education is important. In present India, uneducated people don't get the social status educated people get even if the uneducated people have money. There is nothing to boast about this, but it shows thinking of the society and importance of the education in the society. There is a very good chance that a peon (a messenger or an orderly in an office) commonly found in Mumbai offices has an undergraduate degree. No matter what you do, a rule of thumb in modern India is - you must, at least, have an undergraduate degree. Modern, globalized, knowledge based economies require knowledge workers - educated workers. United States has much better infrastructure for higher education - way better than India. What is lacking here is the will of the people to utilize it. If people did not wake up today, they will be jobless tomorrow and they will be forced to learn this hard way. Fortunately, the president elect understands urgency of the situation.

Another important lesson is living within your means. Americans love to spend and our economy thrives on spenders. Most of the spending is done using borrowed money - credit cards. This is not the case in India. I went to Mumbai in the March of 2008 and I was talking to a home builder about sub prime mortgage problem in United states. "We don't have sub prime problem. Nobody takes loans here to buy houses!" he said. To a large extent this is true. My father bought a condo in Mumbai (at a comparable price in Los Angeles) and he did not borrow a single penny! My father is not considered upper middle class in Mumbai! He saved for this his whole life. I am not saying stop buying houses with loan, but you can at least not join the expensive gym if you can't afford it. You should not buy an expensive car if it's not within your means. People here need to stop spending money on the wants that may not be needs if they can't afford it. They need to learn to distinguish clearly between the wants and the needs. People in India and China have seen worst days before. Their governments do not do enough. India has no unemployment benefits! Because of this, people were forced to discipline themselves! If people here don't learn this now, they will learn this by hard way in coming years. Economic booms and busts will come and go. If financial foundation - savings in the economy are strong, people will be able to withstand these shocks easily.

Thursday, November 27, 2008

It's time to act

Terrorists attacks are not new to India and especially to Mumbai. There have been many bomb blasts in the past, but this time the form of the attack and the targets of the attacks is very unique. Terrorists intend to hurt Indian economy by scaring foreigners. They want Americans and Britishers to stop coming to India, stop investing in India.

What disturbs me as a Mumbaikar (a term commonly used for people of Mumbai like the term 'Angelions' used for the people of Los Angeles) is that the politicians have done nothing to curb terrorism for the past few years - in spite of the frequent terrorist attacks. There is a complete lack of will on part of the politicians to fix this problem. To certain extent, I think there is even lack of understanding of the implications and seriousness of terrorism. For appeasing vote bank, politicians are even thinking of pardoning Afzal guru - the person convicted for 2001 parliament attacks!

Intelligence organizations are not coordinated and virtually non existent. There is no central organization at national level to counter terrorism. Each state has it's own independent police force and even independent intelligence agencies! The Central Bureau of Investigation does not have enough resources and even jurisdiction over investigating such incidents.

There is no clear laid out plan and chain of command when such an incident happens. It's been absolutely chaos in Mumbai and most of the people even don't know who's in charge.

The local police forces are highly under trained, under paid and under weopanized. An average constable on the streets of Mumbai carries a stick instead of a revolver!

I am glad that this attack has drawn world attention. I hope that the politicians will now be forced to bury their differences, come together and create an unified national action plan to curb terrorism. It's high time that Indian politicians learn lessons from countries such as UK, USA and Israel. We have talked tough on many occasions before, it's time to act.

Thursday, November 6, 2008

Monumental, phenomenal, unforgettable, stupendous....

The mood is really upbeat. The air is different. You can clearly feel on the streets of Los Angeles. 2 days ago - on 3rd of November, people around here were cynical, pessimistic and nervous. A day after elections is sunny, air is clear and as John Stuart said in his show - people in New York city are making eye contact on the streets and smiling. Atmosphere here - in Los Angeles - is quite the same. I am amazed by how much difference a leader can make to such a big nation. I don't know about the rural masses, but certainly the urban masses have been energized by the election outcome.

Barack Obama's victory is far more significant than an African American breaking racial barriers. He has emerged as a symbol - a symbol of liberated America, a new vibrant and diverse America. Suddenly Americans are feeling confident again. They feel that they have regained their world leadership.

This victory comes at a right time, when Americans were feeling really low because of the dragging Iraq war and sinking economy. Barack Obama's Hope and Change message was really well taken by the American people. Many of my friends told me - they are voting for a change and not for Barack Obama's specific policies. They wanted America to regain its standing in the world. They felt that Bush's aggressive unilateral policies have brought a shame to America and the world now considers America as an agressive imperialistic power instead of a genuine leader.

He ran his campaign on the premise of uniting people. Republican Party's recent politics was very divisive. He kept saying in the campaign rallies - "There are no red states and blue states - there is only the united states of America". It went very well with the people. Traditionally Republican strongholds such as North Carolina and Virginia proved it that American people does not want politics of divisiveness. They clearly gave a very strong mandate against divisiveness. People of America have shown that they want to embrace the unity in diversity in the rapidly changing demographics.

I hope that Barack Obama will pass the hard test - the test of the time. I hope that the world will be much more peaceful under his leadership. I wish him the best for his success as the 44th president of the United States of America.