Saturday, November 10, 2012

Bootstraping Scripts

Now that we have wrapped up Chapter 4 in class, we're all required to perform bootstrap simulations for the problem set. Econometrics is a class that requires a lot of commitment, not only on grasping the materials and going through the painful algebra, but also in terms of learning how to programme in R.

In the spirit of the last few posts, here then, is the script of loops that I had written to complete the bootstrap simulations, both parametric and non-parametric.

P/S: To anyone who is following this blog faithfully, I swear I will be get back to writing when Winter break begins.

#Simulating DGP under the Null
lmnull<-lm(cs$V2~cs$V3)
#Use the estimate under the null to begin bootstrap.  

Loops for Parametric Bootstrapping

#For Bootstrap Size 99
BST99<-numeric(0)
for(i in 1:99){
       simdtb<-numeric(0)
simdtb<-rnorm(50,mean=0,sd=0.1384)
ybs<-numeric(0)
ybs<-(5.3426+(1.3234*cs$V3)+simdtb)
bsolsi<-lm(ybs~cs$V3+cs$V4)
B3simT<-summary(bsolsi)$coefficients["cs$V4","t value"]
BST99[i]<-B3simT}
#Notice that I applied the absolute function here. We are doing a 2-tailed test,and thus it is important that we apply the absolute operator on the entire
vector. We'd want extreme values that are too large or too small, and then be able to discriminate statistically. 
BST99<-abs(BST99)
#To get R to calculate the frequency of X greater or equal to a specified values while implicitly using a logical argument.A very useful trick.
PBST99<-mean(BST99>=2.349)

#For Bootstrap Size 999
BST999<-numeric(0)
for(i in 1:999){
       simdtb<-numeric(0)
simdtb<-rnorm(50,mean=0,sd=0.1384)
ybs<-numeric(0)
ybs<-(5.3426+(1.3234*cs$V3)+simdtb)
bsolsi<-lm(ybs~cs$V3+cs$V4)
B3simT<-summary(bsolsi)$coefficients["cs$V4","t value"]
BST999[i]<-B3simT}
BST999<-abs(BST999)
PBST999<-mean(BST999>=2.349)

#For Bootstrap Size 9999
BST9999<-numeric(0)
for(i in 1:9999){
       simdtb<-numeric(0)
simdtb<-rnorm(50,mean=0,sd=0.1384)
ybs<-numeric(0)
ybs<-(5.3426+(1.3234*cs$V3)+simdtb)
bsolsi<-lm(ybs~cs$V3+cs$V4)
B3simT<-summary(bsolsi)$coefficients["cs$V4","t value"]
BST9999[i]<-B3simT}
BST9999<-abs(BST9999)
PBST9999<-mean(BST9999>=2.349)



Loops for Non-Parametric Bootstrapping 
# Extracting residuals from the OLS regression under the null, and then scaling them by (n/n-k)^(1/2) where k is the number of restrictions under the null.
nullres<-residuals(lmnull)
scalednullres<-(sqrt(50/48))*nullres
#For Non-Parametric Bootstrap Size 99
BSTNP99<-numeric(0)
for(i in 1:99){
simds<-numeric(0)
simds<-sample(scalednullres,50,replace=TRUE)
ynpbs<-numeric(0)
ynpbs<-(5.3426+(1.3234*cs$V3)+simds)
bolsnpi<-lm(ynpbs~cs$V3+cs$V4)
B3NPSimT<-summary(bolsnpi)$coefficients["cs$V4","t value"]
BSTNP99[i]<-B3NPSimT}
BSTNP99<-abs(BSTNP99)
PBSTNP99<-mean(BSTNP99>=2.349)

#For Non-Parametric Bootstrap Size 999
BSTNP999<-numeric(0)
for(i in 1:999){
simds<-numeric(0)
simds<-sample(scalednullres,50,replace=TRUE)
ynpbs<-numeric(0)
ynpbs<-(5.3426+(1.3234*cs$V3)+simds)
bolsnpi<-lm(ynpbs~cs$V3+cs$V4)
B3NPSimT<-summary(bolsnpi)$coefficients["cs$V4","t value"]
BSTNP999[i]<-B3NPSimT}
BSTNP999<-abs(BSTNP999)
PBSTNP999<-mean(BSTNP999>=2.349)

