Skip to content

Nested Logit

nested_logit module

The components of the derivative of the entropy for a two-layer nested logit model. One nest on each side must consist of the 0 option. The other nests are specified as nested lists. E.g. [[1, 3], [2,4]] describes two nests, one with types 1 and 3, and the other with types 2 and 4. On each side, the nests are the same for each type, with the same parameters.

e0_derivative_mu_nested_logit(muhat, additional_parameters=None)

Returns the derivatives of the parameter-independent part \(e_0\) wrt \(\mu\) for the nested logit.

Parameters:

Name Type Description Default
muhat Matching

a Matching

required
additional_parameters list | None

a list with the nest structure

None

Returns:

Type Description
ThreeArrays

the parameter-independent part of the hessian of the entropy

ThreeArrays

wrt \((\mu,\mu)\).

Source code in cupid_matching/nested_logit.py
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
def e0_derivative_mu_nested_logit(
    muhat: Matching, additional_parameters: list | None = None
) -> ThreeArrays:
    """Returns the derivatives of the parameter-independent part $e_0$
    wrt $\\mu$ for the nested logit.

    Args:
        muhat: a Matching
        additional_parameters: a list with the nest structure

    Returns:
        the parameter-independent part of the hessian of the entropy
        wrt $(\\mu,\\mu)$.
    """
    nests_for_each_x, nests_for_each_y = _get_params(additional_parameters)
    nests_x = change_indices(nests_for_each_x)
    nests_y = change_indices(nests_for_each_y)
    muxy, mux0, mu0y, *_ = muhat.unpack()
    X, Y = muxy.shape

    hess_x = np.zeros((X, Y, Y))
    hess_y = np.zeros((X, Y, X))
    hess_xy = np.zeros((X, Y))
    der_logx0 = 1.0 / mux0
    der_log0y = 1.0 / mu0y

    for x in range(X):
        dlogx0 = der_logx0[x]
        for nest in nests_x:
            mu_xn = np.sum(muxy[x, nest])
            der_logxn = 1.0 / mu_xn
            for y in nest:
                hess_x[x, y, :] = -dlogx0
                hess_x[x, y, nest] -= der_logxn
    for y in range(Y):
        dlog0y = der_log0y[y]
        for nest in nests_y:
            mu_ny = np.sum(muxy[nest, y])
            der_logny = 1.0 / mu_ny
            for x in nest:
                hess_y[x, y, :] = -dlog0y
                hess_y[x, y, nest] -= der_logny
    for x in range(X):
        for y in range(Y):
            hess_xy[x, y] = hess_x[x, y, y] + hess_y[x, y, x]

    return hess_x, hess_y, hess_xy

e0_derivative_r_nested_logit(muhat, additional_parameters=None)

Returns the derivatives of the parameter-independent part \(e_0\) wrt \(r\) for the nested logit.

Parameters:

Name Type Description Default
muhat Matching

a Matching

required
additional_parameters list | None

a list with the nest structure

None

Returns:

Type Description
TwoArrays

the parameter-independent part of the hessian of the entropy

TwoArrays

wrt \((\mu,r)\).

Source code in cupid_matching/nested_logit.py
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
def e0_derivative_r_nested_logit(
    muhat: Matching, additional_parameters: list | None = None
) -> TwoArrays:
    """Returns the derivatives of the parameter-independent part $e_0$
    wrt $r$ for the nested logit.

    Args:
        muhat: a Matching
        additional_parameters: a list with the nest structure

    Returns:
        the parameter-independent part of the hessian of the entropy
        wrt $(\\mu,r)$.
    """
    nests_for_each_x, nests_for_each_y = _get_params(additional_parameters)
    nests_x = change_indices(nests_for_each_x)
    nests_y = change_indices(nests_for_each_y)
    muxy, mux0, mu0y, n, m = muhat.unpack()
    X, Y = muxy.shape

    hess_n = np.zeros((X, Y))
    hess_m = np.zeros((X, Y))
    der_logx0 = 1.0 / mux0
    der_log0y = 1.0 / mu0y

    for x in range(X):
        dlogx0 = der_logx0[x]
        for nest in nests_x:
            for y in nest:
                hess_n[x, y] = dlogx0
    for y in range(Y):
        dlog0y = der_log0y[y]
        for nest in nests_y:
            for x in nest:
                hess_m[x, y] = dlog0y

    return hess_n, hess_m

