Sunteți pe pagina 1din 20

M604 Final Solutions

March 28, 2013

Method of moments: Define the dummy variables dj = 1x=xj and the moment function

1 X
mj = dnj pnj
N n

where the fourth parameter follows from the adding-up criterion p4 = 1 pj6=4 . It is clear that
the MoM estimator equates the P theoretical first moments to the empirical moment are just the
empirical frequencies, so pj = n 1xn =xj .

b.

The MoM asymptotic variance matrix is


1 1 0 1
Cov(p) = G0 G G G G0 G
N
The gradient of the moments at the estimates is just the identity matrix. The covariance matrix of
the moments is the covariance of the moments. It has a particularly simple analytic form

N jj = (1 pj )pj
N ij = pi pj

so the MoM covariance estimator collapses to the binomial variance matrix


1
Cov(p) = .
N

c
The estimates and standard errors are in Table (1). The Wald test gives a test statistic of 0.28. At
one degree of freedom, the p-value is about 60%.

Table 1:
Parameter Coefficient Std err
p1 0.1667 0.0481
p2 0.2000 0.0516
p3 0.1333 0.0439

1
2

a.
Let y be the true weight and y be the measured weight.

y < 100 if y [80, 100]


y = y if y [100, 240]
y > 240 if y [240, 300]

Given E[y |x = 0] = 180, the upper and lower bounds on E [y|y [100, 240], x = 0], call these
Econdl , Econdu are given by the upper and lower bounds of measurement errors on either side.
Multiplying through by the probabilities, the probability weighted contributions are (M el , M eu ) =
( 80 240 100 300
20 + 10 , 20 + 10 )

17
28 + Econdl = 180
20
17
35 + Econdu = 180
20
so (Econdl , Econdu ) = (170.59, 178.82). The true conditional expectation for y [80, 300] must lie
in the range of maximum measurement error
 
17 17
E [y|y [100, 240], x = 0] Econdl + M el , Econdu + M eu
20 20
= [173, 187]

b.
The sample median lies in the correctly measured range and the estimated median is therefore
unaffected by the measurement error, so M (y|x = 0) = 160.
c.
There is at least an issue an issue with significance and an issue with causality in the statement.
The statement refers to the difference in relative frequencies of vegetarian and non-vegetarian stu-
dents in the upper tail of the distribution P (y > 240|omnivore) P (y > 240|veggie) = 10
1
.

Firstly, since there are measurement errors, in the extreme, all omnivores true weight could be
epsilontically above 240 pounds, in which case the difference would not neither economically nor
statistically significant. It could also be that all omnivores were 300 pounds, that would certainly
be economically significant, possibly statistically significant, be we dont know given the informa-
tion.

Secondly, causality: is it the vegetarianism of the vegetarian students that hold their weight down
or are there any other habits correlated with vegetarianism, say an increased propensity of exer-
cise, eating disorders or whatever else that may bring your weight down. As far as we know, the
measurements dont tell.

a. & b.
The fixed effects were generated as
 
T 1
bi = ln 1 + x
bi + max xbit + i
30 5 t

2
for individuals I = 1, . . . , 25, for periods t = 1, . . . , T in b = 1, . . . , 1000 simulations, where
xbit iid U [0, 1] and i is an iid standard normal draw. For small T , there is then a cross-
sectional correlation between i and xi that motivates a fixed-effects estimator. Note that the
random component i of the fixed effect is kept constant within individual and across simulations.1

The latent variable y and the observed variable y are given by the process

