-
Notifications
You must be signed in to change notification settings - Fork 2
/
myfun.m
84 lines (65 loc) · 1.87 KB
/
myfun.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
%this function returns the objective function and gradient function of GMM
%estimation in the first stage.
function [f, g] = myfun(theta)
global p x s mkt yita
beta = theta(1:4);
alpha = theta(5);
sigma = theta(6);
%Step 1: simulate individuals
%N = 10; %# of simulated individuals
%yita = normrnd(0,1,1,N);
%Step 2: utilty matrix of individual i consuming product j in market t
%(size JT*N)
%u = (x*beta + alpha*p)* ones(size(yita)) + sigma*x(:,1)*yita; //WRONG!!!
uf = x*beta + alpha*p;
uf = repmat(uf,1,length(yita));
u = uf + sigma*x(:,1)*yita;
eu = exp(u);
%Step 3: market share predicted
T = length(unique(mkt));
denom = zeros(size(u));
for i = 1:length(yita)
for t = 1:T
IX = mkt == t;
denom(IX,i) = sum(eu(IX,i))+1;
end
end
spi = eu ./ denom; %predicted share of each individual
sp = mean(spi,2); % predicted share averaged across individuals
% step 4: GMM objective function
ksi = sp - s;
f = 0.5 .* ksi' * ksi;
% step 5: Gradient
%%get the four beta parameters done one by one, plus sigma
dedb = zeros(length(p),length(theta));
%B = zeros(size(u));
for m = 1:(length(beta))
ex = eu .* repmat(x(:,m),1,length(yita));
nom = zeros(size(u)); %nom changes for each m iteration
for i = 1:length(yita)
for t = 1:T
IX = mkt == t;
nom(IX,i) = sum(ex(IX,i));
end
end
B = spi.* repmat(x(:,m),1,length(yita)) - spi.* nom./denom;
dedb(:,m) = mean(B,2);
if m == 1
B = repmat(yita,length(p),1) .* B;
dedb(:,length(theta)) = mean(B,2);
end
end
%%get the alpha parameter done
ex = eu .* repmat(p,1,length(yita));
nom = zeros(size(u));
for i = 1:length(yita)
for t = 1:T
IX = mkt == t;
nom(IX,i) = sum(ex(IX,i));
end
end
B = spi.* repmat(p,1,length(yita)) - spi.* nom./denom;
dedb(:,length(beta)+1) = mean(B,2);
% at last ....
g = dedb' * ksi;
end