def test_lrtest(): """ Ensures a correct result of the lrtest. The comparison values were obtained from comparison with lrtest in R's lmtest package """ general = MixedLogit() general.loglikelihood = 1312 restricted = MixedLogit() restricted.loglikelihood = -1305 general.loglikelihood = -1312 general.coeff_ = np.zeros(4) restricted.coeff_ = np.zeros(2) obtained = lrtest(general, restricted) expected = {'pval': 0.0009118819655545164, 'chisq': 14, 'degfree': 2}
def test_predict(): """ Ensures that returned choice probabilities are consistent. """ # There is no need to initialize a random seed as the halton draws produce # reproducible results P = 1 # Without panel data betas = np.array([.1, .1, .1, .1]) X_ = X.reshape(N, P, J, K) model = MixedLogit() model._rvidx, model._rvdist = np.array([True, True]), np.array(['n', 'n']) model.alternatives = np.array([1, 2]) model.coeff_ = betas model.randvars = randvars model._isvars, model._asvars, model._varnames = [], varnames, varnames model._fit_intercept = False model.coeff_names = np.array(["a", "b", "sd.a", "sd.b"]) #model.fit(X, y, varnames, alts, ids, randvars, verbose=0, halton=True) y_pred, proba, freq = model.predict(X, varnames, alts, ids, n_draws=R, return_proba=True, return_freq=True) # Compute choice probabilities by hand draws = model._get_halton_draws(N, R, K) # (N,Kr,R) Br = betas[None, [0, 1], None] + draws * betas[None, [2, 3], None] V = np.einsum('npjk,nkr -> npjr', X_, Br) V[V > 700] = 700 eV = np.exp(V) e_proba = eV / np.sum(eV, axis=2, keepdims=True) expec_proba = e_proba.prod(axis=1).mean(axis=-1) expec_ypred = model.alternatives[np.argmax(expec_proba, axis=1)] alt_list, counts = np.unique(expec_ypred, return_counts=True) expec_freq = dict( zip(list(alt_list), list(np.round(counts / np.sum(counts), 3)))) assert np.array_equal(expec_ypred, y_pred) assert expec_freq == freq