yit = i + xit + it
(
1 if y > 0
y=
0 otherwise

where it is iid standard normal and xit is a scalar. The fixed effects and and the shocks 
are unobserved. The fixed effects are not iid, so neither is {yit } conditional on x. The fixed
effects estimation problem can however be expressed in terms of a concentrated likelihood function
that will transform the problem to an iid structure and that also corresponds with the requested
optimisation algorithm. The iid error structure is also useful to check necessary conditions for
consistency. The log-likelihood function is

ll(, ) = E [ln (f (y|, ))]

where f (yit |, ) = (i + xit )yit (1 (i + xit ))1yit is the common likelihood function for all
i, t. The ML criterion function is
1 XX
llT (, ) = ln(f (yit |, i ))
IT t i

The ML estimator can equivalently2 be represented by


1X
i () arg max
ln(f (yit |, )) (1)
T
t
1 XX
arg max ln(f (yit |,
())
IT
t i

corresponding to the algorithm. We can then express the estimation problem as a concentrated
log-likelihood problem. Holding I fixed, the log likelihood function can be written
1X
ll() E[ln(f (yi |, i ())] (2)
I
i

The criterion function we use for estimation is


1 XX
llT () = ln(f (yit |,
i ()) (3)
IT t i

A necessary condition for consistency is that we can interchange the limit of the maximum (lhs
below), with the maximum of the limit (rhs below)
1X
plimT ln(f (yit |,
i ()) = E [ln (f (yi |,
i ())] for all i
T t
1
Some of you specified a process where each individual got a new draw for every period and most of you made
new draws for each simulation b. That ameliorates the estimation problem by averaging out the fixed effects across
individuals and increases the variation in y. Those who did got results that looked more like consistency for T = 200
than I report below. The reason is that the probability of the occasional likelihood problems at the tails of the fixed
effect distribution decreases when you redraw the fixed effect i for every t = 1, . . . , T . Then the effect is no longer
fixed and the consequence is less individual persistence.
2
In the sense that it leads to an estimator with equivalent asymptotic distribution.

3
for all , , where is a compact parameter set. Uniform convergence in probability of llT ()
to ll() is sufficient to ensure that. First, note that i is estimated using only the data for individ-
ual i conditional on . For fixed T , i will be estimated with some error. As with most non-linear
estimators, estimation errors lead to finite-sample bias. Since T stays fixed, the estimation errors
will persist even if I . The error of any i will then contaminate the estimate of in the outer
loop. This is the incidental parameters problem. When however T , the estimation error in
() goes to zero 3

There are three important effects as T :


Holding I fixed, the fixed effects estimator is consistent as T . Some of you claimed
that there is no consistent estimator of the probit fixed effects (or fixed effects discrete choice
models in general), referring to the incidental parameters problem. The incidental parameters
problem refers to a the asymptotics where T stays fixed and I in (3). As argued above,
that is not the case when T in (3).
The correlation between the regressor and the fixed effect goes away. The fixed effects go from
 
1 1 1
T = 2 : i = ln 1 + x
i + max xit + i
2 5 5 t
to
 
p 1 1000 1
T : i ln 1 + + + i 1.96 + i
2 30 5
i.e. to a common constant plus an individual specific component i . The heterogeneity
approaches the form i + uit = + i + it that is no longer correlated with xit . The
endogeneity issue is therefore decreasing in T . There is however still correlation in the fixed
effects within individuals, so in the limit, we get a familiar random effects error structure
uit = i + it . Both random effects and fixed effects are therefore consistent. There are
reasons why you may want to avoid random effects. The log likelihood then turns into
Z 
1X
ll() = arg max ln f (yi |, )dG()
I
i

for a known distribution function G(). Either Maximum Simulated Likelihood or a Bayesian
MCMC approach is generally required due to the inner integral. This approach might be
computationally burdensome, particularly as T .
The fixed effect i increases in T < 1000, changing the base rate E[P r(yi = 1)]. For some
individuals, the increase in the fixed effect leads to less variation in the outcome y. Less
variation in y may in turn frustrate the estimation of i . In the limit, if yi = 1 for all t, the
the ML estimator is not defined.
 2 In our case, the expected value of i goes from on average of
E[i |T = 2] = 12 ln 1 + 15 + 15 = 0.16 to 1.96 for T = 2 and T > 1000, respectively, with the
4
average take over individuals. As T increases, then P r(y = 0) for most individuals become
a rarer event. Reduced variation in y for a given T makes the parameters more imprecisely
estimated. On the other hand, individuals with a low and negative individual specific fixed
effect i increase the variation in y as the constant grows.
To sum up a somewhat tldr discussion, we then have that
Though there is correlation between the regressor and the fixed effect for finite T , either
random or fixed effects are consistent estimators, yet biased in small samples.
3
Given the iid structure conditional on the fixed effects and assuming boundedness of the log likelihood function,
we get uniform convergence for the probit model. We can then apply Theorem 4.1.1 of Amemiya (1985) and we have
consistency.
4
Noting that the first component xt by LLN is 12 in expectation and that E[maxt xit ] is a first order statistic of
the standard uniform for N = 2 that is equal to 23 .

4
The variation in y changes with T < 1000 as the constant part of the fixed effect grows.
Though increasing T entails convergence to the true values, the true values themselves change,
and with it the base rate. The change in the variation in outcome y may decrease the
estimator precision more than the increased precision from more information, i.e. there may
not be convergence as T grows when T < 1000.
The distribution of for T = 2 and T = 30 is given in Figure (1). A plot of the means of
vs the
mean true value for each individual is given in Figure (2). The results below shows that for some
individuals, there are indeed computational problems due to little variation in y for large i even
for large T .

For T = 2, the distribution of is seen to have a large mass to the right of the true value 1
with a mean of 1.32, reflecting the fact that the fixed effects are biased and inconsistent in small T
samples. In Figure (2), I have plotted the true fixed effects vs the mean estimated effect over all
1000 simulations. The estimated fixed effects are noticeably biased away from zero.5

Increasing the time dimension to T = 30, the distribution of is seen to improve in Figure
(1). The mean is 1.03 displaying convergence to a symmetric distribution centered around the true
value.6 The true fixed effects are seen to have more mass on higher values for T = 30 in Figure
(2), reflecting the change in base rates as grows in T . Though the fixed effects are noticeably
less biased than for T = 2, the estimates still trail off as the variation in y decreases when i
gets far from 0. Particularly above i > 1, the estimates venture away on the positive side of the
true effects. This bias is a potential source of concern for fixed T as the substantial bias of some of
the estimates contaminates the estimates. A substantial bias in is seen to remain even for
relatively large T . The distribution of is still fairly well centered around zero as the bias in is
driven by a few very large outliers with hardly any variation in y where the optimiser is rewarded
for moving out of bounds to achieve a small increase in the fitted frequency.

Density
4
T=2
T = 30
3.5

2.5
Density

1.5

0.5

0
3 2 1 0 1 2 3 4 5 6 7

Figure 1: Distribution of for T = 2 and T = 30


5
Many of you reported trouble with both convergence and runtime, some even claimed that it was practically
impossible to the required simulations within the given exam time, reporting run times of more than a day for a
set of simulations. When many iterations are required, streamlining the code even somewhat, eradicating the worst
for-loops and other best practices pays off. The script for this part runs for about 10 seconds on my standard laptop.
6
The script for this part runs for about 30 seconds.

5
True vs estimated fixed effects
10

Estimated

T=2
T = 30
5
3 2 1 0 1 2 3
True

Figure 2: Estimated vs true fixed effects for T = 2 and T = 30

c.
As T goes to infinity, you can consistently estimate by either
1. Random effects.
2. Fixed effects.
3. Individual-by-individual.
listed in decreasing order of efficiency and increasing level of robustness to cross-sectional bias
contamination and distributional assumptions.
d.
The discussion above suggests we might want a consistent estimator that is robust to small sample
bias. Estimation individual-by-individual is consistent and avoids cross individual contamination
from the finite T bias we saw might still persist even for T = 200 for the is who have their base
rate sufficiently displaced by the increasing fixed effect. I also estimated an FE spec.

The resulting estimates yield the distributions of and in Figures (3) and (4).As expected,
the FE estimates have a tighter distribution than the I-by-I estimates due to the increased flexi-
bility of I-by-I. Predictably, as the constant increases with T , there are some individuals who get
outcome realisations of a series of y with hardly any variance at all. That shows up in the long tail
on the positive side of the distribution in Figure (3) (capped at 3) where the estimation routine
spins out. The fact that there are more observations does not counteract the reduction in variation
in outcome for some outlying individuals as the rare event effect dominates and cause estimation
problems. For most individuals, however, more observations reduces the bias. The distributions
are both centered around the true value. The mean and the median is 1 to more than eight
decimal places, an improvement from the bias for T = 30, where both the mean and the median
were larger than 1 by some hundredths.7

The means of are seen to align well with the true values for < 2, yet again, more biased
for larger . The fixed effects however trail off later this time, at about > 2 compared to about
> 1 for T = 30 and from abs() = 0 for T = 2. Eventually, as the common constant stops in-
7
The script runs for about 4 minutes.

6
creasing at T = 1000, the impact of extra information will override the rare event effect as T ,
yield variation in y and the estimator is consistent as T also for i > 2. There is hardly
any difference between the I-by-I spec and the FE so there is little cross-sectional contamination
in FE. The bias at large is again driven by mean being pushed out by some realisations of y
with barely any variation. This pushes the sample maximum far out in the parameter space for
some b and we get substantial bias. The median is for instance hardly affected as this happens rarely.

By estimating the specification individual by individual, we avoided cross-sectional contamina-


tion at the cost of the efficiency associated with imposing a true restriction. Though the bias in
nuisance parameters seemed to be substantial at larger values, the rarity of the outliers causing
the bias did not move the distribution of far from its true value.

Finally, note that there is a curious dip in the discrepancy between the true and the estimated
at the far right hand side that is seemingly repeated for T = 2, 30 in Figure (2). Whether
the apparent pattern is a fluke or reflects some real statistical effect, I dont know, but running
the simulations a few times from different seeds, it appears not to be a strong pattern, if any at
all.

Density T= 200 < 3


9
Individual
FE
8

5
Density

0
0 0.5 1 1.5 2 2.5 3 3.5

Figure 3: Distribution of for T = 200.

a. & b
MSM can be used to estimate the distributions of offer and acceptance points. I use two theoretical
restrictions. Both distributions are truncated normal on [0,100] and since randomized matching
both is true and common knowledge, we can restrict the covariance between the offer and accep-
tance points to zero. There are then four parameters to estimate o , o2 for the offers and a , a2
for the acceptances.

I use five moments


The relative frequency of accepted offers.
The mean of accepted offers.

7
True vs estimated fixed effects
10

Estimated

T= 200, Individual
T = 200, FE
T= 2, FE
T = 30, FE

5
3 2 1 0 1 2 3
True

Figure 4: True vs estimated for T = 200

The standard errors of accepted offers.


The mean of rejected offers.
The standard errors of rejected offers.
For the candidate set of parameters, I equate the simulated moments to the data. The empirical
moments are given from simulating the data generating process with the given draws. For the
simulation, I make a draw from a standard normal that I keep fixed between simulations and scale
with the parameters , 2 over which I search to avoid convergence issues. I used an identity
weighting matrix and calculated the standard errors by bootstrap.8 The results are given in Table
(2).

Table 2:
Parameter Coeff. Std. Err.
1 22.00 0.06
2 17.96 0.55
12 10.04 0.17
22 40.22 4.53

8
Total computation time including 1000 bootstraps was about 23 minutes.

8
M604 Finals doit 31/03/2013 14:57

Contents
Final M604
1. MOM
3. Probit fixed effects
Plot density of estimates
Plot betas
Estimating individual per individual for T = 200
Density beta
Plot true vs estimated fixed effects
M604 Q4 ultimatum
Standard errors

Final M604

% Set up for local parallelization


matlabpool close force
matlabpool local 4

1. MOM

% Set parameters
N = 60;

p = [1/6 1/5 2/15];

% Hard code the estimates.


m1 = [ones(10,1); zeros(50,1)];
m2 = [zeros(10,1); ones(12,1); zeros(38,1)];
m3 = [zeros(22,1); ones(8,1); zeros(30,1)];

% Moments
M = [m1 m2 m3] - repmat(p, N,1);

% Moment covariance
Sigma = -p'*p;
Sigma(eye(3)==1) = p.*(1-p);
Cov = 1/N*Sigma

% Estimates and standard errors


disp('Estimates and standard errors')
[p' sqrt(diag(Cov))]

% Wald test
R = [1 1 0];
r = 0.4;

wald = (R*p' - r)*inv(R*Cov*R')*(R*p' - r)

file:///Users/Grace/Documents/MATLAB/M604%20CA/M604%20Final/html/M604%20Finals%20doit.html Page 1 of 8
M604 Finals doit 31/03/2013 14:57

disp('Wald test p-value')


1-chi2cdf(wald,1)

3. Probit fixed effects

clc
clear

rseed = 10;

% Set parameters
G = 25;
T = 200;
B = 1000;

[y, x, alphatrue] = m604finalprobitdata(rseed, T, G, B);

% Fixed effects variables


X = kron(eye(G), ones(T,1));
X = repmat(X, [1, 1, B]) ;
X = [X x];

% Estimation
options = ...
optimset('Display', 'notify', ...
'GradObj', 'On', ....
'LargeScale', 'Off', ...
'MaxFunEvals', 10000);

% Preallocate and set parameters


convergencetracker = zeros(B,1);
Theta = zeros(G+1,B);

tic
parfor b = 1:B
% Reset start values and counter
alphanew = alphatrue(:,b);
betanew = 1;
thetanew = ones(G+1,1);
thetaold = zeros(G+1,1);
k = 0;
while abs(thetanew-thetaold) > 10^(-6)
% Update
alphaold = alphanew;
betaold = betanew;
thetaold = [alphanew; betanew];
% Set off iterated optimization run
[alphanew, ~, exitalpha] = fminunc(@(alpha) ...
m604finalprobit(alpha, betaold, y(:,:,b), X(:,:,b), 'alpha'), ...
alphaold, options);
[betanew, ~, exitbeta] = fminunc(@(beta) ...
m604finalprobit(alphanew, beta, y(:,:,b), X(:,:,b), 'beta'), ...
betaold, options);
thetanew = [alphanew; betanew];

file:///Users/Grace/Documents/MATLAB/M604%20CA/M604%20Final/html/M604%20Finals%20doit.html Page 2 of 8
M604 Finals doit 31/03/2013 14:57

% Break loop if iterates for too long


k = k+1;
if k > 100
break
end
convergencecheck = ismember([exitalpha, exitbeta], [0,-1,-3]);
% Keep track of undesirable exits
convergencetracker(b,1) = max(max(convergencecheck));
end
Theta(:,b) = thetanew;
end
toc

disp('Convergence tracker:')
if sum(convergencetracker) == 0
disp('Proper exit')
elseif disp('Improper exit')
end

% Tuck away the results


datatitle = ['ThetaT' num2str(T)];
save(datatitle, 'Theta', 'alphatrue')

Plot density of estimates

load ThetaT2.mat
alphaT2 = Theta(1:end-1,:);
alphatrueT2 = alphatrue;
betaT2 = Theta(end,:);
clear Theta alphatrue
load ThetaT30.mat
alphaT30 = Theta(1:end-1,:);
alphatrueT30 = alphatrue;
betaT30 = Theta(end,:);
clear Theta alphatrue

disp('Mean and median of beta at T = 2 and T = 30')


[mean(betaT2,2) median(betaT2,2); ...
mean(betaT30,2) median(betaT30,2) ]

[dbetaT2, bT2xi] = ksdensity(betaT2);


[dbetaT30, bT30xi] = ksdensity(betaT30);

Plot betas

clf
hold on
plot(bT2xi, dbetaT2, '--k')
plot(bT30xi, dbetaT30,'k')
title('Density \beta', 'fontsize',12)
legend('T = 2', 'T = 30', 'Location', 'Best')
xlabel('\beta')

file:///Users/Grace/Documents/MATLAB/M604%20CA/M604%20Final/html/M604%20Finals%20doit.html Page 3 of 8
M604 Finals doit 31/03/2013 14:57

ylabel('Density')
print(gcf, '-dpdf','m604finalsbetadensity.pdf')

% Plot true vs estimated fixed effects


clf
hold on
title('True vs estimated fixed effects', 'fontsize',12)
scatter(mean(alphatrueT2,2), mean(alphaT2,2),'filled', 'k')
scatter(mean(alphatrueT30,2), mean(alphaT30,2),'x', 'k')
xlabel('True \alpha')
ylabel('Estimated \alpha')
plot([min(mean(alphatrueT30,2)), max(mean(alphatrueT30,2))],...
[min(mean(alphatrueT30,2)),max(mean(alphatrueT30,2))], 'k')
legend('T = 2', 'T = 30', 'Location', 'Best')
print(gcf, '-dpdf', 'm604finalsalphatruevsestimated.pdf')
hold off

Estimating individual per individual for T = 200

clc
clear

rseed = 10;

% Set parameters
G = 25;
T = 200;
B = 1000;

[y, x, Alphatrue] = m604finalprobitdata(rseed, T, G, B);

% Fixed effects variables


X = kron(eye(G), ones(T,1));
X = repmat(X, [1, 1, B]) ;

% Give each individual his own beta


x = repmat(x,[1,G,1]);
x = X.*x;
X = [X x];

% Estimation
options = ...
optimset('Display', 'notify', ...
'GradObj', 'On', ....
'LargeScale', 'Off', ...
'MaxFunEvals', 10000, ...
'DerivativeCheck', 'off');

% Preallocate and set parameters


convergencetracker = zeros(B,1);

% Counter
k = 0;
Alpha = zeros(G,B);
Beta = zeros(G,B);

file:///Users/Grace/Documents/MATLAB/M604%20CA/M604%20Final/html/M604%20Finals%20doit.html Page 4 of 8
M604 Finals doit 31/03/2013 14:57

tic
parfor b = 1:B
% Reset start values and counter
alphanew = Alphatrue(:,b);
alphaold = zeros(G,1);
betanew = ones(G,1);
betaold = zeros(G,1);
thetanew = ones(2*G,1);
thetaold = zeros(2*G,1);
k = 0;
while abs(thetanew-thetaold) > 10^(-6)
% Update
alphaold = alphanew;
betaold = betanew;
thetaold = [alphanew; betanew];
% Set off iterated optimization run
[alphanew, ~, exitalpha] = fminunc(@(alpha) ...
m604finalprobit(alpha, betaold, y(:,:,b), X(:,:,b), 'alpha'), ...
alphaold, options);
[betanew, ~, exitbeta] = fminunc(@(beta) ...
m604finalprobit(alphanew, beta, y(:,:,b), X(:,:,b), 'beta'), ...
betaold, options);
thetanew = [alphanew; betanew];
% Break loop if iterate for too long
k = k+1;
if k > 100
break
end
% Keep track of undesirable exits
convergencecheck = ismember([exitalpha, exitbeta], [0,-1,-3]);
convergencetracker(b,1) = max(max(convergencecheck));
end
Alpha(:,b) = alphanew;
Beta(:,b) = betanew;
end
toc

disp('Convergence tracker:')
if sum(convergencetracker) == 0
disp('Proper exit')
elseif disp('Improper exit')
end

save('AlphaBeta', 'Alpha', 'Beta', 'Alphatrue', 'T', 'G', 'B' );

Density beta

load AlphaBeta.mat
load ThetaT2.mat
alphaT2 = Theta(1:end-1,:);
alphatrueT2 = alphatrue;
betaT2 = Theta(end,:);
clear Theta alphatrue
load ThetaT30.mat
alphaT30 = Theta(1:end-1,:);

file:///Users/Grace/Documents/MATLAB/M604%20CA/M604%20Final/html/M604%20Finals%20doit.html Page 5 of 8
M604 Finals doit 31/03/2013 14:57

alphatrueT30 = alphatrue;
betaT30 = Theta(end,:);
load ThetaT200.mat
alphaT200 = Theta(1:end-1,:);
alphatrueT200 = alphatrue;
betaT200 = Theta(end,:);
clear Theta alphatrue

betas= reshape(Beta,[G*B,1]);
disp('Mean and median of beta')
[mean(betas < 10) median(betas)]
clf
[betasIbI, betasIbIxi] = ksdensity(betas(betas<3));
[betasFE, betasFExi] = ksdensity(betaT200(betaT200<3));

% Plot the densities


clf
hold on
plot(betasIbIxi,betasIbI, 'k')
plot(betasFExi, betasFE,'--k')
title(['Density \beta T= ' num2str(T),' \beta < 3'], 'fontsize',12)
xlabel('\beta')
ylabel('Density')
legend('Individual', 'FE', 'location', 'best')
densityname = ['m604finalsbetadensityT' num2str(T) '.pdf']
print(gcf, '-dpdf', densityname)

Plot true vs estimated fixed effects

clf
hold on
title('True vs estimated fixed effects', 'fontsize',12)
scatter(mean(Alphatrue,2), mean(Alpha,2),'o', 'k')
scatter(mean(alphatrueT200,2), mean(alphaT200,2),100,'d', 'k')
scatter(mean(alphatrueT2,2), mean(alphaT2,2),'filled', 'k')
scatter(mean(alphatrueT30,2), mean(alphaT30,2),'x', 'k')
xlabel('True \alpha')
ylabel('Estimated \alpha')
legend('T= 200, Individual', 'T = 200, FE','T= 2, FE', 'T = 30, FE', ...
'location', 'best')
plot([min(mean(alphatrueT30,2)), max(mean(alphatrueT30,2))], ...
[min(mean(alphatrueT30,2)),max(mean(alphatrueT30,2))],'k')
feffectsname = ['m604finalsalphatruevsestimatedT', num2str(T) '.pdf'];
print(gcf, '-dpdf', feffectsname)
hold off

M604 Q4 ultimatum

rng(101);

% Get data
load ultimatum.csv

offer = ultimatum(:,1);

file:///Users/Grace/Documents/MATLAB/M604%20CA/M604%20Final/html/M604%20Finals%20doit.html Page 6 of 8
M604 Finals doit 31/03/2013 14:57

accept = ultimatum(:,2);
terms=offer.*(accept>0);

% Set parameters
N = size(offer,1);

% Generate the data moments


data_moments(1)=mean(accept);
data_moments(2)=mean(terms(accept>0));
data_moments(3)=std(terms(accept>0));
data_moments(4)=mean(offer(accept==0));
data_moments(5)=std(offer(accept==0));

% Make prefixed standard normal draws and matchings. .


paramdraws = mvnrnd([0 0],eye(2),N);
matchdraws = [randperm(N); 1:N]';

options = optimset('display', 'off', 'MaxFunEvals', 100000);


thetastart = [18 18 20 20];

tic
[mom_estimates,~,exitflag] = fminsearch(@(z) ...
ultimatum_gmm(z,data_moments,paramdraws,matchdraws),...
thetastart, options);
toc

disp('Estimates')
mom_estimates

disp('Exit')
if ismember(exitflag,1) == 1
disp('Proper')
elseif disp('Improper')
end

Standard errors

% Nr of bootstraps
B = 1000;

% Resample pairs of the data, hold the simulation draws fixed. Re-estimate.
N = size(offer,1);
% Sample with replacement
sampler = randi(N,[N,B]);
% Pre-allocate
bestimates = zeros(B,4);
exitcheck = zeros(B,1);

data = [offer, accept, terms];


thetastart = [18 18 20 20];

tic
parfor b = 1:B
samplerb = sampler(:,b);
% Resample the data
data_momentsb = ultimatumdatamoments(data, samplerb);

file:///Users/Grace/Documents/MATLAB/M604%20CA/M604%20Final/html/M604%20Finals%20doit.html Page 7 of 8
M604 Finals doit 31/03/2013 14:57

% Estimates
[mom_estimatesb,~,exitflag] = fminsearch(@(z) ...
ultimatum_gmm(z,data_momentsb,paramdraws,matchdraws), ...
thetastart, options);
bestimates(b,:) = mom_estimatesb;
% Keep track of convergence.
exitcheck(b,1) = exitflag;
end
toc

% Results
disp('MSM estimates with bootstrapped errors')
[mom_estimates' std(bestimates)']

% Convergence check
disp('Exit')
if ne(sum(exitcheck),B) == 0
disp('Proper')
elseif ne(sum(exitcheck),B) == 1
disp('Improper')
end

Published with MATLAB R2012b

file:///Users/Grace/Documents/MATLAB/M604%20CA/M604%20Final/html/M604%20Finals%20doit.html Page 8 of 8
m604finalprobit 28/03/2013 14:53

function [f, g] = m604finalprobit(alpha, beta, y, X, zig)


% M604 finals probit function:

% Argument
theta = [alpha; beta];
v = (2*y-1).*(X*theta);
likh = normcdf(v);

% log-likelihood function
f = log(likh);

% Criterion
f = -sum(f);

% Add penalty for numerical issues.


if isnan(f) == 1
f = 100000;
end

% Set parameters
K = length(theta);
Kalpha = length(alpha);

% Gradient.
if strcmp('alpha', zig) == 1
% Set parameters
X = X(:,1:Kalpha);
% Gradient
g = repmat((2*y-1).*normpdf(v)./likh,1,Kalpha).*X;
g = -sum(g);
elseif strcmp('beta', zig) == 1
% Set parameters
X = X(:,Kalpha+1:end);
% Gradient
g = repmat((2*y-1).*normpdf(v)./likh,1,K-Kalpha).*X;
g = -sum(g);
end

Published with MATLAB R2012b

file:///Users/Grace/Documents/MATLAB/M604%20CA/M604%20Final/html/m604finalprobit.html Page 1 of 1
m604finalprobitdata 28/03/2013 14:53

function [y, x, alphatrue] = m604finalprobitdata(rseed, T, G, B)


% M604 Finals: Returns data for the progibit simulation

% Set seed
rng(rseed)

% 1st dimension: time, 2 dimension: individual, 3 dimension: trial


x = rand([T,G,B]);

% Draw the elements of the fixed effects


xmax = max(x,[],1);
xmax = repmat(xmax,[T,1,1]);
xmax = reshape(xmax, [G*T,1,B]);

xbar = mean(x);
xbar = repmat(xbar,[T,1,1]);
xbar = reshape(xbar, [G*T,1,B]);

ups = randn([1,G,1]);
ups = repmat(ups, [T,1,B]);
ups = reshape(ups,[G*T,1,B]);

% Interact with the coefficients


alpha = log(1 + T/30)*xbar + 1/5*xmax + ups ;

alphatrue = reshape(alpha,[T,G,B]);
alphatrue = mean(alphatrue,1);
alphatrue = reshape(alphatrue,[G,B]);

% Reshape to a panel, every bootstrap in the third dimension


alpha = reshape(alpha, [G*T,1,B]);

x = reshape(x, [G*T,1,B]);

% Latent variable with iid shock


y = alpha + x + randn([G*T,1,B]);

% Choice variable
y = y>0;

Published with MATLAB R2012b

file:///Users/Grace/Documents/MATLAB/M604%20CA/M604%20Final/html/m604finalprobitdata.html Page 1 of 1
ultimatum_gmm 28/03/2013 14:51

function f=ultimatum_gmm(trial_mu_sig,data_moments,paramdraws,matchdraws)
% M604 Finals: Ultimatum game MSM criterion.

% Weight matrix and parameters.


W=eye(length(data_moments));
N = length(paramdraws);

% Add penalty on trial parameters venturing out of the domain to circumvent cumbersome cons
trained
% minimization routine.
if (min(trial_mu_sig)>0)

% Scale the fixed draws and interact with parameters.


mu=trial_mu_sig(1:2);
sig=diag(trial_mu_sig(3:4));
params=paramdraws*(sig^.5)+ones(N,1)*mu;
params=min(100,max(params,0));

% Run ultimatum simulation.


offer=params(matchdraws(:,1),1);
accept= offer>=params(matchdraws(:,2),2);
terms=offer.*(accept>0);

% Simulated moments
sim_moments(1)=mean(accept>0);
sim_moments(2)=mean(terms(accept>0));
sim_moments(3)=std(terms(accept>0));
sim_moments(4)=mean(offer(accept==0));
sim_moments(5)=std(offer(accept==0));

% Moment criterion
pre_f=sim_moments-data_moments;
f=pre_f*W*pre_f';

else

f=10^8 +min(trial_mu_sig)^2;
end

Published with MATLAB R2012b

file:///Users/Grace/Documents/MATLAB/M604%20CA/M604%20Final/html/ultimatum_gmm.html Page 1 of 1
ultimatumdatamoments 28/03/2013 14:53

function f = ultimatumdatamoments(data, sampler)


% M604 finals Ultimatum game: returns data moments for bootstrap purposes
% with parallelization.

% Unpack and sample


offer = data(:,1);
accept = data(:,2);
terms = data(:,3);

offerb = offer(sampler);
acceptb = accept(sampler);
termsb = terms(sampler);

% Bootstrap moments.
data_momentsb(1)=mean(acceptb>0);
data_momentsb(2)=mean(termsb(acceptb >0));
data_momentsb(3)=std(termsb(acceptb>0));
data_momentsb(4)=mean(offerb(acceptb==0));
data_momentsb(5)=std(offerb(acceptb==0));

f = data_momentsb;

Published with MATLAB R2012b

file:///Users/Grace/Documents/MATLAB/M604%20CA/M604%20Final/html/ultimatumdatamoments.html Page 1 of 1

S-ar putea să vă placă și