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 1
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
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;
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');
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;
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');
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;
% 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');
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;
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');
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);
% 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_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
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_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_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
% 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;
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;
% 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;
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%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%