Biogeme: Python Library  2.5
nested.py
Go to the documentation of this file.
1 ## \file
2 # Functions for the nested logit model
3 
4 from biogeme import *
5 from mev import *
6 
7 ## Implements the MEV generating function for the nested logit 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 href="http://docs.python.org/py3k/tutorial/introduction.html#lists">list</a>
25 # containing the list of identifiers of the alternatives belonging to
26 # the nest.
27 # Example:
28 # @code
29 # nesta = MUA , [1,2,3]
30 # nestb = MUB , [4,5,6]
31 # nests = nesta, nestb
32 # @endcode
33 # @return A <a
34 # href="http://docs.python.org/py3k/tutorial/datastructures.html#dictionaries"
35 # target="_blank">dictionary</a> mapping each alternative id with the function
36 # \f[
37 # \frac{\partial G}{\partial y_i}(e^{V_1},\ldots,e^{V_J}) = e^{(\mu_m-1)V_i} \left(\sum_{i=1}^{J_m} e^{\mu_m V_i}\right)^{\frac{1}{\mu_m}-1}
38 #\f]
39 # where \f$m\f$ is the (only) nest containing alternative \f$i\f$, and
40 # \f$G\f$ is the MEV generating function.
41 #
42 def getMevForNested(V,availability,nests) :
43 
44  y = {}
45  for i,v in V.items() :
46  y[i] = exp(v)
47 
48  Gi = {}
49  for m in nests:
50  sumdict = []
51  for i in m[1]:
52  sumdict.append(Elem({0:0.0,1: y[i] ** m[0]},availability[i]!=0))
53  sum = bioMultSum(sumdict)
54  for i in m[1]:
55  Gi[i] = Elem({0:0,1:y[i]**(m[0]-1.0) * sum ** (1.0/m[0] - 1.0)},availability[i]!=0)
56  return Gi
57 
58 
59 ## Implements the nested logit model as a MEV model.
60 # @ingroup models
61 # @param V A <a
62 # href="http://docs.python.org/py3k/tutorial/datastructures.html#dictionaries"
63 # target="_blank">dictionary</a> mapping each alternative id with the
64 # expression of the utility function.
65 # @param availability A <a
66 # href="http://docs.python.org/py3k/tutorial/datastructures.html#dictionaries"
67 # target="_blank">dictionary</a> mapping each alternative id with its
68 # availability condition.
69 # @param nests A <a
70 # href="http://docs.python.org/py3k/tutorial/datastructures.html#tuples-and-sequences">tuple</a>
71 # containing as many items as nests. Each item is also a <a
72 # href="http://docs.python.org/py3k/tutorial/datastructures.html#tuples-and-sequences">tuple</a>
73 # containing two items:
74 # - An <a href="http://biogeme.epfl.ch/expressions.html">expression</a>
75 # representing the nest parameter.
76 # - A <a href="http://docs.python.org/py3k/tutorial/introduction.html#lists">list</a>
77 # containing the list of identifiers of the alternatives belonging to
78 # the nest.
79 # Example:
80 # @code
81 # nesta = MUA , [1,2,3]
82 # nestb = MUB , [4,5,6]
83 # nests = nesta, nestb
84 # @endcode
85 # @param choice expression producing the id of the chosen alternative.
86 # @return Choice probability for the nested logit model, based on the
87 # derivatives of the MEV generating function produced by the function
88 # nested::getMevForNested
89 #
90 def nested(V,availability,nests,choice) :
91  Gi = getMevForNested(V,availability,nests)
92  P = mev(V,Gi,availability,choice)
93  return P
94 
95 ## Implements the log of a nested logit model as a MEV model.
96 # @ingroup models
97 # @param V A <a
98 # href="http://docs.python.org/py3k/tutorial/datastructures.html#dictionaries"
99 # target="_blank">dictionary</a> mapping each alternative id with the
100 # expression of the utility function.
101 # @param availability A <a
102 # href="http://docs.python.org/py3k/tutorial/datastructures.html#dictionaries"
103 # target="_blank">dictionary</a> mapping each alternative id with its
104 # availability condition.
105 # @param nests A <a
106 # href="http://docs.python.org/py3k/tutorial/datastructures.html#tuples-and-sequences">tuple</a>
107 # containing as many items as nests. Each item is also a <a
108 # href="http://docs.python.org/py3k/tutorial/datastructures.html#tuples-and-sequences">tuple</a>
109 # containing two items:
110 # - An <a href="http://biogeme.epfl.ch/expressions.html">expression</a>
111 # representing the nest parameter.
112 # - A <a href="http://docs.python.org/py3k/tutorial/introduction.html#lists">list</a>
113 # containing the list of identifiers of the alternatives belonging to
114 # the nest.
115 # Example:
116 # @code
117 # nesta = MUA , [1,2,3]
118 # nestb = MUB , [4,5,6]
119 # nests = nesta, nestb
120 # @endcode
121 # @param choice expression producing the id of the chosen alternative.
122 # @return Log of choice probability for the nested logit model, based on the
123 # derivatives of the MEV generating function produced by the function
124 # nested::getMevForNested
125 #
126 def lognested(V,availability,nests,choice) :
127  Gi = getMevForNested(V,availability,nests)
128  logP = logmev(V,Gi,availability,choice)
129  return logP
130 
131 
132 
133 ## Implements the nested logit model as a MEV model, where mu is also a
134 ## parameter, if the user wants to test different normalization
135 ## schemes.
136 # @ingroup models
137 # @param V A <a
138 # href="http://docs.python.org/py3k/tutorial/datastructures.html#dictionaries"
139 # target="_blank">dictionary</a> mapping each alternative id with the
140 # expression of the utility function.
141 # @param availability A <a
142 # href="http://docs.python.org/py3k/tutorial/datastructures.html#dictionaries"
143 # target="_blank">dictionary</a> mapping each alternative id with its
144 # availability condition.
145 # @param nests A <a
146 # href="http://docs.python.org/py3k/tutorial/datastructures.html#tuples-and-sequences">tuple</a>
147 # containing as many items as nests. Each item is also a <a
148 # href="http://docs.python.org/py3k/tutorial/datastructures.html#tuples-and-sequences">tuple</a>
149 # containing two items:
150 # - An <a href="http://biogeme.epfl.ch/expressions.html">expression</a>
151 # representing the nest parameter.
152 # - A <a href="http://docs.python.org/py3k/tutorial/introduction.html#lists">list</a>
153 # containing the list of identifiers of the alternatives belonging to
154 # the nest.
155 # Example:
156 # @code
157 # nesta = MUA , [1,2,3]
158 # nestb = MUB , [4,5,6]
159 # nests = nesta, nestb
160 # @endcode
161 # @param choice expression producing the id of the chosen alternative.
162 # @param mu expression producing the value of the top-level scale parameter.
163 # @return The nested logit choice probability based on the following derivatives of the MEV generating function:
164 # \f[
165 # \frac{\partial G}{\partial y_i}(e^{V_1},\ldots,e^{V_J}) = \mu e^{(\mu_m-1)V_i} \left(\sum_{i=1}^{J_m} e^{\mu_m V_i}\right)^{\frac{\mu}{\mu_m}-1}
166 #\f]
167 # where \f$m\f$ is the (only) nest containing alternative \f$i\f$, and
168 # \f$G\f$ is the MEV generating function.
169 #
170 def nestedMevMu(V,availability,nests,choice,mu) :
171 
172  y = {}
173  for i,v in V.items() :
174  y[i] = exp(v)
175 
176  Gi = {}
177  for m in nests:
178  sum = {}
179  for i in m[1]:
180  sum[i] = Elem({0:0,1: y[i] ** m[0]},availability[i]!=0)
181  for i in m[1]:
182  Gi[i] = Elem({0:0,1:mu * y[i]**(m[0]-1.0) * bioMultSum(sum) ** (mu/m[0] - 1.0)},availability[i]!=0)
183  P = mev(V,Gi,availability,choice)
184  return P
185 
186 
187 ## Implements the log of the nested logit model as a MEV model, where mu is also a
188 ## parameter, if the user wants to test different normalization
189 ## schemes.
190 # @ingroup models
191 # @param V A <a
192 # href="http://docs.python.org/py3k/tutorial/datastructures.html#dictionaries"
193 # target="_blank">dictionary</a> mapping each alternative id with the
194 # expression of the utility function.
195 # @param availability A <a
196 # href="http://docs.python.org/py3k/tutorial/datastructures.html#dictionaries"
197 # target="_blank">dictionary</a> mapping each alternative id with its
198 # availability condition.
199 # @param nests A <a
200 # href="http://docs.python.org/py3k/tutorial/datastructures.html#tuples-and-sequences">tuple</a>
201 # containing as many items as nests. Each item is also a <a
202 # href="http://docs.python.org/py3k/tutorial/datastructures.html#tuples-and-sequences">tuple</a>
203 # containing two items:
204 # - An <a href="http://biogeme.epfl.ch/expressions.html">expression</a>
205 # representing the nest parameter.
206 # - A <a href="http://docs.python.org/py3k/tutorial/introduction.html#lists">list</a>
207 # containing the list of identifiers of the alternatives belonging to
208 # the nest.
209 # Example:
210 # @code
211 # nesta = MUA , [1,2,3]
212 # nestb = MUB , [4,5,6]
213 # nests = nesta, nestb
214 # @endcode
215 # @param choice expression producing the id of the chosen alternative.
216 # @param mu expression producing the value of the top-level scale parameter.
217 # @return The nested logit choice probability based on the following derivatives of the MEV generating function:
218 # \f[
219 # \frac{\partial G}{\partial y_i}(e^{V_1},\ldots,e^{V_J}) = \mu e^{(\mu_m-1)V_i} \left(\sum_{i=1}^{J_m} e^{\mu_m V_i}\right)^{\frac{\mu}{\mu_m}-1}
220 #\f]
221 # where \f$m\f$ is the (only) nest containing alternative \f$i\f$, and
222 # \f$G\f$ is the MEV generating function.
223 #
224 def lognestedMevMu(V,availability,nests,choice,mu) :
225 
226  y = {}
227  for i,v in V.items() :
228  y[i] = exp(v)
229 
230  Gi = {}
231  for m in nests:
232  sum = {}
233  for i in m[1]:
234  sum[i] = Elem({0:0,1: y[i] ** m[0]},availability[i]!=0)
235  for i in m[1]:
236  Gi[i] = Elem({0:0,1:mu * y[i]**(m[0]-1.0) * bioMultSum(sum) ** (mu/m[0] - 1.0)},availability[i]!=0)
237  logP = logmev(V,Gi,availability,choice)
238  return logP
def logmev(V, Gi, av, choice)
Log of the choice probability for a MEV model.
Definition: mev.py:75
Definition: mev.py:1
def lognested(V, availability, nests, choice)
Implements the log of a nested logit model as a MEV model.
Definition: nested.py:126
def getMevForNested(V, availability, nests)
Implements the MEV generating function for the nested logit model.
Definition: nested.py:42
def lognestedMevMu(V, availability, nests, choice, mu)
Implements the log of the nested logit model as a MEV model, where mu is also a parameter, if the user wants to test different normalization schemes.
Definition: nested.py:224
def nested(V, availability, nests, choice)
Implements the nested logit model as a MEV model.
Definition: nested.py:90
def nestedMevMu(V, availability, nests, choice, mu)
Implements the nested logit model as a MEV model, where mu is also a parameter, if the user wants to ...
Definition: nested.py:170
Copyright 2016 Michel Bierlaire