e0_nested_logit(muhat, additional_parameters=None)

Returns the values of the parameter-independent part \(e_0\) for the nested logit.

Parameters:

Name Type Description Default
muhat Matching

a Matching

required
additional_parameters list | None

a list with the nest structure

None

Returns:

Type Description
np.ndarray

the (X,Y) matrix of the parameter-independent part

np.ndarray

of the first derivative of the entropy.

Source code in cupid_matching/nested_logit.py
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
def e0_nested_logit(
    muhat: Matching, additional_parameters: list | None = None
) -> np.ndarray:
    """Returns the values of the parameter-independent part $e_0$
    for the nested logit.

    Args:
        muhat: a Matching
        additional_parameters: a list with the nest structure

    Returns:
        the (X,Y) matrix of the parameter-independent part
        of the first derivative of the entropy.
    """
    nests_for_each_x, nests_for_each_y = _get_params(additional_parameters)
    nests_x = change_indices(nests_for_each_x)
    nests_y = change_indices(nests_for_each_y)
    muxy, mux0, mu0y, *_ = muhat.unpack()
    X, Y = muxy.shape
    e0_vals = np.zeros((X, Y))

    for x in range(X):
        mux0_x = mux0[x]
        for nest in nests_x:
            mu_xn = np.sum(muxy[x, nest])
            e0_vals[x, nest] = -log(mu_xn / mux0_x)
    for y in range(Y):
        mu0y_y = mu0y[y]
        for nest in nests_y:
            mu_ny = np.sum(muxy[nest, y])
            e0_vals[nest, y] -= log(mu_ny / mu0y_y)
    return e0_vals

e_derivative_mu_nested_logit(muhat, additional_parameters=None)

Returns the derivatives of the parameter-dependent part \(e\) wrt \(\mu\) for the nested logit.

Parameters:

Name Type Description Default
muhat Matching

a Matching

required
additional_parameters list | None

a list with the nest structure

None

Returns:

Type Description
ThreeArrays

the parameter-dependent part of the hessian of the entropy

ThreeArrays

wrt \((\mu,\mu)\).

Source code in cupid_matching/nested_logit.py
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
def e_derivative_mu_nested_logit(
    muhat: Matching, additional_parameters: list | None = None
) -> ThreeArrays:
    """Returns the derivatives of the parameter-dependent part $e$
     wrt $\\mu$ for the nested logit.

    Args:
        muhat: a Matching
        additional_parameters: a list with the nest structure

    Returns:
        the parameter-dependent part of the hessian of the entropy
        wrt $(\\mu,\\mu)$.
    """
    nests_for_each_x, nests_for_each_y = _get_params(additional_parameters)
    nests_x = change_indices(nests_for_each_x)
    nests_y = change_indices(nests_for_each_y)
    n_rhos = len(nests_for_each_x)
    n_deltas = len(nests_for_each_y)
    n_alpha = n_rhos + n_deltas

    muxy, *_ = muhat.unpack()
    X, Y = muxy.shape

    hess_x = np.zeros((X, Y, Y, n_alpha))
    hess_y = np.zeros((X, Y, X, n_alpha))
    hess_xy = np.zeros((X, Y, n_alpha))
    der_logxy = 1.0 / muxy

    for x in range(X):
        for i_n, nest in enumerate(nests_x):
            mux_nest_n = muxy[x, nest]
            mu_xn = np.sum(mux_nest_n)
            der_logxn = 1.0 / mu_xn
            for t in nest:
                hess_x[x, nest, t, i_n] = der_logxn
            hess_xy[x, nest, i_n] = der_logxn - der_logxy[x, nest]

    for y in range(Y):
        for i_n, nest in enumerate(nests_y):
            muy_nest_n = muxy[nest, y]
            mu_ny = np.sum(muy_nest_n)
            der_logny = 1.0 / mu_ny
            i_n2 = i_n + n_rhos
            for z in nest:
                hess_y[nest, y, z, i_n2] = der_logny
            hess_xy[nest, y, i_n2] = der_logny - der_logxy[nest, y]

    return hess_x, hess_y, hess_xy

