-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
damiancclarke
committed
Jun 5, 2014
1 parent
cadd3a3
commit 279b589
Showing
69 changed files
with
2,126 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
*~ | ||
*.asv |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
function [LL, ll_i] = BinaryLogitLL(beta, y, x) | ||
|
||
Lambda_xb = Lambda(x * beta); | ||
|
||
ll_i = y .* log(Lambda_xb) + (1 - y) .* log(1 - Lambda_xb); | ||
|
||
LL = -sum(ll_i); | ||
|
||
return |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
function [LL, ll_i] = BinaryLogitSimulatedLL(beta, y, x, R) | ||
|
||
rng(1); | ||
|
||
N = size(y, 1); | ||
Simulated_y = NaN(N, R); | ||
|
||
for count = 1:R | ||
Simulated_y(:, count) = SimulateBinaryLogit(x, beta); | ||
end | ||
|
||
SimulatedProb = mean(Simulated_y, 2); | ||
|
||
ll_i = y .* log(SimulatedProb) + (1 - y) .* log(1 - SimulatedProb ); | ||
LL = -sum(ll_i); | ||
|
||
return |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
function GraphSimulatedData(utility, y) | ||
|
||
cla | ||
scatter((utility(y == 1, 1) - utility(y == 1, 3)), (utility(y == 1, 2) - utility(y == 1, 3))) | ||
hold on | ||
scatter((utility(y == 2, 1) - utility(y == 2, 3)), (utility(y == 2, 2) - utility(y == 2, 3))) | ||
hold on | ||
scatter((utility(y == 3, 1) - utility(y == 3, 3)), (utility(y == 3, 2) - utility(y==3 , 3))) | ||
hold on | ||
plot([min((utility(:, 1) - utility(:, 3))), 0], [0, 0], 'LineWidth', 3, 'Color', [0, 0, 0]) | ||
hold on | ||
plot([0, max(utility(:, 1) - utility(:, 3))], ... | ||
[0, max(utility(:, 1) - utility(:, 3))], 'LineWidth', 3, 'Color', [0, 0, 0]) | ||
hold on | ||
plot([0, 0], [min(utility(:, 1) - utility(:, 3)), 0], 'LineWidth', 3, 'Color', [0, 0, 0]) | ||
|
||
|
||
return |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
function prob = Lambda(z) | ||
|
||
prob = exp(z) ./ (1 + exp(z)); | ||
|
||
return |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
function LL = MNLogitLL(betavec, y, x) | ||
|
||
N = size(x, 1); | ||
K = size(x, 2); | ||
J = size(betavec, 1)/K + 1; | ||
|
||
beta = reshape(betavec, K, J - 1); | ||
|
||
expxb = exp(x * beta); | ||
expxb_augmented = [expxb, ones(N, 1)]; | ||
|
||
MyIndex = NaN(N, J); | ||
|
||
for count = 1:J | ||
MyIndex(:, count) = (y == count); | ||
end | ||
|
||
ll_i = log(sum(expxb_augmented .* MyIndex, 2) ./ sum(expxb_augmented, 2)); | ||
|
||
LL = -sum(ll_i); | ||
|
||
return |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
function LL = MNLogitSimulatedLL(beta, y, x, R) | ||
|
||
rng(1); | ||
|
||
N = size(y, 1); | ||
J = max(y); | ||
|
||
Simulated_y = NaN(N, R); | ||
SimulatedProb = NaN(N, J); | ||
|
||
for count = 1:R | ||
Simulated_y(:, count) = SimulateMNLogit(x, beta); | ||
end | ||
|
||
for count = 1:J | ||
SimulatedProb(:, count) = mean(Simulated_y == count, 2); | ||
MyIndex(:, count) = (y == count); | ||
end | ||
|
||
ll_i = sum(MyIndex .* log(SimulatedProb), 2); | ||
LL = -sum(ll_i); | ||
|
||
return |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
function LL = MNProbitSimulatedLL(parameters, y, x, R) | ||
|
||
betavec = parameters(1:end-1); | ||
rho = parameters(end); | ||
|
||
omega = [1, rho; rho, 1]; | ||
|
||
rng(1); | ||
|
||
N = size(y, 1); | ||
J = max(y); | ||
|
||
Simulated_y = NaN(N, R); | ||
SimulatedProb = NaN(N, J); | ||
|
||
for count = 1:R | ||
Simulated_y(:, count) = SimulateMNProbit(x, betavec, omega); | ||
end | ||
|
||
for count = 1:J | ||
SimulatedProb(:, count) = mean(Simulated_y == count, 2); | ||
MyIndex(:, count) = (y == count); | ||
end | ||
|
||
ll_i = sum(MyIndex .* log(SimulatedProb), 2); | ||
LL = -sum(ll_i); | ||
|
||
return |
29 changes: 29 additions & 0 deletions
29
II_DiscreteChoice/DiscreteChoice/MNProbitSimulatedLL_Smoothed.m
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
function LL = MNProbitSimulatedLL_Smoothed(parameters, y, x, R) | ||
|
||
betavec = parameters(1:end-1); | ||
rho = parameters(end); | ||
|
||
omega = [1, rho; rho, 1]; | ||
|
||
rng(1); | ||
|
||
N = size(y, 1); | ||
J = max(y); | ||
|
||
SimulatedS = NaN(N, J, R); | ||
SimulatedProb = NaN(N, J); | ||
|
||
for count = 1:R | ||
SimulatedS(:, :, count) = SimulateMNProbit_Smoothed(x, betavec, omega); | ||
end | ||
|
||
SimulatedProb = mean(SimulatedS, 3); | ||
|
||
for count = 1:J | ||
MyIndex(:, count) = (y == count); | ||
end | ||
|
||
ll_i = sum(MyIndex .* log(SimulatedProb), 2); | ||
LL = -sum(ll_i); | ||
|
||
return |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
function y = SimulateBinaryLogit(x, beta) | ||
|
||
% This function returns a binary outcome, simulating a logit model. | ||
|
||
N = size(x, 1); | ||
J = 2; | ||
|
||
% First, simulate values for epsilon... | ||
|
||
epsilon = -log(-log(rand(N, J)));; | ||
|
||
% Second, simulate the utility for two options... | ||
|
||
beta_augmented = [beta, 0 * beta]; | ||
utility = x * beta_augmented + epsilon; | ||
|
||
% Third, simulate the choice for each individual... | ||
|
||
[junk, choice] = max(utility, [], 2); | ||
y = (choice == 1); | ||
|
||
return |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
function y = SimulateMNLogit(x, betavec) | ||
|
||
N = size(x, 1); | ||
K = size(x, 2); | ||
J = size(betavec, 1)/K + 1; | ||
|
||
beta = reshape(betavec, K, J - 1); | ||
|
||
% First, simulate values for epsilon... | ||
|
||
epsilon = -log(-log(rand(N, J)));; | ||
|
||
% Second, simulate the utility for two options... | ||
|
||
beta_augmented = [beta, zeros(K, 1)]; | ||
utility = x * beta_augmented + epsilon; | ||
|
||
% Third, simulate the choice for each individual... | ||
|
||
[junk, y] = max(utility, [], 2); | ||
|
||
% GraphSimulatedData(utility, y) | ||
|
||
return |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
function choice = SimulateMNProbit(x, betavec, omega) | ||
|
||
N = size(x, 1); | ||
K = size(x, 2); | ||
J = size(betavec, 1)/K + 1; | ||
|
||
beta = reshape(betavec, K, J - 1); | ||
xb = x * beta; | ||
|
||
diff = [xb + mvnrnd(zeros(J - 1, 1), omega, N), zeros(N, 1)]; | ||
[junk, choice] = max(diff, [], 2); | ||
|
||
GraphSimulatedData(diff, choice) | ||
|
||
return |
20 changes: 20 additions & 0 deletions
20
II_DiscreteChoice/DiscreteChoice/SimulateMNProbit_Smoothed.m
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
function S = SimulateMNProbit_Smoothed(x, betavec, omega) | ||
|
||
smoother = .01; | ||
|
||
N = size(x, 1); | ||
K = size(x, 2); | ||
J = size(betavec, 1)/K + 1; | ||
|
||
beta = reshape(betavec, K, J - 1); | ||
xb = x * beta; | ||
|
||
diff = [xb + mvnrnd(zeros(J - 1, 1), omega, N), zeros(N, 1)]; | ||
|
||
expdiff = exp(diff/smoother); | ||
|
||
for count = 1:J | ||
S(:, count) = expdiff(:, count) ./ sum(expdiff, 2); | ||
end | ||
|
||
return |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
function LL = BayesianNashLL(parameters, Y, X) | ||
|
||
mu_1 = parameters(1); | ||
mu_2 = parameters(2); | ||
delta_1 = parameters(3); | ||
delta_2 = parameters(4); | ||
rho = parameters(5); | ||
|
||
N = size(X, 1); | ||
ll = NaN(N, 1); | ||
|
||
[UniqueData, m, n] = unique(X, 'rows'); | ||
|
||
cutoffs_small = NaN(size(UniqueData, 1), 2); | ||
for count = 1:size(UniqueData, 1) | ||
cutoffs_small(count, :) = SolveBayesianNash(mu_1, mu_2, ... | ||
delta_1 * UniqueData(count, 1), delta_2 * UniqueData(count, 2), rho); | ||
end | ||
|
||
cutoffs_large = cutoffs_small(n, :); | ||
|
||
for count = 1:N | ||
q1 = 2 * Y(count, 1) - 1; | ||
q2 = 2 * Y(count, 2) - 1; | ||
|
||
ll(count) = log(bvnl( q1 * (mu_1 - cutoffs_large(count, 1)), ... | ||
q2 * (mu_2 - cutoffs_large(count, 2)), q1 * q2 * rho)); | ||
|
||
end | ||
|
||
LL = -sum(ll); | ||
|
||
return |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
function Loss = BayesianNashLoss(x, mu_i, mu_j, delta_i, delta_j, rho) | ||
|
||
xi_star = SolveBestResponse(mu_i, x(2), mu_j, rho, delta_i); | ||
xj_star = SolveBestResponse(mu_j, x(1), mu_i, rho, delta_j); | ||
|
||
Loss = (x(1) - xi_star)^2 + (x(2) - xj_star)^2; | ||
|
||
return |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
function Loss = BestResponseLoss(x_i, mu_i, x_j, mu_j, rho, delta_i) | ||
|
||
% This function returns the loss for player i's best response. | ||
|
||
Loss = (x_i - delta_i * (2 * norm_cdf((x_j - mu_j - rho * (x_i - mu_i))/sqrt(1 - rho^2)) - 1))^2; | ||
|
||
return |
79 changes: 79 additions & 0 deletions
79
II_DiscreteChoice/Games/BinaryBayesian/RunBinaryBayesian.m
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
% RunBinaryBayesian | ||
|
||
clear | ||
cla | ||
|
||
% Initialise the parameters... | ||
|
||
mu_i = 1; | ||
mu_j = -1; | ||
|
||
rho = 0.75 | ||
|
||
delta_i = -0.5; | ||
delta_j = -0.5; | ||
|
||
% Generate a grid for x_j... | ||
|
||
x_j = [-5:.1:5]'; | ||
|
||
% Solve the best response for x_i... | ||
|
||
x_i_star = NaN(size(x_j)); | ||
|
||
for count = 1:size(x_i_star, 1) | ||
x_i_star(count, 1) = SolveBestResponse(mu_i, x_j(count, 1), mu_j, rho, delta_i); | ||
end | ||
|
||
line(x_j, x_i_star, 'LineWidth', 2, 'Color', [1, 0, 0]) | ||
|
||
% Generate a grid for x_i... | ||
|
||
x_i = [-5:.1:5]'; | ||
|
||
% Solve the best response for x_j... | ||
|
||
x_j_star = NaN(size(x_i)); | ||
|
||
for count = 1:size(x_i_star, 1) | ||
x_j_star(count, 1) = SolveBestResponse(mu_j, x_i(count, 1), mu_i, rho, delta_j); | ||
end | ||
|
||
hold on | ||
line(x_j_star, x_i, 'LineWidth', 2, 'Color', [0, 0, 1]) | ||
|
||
% Solve the model... | ||
|
||
BayesianNash = SolveBayesianNash(mu_i, mu_j, delta_i, delta_j, rho); | ||
|
||
hold on | ||
scatter(BayesianNash(2), BayesianNash(1), 100, [0, 0, 0], 'filled') | ||
|
||
% Simulate... | ||
|
||
mu_1 = 1; | ||
mu_2 = -1; | ||
beta_1 = -0.2; | ||
beta_2 = -0.7; | ||
rho = 0.5; | ||
|
||
[y, x] = SimulateBayesianNash(1000, mu_1, mu_2, beta_1, beta_2, rho); | ||
|
||
return | ||
|
||
% Estimate.... | ||
|
||
parameters_init = [mu_1, mu_2, beta_1, beta_2, rho]'; | ||
|
||
lb = [-2, -2, -2, -2, 0]; | ||
ub = [2, 2, 0, 0, 0.99]; | ||
|
||
options = optimset('Algorithm', 'sqp', 'Display', 'iter', 'DiffMinChange', 1e-4); | ||
|
||
[result.parameters, result.LL, result.exitflag] = ... | ||
fmincon(@(parameters) BayesianNashLL(parameters, y, x), parameters_init, ... | ||
[], [], [], [], lb, ub, @(parameters) UniqueEquilibriumConstraint(parameters), options); | ||
|
||
[parameters_init, result.parameters] | ||
|
||
return |
Oops, something went wrong.