(data, chosen, numalts, GPU=False, coeffrange=(-3, 3),
                 weights=None, lcgrad=False, beta=None)
return log_likelihood, fit_parameters
Calculate coefficients of the MNL model.

    Parameters
    ----------
    data : 2D array
        The data are expected to be in "long" form where each row is for
        one alternative. Alternatives are in groups of `numalts` rows per
        choosers. Alternatives must be in the same order for each chooser.
    chosen : 2D array
        This boolean array has a row for each chooser and a column for each
        alternative. The column ordering for alternatives is expected to be
        the same as their row ordering in the `data` array.
        A one (True) indicates which alternative each chooser has chosen.
    numalts : int
        The number of alternatives.
    GPU : bool, optional
    coeffrange : tuple of floats, optional
        Limits of (min, max) to which coefficients are clipped.
    weights : ndarray, optional
    lcgrad : bool, optional
    beta : 1D array, optional
        Any initial guess for the coefficients.

    Returns
    -------
    log_likelihood : dict
        Dictionary of log-likelihood values describing the quality of
        the model fit.
    fit_parameters : pandas.DataFrame
        Table of fit parameters with columns 'Coefficient', 'Std. Error',
        'T-Score'. Each row corresponds to a column in `data` and are given
        in the same order as in `data`.

    See Also
    --------
    scipy.optimize.fmin_l_bfgs_b : The optimization routine used.
Calculate coefficients of the MNL model.
Calculate coefficients of the MNL model .
def mnl_estimate(data, chosen, numalts, GPU=False, coeffrange=(-3, 3),
                 weights=None, lcgrad=False, beta=None):
    """
    Calculate coefficients of the MNL model.

    Parameters
    ----------
    data : 2D array
        The data are expected to be in "long" form where each row is for
        one alternative. Alternatives are in groups of `numalts` rows per
        choosers. Alternatives must be in the same order for each chooser.
    chosen : 2D array
        This boolean array has a row for each chooser and a column for each
        alternative. The column ordering for alternatives is expected to be
        the same as their row ordering in the `data` array.
        A one (True) indicates which alternative each chooser has chosen.
    numalts : int
        The number of alternatives.
    GPU : bool, optional
    coeffrange : tuple of floats, optional
        Limits of (min, max) to which coefficients are clipped.
    weights : ndarray, optional
    lcgrad : bool, optional
    beta : 1D array, optional
        Any initial guess for the coefficients.

    Returns
    -------
    log_likelihood : dict
        Dictionary of log-likelihood values describing the quality of
        the model fit.
    fit_parameters : pandas.DataFrame
        Table of fit parameters with columns 'Coefficient', 'Std. Error',
        'T-Score'. Each row corresponds to a column in `data` and are given
        in the same order as in `data`.

    See Also
    --------
    scipy.optimize.fmin_l_bfgs_b : The optimization routine used.

    """
    logger.debug(
        'start: MNL fit with len(data)={} and numalts={}'.format(
            len(data), numalts))
    atype = 'numpy' if not GPU else 'cuda'

    numvars = data.shape[1]
    numobs = data.shape[0] // numalts

    if chosen is None:
        chosen = np.ones((numobs, numalts))  # used for latent classes

    data = np.transpose(data)
    chosen = np.transpose(chosen)

    data, chosen = PMAT(data, atype), PMAT(chosen, atype)
    if weights is not None:
        weights = PMAT(np.transpose(weights), atype)

    if beta is None:
        beta = np.zeros(numvars)
    bounds = [coeffrange] * numvars

    with log_start_finish('scipy optimization for MNL fit', logger):
        args = (data, chosen, numalts, weights, lcgrad)
        bfgs_result = scipy.optimize.fmin_l_bfgs_b(mnl_loglik,
                                                   beta,
                                                   args=args,
                                                   fprime=None,
                                                   factr=10,
                                                   approx_grad=False,
                                                   bounds=bounds
                                                   )

    if bfgs_result[2]['warnflag'] > 0:
        logger.warn("mnl did not converge correctly: %s",  bfgs_result)

    beta = bfgs_result[0]
    stderr = mnl_loglik(
        beta, data, chosen, numalts, weights, stderr=1, lcgrad=lcgrad)

    l0beta = np.zeros(numvars)
    l0 = -1 * mnl_loglik(l0beta, *args)[0]
    l1 = -1 * mnl_loglik(beta, *args)[0]

    log_likelihood = {
        'null': float(l0[0][0]),
        'convergence': float(l1[0][0]),
        'ratio': float((1 - (l1 / l0))[0][0])
    }

    fit_parameters = pd.DataFrame({
        'Coefficient': beta,
        'Std. Error': stderr,
        'T-Score': beta / stderr})

    logger.debug('finish: MNL fit')
    return log_likelihood, fit_parameters
def mnl_estimate ( data , chosen , numalts , GPU = False , coeffrange = ( - 3 , 3 ) , weights = None , lcgrad = False , beta = None ) : logger . debug ( 'start: MNL fit with len(data)={} and numalts={}' . format ( len ( data ) , numalts ) ) atype = 'numpy' if not GPU else 'cuda' numvars = data . shape [ 1 ] numobs = data . shape [ 0 ] // numalts if chosen is None : chosen = np . ones ( ( numobs , numalts ) ) # used for latent classes data = np . transpose ( data ) chosen = np . transpose ( chosen ) data , chosen = PMAT ( data , atype ) , PMAT ( chosen , atype ) if weights is not None : weights = PMAT ( np . transpose ( weights ) , atype ) if beta is None : beta = np . zeros ( numvars ) bounds = [ coeffrange ] * numvars with log_start_finish ( 'scipy optimization for MNL fit' , logger ) : args = ( data , chosen , numalts , weights , lcgrad ) bfgs_result = scipy . optimize . fmin_l_bfgs_b ( mnl_loglik , beta , args = args , fprime = None , factr = 10 , approx_grad = False , bounds = bounds ) if bfgs_result [ 2 ] [ 'warnflag' ] > 0 : logger . warn ( "mnl did not converge correctly: %s" , bfgs_result ) beta = bfgs_result [ 0 ] stderr = mnl_loglik ( beta , data , chosen , numalts , weights , stderr = 1 , lcgrad = lcgrad ) l0beta = np . zeros ( numvars ) l0 = - 1 * mnl_loglik ( l0beta , * args ) [ 0 ] l1 = - 1 * mnl_loglik ( beta , * args ) [ 0 ] log_likelihood = { 'null' : float ( l0 [ 0 ] [ 0 ] ) , 'convergence' : float ( l1 [ 0 ] [ 0 ] ) , 'ratio' : float ( ( 1 - ( l1 / l0 ) ) [ 0 ] [ 0 ] ) } fit_parameters = pd . DataFrame ( { 'Coefficient' : beta , 'Std. Error' : stderr , 'T-Score' : beta / stderr } ) logger . debug ( 'finish: MNL fit' ) return log_likelihood , fit_parameters