#For Non-Parametric Bootstrap Size 9999
BSTNP9999<-numeric(0)
for(i in 1:9999){
simds<-numeric(0)
simds<-sample(scalednullres,50,replace=TRUE)
ynpbs<-numeric(0)
ynpbs<-(5.3426+(1.3234*cs$V3)+simds)
bolsnpi<-lm(ynpbs~cs$V3+cs$V4)
B3NPSimT<-summary(bolsnpi)$coefficients["cs$V4","t value"]
BSTNP9999[i]<-B3NPSimT}
BSTNP9999<-abs(BSTNP9999)
PBSTNP9999<-mean(BSTNP9999>=2.349)

Wednesday, October 24, 2012

Now that was an embarassment, loops are very inefficient

R's learning curve can be very steep, and thankfully I am surrounded by intelligent and smart colleagues. The point is that the loop script in the last post is embarrassingly inefficient, and will work horribly if one intends to simulate a large sample.

Here's a much more elegant solution, for 3.1 of EMT,but this time using the function argument in R.The script is provided by Evan Jo.


ARsample<-function(beta1=1,beta2=.8,n=25,y0=0){
y<-vector(length=n+1);y[1]<-y0
for (i in 2:n+1){
y[i]<-beta1+beta2*y[i-1]+rnorm(1)}

m<-embed(y,2);return(data.frame(y=m[,1],y1=m[,2]))};

meanbeta<-function(samplesize=25,n=100){
m<-data.frame(beta1=NA,beta2=NA);
for(i in 1:n){m[i,]<-coef(lm(y~y1,data=ARsample(n=samplesize)))}
return(colMeans(m));}

In particular look at how efficient this script is compared to the previous one.

And finally, this is why the honours stream is so much more intellectually rewarding to be in.

PS: Don't worry, I will still be writing about economics, and hopefully when Quantdary takes off we'll start having more interesting economics/finance related posts.

Saturday, October 20, 2012

Simulation Script for ETM Question 3.1(Public Service)

I promised I'll update my blog, but I have been really really busy. Anyway, this post is not about the KLCI as promised(sorry to disappoint,I hardly had time for myself either), but rather, it is a loop for Q3.1 in Davidson and McKinnon's Econometric Theory and Methods. For all the poor souls who have just started learning R.




B1<-1
B2<-0.8
y0<-0
B1hat<-numeric(0)
B2hat<-numeric(0)

for(j in 1:100){
residualsj<-numeric(0)
residualsj<-rnorm(200,0,1)
ysimj<-y0
for(i in 1:200){
ynew<-B1+B2*ysimj[i]+residualsj[i]
ysimj<-c(ysimj,ynew)
}
OLSj<-lm(ysimj[2:201]~ysimj[1:200])
beta1j<-coefficients(OLSj)[1]
beta2j<-coefficients(OLSj)[2]
B1hat[j]<-beta1j
B2hat[j]<-beta2j
}


Time permitting I'll write an explanatory note with the #, although I hardly doubt it with midterms and a lot of other deadlines vying for whatever precious time I have. To change the sample size just change the numbers in the "i" part of the loop. To change the number of runs, just change the numbers in the j part of the loop.

Update:I made some errors in the previous post and those have since be corrected. Note that this script is meant to simulate an OLS drawing from 200 observations 100 times, and feel free to tweak around to adjust the trials and sample size.

This should also make it easier for you to attempt the last 2 questions. For information on how to extract vectors out from a data frame and then transforming it, I'd refer you to Mirza Trokic's website.
http://www.mirzatrokic.ca/aes.html

He has the most concise and helpful R guide that I have ever seen.


Sunday, April 15, 2012

Orszag on the secondary effects of Food Stamp programmes

Readers of this blog may be familiar with my affinity for food stamp programmes. Not only have I argued in a previous post that this is the holy grail of automatic stabalisers, but I also feel it is a social safety net with incontrovertible utility.

Peter Orszag (former chairman of the Office of Management and Budget for President Obama) recently wrote an opinion piece for Bloomberg about the secondary effects of Food Stamp programmes on students.

As a student of econometrics and behavioural economics I am inspired by the work that has been done to quantify the impacts of such programmes.

P/S: Due to the rigour of the my academic programme I have not updated my blog as frequently as I had hoped. As a sneak peek, the next post has to do with the KLCI Index, so Malaysian readers interested in equities might find it interesting.

Tuesday, October 4, 2011

SNED Stock Market Challenge:Beat the Market

So a little brain child of my fellow executives and I will be on full swing at McGill soon.

Here's the link.

Basically this works like a fantasy soccer/hockey/football game, the top 3 player with the highest portfolio value at the end of the period will walk away with the money from the pool.

50% of the registeration fee will be diverted to support TamTams Africa and finance SNED's microfinance portfolio while the other 50% will be directed to the prize pool.

