function simulate_adoption_all_cases_gss

% function simulate_adoption_all_cases_gss
% Simulates internet adoption under different conditions of 
% network externalities and homophily
%
% Paul DiMaggio and Filiz Garip (c)
% Department of Sociology
% Princeton University, Harvard University
% Created: 28 March 2005
% Last updated: 3 June 2008, 9am
% File Name: simulate_adoption_all_cases_gss.m

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (0)   INITIALIZE PARAMETERS  %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear;

tic;
        % Simulation parameters
rp          = 1000;       % Number of repetitions  
        
        % Network parameters
h           = 0;        % Homophily parameter
nig         = 3;        % Number of income groups
neg         = 4;        % Number of education groups

        % Adoption parameters
alpha       = 0.5;      % Network size exponent
gamma       = 0.5;     % Income exponent
k           = 0.1;                       
init_price  = 60;
max_time    = 100;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (I)   NO NETWORK EXTERNALITIES  %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

delta       = 0;                                % No network externalities case

% Create matrices to hold output over repetitions
p_no        = zeros(rp, max_time);              % Price matrix - Rows: Repetitions, Columns: Time
all_no      = zeros(rp, max_time);              % Overall adoption rate matrix over repetitions
igr_no      = zeros(rp, max_time, nig);         % Adoption rate matrix by income group
egr_no      = zeros(rp, max_time, neg);         % Adoption rate matrix by education group
r_no        = zeros(rp, max_time,2);            % Adoption rate matrix by race

% Generate a population to be used in all cases
[rie]= generate_population_gss;
ps = size(rie,1);
disp('No Network Externalities - Repetition #:');
for i=1:rp
    % Generate a network of relations
    [tie] = establish_ties_gss(h, rie);
    nrig  = rie(:,1:6); 
    % Simulate adoption 
    [all, igr, egr, r, p]     = simulate_adoption_w_general_net_ext_gss(tie, nrig, alpha, gamma, delta, k, init_price, max_time);
    % Store output
    p_no(i,:)           = p;
    all_no(i,:)         = all;
    for j=1:nig
        igr_no(i,:,j)    = igr(j,:);
    end
    for j=1:neg
        egr_no(i,:,j)    = egr(j,:);
    end
    r_no(i,:,1)         = r(1,:);
    r_no(i,:,2)         = r(2,:);   
    disp(i);
end

% Summarize the results
mn_all         = mean(all_no,1);
std_all        = std(all_no,1);
mn_igr         = mean(igr_no,1);
std_igr        = std(igr_no,1);
mn_egr         = mean(egr_no,1);
std_egr        = std(egr_no,1);
mn_r           = mean(r_no,1);
std_r          = std(r_no,1);
mn_p           = mean(p_no,1);
std_p          = std(p_no,1);

% Save the output as a workspace
save NoNetExt   p_no  all_no  igr_no egr_no  r_no  mn_all  std_all  mn_igr  std_igr mn_egr std_egr mn_r  std_r  mn_p  std_p

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (II)  GENERAL NETWORK EXTERNALITIES  %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

delta       = 0.1 ;    

% Create matrices to hold output over repetitions
p_ge        = zeros(rp, max_time);              % Price matrix - Rows: Repetitions, Columns: Time
all_ge      = zeros(rp, max_time);              % Overall adoption rate matrix over repetitions
igr_ge      = zeros(rp, max_time, nig);         % Adoption rate matrix by income group
egr_ge      = zeros(rp, max_time, neg);         % Adoption rate matrix by education group
r_ge        = zeros(rp, max_time,2);            % Adoption rate matrix by race

disp('General Network Externalities - Repetition #:');
for i=1:rp
   % Generate a network of relations
    [tie] = establish_ties_gss(h, rie);
    nrig  = rie(:,1:6); 
    % Simulate adoption 
    [all, igr, egr, r, p]     = simulate_adoption_w_general_net_ext_gss(tie, nrig, alpha, gamma, delta, k, init_price, max_time);
    % Store output
    p_ge(i,:)           = p;
    all_ge(i,:)         = all;
    for j=1:nig
        igr_ge(i,:,j)    = igr(j,:);
    end
    for j=1:neg
        egr_ge(i,:,j)    = egr(j,:);
    end
    r_ge(i,:,1)         = r(1,:);
    r_ge(i,:,2)         = r(2,:);  
    disp(i);
