Biogeme: Python Library  2.5
logit.py
Go to the documentation of this file.
1 ## \file
2 # Functions for the logit model
3 
4 from biogeme import *
5 
6 ## Compute the logit choice probability with utilities scaled according to the group
7 # @ingroup models
8 # @param V A <a href="http://docs.python.org/py3k/tutorial/datastructures.html#dictionaries" target="_blank">dictionary</a> mapping each alternative id with the
9 # expression of the utility function.
10 # @param availability A <a href="http://docs.python.org/py3k/tutorial/datastructures.html#dictionaries" target="_blank">dictionary</a> mapping each alternative id with its availability condition.
11 # @param choice expression producing the id of the chosen alternative.
12 # @param group id of the group, or market segment.
13 # @param scale A <a href="http://docs.python.org/py3k/tutorial/datastructures.html#dictionaries" target="_blank">dictionary</a> mapping each group id with the expression providing the corresponding scale.
14 # @return Choice probability for chosen alternative \f$i\f$ and individual in group \f$g\f$: \f[ \frac{e^{\mu_g V_i}}{\sum_j a_j e^{\mu_g V_j}}\f] where \f$a_j\f$ is 1 if alternative \f$j\f$ is available, 0 otherwise, and \f$\mu_g\f$ is the scale parameter associated with group \f$g\f$.
15 def logit_av_scale(V,availability,choice,group,scale):
16  s = Elem(scale,group)
17  scaledV = {}
18  availBinary = {}
19  for k,v in V.items() :
20  availBinary[k] = int(availability[k] != 0)
21  scaledV[k] = s * v
22  P = bioLogit(scaledV,availability,choice)
23  return P
24 
25 ## Compute the logit choice probability, where all alternatives are available.
26 # @ingroup biogeme
27 # @ingroup models
28 # @param V A <a href="http://docs.python.org/py3k/tutorial/datastructures.html#dictionaries" target="_blank">dictionary</a> mapping each alternative id with the
29 # expression of the utility function.
30 # @param choice expression producing the id of the chosen alternative.
31 # @return Choice probability for chosen alternative \f$i\f$ and individual in group \f$g\f$: \f[ \frac{e^{V_i}}{\sum_j e^{V_j}}\f]
32 def logit(V,choice) :
33  avail = {}
34  for k,v in V.items() :
35  avail[k] = 1
36  P = bioLogit(V,avail,choice)
37  return P
38 
39 ## Compute the logit choice probability with utilities scaled according to the group
40 # @ingroup biogeme
41 # @ingroup models
42 # @param V A <a href="http://docs.python.org/py3k/tutorial/datastructures.html#dictionaries" target="_blank">dictionary</a> mapping each alternative id with the
43 # expression of the utility function.
44 # @param choice expression producing the id of the chosen alternative.
45 # @param group id of the group, or market segment.
46 # @param scale A <a href="http://docs.python.org/py3k/tutorial/datastructures.html#dictionaries" target="_blank">dictionary</a> mapping each group id with the expression providing the corresponding scale.
47 # @return Choice probability for chosen alternative \f$i\f$ and individual in group \f$g\f$: \f[ \frac{e^{\mu_g V_i}}{\sum_j e^{\mu_g V_j}}\f] where \f$\mu_g\f$ is the scale parameter associated with group \f$g\f$.
48 def logit_scale(V,choice,group,scale) :
49  s = Elem(scale,group)
50  chosen = Elem(V,choice)
51  den = 0
52  for i,v in V.items() :
53  den += exp(s * (v-chosen))
54  P = 1.0 / den
55  return P
56 
57 
58 
59 
def logit(V, choice)
Compute the logit choice probability, where all alternatives are available.
Definition: logit.py:32
def logit_av_scale(V, availability, choice, group, scale)
Compute the logit choice probability with utilities scaled according to the group.
Definition: logit.py:15
def logit_scale(V, choice, group, scale)
Compute the logit choice probability with utilities scaled according to the group.
Definition: logit.py:48
Copyright 2016 Michel Bierlaire