Saturday, July 30, 2011

"Stamping" inflation out of national woes

The Star reports that the government is considering a food stamp programme to help alleviate the burdens of Malaysians in accommodating inflation. In a nice coincidence I was drafting this post a few days back, so in principle this is a programme that I endorse strongly.

The gradual phasing-out of subsidy programmes announced by the government has become a national woe, leading many to view the adjustment of commodities in the domestic market to world prices a painful pill to swallow in the short term. In all fairness the long term benefits that accrue from subsidy removal programmes
are huge. However that doesn't mean that we should act as strict utilitarianism and ask those who are harmed the most by this transition to sacrifice for the greater good. As income is not catching up with the removal of subsidies and the general rise of the price level, most Malaysians are feeling the pinch as their purchasing power declines.

As an aside,there is a link between net disposable income (wages,capital gains etc minus taxes and added with subsidy/allowance/benefits), the price level and purchasing power. It follows logically from
purchasing power = net domestic income/price level that we get:
Change in purchasing power = change in net disposable income/change in price level

Why is a food stamp programme a good idea?
Perhaps if one were to ask Bill Clinton this question he might answer it all depends on what you mean by "good."So let it be noted that the main criteria behind my support for the programme are 1) the efficacy of the food stamps to sustain the purchasing power of affected groups as Malaysia adjusts to the world price for hitherto subsidised commodities, 2) the potential role of the programme as a stabaliser, and 3)the empirical backing for the multiplier effect of the programme.

  1. Efficacy of food stamps to sustain purchasing power
    The efficacy of food stamps to sustain purchasing power should not be evaluated chiefly by how much it maintains purchasing power by increasing net disposable income to offset inflation. Purchasing power ought to be sustained and/or increased with a combination of nominal wage adjustments,tax effects, inflation management in addition to entitlement programmes like food stamp programmes and unemployment insurance. Furthermore the case for the efficacy of food stamps is consolidated in the sense that it achieves the objective of protecting the purchasing powers of citizens without introducing the price distortions, resource misallocation and deadweight loss to the economy introduced by subsidy programmes. In a related vein, my objection to the blanket-subsidy programme in place is that it applies to people for all levels of income. By subsidising say, the fuel of gas-guzzling cars we are essentially imposing an implicit penalty on people who do not own expensive gas-guzzlers. In essence this is a regressive penalty for the lower income group, it would be the same as asking residents in low-cost apartments to pay a 25% property tax so that we can levy a 15% property tax for those living in penthouses or condominiums. In addition, subsiding the high-income group is akin to saying at higher levels of income, an additional 1 dollar has the same utility as an additional 1 dollar at lower levels of income. This is a fallacy for a welfare state that justifies its social welfare programmes out of Benthamite philosophy! (See Reinhardt for instance).  So in summary the food stamp programme is a good idea for it achieves the desired social goal without the problems introduced by subsidy programmes. 
  2. The potential role of the programme as a stabaliserThe Economist newspaper recently published an article about the food stamp programme in the United States of America. The article observes that "spending on food stamps has risen so quickly because, unusually, almost all the needy are automatically and and indefinitely eligible for them." This makes the progamme a good stabaliser since spending on the programme will automatically increase during a recession and decrease during a boom.So in instituting this programme we not only have a mechanism that maintains the purchasing power of citizens in an equitable manner, it also helps moderate the swings in recessions and booms. Of course architects of the programme must be careful to delineate the cut-off point and above all be sensitive to the marginal tax that may be affected, or else in its efforts to break out of the "middle-income nation trap" it might inadvertently introduce an income trap with its policies. This conjecture will be discussed in the policy challenges part of the post. 
  3. The empirical backing for the multiplier effect of the programme
    From the same Economist article above, 
"When Moody’s Analytics assessed different forms of stimulus, it found that food stamps were the most effective, increasing economic activity by $1.73 for every dollar spent. Unemployment insurance came in second, at $1.62, whereas most tax cuts yielded a dollar or less."

So in addition to all of the advantages discussed above it appears that food stamps generate more   income per dollar spent. Automatic fiscal stabalisers that have a high (relatively speaking) multiplier effect is the holy grail of all fiscal policies. Rarely are there any effective mechanisms that can stimulate the economy automatically without having economic rationality hijacked by populist but harmful politicking.

