'Derivative of the expected endog with respect to exog for the
multinomial model, used in analyzing marginal effects.
Parameters
exog : array-like
The exogeneous data at which the derivative is computed,
number of rows must be a multiple of `ncut`.
lpr : array-like
The linear predictor values, length must be multiple of
`ncut`.
Returns
The value of the derivative of the expected endog with respect
to exog.
Notes
offset_exposure must be set at None for the multinoial family.'

def mean_deriv_exog(self, exog, params, offset_exposure=None):

    if (offset_exposure is not None):
        warnings.warn('Offset/exposure    ignored    for    the    multinomial    family', ValueWarning)
    lpr = np.dot(exog, params)
    expval = np.exp(lpr)
    expval_m = np.reshape(expval, ((len(expval) // self.ncut), self.ncut))
    denom = (1 + expval_m.sum(1))
    denom = np.kron(denom, np.ones(self.ncut, dtype=np.float64))
    bmat0 = np.outer(np.ones(exog.shape[0]), params)
    qmat = []
    for j in range(self.ncut):
        ee = np.zeros(self.ncut, dtype=np.float64)
        ee[j] = 1
        qmat.append(np.kron(ee, np.ones((len(params) // self.ncut))))
    qmat = np.array(qmat)
    qmat = np.kron(np.ones(((exog.shape[0] // self.ncut), 1)), qmat)
    bmat = (bmat0 * qmat)
    dmat = ((expval[:, None] * bmat) / denom[:, None])
    expval_mb = np.kron(expval_m, np.ones((self.ncut, 1)))
    expval_mb = np.kron(expval_mb, np.ones((1, self.ncut)))
    dmat -= ((expval[:, None] * (bmat * expval_mb)) / (denom[:, None] ** 2))
    return dmat
