Biogeme: Python Library  2.5
cnl.py
Go to the documentation of this file.
1 ## \file
2 # Functions for the cross-nested logit model
3 
4 from biogeme import *
5 from mev import *
6 
7 ## Implements the cross-nested logit model as a MEV model.
8 # \ingroup models
9 # \param V A <a
10 # href="http://docs.python.org/py3k/tutorial/datastructures.html#dictionaries"
11 # target="_blank">dictionary</a> mapping each alternative id with the
12 # expression of the utility function.
13 # \param availability A <a
14 # href="http://docs.python.org/py3k/tutorial/datastructures.html#dictionaries"
15 # target="_blank">dictionary</a> mapping each alternative id with its
16 # availability condition.
17 # \param nests A <a
18 # href="http://docs.python.org/py3k/tutorial/datastructures.html#tuples-and-sequences">tuple</a>
19 # containing as many items as nests. Each item is also a <a
20 # href="http://docs.python.org/py3k/tutorial/datastructures.html#tuples-and-sequences">tuple</a>
21 # containing two items:
22 # - An <a href="http://biogeme.epfl.ch/expressions.html">expression</a>
23 # representing the nest parameter.
24 # - A <a
25 # href="http://docs.python.org/py3k/tutorial/datastructures.html#dictionaries"
26 # target="_blank">dictionary</a> mapping the alternative ids with the cross-nested parameters for the corresponding nest.
27 # Example with two nests and 6 alternatives:
28 # \code
29 #alphaA = {1: alpha1a,
30 # 2: alpha2a,
31 # 3: alpha3a,
32 # 4: alpha4a,
33 # 5: alpha5a,
34 # 6: alpha6a}
35 #alphaB = {1: alpha1b,
36 # 2: alpha2b,
37 # 3: alpha3b,
38 # 4: alpha4b,
39 # 5: alpha5b,
40 # 6: alpha6b}
41 #nesta = MUA , alphaA
42 #nestb = MUB , alphaB
43 #nests = nesta, nestb
44 # \endcode
45 # \return Choice probability for the cross-nested logit model.
46 #
47 def cnl_avail(V,availability,nests,choice) :
48  Gi = {}
49  Gidict = {}
50  for k in V:
51  Gidict[k] = []
52  for m in nests:
53  biosumlist = []
54  for i,a in m[1].items():
55  biosumlist.append(Elem({0:0,1:a**(m[0]) * exp(m[0] * (V[i]))},availability[i] != 0))
56  biosum = bioMultSum(biosumlist)
57  for i,a in m[1].items():
58  Gidict[i].append(Elem({0:0,1:(biosum**((1.0/m[0])-1.0)) * (a**m[0]) * exp((m[0]-1.0)*(V[i]))},availability[i] != 0))
59  for k in V:
60  Gi[k] = bioMultSum(Gidict[k])
61  P = mev(V,Gi,availability,choice)
62  return P
63 
64 
65 ## Implements the log of the cross-nested logit model as a MEV model.
66 # \ingroup models
67 # \param V A <a
68 # href="http://docs.python.org/py3k/tutorial/datastructures.html#dictionaries"
69 # target="_blank">dictionary</a> mapping each alternative id with the
70 # expression of the utility function.
71 # \param availability A <a
72 # href="http://docs.python.org/py3k/tutorial/datastructures.html#dictionaries"
73 # target="_blank">dictionary</a> mapping each alternative id with its
74 # availability condition.
75 # \param nests A <a
76 # href="http://docs.python.org/py3k/tutorial/datastructures.html#tuples-and-sequences">tuple</a>
77 # containing as many items as nests. Each item is also a <a
78 # href="http://docs.python.org/py3k/tutorial/datastructures.html#tuples-and-sequences">tuple</a>
79 # containing two items:
80 # - An <a href="http://biogeme.epfl.ch/expressions.html">expression</a>
81 # representing the nest parameter.
82 # - A <a
83 # href="http://docs.python.org/py3k/tutorial/datastructures.html#dictionaries"
84 # target="_blank">dictionary</a> mapping the alternative ids with the cross-nested parameters for the corresponding nest.
85 # Example with two nests and 6 alternatives:
86 # \code
87 #alphaA = {1: alpha1a,
88 # 2: alpha2a,
89 # 3: alpha3a,
90 # 4: alpha4a,
91 # 5: alpha5a,
92 # 6: alpha6a}
93 #alphaB = {1: alpha1b,
94 # 2: alpha2b,
95 # 3: alpha3b,
96 # 4: alpha4b,
97 # 5: alpha5b,
98 # 6: alpha6b}
99 #nesta = MUA , alphaA
100 #nestb = MUB , alphaB
101 #nests = nesta, nestb
102 # \endcode
103 # \return Choice probability for the cross-nested logit model.
104 #
105 def logcnl_avail(V,availability,nests,choice) :
106  Gi = {}
107  Gidict = {}
108  for k in V:
109  Gidict[k] = []
110  for m in nests:
111  biosumlist = []
112  for i,a in m[1].items():
113  biosumlist.append(Elem({0:0,1:a**(m[0]) * exp(m[0] * (V[i]))},availability[i] != 0))
114  biosum = bioMultSum(biosumlist)
115  for i,a in m[1].items():
116  Gidict[i].append(Elem({0:0,1:(biosum**((1.0/m[0])-1.0)) * (a**m[0]) * exp((m[0]-1.0)*(V[i]))},availability[i] != 0))
117  for k in V:
118  Gi[k] = bioMultSum(Gidict[k])
119  logP = logmev(V,Gi,availability,choice)
120  return logP
121 
122 
123 ## Implements the cross-nested logit model as a MEV model with the homogeneity parameters is explicitly involved
124 # \ingroup models
125 # \param V A <a
126 # href="http://docs.python.org/py3k/tutorial/datastructures.html#dictionaries"
127 # target="_blank">dictionary</a> mapping each alternative id with the
128 # expression of the utility function.
129 # \param availability A <a
130 # href="http://docs.python.org/py3k/tutorial/datastructures.html#dictionaries"
131 # target="_blank">dictionary</a> mapping each alternative id with its
132 # availability condition.
133 # \param nests A <a
134 # href="http://docs.python.org/py3k/tutorial/datastructures.html#tuples-and-sequences">tuple</a>
135 # containing as many items as nests. Each item is also a <a
136 # href="http://docs.python.org/py3k/tutorial/datastructures.html#tuples-and-sequences">tuple</a>
137 # containing two items:
138 # - An <a href="http://biogeme.epfl.ch/expressions.html">expression</a>
139 # representing the nest parameter.
140 # - A <a
141 # href="http://docs.python.org/py3k/tutorial/datastructures.html#dictionaries"
142 # target="_blank">dictionary</a> mapping the alternative ids with the cross-nested parameters for the corresponding nest.
143 # Example with two nests and 6 alternatives:
144 # \code
145 #alphaA = {1: alpha1a,
146 # 2: alpha2a,
147 # 3: alpha3a,
148 # 4: alpha4a,
149 # 5: alpha5a,
150 # 6: alpha6a}
151 #alphaB = {1: alpha1b,
152 # 2: alpha2b,
153 # 3: alpha3b,
154 # 4: alpha4b,
155 # 5: alpha5b,
156 # 6: alpha6b}
157 #nesta = MUA , alphaA
158 #nestb = MUB , alphaB
159 #nests = nesta, nestb
160 # \endcode
161 # \param bmu Homogeneity parameter \f$\mu\f$.
162 # \return Choice probability for the cross-nested logit model.
163 def cnlmu(V,availability,nests,choice,bmu) :
164  Gi = {}
165  Gidict = {}
166  for k in V:
167  Gidict[k] = []
168  for m in nests:
169  biosumdict = []
170  for i,a in m[1].items():
171  biosumdict.append(Elem({0:0,1:a**(m[0]/bmu) * exp(m[0] * (V[i]))},availability[i] != 0))
172  biosum = bioMultSum(biosumdict)
173  for i,a in m[1].items():
174  Gidict[i].append(Elem({0:0,1:bmu * (biosum**((bmu/m[0])-1.0)) * (a**(m[0]/bmu)) * exp((m[0]-1.0)*(V[i]))},availability[i] != 0))
175  for k in V:
176  Gi[k] = bioMultSum(Gidict[k])
177  P = mev(V,Gi,availability,choice)
178  return P
179 
180 ## Implements the log of the cross-nested logit model as a MEV model with the homogeneity parameters is explicitly involved
181 # \ingroup models
182 # \param V A <a
183 # href="http://docs.python.org/py3k/tutorial/datastructures.html#dictionaries"
184 # target="_blank">dictionary</a> mapping each alternative id with the
185 # expression of the utility function.
186 # \param availability A <a
187 # href="http://docs.python.org/py3k/tutorial/datastructures.html#dictionaries"
188 # target="_blank">dictionary</a> mapping each alternative id with its
189 # availability condition.
190 # \param nests A <a
191 # href="http://docs.python.org/py3k/tutorial/datastructures.html#tuples-and-sequences">tuple</a>
192 # containing as many items as nests. Each item is also a <a
193 # href="http://docs.python.org/py3k/tutorial/datastructures.html#tuples-and-sequences">tuple</a>
194 # containing two items:
195 # - An <a href="http://biogeme.epfl.ch/expressions.html">expression</a>
196 # representing the nest parameter.
197 # - A <a
198 # href="http://docs.python.org/py3k/tutorial/datastructures.html#dictionaries"
199 # target="_blank">dictionary</a> mapping the alternative ids with the cross-nested parameters for the corresponding nest.
200 # Example with two nests and 6 alternatives:
201 # \code
202 #alphaA = {1: alpha1a,
203 # 2: alpha2a,
204 # 3: alpha3a,
205 # 4: alpha4a,
206 # 5: alpha5a,
207 # 6: alpha6a}
208 #alphaB = {1: alpha1b,
209 # 2: alpha2b,
210 # 3: alpha3b,
211 # 4: alpha4b,
212 # 5: alpha5b,
213 # 6: alpha6b}
214 #nesta = MUA , alphaA
215 #nestb = MUB , alphaB
216 #nests = nesta, nestb
217 # \endcode
218 # \param bmu Homogeneity parameter \f$\mu\f$.
219 # \return Log of choice probability for the cross-nested logit model.
220 def logcnlmu(V,availability,nests,choice,bmu) :
221  Gi = {}
222  Gidict = {}
223  for k in V:
224  Gidict[k] = []
225  for m in nests:
226  biosumlist = []
227  for i,a in m[1].items():
228  biosumlist.append(Elem({0:0,1:a**(m[0]/bmu) * exp(m[0] * (V[i]))},availability[i] != 0))
229  biosum = bioMultSum(biosumlist)
230  for i,a in m[1].items():
231  Gidict[i].append(Elem({0:0,1:bmu * (biosum**((bmu/m[0])-1.0)) * (a**(m[0]/bmu)) * exp((m[0]-1.0)*(V[i]))},availability[i] != 0))
232  for k in V:
233  Gi[k] = bioMultSum(Gidict[k])
234  logP = logmev(V,Gi,availability,choice)
235  return logP
236 
def logmev(V, Gi, av, choice)
Log of the choice probability for a MEV model.
Definition: mev.py:75
Definition: mev.py:1
def cnlmu(V, availability, nests, choice, bmu)
Implements the cross-nested logit model as a MEV model with the homogeneity parameters is explicitly ...
Definition: cnl.py:163
def cnl_avail(V, availability, nests, choice)
Implements the cross-nested logit model as a MEV model.
Definition: cnl.py:47
def logcnl_avail(V, availability, nests, choice)
Implements the log of the cross-nested logit model as a MEV model.
Definition: cnl.py:105
def logcnlmu(V, availability, nests, choice, bmu)
Implements the log of the cross-nested logit model as a MEV model with the homogeneity parameters is ...
Definition: cnl.py:220
Copyright 2016 Michel Bierlaire