options crt; name nest3 'General 3-Level Nested Logit Program Structure' ; ? ? general 3-level nested logit program structure ? TSP 4.2B (November 1994) ? Author: Paul Ruud ?============================================================ ? using nomenclature the nomenclature of Axel Boersch-Supan, L stands ? for Limbs, B for Branches, and T for Twigs ? to modify this code for a particular structure, one must ? (1) change the list statements below to specify no. of ? options at each node ? (2) change the frml statements below to compute the exponentials ? of the corresponding inclusive values ? (3) initialize the parameters, and impose normalizations ? (a) tau's are coefficients for Limbs; tau1 for Limb 1, ? tau2 for Limb 2, and so on ? (b) theta's are coefficients for Branches; theta11 for ? Limb 1 on Branch 1, theta12 for Limb 2 on Branch ? 1, and so on list listL 1-3 ; frml Lt L1 + L2 + L3 ; list listB1 1 ; frml Bt1 B11 ; list listT11 1-2 ; frml Tt11 T111 + T112 ; list listB2 1-3 ; frml Bt2 B21 + B22 + B23 ; list listT21 1-2 ; frml Tt21 T211 + T212 ; list listT22 1-2 ; frml Tt22 T221 + T222 ; list listT23 1-3 ; frml Tt23 T231 + T232 + T233 ; list listB3 1-2 ; frml Bt3 B31 + B32 ; list listT31 1 ; frml Tt31 T311 ; list listT32 1-2 ; frml Tt32 T321 + T322 ; const tau1 1 ; ? due to assumed tree structure param tau2 1 tau3 1 ; param theta11 1; param theta21 1 theta22 1 theta23 1 ; const theta31 1 ; ? due to assumed tree structure param theta32 1 ; param b1 b2 ; frml ellf llf = ll1 + ll11 + ll111 + ll112 + ll2 + ll21 + ll211 + ll212 + ll22 + ll221 + ll222 + ll23 + ll231 + ll232 + ll233 + ll3 + ll31 + ll311 + ll32 + ll321 + ll322 -log(Lt) ; eqsub ellf Lt; dot listL ; frml ll. y.*(log(L.) - log(Bt.)); frml L. exp(w.*tau.) ; frml w. log(Bt.) ; eqsub ellf ll. L. w. Bt. ; frml tau tau. ; dot listB. ; frml ll.. y..*(log(B..) - log(Tt..)) ; frml B.. exp(v..*theta../tau) ; frml v.. log(Tt..) ; eqsub ellf ll.. B.. v.. Tt.. ; frml theta theta.. ; dot listT.. ; frml ll... y...*log(T...) ; frml T... exp(xb.../theta) ; frml xb... x1...*b1 + x2...*b2 ; eqsub ellf ll... T... xb... ; enddot ; eqsub ellf theta ; enddot ; eqsub ellf tau ; enddot ; ? print ellf ; ? this will print out the LLF in all its glory ?============================================================ ? generate a test data set ? normally distributed characteristics ? i.i.d. standard normal disturbances ? coefficients equal magnitude and opposite sign random(seedin=498724) ; smpl 1 1000 ; genr vmax = -1e30 ; dot listL ; dot listB. ; dot listT.. ; random x1... ; random x2... ; random e... ; genr v... = x1... - x2... + e... ; genr test = vmax>=v... ; genr vmax = vmax*test + v...*(1-test) ; enddot ; enddot ; enddot ; dot listL ; genr u = 0 ; dot listB. ; genr t = 0 ; dot listT.. ; genr y... = (vmax<=v...) ; genr t = t + y... ; enddot ; genr y.. = t ; genr u = u + t ; enddot ; genr y. = u ; enddot ; ?============================================================ ? try it out ml ellf ;