Policy Challenges
Perhaps the greatest policy challenge is to delineate the cut-off points. Civil servants drafting out the proposal must first decide whether the value of food stamps issued is a linear function of income (that is decreasing value of food stamps issued as income increases), or would it be a much more "piece-wise" approach i.e. (food stamps valued at MYR X for the income range a to b; food stamps valued at MYR Y for the income range c to d etc etc). In addition it is worth reiterating that I said  "Purchasing power ought to be sustained and/or increased with a combination of nominal wage adjustments,tax effects, inflation management in addition to entitlement programmes like food stamp programmes and unemployment insurance." I have warned of the dangers of inadvertently institutionalising an income trap in our efforts to break out of the middle-income nation trap. This blog post from Prof Greg Mankiw sums up my fear.

My suggestion to the DPM is to draw in officials from the revenue agency, central bank, treasury,social welfare department and the economic planning board to draft out the proposal together. The success of the programme, and that of eliminating the danger of an institutionalised income trap requires a broad view, not just implementing a food stamp programme in isolation.

Update:The minister for Women,Family and Community Development says that the programme is targeted mainly at the poor. The federal poverty(absolute) level in the Peninsula is RM430/month. So the effects discussed above would be small(since absolute poverty according to official figures have gone down) but still significant. Yes,I know someone fudged the data for poverty by redefining the baseline, but that is something for another blog post. This also means that policies targeting sustainable purchasing power will have to come from nominal wage adjustment(which is why the minimum wage law was legislated), marginal tax adjustments in the future and of course, whether the Central Bank will validate inflation (it elected not to raise interest rate in the last round). 

Thursday, July 28, 2011

"It makes economic sense, if you think economically."

One day in the midst of an economics class amongst a sea of hungover and facebook-occupied freshmen I began to ponder the implications of the Economics professor's statement.

"It makes economic sense, if you think economically." 


There's an air of dissonance to the statement, unsettling even for it projects a sense of nihilism. If a single problem can make various forms of sense, "common sense, social sense, moral sense" what have you, that would have meant a single objective problem would have equally valid explanations drawn from established theories from varying disciplines. Of course in most cases this is fine, the world is sufficiently complex that a lot of factors are at work simultaneously.

But what if all of these trustworthy tools of academia- fortified by statistically significant empirical evidence - offer significantly different predictions? That would mean a moment of crisis for all of social science, as hard sciences like physics (quantum mechanics vs theory of relativity) could empathise.

That led me to think about the notion of the economic man, the rational decision maker populating economic models that came to life from Morgenstern and von Neumanns' pages. Economists are prone to be exasperated when they are accused of being out of touch with reality to populate their models with such agents. Yet one can easily see how economists come to such expectations of the ordinary Joes, it just follows logically if one's goal is to optimise scarce resources, even when some of the payoffs are random and/or only probabilistic. Yet psychologists(Kahnemann and Tversky for instance) have aptly demonstrated that empirical evidence weights unfavourly against the economists' expectation, and when one looks at the data set they presented one could only think that it then,to modify my professor's quote " to make psychological sense, if you think psychologically."

So what do we make of all these? In spite of how much economists (especially those writing textbooks) pride themselves to ground their thoughts more on positive rather than normative analysis, the expected utility theorem is in effect a logically superior normative analysis.On the other hand, Cognitive Psychology and some sub-fields of Sociology may offer better positive descriptions of  the behaviour of agents. For a science that aims to study how to optimise scare resources, all scientific endavours within Economics is fueled by the motivation to make the most all of scare resource. Economists are widely documented to be the ones with "economically correct" answers (see Thaler for instance).

Ultimately my opinion is that research in Economics should continue in two distinct but ultimately convergent paths. The first concerns the question "how do we optimise?" which is built up on the surfeit of theorems that have blessed our understanding of how the economy works; the second concerns "how do agents actually behave?" which could be built up and drawn from psychology,sociology and behavioural economics. Perhaps the best anecdote is provided by Richard Thaler, who in modifying Friedman's billiard player analogy offered that perhaps the expert billiard playing making strategic moves with his understanding of physics and mathematics is to economists as the average players who plans at most 2-3 strategic moves ahead as that of the average decision maker.

Only when we are certain of how the actual behaviour of agents diverge from rational behaviour that we can offer policy prescriptions that are guided by reason, supported by empirical evidenced and refined by moral intuitions.

Suggested Reading
  1. "Choices, Values and Frames." Daniel Kahnemann and Amos Tversky. Published in American Psychologist, 39:4,341-50
  2. "Prospect Theory, An Analysis of Decision under Risk". Daniel Kahnemann and Amos Tversky. Published in Econometrica, 47:2,263-91, 1979.
  3. "Mental Accounting Matters." Richard Thaler.
  4. "Toward a Positive Theory of Consumer Choice." Richard Thaler.