e_derivative_r_nested_logit(muhat, additional_parameters=None)

Returns the derivatives of the parameter-dependent part \(e\) wrt \(r\) for the nested logit.

Parameters:

Name Type Description Default
muhat Matching

a Matching

required
additional_parameters list | None

a list with the nest structure

None

Returns:

Type Description
TwoArrays

the parameter-dependent part of the hessian of the entropy

TwoArrays

wrt \((\mu,r)\).

Source code in cupid_matching/nested_logit.py
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
def e_derivative_r_nested_logit(
    muhat: Matching, additional_parameters: list | None = None
) -> TwoArrays:
    """Returns the derivatives of the parameter-dependent part $e$
     wrt $r$ for the nested logit.

    Args:
        muhat: a Matching
        additional_parameters: a list with the nest structure

    Returns:
        the parameter-dependent part of the hessian of the entropy
        wrt $(\\mu,r)$.
    """
    nests_for_each_x, nests_for_each_y = _get_params(additional_parameters)
    n_rhos = len(nests_for_each_x)
    n_deltas = len(nests_for_each_y)
    n_alpha = n_rhos + n_deltas

    muxy, *_ = muhat.unpack()
    X, Y = muxy.shape

    hess_n = np.zeros((X, Y, n_alpha))
    hess_m = np.zeros((X, Y, n_alpha))

    return hess_n, hess_m

e_nested_logit(muhat, additional_parameters=None)

Returns the values of the parameter-dependent part \(e\) for the nested logit.

Parameters:

Name Type Description Default
muhat Matching

a Matching

required
additional_parameters list | None

a list with the nest structure

None

Returns:

Type Description
np.ndarray

the (X,Y,n_alpha) array of the parameter-dependent part

np.ndarray

of the first derivative of the entropy.

Source code in cupid_matching/nested_logit.py
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
def e_nested_logit(
    muhat: Matching, additional_parameters: list | None = None
) -> np.ndarray:
    """Returns the values of the parameter-dependent part  $e$
    for the nested logit.

    Args:
        muhat: a Matching
        additional_parameters: a list with the nest structure

    Returns:
        the (X,Y,n_alpha) array of the parameter-dependent part
        of the first derivative of the entropy.
    """
    nests_for_each_x, nests_for_each_y = _get_params(additional_parameters)
    nests_x = change_indices(nests_for_each_x)
    nests_y = change_indices(nests_for_each_y)
    n_rhos = len(nests_for_each_x)
    n_deltas = len(nests_for_each_y)
    n_alpha = n_rhos + n_deltas

    muxy, *_ = muhat.unpack()
    X, Y = muxy.shape

    e_vals = np.zeros((X, Y, n_alpha))

    for x in range(X):
        for i_n, nest in enumerate(nests_x):
            mux_nest_n = muxy[x, nest]
            mu_xn = np.sum(mux_nest_n)
            e_vals[x, nest, i_n] = -np.log(mux_nest_n / mu_xn)

    for y in range(Y):
        for i_n, nest in enumerate(nests_y):
            muy_nest_n = muxy[nest, y]
            mu_ny = np.sum(muy_nest_n)
            e_vals[nest, y, (i_n + n_rhos)] -= np.log(muy_nest_n / mu_ny)

    return e_vals