AjayShah

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Saturday, 4 December 2010

A more efficient piece of code

Posted on 03:14 by Unknown


CMIE's firm databases use a fine-grained product code to identify
each product. Each firm is also allocated to a product code based on
its predominant activities. I like to reconstruct a coarse
classification out of this that suits my tastes. I do this using
this R function:




cmie.14.industries <- function(s) {
values.8 <- c("Food","Textiles",
"Chemicals","NonMetalMin",
"Metals","Machinery",
"TransportEq","MiscManuf",
"Diversified","Serv.IT")
names(values.8) <- c("01010101", "01010102",
"01010103", "01010104",
"01010105", "01010106",
"01010107", "01010108",
"01010109","01010408")
values.6 <- c("Serv.Construction","Serv.Other",
"Mining","Electricity")
names(values.6) <- c("010106","010104","010102",
"010103")

if (is.na(s)) {return(NA)}

leading8 <- substr(s, 1, 8)
attempt <- values.8[leading8]
if (!is.na(attempt)) {return(attempt)}

leading6 <- substr(s, 1, 6)
attempt <- values.6[leading6]
if (!is.na(attempt)) {return(attempt)}

leading4 <- substr(s, 1, 4)
if (leading4 == "0102") {return("Serv.Finance")}

return("MISTAKE")
}


This maps each firm into one of 14 coarse categories. Here are some
examples of this in action:




> cmie.14.industries("0102090000000000")
"Serv.Finance"
> cmie.14.industries("0101041502000000")
"Serv.Other"
> cmie.14.industries("0101010601010000")
"Machinery"


So in short, the function cmie.14.industries() maps a
string like "0101010601010000" into a set of 14 broad industry names
such as "Machinery".



Faced with a file with roughly 48,000 firm-years, at first blush,
it seems that this function has to be run 48,000 times. For a given
firm, this classification could change over time, so it isn't just a
matter of doing this once for each firm. Here is one simple way to do
it:




badway <- function(task) {
result <- rep("", length(task))
for (i in 1:length(task)) {
result[i] <- cmie.14.industries(task[i])
}
result
}