end

% Summarize the results
mn_all_ge         = mean(all_ge,1);
std_all_ge        = std(all_ge,1);
mn_igr_ge          = mean(igr_ge,1);
std_igr_ge         = std(igr_ge,1);
mn_egr_ge          = mean(egr_ge,1);
std_egr_ge         = std(egr_ge,1);
mn_r_ge           = mean(r_ge,1);
std_r_ge          = std(r_ge,1);
mn_p_ge           = mean(p_ge,1);
std_p_ge          = std(p_ge,1);

% Save the output as a workspace
save GenNetExt   p_ge  all_ge  igr_ge egr_ge  r_ge  mn_all_ge  std_all_ge  mn_igr_ge  std_igr_ge mn_egr_ge  std_egr_ge  mn_r_ge  std_r_ge  mn_p_ge  std_p_ge

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (III) SPECIFIC NETWORK EXTERNALITIES W NO HOMOPHILY %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Create matrices to hold output over repetitions
p_se        = zeros(rp, max_time);              % Price matrix - Rows: Repetitions, Columns: Time
all_se      = zeros(rp, max_time);              % Overall adoption rate matrix over repetitions
igr_se      = zeros(rp, max_time, nig);         % Adoption rate matrix by income group
egr_se      = zeros(rp, max_time, neg);         % Adoption rate matrix by education group
r_se        = zeros(rp, max_time,2);            % Adoption rate matrix by race

disp('Specific Network Externalities - Repetition #:');
for i=1:rp
    % Generate a network of relations
    [tie] = establish_ties_gss(h, rie);
    nrig  = rie(:,1:6); 
    % Simulate adoption 
    [all, igr, egr, r, p]   = simulate_adoption_w_specific_net_ext_gss(tie, nrig, alpha, gamma, delta, k, init_price, max_time);
    % Store output
    p_se(i,:)           = p;
    all_se(i,:)         = all;
    for j=1:nig
        igr_se(i,:,j)    = igr(j,:);
    end
    for j=1:neg
        egr_se(i,:,j)    = egr(j,:);
    end
    r_se(i,:,1)         = r(1,:);
    r_se(i,:,2)         = r(2,:); 
    disp(i);
end

% Summarize the results
mn_all_se         = mean(all_se,1);
std_all_se        = std(all_se,1);
mn_igr_se          = mean(igr_se,1);
std_igr_se         = std(igr_se,1);
mn_egr_se          = mean(egr_se,1);
std_egr_se         = std(egr_se,1);
mn_r_se           = mean(r_se,1);
std_r_se          = std(r_se,1);
mn_p_se           = mean(p_se,1);
std_p_se          = std(p_se,1);

% Save the output as a workspace
save SpeNetExt   p_se  all_se  igr_se egr_se  r_se  mn_all_se  std_all_se  mn_igr_se  std_igr_se mn_egr_se  std_egr_se mn_r_se  std_r_se  mn_p_se  std_p_se

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (IV) SPECIFIC NETWORK EXTERNALITIES W HOMOPHILY = 0.25 %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

h           = 0.25;                              % Homophily parameter 

% Create matrices to hold output over repetitions
p_seh25        = zeros(rp, max_time);              % Price matrix - Rows: Repetitions, Columns: Time
all_seh25      = zeros(rp, max_time);              % Overall adoption rate matrix over repetitions
igr_seh25      = zeros(rp, max_time, nig);         % Adoption rate matrix by income group
egr_seh25      = zeros(rp, max_time, neg);         % Adoption rate matrix by education group
r_seh25        = zeros(rp, max_time,2);            % Adoption rate matrix by race

disp('Specific Network Externalities with H=0.25 - Repetition #:');
for i=1:rp
    % Generate a network of relations
    [tie] = establish_ties_gss(h, rie);
    nrig  = rie(:,1:6);
    % Simulate adoption 
    [all, igr, egr, r, p]   = simulate_adoption_w_specific_net_ext_gss(tie, nrig, alpha, gamma, delta, k, init_price, max_time);
    % Store output
    p_seh25(i,:)           = p;
    all_seh25(i,:)         = all;
    for j=1:nig
        igr_seh25(i,:,j)    = igr(j,:);
    end
    for j=1:neg
        egr_seh25(i,:,j)    = egr(j,:);
    end
    r_seh25(i,:,1)         = r(1,:);
    r_seh25(i,:,2)         = r(2,:);
    disp(i);
