Matlab Code for Final Version
Transcript of Matlab Code for Final Version
Matlab code for
Moeltner, K., J. S. Shonkwiler, (forthcoming). Correcting for Size Biased Sampling in Random Utility Models. American Journal of Agricultural Economics.
% Main Script
% Fresh Script for jet ski paper for all models after getting Danielle's expanded data set
% this version w/o 7 persons that had >40 trips total.
% load e:\workspace\klaus\lil_tahoe\mlab\worksp\freshto_mlab2.raw;% data=[freshto_mlab2(:,end) freshto_mlab2(:,1:end-1)]; % bring running id back to front% clear freshto_mlab2;% save e:\workspace\klaus\lil_tahoe\mlab\worksp\freshdata2 data;% 'done'% break
load e:\workspace\klaus\lil_tahoe\mlab\worksp\freshdata2;load e:\workspace\klaus\lil_tahoe\mlab\worksp\h_mat1000; % that h_mat can handle up to 5 RCs
% Contents of data:
%1 % id running visitor panel id%2 % site lake index%3 % sur_y survey year%4 % own 1 if own jet ski, 0 if rented%5 % jetonly 1 if jet skiing = sole reason for visit%6 % jetprim 1 if other activities as well, but jet ski=primary%7 % milecost per mile autocost in cents%8 % trips trips this year to the six lakes%9 % dist distance traveled, one way%10 % gender gender, some missing values ("999")%11 % age age, some missing values%12 % educ education level, some missing values%13 % income in dollars,(category midpoint)%14 % sur_site site of interview%15 % food 1 if site has food services%16 % elev lake elevation in feet%17 % size surface area in acres%18 % shore length of shoreline in miles%19 % cantrent 1 if visitor = renter, but lake has no rental%20 % ban 1 = owned jet ski banned at Tahoe%21 % autocost =per-mile cost in $%22 % timecost time cost based on income/(3*2080); for round trip%23 % tc autocost + timecost%24:29 %site intercepts
% scale & explicitly label some variablesfood=data(:,15);elev=data(:,16)./100;% elevation in 1000ft or 100ft (try both)surface=(data(:,17)./639.5)/10; % translation: units of 10 squ. milesshore=data(:,18)./10; % units of 10 miles
1
shape =(10*surface)./(10*shore); % square miles / miles
% declare data to be used here to free memory later during loop
id = max(data(:,1)); % total number of respondentsd = max(data(:,2)); % total number of lakesn = id*d; % total number of observations
y=data(:,8);s=data(:,14); % lake of intercept
% compute new TCdist=data(:,9);own=data(:,4);timecost=data(:,22);tc=2*dist.*(0.4*own+0.3*(1-own))+timecost;
% Open log file [fid]=fopen('e:\workspace\Klaus\lil_tahoe\mlab\logs\freshmix_correctprice_dmcorronly.txt','w');if fid==-1; warning('File could not be opened');else; disp('File opened successfully');end;
header.title='Jet ski models';header.date=date;header.name='Klaus Moeltner';fprintf(fid,'Title:\t%s \n',header.title);fprintf(fid,'Date:\t\t%s \n',header.date);fprintf(fid,'Author:\t%s \n',header.name);fprintf(fid,'*******************************************************\n\n');header.model ='Uncorrected MNL';fprintf(fid,'Model:\t%s \n',header.model);fprintf(fid,'\n');fprintf(fid,'*******************************************************\n\n');jet.obs=n;fprintf(fid,'Number of observations =\t%6.0f \n',jet.obs);fprintf(fid,'\n');
r=1000;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Simple MNL model %
load e:\workspace\klaus\lil_tahoe\mlab\worksp\freshmnlmix40;clear X y;
X =[tc ... % price data(:,19:20) ... % lake restrictions data(:,25) ... % site intercept
2
elev surface]; % lake features
y=data(:,8);s=data(:,14); % lake of intercept
id = max(data(:,1)); % total number of respondentsd = max(data(:,2)); % total number of lakesn = id*d; % total number of observations
cf = 4; number of fixed coefficientscr = 2; total number of random coefficients, correlated or notc=cf+cr;cr_cov = 0; number of correlated random coefficients - unchanged from first roundcr_nocov = cr-cr_cov;
jet.rep=r;fprintf(fid,'Number of Halton replications =\t%4.0f\n',jet.rep);fprintf(fid,'\n');
% Starting valuesstartmean= [-0.25 ... %price -3.7 -2 ... % lake restrictions -0.9 ... % site intercept -0.05 0.03]'; % lake features jet.beta_in = startmean;fprintf(fid,'Starting mean vector for coefficients =\n\n');fprintf(fid,'%6.2f\n',jet.beta_in);fprintf(fid,'\n');
% declare full VCOV matrix for random coeffcients, with upper left corner for uncorrleated ones
int1 =0.001*ones(cr_nocov,1);int2 = diag(int1); % submatrix for uncorrelated RC'sint3 = zeros(cr_nocov, cr_cov);int4 = [int2 int3]; % creates upper part of full VCOVint5 = zeros(cr_cov, cr_nocov);int6 = 0.001*ones(cr_cov,1);int7 = diag(int6)+0.01 ;% submatrix for correlated coefficientsint8 = [int5 int7];vcov_in = [int4; int8] ;%works fineclear int*;
int =(chol(vcov_in)); %upper triangularvcovsend = nonzeros(int); %yields cr_nocov + (cr_cov*(cr_cov+1))/2 column vector with all non-zero elements%of int. collected as if from a lower Chol, row by rowS=sparse(int); %stores position of nonzero elements of int - useful to reconstruct it in llf
% para_in=[startmean;vcovsend];para_in=x;% para_in=[x(1:c);x(c+2:end)]; % omit Donner for second series of models
3
% Actual Maximization routinex0 = para_in;options=optimset('LargeScale','off','display','iter','TolFun',0.001,'TolX',0.001,'GradObj','off',... 'Hessian','off','DerivativeCheck','off');
tic, [x,fval,exitflag,output,grad,hessian]=... fminunc('jetllf_mnlmix',x0,options,X,y,h_mat,id,d,r,cf,cr,cr_cov,S)...,time=toc;
jet.run_time=time/60;jet.options=options;jet.output=output;jet.fval=fval;jet.exitflag=exitflag;
fprintf(fid,'Optimization Options:\n\n');fprintf(fid,'Large scale algorithm:\t%s\n',jet.options.LargeScale);fprintf(fid,'User supplied gradient:\t%s\n',jet.options.GradObj);fprintf(fid,'User supplied hessian:\t%s\n',jet.options.Hessian);fprintf(fid,'Termination tolerance on function level:\t%6.5f\n',jet.options.TolFun);fprintf(fid,'Termination tolerance on parameters:\t%6.5f\n',jet.options.TolX);fprintf(fid,'\n');fprintf(fid,'Optimization Output:\n\n');fprintf(fid,'Iterations:\t\t\t\t\t\t\t%6.0f\n',jet.output.iterations);fprintf(fid,'Function evaluations:\t\t\t\t\t%6.0f\n',jet.output.funcCount);fprintf(fid,'Infinity norm of gradient at solution:\t\t%6.4f\n',jet.output.firstorderopt);fprintf(fid,'Value of objective function at solution:\t\t%6.4f\n',jet.fval);fprintf(fid,'Algorithm:\t\t\t\t\t\t\t%s\n',jet.output.algorithm);fprintf(fid,'Exit condition (">0" = convergence):\t\t%1.0f\n',jet.exitflag);fprintf(fid,'\n');
% Conventional Hessian
H1=inv(hessian);d1 = diag(H1);jet.hess = H1; %capture, but don't print
results = x;se = sqrt(d1);t_val = results./se;
outmat=[x se t_val]ll=length(x);count=[1:100];jet.out = [count(1:ll);results';se';t_val'];
fprintf(fid,'Output table for jetmodel - simple MNL: \n\n');fprintf(fid,'Label\tCoeff.\ts.e.\t\tt-value\n\n');fprintf(fid,'% 2.0f\t%6.3f\t%6.3f\t%6.3f\n',jet.out);fprintf(fid,'\n');
fprintf(fid,'Elapsed time (in minutes):\t%6.2f\n',jet.run_time);fprintf(fid,'\n');
4
% White-corrected se's
L = L_mnlmix(x,X,y,h_mat,id,d,r,cf,cr,cr_cov,S); % save for later (Vuong test etc.)G = numgrad('L_mnlmix',x,X,y,h_mat,id,d,r,cf,cr,cr_cov,S);
int1=H1*G'*G*H1; % H already invertedint2=diag(int1);se=sqrt(int2);
jet.out2 = [1:length(x); x'; se'; (x./se)'];
fprintf(fid,'White-corrected output for jetmodel - simple MNL: \n\n');fprintf(fid,'Label\tCoeff.\ts.e.\t\tt-value\n\n');fprintf(fid,'% 2.0f\t%6.3f\t%6.3f\t%6.3f\n',jet.out2);fprintf(fid,'\n');
save e:\workspace\klaus\lil_tahoe\mlab\worksp\freshmnlmix40_1000 x X y jet L G H1;clear x jet L G H1;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Corrected MNL model
load e:\workspace\klaus\lil_tahoe\mlab\worksp\freshmnlcorrmix40;clear X y;
X =[tc ... % price data(:,19:20) ... % lake restrictions data(:,25) ... % site intercept elev surface]; % lake features
id = max(data(:,1)); % total number of respondentsd = max(data(:,2)); % total number of lakesn = id*d; % total number of observations
y=data(:,8);s=data(:,14); % lake of intercept
header.model ='Corrected MNL';fprintf(fid,'Model:\t%s \n',header.model);fprintf(fid,'\n');fprintf(fid,'*******************************************************\n\n');
% Starting values
jet.beta_in = startmean;fprintf(fid,'Starting mean vector for coefficients =\n\n');fprintf(fid,'%6.2f\n',jet.beta_in);fprintf(fid,'\n');
% declare full VCOV matrix for random coeffcients, with upper left corner for uncorrleated ones
5
int1 =0.001*ones(cr_nocov,1);int2 = diag(int1); % submatrix for uncorrelated RC'sint3 = zeros(cr_nocov, cr_cov);int4 = [int2 int3]; % creates upper part of full VCOVint5 = zeros(cr_cov, cr_nocov);int6 = 0.001*ones(cr_cov,1);int7 = diag(int6)+0.01 ;% submatrix for correlated coefficientsint8 = [int5 int7];vcov_in = [int4; int8] ;%works fineclear int*;
int =(chol(vcov_in)); %upper triangularvcovsend = nonzeros(int); %yields cr_nocov + (cr_cov*(cr_cov+1))/2 column vector with all non-zero elements%of int. collected as if from a lower Chol, row by rowS=sparse(int); %stores position of nonzero elements of int - useful to reconstruct it in llf
% para_in=[startmean;vcovsend];para_in=x;% para_in=[x(1:c);x(c+2:end)]; % omit Donner for second series of models
% Actual Maximization routinex0 = para_in;options=optimset('LargeScale','off','display','iter','TolFun',0.001,'TolX',0.001,'GradObj','off',... 'Hessian','off','DerivativeCheck','off');
tic, [x,fval,exitflag,output,grad,hessian]=... fminunc('jetllf_mnlcorrmix',x0,options,X,y,h_mat,s,id,d,r,cf,cr,cr_cov,S)...,time=toc;
jet.run_time=time/60;jet.options=options;jet.output=output;jet.fval=fval;jet.exitflag=exitflag;
fprintf(fid,'Optimization Options:\n\n');fprintf(fid,'Large scale algorithm:\t%s\n',jet.options.LargeScale);fprintf(fid,'User supplied gradient:\t%s\n',jet.options.GradObj);fprintf(fid,'User supplied hessian:\t%s\n',jet.options.Hessian);fprintf(fid,'Termination tolerance on function level:\t%6.5f\n',jet.options.TolFun);fprintf(fid,'Termination tolerance on parameters:\t%6.5f\n',jet.options.TolX);fprintf(fid,'\n');fprintf(fid,'Optimization Output:\n\n');fprintf(fid,'Iterations:\t\t\t\t\t\t\t%6.0f\n',jet.output.iterations);fprintf(fid,'Function evaluations:\t\t\t\t\t%6.0f\n',jet.output.funcCount);fprintf(fid,'Infinity norm of gradient at solution:\t\t%6.4f\n',jet.output.firstorderopt);fprintf(fid,'Value of objective function at solution:\t\t%6.4f\n',jet.fval);fprintf(fid,'Algorithm:\t\t\t\t\t\t\t%s\n',jet.output.algorithm);fprintf(fid,'Exit condition (">0" = convergence):\t\t%1.0f\n',jet.exitflag);fprintf(fid,'\n');
% Conventional Hessian
H1=inv(hessian);
6
d1 = diag(H1);jet.hess = H1; %capture, but don't print
results = x;se = sqrt(d1);t_val = results./se;
outmat=[x se t_val]ll=length(x);count=[1:100];jet.out = [count(1:ll);results';se';t_val'];
fprintf(fid,'Output table for jetmodel - corrected MNL: \n\n');fprintf(fid,'Label\tCoeff.\ts.e.\t\tt-value\n\n');fprintf(fid,'% 2.0f\t%6.3f\t%6.3f\t%6.3f\n',jet.out);fprintf(fid,'\n');
fprintf(fid,'Elapsed time (in minutes):\t%6.2f\n',jet.run_time);fprintf(fid,'\n');
% White-corrected se's
L = L_mnlcorrmix(x,X,y,h_mat,s,id,d,r,cf,cr,cr_cov,S); % save for later (Vuong test etc.)G = numgrad('L_mnlcorrmix',x,X,y,h_mat,s,id,d,r,cf,cr,cr_cov,S);
int1=H1*G'*G*H1; % H already invertedint2=diag(int1);se=sqrt(int2);
jet.out2 = [1:length(x); x'; se'; (x./se)'];
fprintf(fid,'White-corrected output for jetmodel: \n\n');fprintf(fid,'Label\tCoeff.\ts.e.\t\tt-value\n\n');fprintf(fid,'% 2.0f\t%6.3f\t%6.3f\t%6.3f\n',jet.out2);fprintf(fid,'\n');
save e:\workspace\klaus\lil_tahoe\mlab\worksp\freshmnlcorrmix40_1000 x X y jet L G H1;clear x jet L G H1;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Uncorrected DM model
load e:\workspace\klaus\lil_tahoe\mlab\worksp\freshdmmix40;clear X y;
X =[tc ... % price data(:,19:20) ... % lake restrictions data(:,25) ... % site intercept elev surface]; % lake features
y=data(:,8);s=data(:,14); % lake of intercept
7
id = max(data(:,1)); % total number of respondentsd = max(data(:,2)); % total number of lakesn = id*d; % total number of observations
header.model ='Uncorrected DM';fprintf(fid,'Model:\t%s \n',header.model);fprintf(fid,'\n');fprintf(fid,'*******************************************************\n\n');
% Starting valuesstartmean= [.8 -.2 -4 -1.4 -0.8 -0.05 -.05]'; % add starting value for sqrt(alpha)
jet.beta_in = startmean;fprintf(fid,'Starting mean vector for coefficients =\n\n');fprintf(fid,'%6.2f\n',jet.beta_in);fprintf(fid,'\n');
% declare full VCOV matrix for random coeffcients, with upper left corner for uncorrleated ones
int1 =0.001*ones(cr_nocov,1);int2 = diag(int1); % submatrix for uncorrelated RC'sint3 = zeros(cr_nocov, cr_cov);int4 = [int2 int3]; % creates upper part of full VCOVint5 = zeros(cr_cov, cr_nocov);int6 = 0.001*ones(cr_cov,1);int7 = diag(int6)+0.01 ;% submatrix for correlated coefficientsint8 = [int5 int7];vcov_in = [int4; int8] ;%works fineclear int*;
int =(chol(vcov_in)); %upper triangularvcovsend = nonzeros(int); %yields cr_nocov + (cr_cov*(cr_cov+1))/2 column vector with all non-zero elements%of int. collected as if from a lower Chol, row by rowS=sparse(int); %stores position of nonzero elements of int - useful to reconstruct it in llf
% para_in=[startmean;vcovsend];para_in=[startmean;0.13; 0.1];% para_in=[x(1:c);x(c+2:end)]; % omit Donner for second series of models
% Actual Maximization routinex0 = para_in;options=optimset('LargeScale','off','display','iter','TolFun',0.001,'TolX',0.001,'GradObj','off',... 'Hessian','off','DerivativeCheck','off');
tic, [x,fval,exitflag,output,grad,hessian]=... fminunc('jetllf_dmmixfresh',x0,options,X,y,h_mat,id,d,r,cf,cr,cr_cov,S)...,time=toc;
jet.run_time=time/60;jet.options=options;jet.output=output;
8
jet.fval=fval;jet.exitflag=exitflag;
fprintf(fid,'Optimization Options:\n\n');fprintf(fid,'Large scale algorithm:\t%s\n',jet.options.LargeScale);fprintf(fid,'User supplied gradient:\t%s\n',jet.options.GradObj);fprintf(fid,'User supplied hessian:\t%s\n',jet.options.Hessian);fprintf(fid,'Termination tolerance on function level:\t%6.5f\n',jet.options.TolFun);fprintf(fid,'Termination tolerance on parameters:\t%6.5f\n',jet.options.TolX);fprintf(fid,'\n');fprintf(fid,'Optimization Output:\n\n');fprintf(fid,'Iterations:\t\t\t\t\t\t\t%6.0f\n',jet.output.iterations);fprintf(fid,'Function evaluations:\t\t\t\t\t%6.0f\n',jet.output.funcCount);fprintf(fid,'Infinity norm of gradient at solution:\t\t%6.4f\n',jet.output.firstorderopt);fprintf(fid,'Value of objective function at solution:\t\t%6.4f\n',jet.fval);fprintf(fid,'Algorithm:\t\t\t\t\t\t\t%s\n',jet.output.algorithm);fprintf(fid,'Exit condition (">0" = convergence):\t\t%1.0f\n',jet.exitflag);fprintf(fid,'\n');
% Conventional Hessian
H1=inv(hessian);d1 = diag(H1);jet.hess = H1; %capture, but don't print
results = x;se = sqrt(d1);t_val = results./se;
outmat=[x se t_val]ll=length(x);count=[1:100];jet.out = [count(1:ll);results';se';t_val'];
fprintf(fid,'Output table for jetmodel - simple MNL: \n\n');fprintf(fid,'Label\tCoeff.\ts.e.\t\tt-value\n\n');fprintf(fid,'% 2.0f\t%6.3f\t%6.3f\t%6.3f\n',jet.out);fprintf(fid,'\n');
fprintf(fid,'Elapsed time (in minutes):\t%6.2f\n',jet.run_time);fprintf(fid,'\n');
% White-corrected se's
L = L_dmmix(x,X,y,h_mat,id,d,r,cf,cr,cr_cov,S); % save for later (Vuong test etc.)G = numgrad('L_dmmix',x,X,y,h_mat,id,d,r,cf,cr,cr_cov,S);
int1=H1*G'*G*H1; % H already invertedint2=diag(int1);se=sqrt(int2);
jet.out2 = [1:length(x); x'; se'; (x./se)'];
fprintf(fid,'White-corrected output for jetmodel: \n\n');fprintf(fid,'Label\tCoeff.\ts.e.\t\tt-value\n\n');fprintf(fid,'% 2.0f\t%6.3f\t%6.3f\t%6.3f\n',jet.out2);
9
fprintf(fid,'\n');
save e:\workspace\klaus\lil_tahoe\mlab\worksp\freshdmmix40_1000 x X y jet L G H1;clear x jet L G H1;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Corrected DM model
load e:\workspace\klaus\lil_tahoe\mlab\worksp\freshdmcorrmix40;clear X y;
X =[tc ... % price data(:,19:20) ... % lake restrictions data(:,25) ... % site intercept elev surface]; % lake features
y=data(:,8);s=data(:,14); % lake of intercept
id = max(data(:,1)); % total number of respondentsd = max(data(:,2)); % total number of lakesn = id*d; % total number of observations
header.model ='Corrected DM';fprintf(fid,'Model:\t%s \n',header.model);fprintf(fid,'\n');fprintf(fid,'*******************************************************\n\n');
jet.beta_in = startmean;fprintf(fid,'Starting mean vector for coefficients =\n\n');fprintf(fid,'%6.2f\n',jet.beta_in);fprintf(fid,'\n');
% para_in=[startmean;vcovsend]; % use same stuff from previous modelpara_in=[1 -0.1 -2 -2 -0.7 -0.04 0.03 0.1 0.1]';% para_in=[x(1:c);x(c+2:end)]; % omit Donner for second series of models
% Actual Maximization routinex0 = para_in;options=optimset('LargeScale','off','display','iter','TolFun',0.001,'TolX',0.001,'GradObj','off',... 'Hessian','off','DerivativeCheck','off');
tic, [x,fval,exitflag,output,grad,hessian]=... fminunc('jetllf_dmcorrmixfresh',x0,options,X,y,h_mat,s,id,d,r,cf,cr,cr_cov,S)...,time=toc;
jet.run_time=time/60;jet.options=options;jet.output=output;jet.fval=fval;jet.exitflag=exitflag;
fprintf(fid,'Optimization Options:\n\n');fprintf(fid,'Large scale algorithm:\t%s\n',jet.options.LargeScale);
10
fprintf(fid,'User supplied gradient:\t%s\n',jet.options.GradObj);fprintf(fid,'User supplied hessian:\t%s\n',jet.options.Hessian);fprintf(fid,'Termination tolerance on function level:\t%6.5f\n',jet.options.TolFun);fprintf(fid,'Termination tolerance on parameters:\t%6.5f\n',jet.options.TolX);fprintf(fid,'\n');fprintf(fid,'Optimization Output:\n\n');fprintf(fid,'Iterations:\t\t\t\t\t\t\t%6.0f\n',jet.output.iterations);fprintf(fid,'Function evaluations:\t\t\t\t\t%6.0f\n',jet.output.funcCount);fprintf(fid,'Infinity norm of gradient at solution:\t\t%6.4f\n',jet.output.firstorderopt);fprintf(fid,'Value of objective function at solution:\t\t%6.4f\n',jet.fval);fprintf(fid,'Algorithm:\t\t\t\t\t\t\t%s\n',jet.output.algorithm);fprintf(fid,'Exit condition (">0" = convergence):\t\t%1.0f\n',jet.exitflag);fprintf(fid,'\n');
% Conventional Hessian
H1=inv(hessian);d1 = diag(H1);jet.hess = H1; %capture, but don't print
results = x;se = sqrt(d1);t_val = results./se;
outmat=[x se t_val]ll=length(x);count=[1:100];jet.out = [count(1:ll);results';se';t_val'];
fprintf(fid,'Output table for jetmodel - simple MNL: \n\n');fprintf(fid,'Label\tCoeff.\ts.e.\t\tt-value\n\n');fprintf(fid,'% 2.0f\t%6.3f\t%6.3f\t%6.3f\n',jet.out);fprintf(fid,'\n');
fprintf(fid,'Elapsed time (in minutes):\t%6.2f\n',jet.run_time);fprintf(fid,'\n');
% White-corrected se's
L = L_dmcorrmix(x,X,y,h_mat,s,id,d,r,cf,cr,cr_cov,S); % save for later (Vuong test etc.)G = numgrad('L_dmcorrmix',x,X,y,h_mat,s,id,d,r,cf,cr,cr_cov,S);
int1=H1*G'*G*H1; % H already invertedint2=diag(int1);se=sqrt(int2);
jet.out2 = [1:length(x); x'; se'; (x./se)'];
fprintf(fid,'White-corrected output: \n\n');fprintf(fid,'Label\tCoeff.\ts.e.\t\tt-value\n\n');fprintf(fid,'% 2.0f\t%6.3f\t%6.3f\t%6.3f\n',jet.out2);fprintf(fid,'\n');
save e:\workspace\klaus\lil_tahoe\mlab\worksp\freshdmcorrmix40_1000 x X y jet L G H1;clear x jet L G H1;
11
st=fclose(fid);
if st==0; disp('File closed successfully');else; warning('Problem with closing file');end;
12
% Log-Likelihood Functions for all 4 Models
function [LL_all] = jetllf_mnlmix(x,X,y,h_mat,id,d,r,cf,cr,cr_cov,S)
% the incoming order of parameters in x is:
% fixed coefficients (alpha=first)% uncorrelated RCs% correlated RCs% Cholesky terms for RCs
% note: The shuffled Halton matrix is arranged in r stacked blocks of id by cr% columns; so the first id rows represent the consecutive list of id% individuals and their associated set of coefficients
n = id*d; % total number of observationsc = cf+cr; % total number of coefficientscr_nocov = cr-cr_cov; % number of uncorrelated random coefficients
b = x(1:c);% vector of coefficient means, including fixed coefficientsb_f = x(1:cf);% subvector of fixed coefficientsb_r = x(cf+1:cf+cr);% subvector of random coefficients
%re-construct chol(vcov_in) based on elements of x
A = zeros(cr,cr);int = full(S);%recreates upper cholesky matrixi=find(int>0 | int<0);A(i) = x(c+1:end); % re-creates upper cholesky in relation to xdel_sub = A'; %get lower choleskyclear int A;
h_mat = h_mat(:,1:cr);% h_mat is now (id x r) by crp_vec=zeros(id,1);
j=1;k=1;
for j=1:r
% draw betas for given rn_mat = (h_mat(k:j*id,1:cr))'; % cr by idint = repmat(b_r,1,id); % blow up random coeff vector to matrix with same dim. as n_matbr_mat = (int + del_sub*n_mat)';%id by crclear int;z=ones(d,1); br_mat=kron(br_mat,z);int = repmat(b_f',n,1);b_mat =[int br_mat];% n by cclear int br_mat;
% compute choice probabilities for given rp1 = exp(sum(X.*b_mat,2)); % exp(XB) with constant-w/in-individuals beta sets; size n by 1
13
f=find(isinf(p1)); p1(f)=1.0e+10;f=find(p1==0);p1(f)=1.0e-10;int = reshape(p1,d,id); int = sum(int); int = repmat(int,d,1); % blow up to d by t*idp2 = reshape(int,n,1); % stacks everything to be compatible with p1;p = p1./p2; % n by 1 vector of choice probabilities for each person, (day), and sitef=find(isinf(p)); p(f)=1.0e+10;f=find(p==0);p(f)=1.0e-10;clear p1 p2 int;
% Compute LLH component simultaneously for all individuals for given r
int1=reshape(y,d,id)'; %id by d like Scott'sint2=reshape(p,d,id)'; %id by d like Scott's
Y=sum(int1,2); % total number of trips per person, id by 1
p_add = exp(gammaln(Y+1)+sum(int1.*log(int2),2)-sum(gammaln(int1+1),2));
p_vec=p_vec+p_add;% f=find(isinf(p_vec)); % p_vec(f)=1.0e+10; % if length(f)>0% error('p_vec violation');% end
j=j+1;k=k+id; % move on to next set of betas
end
p_agg = p_vec/r; % get average of simulated probabilities for each person; id by 1f = find(p_agg==0);p_agg(f)=1.0e-10;% if length(f)>0% error('p_agg violation')% end
LL_all = -sum(log(p_agg));
function [LL_all] = jetllf_mnlcorrmix(x,X,y,h_mat,s,id,d,r,cf,cr,cr_cov,S)
% the incoming order of parameters in x is:
% fixed coefficients (alpha=first)
14
% uncorrelated RCs% correlated RCs% Cholesky terms for RCs
% note: The shuffled Halton matrix is arranged in r stacked blocks of id by cr% columns; so the first id rows represent the consecutive list of id% individuals and their associated set of coefficients
n = id*d; % total number of observationsc = cf+cr; % total number of coefficientscr_nocov = cr-cr_cov; % number of uncorrelated random coefficients
b = x(1:c);% vector of coefficient means, including fixed coefficientsb_f = x(1:cf);% subvector of fixed coefficientsb_r = x(cf+1:cf+cr);% subvector of random coefficients
%re-construct chol(vcov_in) based on elements of x
A = zeros(cr,cr);int = full(S);%recreates upper cholesky matrixi=find(int>0 | int<0);A(i) = x(c+1:end); % re-creates upper cholesky in relation to xdel_sub = A'; %get lower choleskyclear int A;
h_mat = h_mat(:,1:cr);% h_mat is now (id x r) by crp_vec=zeros(id,1);
j=1;k=1;
for j=1:r
% draw betas for given rn_mat = (h_mat(k:j*id,1:cr))'; % cr by idint = repmat(b_r,1,id); % blow up random coeff vector to matrix with same dim. as n_matbr_mat = (int + del_sub*n_mat)';%id by crclear int;z=ones(d,1); br_mat=kron(br_mat,z);int = repmat(b_f',n,1);b_mat =[int br_mat];% n by cclear int br_mat;
% compute choice probabilities for given rp1 = exp(sum(X.*b_mat,2)); % exp(XB) with constant-w/in-individuals beta sets; size n by 1f=find(isinf(p1)); p1(f)=1.0e+10;f=find(p1==0);p1(f)=1.0e-10;int = reshape(p1,d,id); int = sum(int); int = repmat(int,d,1); % blow up to d by t*idp2 = reshape(int,n,1); % stacks everything to be compatible with p1;p = p1./p2; % n by 1 vector of choice probabilities for each person, (day), and site
15
f=find(isinf(p)); p(f)=1.0e+10;f=find(p==0);p(f)=1.0e-10;clear p1 p2 int;
% Compute LLH component simultaneously for all individuals for given r
int1=reshape(y,d,id)'; %id by d like Scott'sint2=reshape(p,d,id)'; %id by d like Scott'sint3=reshape(s,d,id)'; % site of interview indicator
Y=sum(int1,2); % total number of trips per person, id by 1
p_add=exp(gammaln(Y+1)+sum(int1.*log(int2),2)-sum(gammaln(int1+1),2)+ log(sum(int1.*int3,2))... -log(sum(int2.*int3,2))-log(Y));
p_vec=p_vec+p_add;% f=find(isinf(p_vec)); % p_vec(f)=1.0e+10; % if length(f)>0% error('p_vec violation');% end
j=j+1;k=k+id; % move on to next set of betas
end
p_agg = p_vec/r; % get average of simulated probabilities for each person; id by 1f = find(p_agg==0);p_agg(f)=1.0e-10;% if length(f)>0% error('p_agg violation')% end
LL_all = -sum(log(p_agg));
function [LL_all] = jetllf_dmmixfresh(x,X,y,h_mat,id,d,r,cf,cr,cr_cov,S)
% this is with fixed alpha, in contrast to jetllf_dm_mix% the incoming order of parameters in x is:
% fixed coefficients, not counting alpha% uncorrelated RCs, not counting alpha% correlated RCs, not counting alpha% mean of log(alpha)% Cholesky terms for RCs, not counting alpha
16
% standard deviation for log(alpha)
% note: The shuffled Halton matrix is arranged in r stacked blocks of id by cr+1% columns; so the first id rows represent the consecutive list of id% individuals and their associated set of coefficients
n = id*d; % total number of observationsc = cf+cr; % total number of coefficients, excluding alphacr_nocov = cr-cr_cov; % number of uncorrelated random coefficients
b = x(2:c+1);% vector of coefficient means, including fixed coefficientsb_f = x(2:cf+1);% subvector of fixed coefficientsb_r = x(cf+2:cf+1+cr);% subvector of random coefficients
a=1/(x(1)^2);
%re-construct chol(vcov_in) based on elements of x
A = zeros(cr,cr);int = full(S);%recreates upper cholesky matrixi=find(int>0 | int<0);A(i) = x(c+2:end); % re-creates upper cholesky in relation to xdel_sub = A'; %get lower choleskyclear int A;
h_mat = h_mat(:,1:cr);p_vec=zeros(id,1);
j=1;k=1;
for j=1:r
% draw betas for given rn_mat = (h_mat(k:j*id,1:cr))'; % cr by idint = repmat(b_r,1,id); % blow up random coeff vector to matrix with same dim. as n_matbr_mat = (int + del_sub*n_mat)';%id by crclear int;z=ones(d,1); %6 by 1br_mat=kron(br_mat,z);int = repmat(b_f',n,1);b_mat =[int br_mat];% n by cclear int br_mat;
% compute choice probabilities for given rp1 = exp(sum(X.*b_mat,2)); % exp(XB) with constant-w/in-individuals beta sets; size n by 1f=find(isinf(p1)); p1(f)=1.0e+10;f=find(p1==0);p1(f)=1.0e-10;int = reshape(p1,d,id); int = sum(int); int = repmat(int,d,1); % blow up to d by t*id
17
p2 = reshape(int,n,1); % stacks everything to be compatible with p1;p = p1./p2; % n by 1 vector of choice probabilities for each person, (day), and siteclear p1 p2 int;
% Compute LLH component simultaneously for all individuals for given r
int1=reshape(y,d,id)'; %id by d like Scott'sint2=reshape(p,d,id)'; %id by d like Scott's
Y=sum(int1,2); % total number of trips per person, id by 1
p_add = exp(gammaln(Y+1)+gammaln(a)-gammaln(Y+a)+sum(gammaln(int1+a*int2),2)... -sum(gammaln(int1+1),2)-sum(gammaln(a*int2),2));
p_vec=p_vec+p_add;f=find(isinf(p_vec)); p_vec(f)=1.0e+10; % if length(f)>0% error('p_vec violation');% end
j=j+1;k=k+id; % move on to next person
end
p_agg = p_vec/r; % get average of simulated probabilities for each person; id by 1f = find(p_agg==0);p_agg(f)=1.0e-10;% if length(f)>0% error('p_agg violation')% end
LL_all = -sum(log(p_agg));
function [LL_all] = jetllf_dmcorrmixfresh(x,X,y,h_mat,s,id,d,r,cf,cr,cr_cov,S)
% fixed alpha in contrst to original function% the incoming order of parameters in x is:
% fixed coefficients, not counting alpha% uncorrelated RCs, not counting alpha% correlated RCs, not counting alpha% mean of log(alpha)% Cholesky terms for RCs, not counting alpha% standard deviation for log(alpha)
% note: The shuffled Halton matrix is arranged in r stacked blocks of id by cr+1% columns; so the first id rows represent the consecutive list of id% individuals and their associated set of coefficients
18
n = id*d; % total number of observationsc = cf+cr; % total number of coefficients, excluding alphacr_nocov = cr-cr_cov; % number of uncorrelated random coefficients
b = x(2:c+1);% vector of coefficient means, including fixed coefficientsb_f = x(2:cf+1);% subvector of fixed coefficientsb_r = x(cf+2:cf+1+cr);% subvector of random coefficients
a=1/(x(1)^2);
%re-construct chol(vcov_in) based on elements of x
A = zeros(cr,cr);int = full(S);%recreates upper cholesky matrixi=find(int>0 | int<0);A(i) = x(c+2:end); % re-creates upper cholesky in relation to xdel_sub = A'; %get lower choleskyclear int A;
h_mat = h_mat(:,1:cr);p_vec=zeros(id,1);
j=1;k=1;
for j=1:r
% draw betas for given rn_mat = (h_mat(k:j*id,1:cr))'; % cr by idint = repmat(b_r,1,id); % blow up random coeff vector to matrix with same dim. as n_matbr_mat = (int + del_sub*n_mat)';%id by crclear int;z=ones(d,1); %6 by 1br_mat=kron(br_mat,z);int = repmat(b_f',n,1);b_mat =[int br_mat];% n by cclear int br_mat;
% compute choice probabilities for given rp1 = exp(sum(X.*b_mat,2)); % exp(XB) with constant-w/in-individuals beta sets; size n by 1f=find(isinf(p1)); p1(f)=1.0e+10;f=find(p1==0);p1(f)=1.0e-10;int = reshape(p1,d,id); int = sum(int); int = repmat(int,d,1); % blow up to d by t*idp2 = reshape(int,n,1); % stacks everything to be compatible with p1;p = p1./p2; % n by 1 vector of choice probabilities for each person, (day), and siteclear p1 p2 int;f=find(isinf(p)); p(f)=1.0e+10;f=find(p==0);
19
p(f)=1.0e-10;clear p1 p2 int;
% Compute LLH component simultaneously for all individuals for given r
int1=reshape(y,d,id)'; %id by d like Scott'sint2=reshape(p,d,id)'; %id by d like Scott'sint3=reshape(s,d,id)'; % site of interview indicator
Y=sum(int1,2); % total number of trips per person, id by 1
p_add=exp(gammaln(Y+1)+gammaln(a)-gammaln(Y+a)+sum(gammaln(int1+a*int2),2)... -sum(gammaln(int1+1),2)-sum(gammaln(a*int2),2)+log(sum(int1.*int3,2))... -log(sum(int2.*int3,2))-log(Y));
f=find(isinf(p_add)); p_add(f)=1.0e+10;f=find(p_add==0);p_add(f)=1.0e-10;
p_vec=p_vec+p_add;f=find(isinf(p_vec)); p_vec(f)=1.0e+10; % if length(f)>0% error('p_vec violation');% end
j=j+1;k=k+id; % move on to next set of id elements of h_mat
end
p_agg = p_vec/r; % get average of simulated probabilities for each person; id by 1f = find(p_agg==0);p_agg(f)=1.0e-10;% if length(f)>0% error('p_agg violation')% end
LL_all = -sum(log(p_agg));
(note: the L_- functions in the main script are identical to the likelihood functions, except for the last line, which reads
LL =(log(p_agg));
So the LLHF returns the negative sum of individual LLH elements (since "fminunc" mimimizes functions). The L-function returns the entire n by 1 vector of individual LLH elements
20
% Numerical gradient
function[G] = numgrad(FUN,x0,varargin);
% PURPOSE: computes numerical Jacobian matrix%% INPUTS:
% FUN = Handle or name for function that generates a n x 1 vector of % individual LLH components% Note: That function is specific to a given application & should be % generated & saved w/in that application% x0 = k x 1 vector of betas% varargin = all inputs needed for evaluating FUN, other than x0 % % % OUTPUT: G = n x k numerical Jacobian%% Written by:% Klaus Moeltner% Department of Applied Economics & Statistics% University of Nevada, Reno% based on GAUSS' "gradp" command
L0 = feval(FUN,x0,varargin{:}); % n by 1n=length(L0);k = length(x0);
G=zeros(n,k);
% Compute step size (amount of perturbation of x0)
ax0 = abs(x0);dax0 = x0./ax0;dh=(1e-8)*max([ax0 1e-2*ones(k,1)]')'.*dax0; % k x 1xdh = x0+dh;dh = xdh-x0; % apparently increases precision
int1 = diag(dh); % k by k square matrix with xdh on diagonalint2 = repmat(x0,1,k); % k by karg = int2+int1;
i=1;for i=1:k; G(:,i)=feval(FUN,arg(:,i),varargin{:});end
int1=repmat(L0,1,k); % n by kint2=repmat(dh',n,1);G = (G-int1)./int2;
21
% Halton Draws
function[h]=halton(n,q);
% creates a halton sequence a la Feenberg and Skinner% n= length of sequence% q=prime number
phi=zeros(n+11,1);i=1;
while i<n+11; i=i+1; y=1/q; x=1-phi(i-1); while x<=(y+1.e-11) y=y/q; end phi(i)=phi(i-1)+(q+1)*y-1;end
h=phi(12:n+11);
clear i y x phi
% Shuffling of Haltons
function[M]=shuffle(h_mat,n,r,cr)
% Program to reshuffle basic matrix of norminv(Halton) a la Train% for simple cross-sectional setup
% Inputs: h_mat = (n * r) by cr matrix of norminv(Halton)% n = number of individuals% r = number of reps% cr = number of random coefficients
% Output: a matrix M of exact same dimensions (n*r,cr), but reshuffled such that% first individual gets first row of obs for each vector of repetitions, second indiv gets second% row etc.
M = zeros(n,r*cr);
i=1;k=1;for i=1:n;int = h_mat(k:i*r,:); %first r rows of h_matint = reshape(int,1,r*cr);% same as stacking all columns and flipping them into one long rowM(i,:)=int;k=k+r;i=i+1;
22
end
M = reshape(M,n*r,cr);
% works fine - tested it!!% example original matrix; n=4,r=3,cr=3(number of columns)% 1 101 201% 2 102 202% 3 103 203% 4 104 204% 5 105 205% 6 106 206% 7 107 207% 8 108 208% 9 109 209% 10 110 210% 11 111 211% 12 112 212 % after shuffling% 1 101 201% 4 104 204% 7 107 207% 10 110 210% 2 102 202% 5 105 205% 8 108 208% 11 111 211% 3 103 203% 6 106 206% 9 109 209% 12 112 212
23
% Post-Estimation Script
% this script is for Scott's suggested strategy to examine the asymptotic% properties of the differences of means
% Open log file [fid]=fopen('e:\workspace\klaus\lil_tahoe\mlab\logs\welfare_take3.txt','w');if fid==-1; warning('File could not be opened');else; disp('File opened successfully');end;
load e:\workspace\klaus\lil_tahoe\mlab\worksp\freshdata2;
id = max(data(:,1)); % total number of respondentsd = max(data(:,2)); % total number of lakesn = id*d; % total number of observations
y=data(:,8);s=data(:,14); % lake of interceptsite=data(:,2);
clear data;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% sample counts per site%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
f=find(s==1);g=find(s==0);
f1=find(site==1);f2=find(site==2);f3=find(site==3);f4=find(site==4);f5=find(site==5);f6=find(site==6);
sitef=site(f);f1f=find(sitef==1);f2f=find(sitef==2);f3f=find(sitef==3);f4f=find(sitef==4);f5f=find(sitef==5);f6f=find(sitef==6);
siteg = site(g);f1g = find(siteg==1);f2g = find(siteg==2);f3g = find(siteg==3);f4g = find(siteg==4);f5g = find(siteg==5);f6g = find(siteg==6);
24
load e:\workspace\klaus\lil_tahoe\mlab\worksp\postest5;
trip51 = trip1;trip52 = trip2;trip53 = trip3;trip54 = trip4;trip55 = trip5;trip56 = trip6;
pt51 = pt1;pt52 = pt2;pt53 = pt3;pt54 = pt4;pt55 = pt5;pt56 = pt6;
clear trip1 trip2 trip3 trip4 trip5 trip6 pt1 pt2 pt3 pt4 pt5 pt6;
load e:\workspace\klaus\lil_tahoe\mlab\worksp\postest6;
trip61 = trip1;trip62 = trip2;trip63 = trip3;trip64 = trip4;trip65 = trip5;trip66 = trip6;
pt61 = pt1;pt62 = pt2;pt63 = pt3;pt64 = pt4;pt65 = pt5;pt66 = pt6;
clear trip1 trip2 trip3 trip4 trip5 trip6 pt1 pt2 pt3 pt4 pt5 pt6;
load e:\workspace\klaus\lil_tahoe\mlab\worksp\postest7;
trip71 = trip1;trip72 = trip2;trip73 = trip3;trip74 = trip4;trip75 = trip5;trip76 = trip6;
pt71 = pt1;pt72 = pt2;pt73 = pt3;pt74 = pt4;pt75 = pt5;pt76 = pt6;
25
clear trip1 trip2 trip3 trip4 trip5 trip6 pt1 pt2 pt3 pt4 pt5 pt6;
load e:\workspace\klaus\lil_tahoe\mlab\worksp\postest8;
trip81 = trip1;trip82 = trip2;trip83 = trip3;trip84 = trip4;trip85 = trip5;trip86 = trip6;
pt81 = pt1;pt82 = pt2;pt83 = pt3;pt84 = pt4;pt85 = pt5;pt86 = pt6;
clear trip1 trip2 trip3 trip4 trip5 trip6 pt1 pt2 pt3 pt4 pt5 pt6;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% trips%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
E=cov([trip51 trip61 trip71 trip81]);M=mean([trip51 trip61 trip71 trip81]);x5=M(1);x6=M(2);x7=M(3);x8=M(4);v5=E(1,1);v6=E(2,2);v7=E(3,3);v8=E(4,4);cov58=E(1,end);cov68=E(2,end);cov78=E(3,end);d58=x5-x8;d68=x6-x8;d78=x7-x8;s58=sqrt((v5+v8-2*cov58)/id);s68=sqrt((v6+v8-2*cov68)/id);s78=sqrt((v7+v8-2*cov78)/id);D1=[d58 s58 d68 s68 d78 s78];
E=cov([trip52 trip62 trip72 trip82]);M=mean([trip52 trip62 trip72 trip82]);x5=M(1);x6=M(2);x7=M(3);x8=M(4);v5=E(1,1);v6=E(2,2);v7=E(3,3);v8=E(4,4);cov58=E(1,end);
26
cov68=E(2,end);cov78=E(3,end);d58=x5-x8;d68=x6-x8;d78=x7-x8;s58=sqrt((v5+v8-2*cov58)/id);s68=sqrt((v6+v8-2*cov68)/id);s78=sqrt((v7+v8-2*cov78)/id);D2=[d58 s58 d68 s68 d78 s78];
E=cov([trip53 trip63 trip73 trip83]);M=mean([trip53 trip63 trip73 trip83]);x5=M(1);x6=M(2);x7=M(3);x8=M(4);v5=E(1,1);v6=E(2,2);v7=E(3,3);v8=E(4,4);cov58=E(1,end);cov68=E(2,end);cov78=E(3,end);d58=x5-x8;d68=x6-x8;d78=x7-x8;s58=sqrt((v5+v8-2*cov58)/id);s68=sqrt((v6+v8-2*cov68)/id);s78=sqrt((v7+v8-2*cov78)/id);D3=[d58 s58 d68 s68 d78 s78];
E=cov([trip54 trip64 trip74 trip84]);M=mean([trip54 trip64 trip74 trip84]);x5=M(1);x6=M(2);x7=M(3);x8=M(4);v5=E(1,1);v6=E(2,2);v7=E(3,3);v8=E(4,4);cov58=E(1,end);cov68=E(2,end);cov78=E(3,end);d58=x5-x8;d68=x6-x8;d78=x7-x8;s58=sqrt((v5+v8-2*cov58)/id);s68=sqrt((v6+v8-2*cov68)/id);s78=sqrt((v7+v8-2*cov78)/id);D4=[d58 s58 d68 s68 d78 s78];
E=cov([trip55 trip65 trip75 trip85]);M=mean([trip55 trip65 trip75 trip85]);x5=M(1);x6=M(2);x7=M(3);
27
x8=M(4);v5=E(1,1);v6=E(2,2);v7=E(3,3);v8=E(4,4);cov58=E(1,end);cov68=E(2,end);cov78=E(3,end);d58=x5-x8;d68=x6-x8;d78=x7-x8;s58=sqrt((v5+v8-2*cov58)/id);s68=sqrt((v6+v8-2*cov68)/id);s78=sqrt((v7+v8-2*cov78)/id);D5=[d58 s58 d68 s68 d78 s78];
E=cov([trip56 trip66 trip76 trip86]);M=mean([trip55 trip65 trip76 trip86]);x5=M(1);x6=M(2);x7=M(3);x8=M(4);v5=E(1,1);v6=E(2,2);v7=E(3,3);v8=E(4,4);cov58=E(1,end);cov68=E(2,end);cov78=E(3,end);d58=x5-x8;d68=x6-x8;d78=x7-x8;s58=sqrt((v5+v8-2*cov58)/id);s68=sqrt((v6+v8-2*cov68)/id);s78=sqrt((v7+v8-2*cov78)/id);D6=[d58 s58 d68 s68 d78 s78];
Dtrips=[D1;D2;D3;D4;D5;D6];Dtrips=[(1:1:6); Dtrips']
fprintf(fid,'trip differences: \n\n');fprintf(fid,'site\td58\t\ts58\t\td68\t\ts68\t\td78\t\ts78\n\n');fprintf(fid,'% 2.0f\t%6.3f\t%6.3f\t%6.3f\t%6.3f\t%6.3f\t%6.3f\n',Dtrips);fprintf(fid,'\n');fprintf(fid,'\n');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% welfare%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
E=cov([pt51 pt61 pt71 pt81]);M=mean([pt51 pt61 pt71 pt81]);x5=M(1);x6=M(2);x7=M(3);x8=M(4);
28
v5=E(1,1);v6=E(2,2);v7=E(3,3);v8=E(4,4);cov58=E(1,end);cov68=E(2,end);cov78=E(3,end);d58=x5-x8;d68=x6-x8;d78=x7-x8;s58=sqrt((v5+v8-2*cov58)/id);s68=sqrt((v6+v8-2*cov68)/id);s78=sqrt((v7+v8-2*cov78)/id);D1=[d58 s58 d68 s68 d78 s78];
E=cov([pt52 pt62 pt72 pt82]);M=mean([pt52 pt62 pt72 pt82]);x5=M(1);x6=M(2);x7=M(3);x8=M(4);v5=E(1,1);v6=E(2,2);v7=E(3,3);v8=E(4,4);cov58=E(1,end);cov68=E(2,end);cov78=E(3,end);d58=x5-x8;d68=x6-x8;d78=x7-x8;s58=sqrt((v5+v8-2*cov58)/id);s68=sqrt((v6+v8-2*cov68)/id);s78=sqrt((v7+v8-2*cov78)/id);D2=[d58 s58 d68 s68 d78 s78];
E=cov([pt53 pt63 pt73 pt83]);M=mean([pt53 pt63 pt73 pt83]);x5=M(1);x6=M(2);x7=M(3);x8=M(4);v5=E(1,1);v6=E(2,2);v7=E(3,3);v8=E(4,4);cov58=E(1,end);cov68=E(2,end);cov78=E(3,end);d58=x5-x8;d68=x6-x8;d78=x7-x8;s58=sqrt((v5+v8-2*cov58)/id);s68=sqrt((v6+v8-2*cov68)/id);s78=sqrt((v7+v8-2*cov78)/id);D3=[d58 s58 d68 s68 d78 s78];
29
E=cov([pt54 pt64 pt74 pt84]);M=mean([pt54 pt64 pt74 pt84]);x5=M(1);x6=M(2);x7=M(3);x8=M(4);v5=E(1,1);v6=E(2,2);v7=E(3,3);v8=E(4,4);cov58=E(1,end);cov68=E(2,end);cov78=E(3,end);d58=x5-x8;d68=x6-x8;d78=x7-x8;s58=sqrt((v5+v8-2*cov58)/id);s68=sqrt((v6+v8-2*cov68)/id);s78=sqrt((v7+v8-2*cov78)/id);D4=[d58 s58 d68 s68 d78 s78];
E=cov([pt55 pt65 pt75 pt85]);M=mean([pt55 pt65 pt75 pt85]);x5=M(1);x6=M(2);x7=M(3);x8=M(4);v5=E(1,1);v6=E(2,2);v7=E(3,3);v8=E(4,4);cov58=E(1,end);cov68=E(2,end);cov78=E(3,end);d58=x5-x8;d68=x6-x8;d78=x7-x8;s58=sqrt((v5+v8-2*cov58)/id);s68=sqrt((v6+v8-2*cov68)/id);s78=sqrt((v7+v8-2*cov78)/id);D5=[d58 s58 d68 s68 d78 s78];
E=cov([pt56 pt66 pt76 pt86]);M=mean([pt56 pt66 pt76 pt86]);x5=M(1);x6=M(2);x7=M(3);x8=M(4);v5=E(1,1);v6=E(2,2);v7=E(3,3);v8=E(4,4);cov58=E(1,end);cov68=E(2,end);cov78=E(3,end);d58=x5-x8;d68=x6-x8;
30
d78=x7-x8;s58=sqrt((v5+v8-2*cov58)/id);s68=sqrt((v6+v8-2*cov68)/id);s78=sqrt((v7+v8-2*cov78)/id);D6=[d58 s58 d68 s68 d78 s78];
Dpt=[D1;D2;D3;D4;D5;D6];Dpt=[(1:1:6); Dpt']
fprintf(fid,'welfare differences: \n\n');fprintf(fid,'site\td58\t\ts58\t\td68\t\ts68\t\td78\t\ts78\n\n');fprintf(fid,'% 2.0f\t%6.3f\t%6.3f\t%6.3f\t%6.3f\t%6.3f\t%6.3f\n',Dpt);fprintf(fid,'\n');fprintf(fid,'\n');
st=fclose(fid);
if st==0; disp('File closed successfully');else; warning('Problem with closing file');end;
31