This is just a loop that runs over everything in the supplied
vector and calls cmie.14.industries() for each
element. The only concession to efficiency is that the empty vector
`result' is allocated ahead of time.



This proves to be quite slow. None of the standard R vectorisation
ideas offer much relief.



The key idea for obtaining a leap in performance was that while I
had to run through 48,000 firm-years, the industry codes actually
attain only a modest list of possibilities. This makes possible a
table lookup:




goodway <- function(task) {
possibilities <- unique(task)
values <- rep("", length(possibilities))
for (i in 1:length(possibilities)) {
values[i] <- cmie.14.industries(possibilities[i])
}
names(values) <- possibilities
values[task]
}


For a problem of size 1000, this works out to be 13.5 times
faster:




> load("task.rda")
> length(task)
[1] 1000
> system.time(res1 <- badway(task))
user system elapsed
0.030 0.000 0.031
> system.time(res2 <- goodway(task))
user system elapsed
0.002 0.000 0.002


This is just a demo with a 1000-sized task. In my production
situation, the performance difference is even greater,
since badway() calls cmie.14.industries()
48,000 times while goodway() only calls it a few hundred
times.




Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Posted in information technology | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • A sea change in the knowledge of the young in India
    In 1887, roughly 14 million children were born in India, and we got one Ramanujan. It seems reasonable to think that there were 9 others who...
  • The glacial pace of change: QFI edition
    In the Percy Mistry report , there are some striking examples of the inability of the Indian policy process to deliver change at a reasonabl...
  • 11th Conference of the Macro/Finance Group
    All the materials are up on the website.
  • The two escape routes away from domestic formal-sector finance
    Three problems afflict formal-sector finance in India today: capital controls, taxation, and financial policy. The most important financial ...
  • A season for bad ideas
    One feature of each period of turbulence is that we get an upsurge of out of the box thinking. While it is always good to think out of the b...
  • The disaster at Maruti
    The news from Maruti is disgusting . I have been curiously watching  how the stock market takes it in : That Maruti has serious labour prob...
  • Legal process in rule-making: A success story in an unexpected place
    by Shubho Roy and Deepaloke Chatterjee. Rule-making process: A critical component of the rule of law A despotic king has absolute power. Wh...
  • Did the Indian capital controls work as a tool of macroeconomic policy?
    Ila Patnaik and I wrote a paper titled  Did the Indian capital controls work as a tool of macroeconomic policy? The abstract of this paper r...
  • The problems of the economics profession
    Ronald Coase has an interesting new piece titled Saving economics from the economics profession . You may like to see What is wrong with Eco...
  • Interesting readings
    Credit Suisse has got a Permission to open One Branch in India. [ also see ] Some good news on IIP measurement in the offing....

Categories

  • announcements (53)
  • author: Harsh Vardhan (5)
  • author: Jeetendra (3)
  • author: Percy Mistry (3)
  • author: Pratik Datta (6)
  • author: Shubho Roy (12)
  • author: Suyash Rai (6)
  • author: Viral Shah (7)
  • banking (26)
  • Bombay (15)
  • bond market (11)
  • business cycle (20)
  • capital controls (39)
  • China (21)
  • commodity futures (3)
  • competition (20)
  • consumer protection (3)
  • credit market (10)
  • currency regime (45)
  • democracy (37)
  • derivatives (31)
  • education (8)
  • education (elementary) (11)
  • education (higher) (10)
  • empirical finance (4)
  • energy (6)
  • entrepreneurship (9)
  • environment (1)
  • equity (15)
  • ethics (23)
  • farmer suicide (1)
  • finance (innovation) (11)
  • financial firms (23)
  • financial market liquidity (25)
  • financial sector policy (90)
  • GDP growth (37)
  • geography (3)
  • global macro (19)
  • global warming (1)
  • health policy (1)
  • hedge funds (1)
  • history (19)
  • IMF (2)
  • incentives (9)
  • inflation (33)
  • informal sector (14)
  • information technology (34)
  • infrastructure (14)
  • international financial centre (18)
  • international relations (8)
  • labour market (17)
  • legal system (67)
  • market failure (1)
  • media (6)
  • migration (6)
  • monetary policy (46)
  • mores (5)
  • national security (1)
  • offtopic (2)
  • outbound FDI (3)
  • payments (9)
  • pension reforms (8)
  • police (3)
  • policy process (64)
  • politics (12)
  • privatisation (7)
  • prudential regulation (1)
  • PSU banks (7)
  • public administration (6)
  • public goods (26)
  • publicfinance (expenditure) (19)
  • publicfinance (tax (GST)) (9)
  • publicfinance (tax) (14)
  • publicfinance.deficit (8)
  • publicfinance.expenditure.transfers (10)
  • real estate (5)
  • redistribution (10)
  • regulatory governance (2)
  • reserves (3)
  • resolution (2)
  • risk management (3)
  • securities regulation (25)
  • socialism (33)
  • statistical system (31)
  • success (5)
  • systemic risk (3)
  • telecom (12)
  • the firm (22)
  • trade (21)
  • urban reforms (9)
  • volatility (3)
  • World Bank (4)
  • world of ideas (16)

Blog Archive

  • ►  2013 (81)
    • ►  September (6)
    • ►  August (12)
    • ►  July (10)
    • ►  June (18)
    • ►  May (7)
    • ►  April (13)
    • ►  March (6)
    • ►  February (3)
    • ►  January (6)
  • ►  2012 (102)
    • ►  December (7)
    • ►  November (10)
    • ►  October (11)
    • ►  September (7)
    • ►  August (5)
    • ►  July (10)
    • ►  June (11)
    • ►  May (7)
    • ►  April (8)
    • ►  March (6)
    • ►  February (8)
    • ►  January (12)
  • ►  2011 (112)
    • ►  December (8)
    • ►  November (10)
    • ►  October (10)
    • ►  September (8)
    • ►  August (4)
    • ►  July (4)
    • ►  June (13)
    • ►  May (9)
    • ►  April (9)
    • ►  March (8)
    • ►  February (18)
    • ►  January (11)
  • ▼  2010 (131)
    • ▼  December (11)
      • Interesting readings
      • Discussions on 'Mythbusting: Current account defic...
      • Mythbusting: Current account deficit edition
      • Talk on US financial regulatory reform, by Viral A...
      • Appropriate regulatory structure for development
      • Emerging markets finance conference at IGIDR
      • A puzzling data revision
      • Interesting readings
      • A club of 19
      • Alternative stock market indexes
      • A more efficient piece of code
    • ►  November (6)
    • ►  October (10)
    • ►  September (7)
    • ►  August (17)
    • ►  July (8)
    • ►  June (5)
    • ►  May (13)
    • ►  April (12)
    • ►  March (20)
    • ►  February (10)
    • ►  January (12)
  • ►  2009 (74)
    • ►  December (11)
    • ►  November (13)
    • ►  October (14)
    • ►  September (11)
    • ►  August (25)
Powered by Blogger.

About Me

Unknown
View my complete profile