end

% Summarize the results
mn_all_seh25         = mean(all_seh25,1);
std_all_seh25        = std(all_seh25,1);
mn_igr_seh25         = mean(igr_seh25,1);
std_igr_seh25        = std(igr_seh25,1);
mn_egr_seh25         = mean(egr_seh25,1);
std_egr_seh25        = std(egr_seh25,1);
mn_r_seh25           = mean(r_seh25,1);
std_r_seh25          = std(r_seh25,1);
mn_p_seh25           = mean(p_seh25,1);
std_p_seh25          = std(p_seh25,1);

% Save the output as a workspace
save SpeNetExtH25   p_seh25  all_seh25 igr_seh25 egr_seh25  r_seh25  mn_all_seh25  std_all_seh25  mn_igr_seh25 std_igr_seh25 mn_egr_seh25  std_egr_seh25  mn_r_seh25  std_r_seh25  mn_p_seh25  std_p_seh25

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (V) SPECIFIC NETWORK EXTERNALITIES W HOMOPHILY = 0.5 %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

h           = 0.5;                              % Homophily parameter 

% Create matrices to hold output over repetitions
p_seh5        = zeros(rp, max_time);              % Price matrix - Rows: Repetitions, Columns: Time
all_seh5      = zeros(rp, max_time);              % Overall adoption rate matrix over repetitions
igr_seh5      = zeros(rp, max_time, nig);         % Adoption rate matrix by income group
egr_seh5      = zeros(rp, max_time, neg);         % Adoption rate matrix by education group
r_seh5        = zeros(rp, max_time,2);            % Adoption rate matrix by race

disp('Specific Network Externalities with H=0.5 - Repetition #:');
for i=1:rp
    % Generate a network of relations
    [tie] = establish_ties_gss(h, rie);
    nrig  = rie(:,1:6);
    % Simulate adoption 
    [all, igr, egr, r, p]   = simulate_adoption_w_specific_net_ext_gss(tie, nrig, alpha, gamma, delta, k, init_price, max_time);
    % Store output
    p_seh5(i,:)           = p;
    all_seh5(i,:)         = all;
    for j=1:nig
        igr_seh5(i,:,j)    = igr(j,:);
    end
    for j=1:neg
        egr_seh5(i,:,j)    = egr(j,:);
    end
    r_seh5(i,:,1)         = r(1,:);
    r_seh5(i,:,2)         = r(2,:);
    disp(i);
end

% Summarize the results
mn_all_seh5         = mean(all_seh5,1);
std_all_seh5        = std(all_seh5,1);
mn_igr_seh5         = mean(igr_seh5,1);
std_igr_seh5        = std(igr_seh5,1);
mn_egr_seh5         = mean(egr_seh5,1);
std_egr_seh5        = std(egr_seh5,1);
mn_r_seh5           = mean(r_seh5,1);
std_r_seh5          = std(r_seh5,1);
mn_p_seh5           = mean(p_seh5,1);
std_p_seh5          = std(p_seh5,1);

% Save the output as a workspace
save SpeNetExtH5   p_seh5  all_seh5  igr_seh5 egr_seh5  r_seh5  mn_all_seh5  std_all_seh5  mn_igr_seh5  std_igr_seh5 mn_egr_seh5  std_egr_seh5  mn_r_seh5  std_r_seh5  mn_p_seh5  std_p_seh5

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (VI) SPECIFIC NETWORK EXTERNALITIES W HOMOPHILY = 0.5 %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

h           = 0.75;                              % Homophily parameter 

% Create matrices to hold output over repetitions
p_seh75        = zeros(rp, max_time);              % Price matrix - Rows: Repetitions, Columns: Time
all_seh75      = zeros(rp, max_time);              % Overall adoption rate matrix over repetitions
igr_seh75      = zeros(rp, max_time, nig);         % Adoption rate matrix by income group
egr_seh75      = zeros(rp, max_time, neg);         % Adoption rate matrix by education group
r_seh75        = zeros(rp, max_time,2);            % Adoption rate matrix by race

disp('Specific Network Externalities with H=0.75 - Repetition #:');
for i=1:rp
   % Generate a network of relations
    [tie] = establish_ties_gss(h, rie);
    nrig  = rie(:,1:6);
    % Simulate adoption 
    [all, igr, egr, r, p]   = simulate_adoption_w_specific_net_ext_gss(tie, nrig, alpha, gamma, delta, k, init_price, max_time);
    % Store output
    p_seh75(i,:)           = p;
    all_seh75(i,:)         = all;
    for j=1:nig
        igr_seh75(i,:,j)    = igr(j,:);
    end
    for j=1:neg
        egr_seh75(i,:,j)    = egr(j,:);
    end
    r_seh75(i,:,1)         = r(1,:);
    r_seh75(i,:,2)         = r(2,:);
    disp(i);
end

% Summarize the results
mn_all_seh75         = mean(all_seh75,1);
std_all_seh75        = std(all_seh75,1);
mn_igr_seh75         = mean(igr_seh75,1);
std_igr_seh75        = std(igr_seh75,1);
mn_egr_seh75         = mean(egr_seh75,1);
std_egr_seh75        = std(egr_seh75,1);
mn_r_seh75           = mean(r_seh75,1);
std_r_seh75          = std(r_seh75,1);
mn_p_seh75           = mean(p_seh75,1);
std_p_seh75          = std(p_seh75,1);

% Save the output as a workspace
save SpeNetExtH75   p_seh75  all_seh75  igr_seh75 egr_seh75  r_seh75  mn_all_seh75  std_all_seh75  mn_igr_seh75  std_igr_seh75 mn_egr_seh75  std_egr_seh75  mn_r_seh75  std_r_seh75  mn_p_seh75  std_p_seh75

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%   (VII) SPECIFIC NETWORK EXTERNALITIES W HOMOPHILY = 1   %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

h           = 1;                                % Homophily parameter 

% Create matrices to hold output over repetitions
p_seh1        = zeros(rp, max_time);              % Price matrix - Rows: Repetitions, Columns: Time
all_seh1      = zeros(rp, max_time);              % Overall adoption rate matrix over repetitions
igr_seh1      = zeros(rp, max_time, nig);         % Adoption rate matrix by income group
egr_seh1      = zeros(rp, max_time, neg);         % Adoption rate matrix by education group
r_seh1        = zeros(rp, max_time,2);            % Adoption rate matrix by race

disp('Specific Network Externalities with H=1.0 - Repetition #:');
for i=1:rp
   % Generate a network of relations
   [tie] = establish_ties_gss(h, rie);
   nrig  = rie(:,1:6); 
    % Simulate adoption 
    [all, igr, egr, r, p]   = simulate_adoption_w_specific_net_ext_gss(tie, nrig, alpha, gamma, delta, k, init_price, max_time);
    % Store output
    p_seh1(i,:)           = p;
    all_seh1(i,:)         = all;
    for j=1:nig
        igr_seh1(i,:,j)    = igr(j,:);
    end
    for j=1:neg
        egr_seh1(i,:,j)    = egr(j,:);
    end
    r_seh1(i,:,1)         = r(1,:);
    r_seh1(i,:,2)         = r(2,:); 
    disp(i);
end

% Summarize the results
mn_all_seh1         = mean(all_seh1,1);
std_all_seh1        = std(all_seh1,1);
mn_igr_seh1          = mean(igr_seh1,1);
std_igr_seh1         = std(igr_seh1,1);
mn_egr_seh1          = mean(egr_seh1,1);
std_egr_seh1         = std(egr_seh1,1);
mn_r_seh1           = mean(r_seh1,1);
std_r_seh1          = std(r_seh1,1);
mn_p_seh1           = mean(p_seh1,1);
std_p_seh1          = std(p_seh1,1);

% Save the output as a workspace
save SpeNetExtH1   p_seh1  all_seh1  igr_seh1 egr_seh1  r_seh1  mn_all_seh1  std_all_seh1  mn_igr_seh1  std_igr_seh1 mn_egr_seh1  std_egr_seh1  mn_r_seh1  std_r_seh1  mn_p_seh1  std_p_seh1

toc
