<commit_before># -*- coding: utf-8 -*-
"""
Created on Mon Jul 18 18:15:50 2016

@name:      Mixed MultiNomial Logit
@author:    <NAME>
@summary:   Contains functions necessary for estimating mixed multinomial logit
            models (with the help of the "base_multinomial_cm.py" file).
            Version 1 only works for MNL kernels and only for mixing of index
            coefficients.

General References
------------------
Train, K., 2009. Discrete Choice Models With Simulation. 2 ed., Cambridge
    University Press, New York, NY, USA.
"""

import warnings
import numpy as np
from scipy.sparse import csr_matrix

import base_multinomial_cm_v2 as base_mcm
import choice_calcs as cc
import mixed_logit_calcs as mlc
from choice_tools import get_dataframe_from_data
from choice_tools import create_design_matrix
from choice_tools import create_long_form_mappings
from display_names import model_type_to_display_name
from estimation import EstimationObj
from estimation import estimate

# Alias necessary functions for model estimation
general_calc_probabilities = cc.calc_probabilities
general_sequence_probs = mlc.calc_choice_sequence_probs
general_log_likelihood = mlc.calc_mixed_log_likelihood
general_gradient = mlc.calc_mixed_logit_gradient
general_bhhh = mlc.calc_bhhh_hessian_approximation_mixed_logit

_msg_1 = "The Mixed MNL Model has no shape parameters. "
_msg_2 = "shape_names and shape_ref_pos will be ignored if passed."
_shape_ignore_msg = _msg_1 + _msg_2

# Create a warning string that will be issued if ridge regression is performed.
_msg_3 = "NOTE: An L2-penalized regression is being performed. The "
_msg_4 = "reported standard errors and robust standard errors "
_msg_5 = "***WILL BE INCORRECT***."
_ridge_warning_msg = _msg_3 + _msg_4 + _msg_5


def split_param_vec(beta, return_all_types=False, *args, **kwargs):
    """
    Parameters
    ----------
    beta : 1D numpy array.
        All elements should by ints, floats, or longs. Should have 1 element
        for each utility coefficient being estimated (i.e. num_features).
    return_all_types : bool, optional.
        Determines whether or not a tuple of 4 elements will be returned (with
        one element for the nest, shape, intercept, and index parameters for
        this model). If False, a tuple of 3 elements will be returned, as
        described below.

    Returns
    -------
    tuple.
        `(None, None, beta)`. This function is merely for compatibility with
        the other choice model files.

    Note
    ----
    If `return_all_types == True` then the function will return a tuple of four
    objects. In order, these objects will either be None or the arrays
    representing the arrays corresponding to the nest, shape, intercept, and
    index parameters.
    """
    if return_all_types:
        return None, None, None, beta
    else:
        return None, None, beta


def mnl_utility_transform(sys_utility_array, *args, **kwargs):
    """
    Parameters
    ----------
    sys_utility_array : ndarray.
        Should have 1D or 2D. Should have been created by the dot product of a
        design matrix and an array of index coefficients.

    Returns
    -------
        systematic_utilities : 2D ndarray.
            The input systematic utilities. If `sys_utility_array` is 2D, then
            `sys_utility_array` is returned. Else, returns<commit_msg>Updated documentation for accuracy.<commit_after># -*- coding: utf-8 -*-
"""
Created on Mon Jul 18 18:15:50 2016

@name:      Mixed MultiNomial Logit
@author:    <NAME>
@summary:   Contains functions necessary for estimating mixed multinomial logit
            models (with the help of the "base_multinomial_cm.py" file).
            Version 1 only works for MNL kernels and only for mixing of index
            coefficients.

General References
------------------
<NAME>., 2009. Discrete Choice Models With Simulation. 2 ed., Cambridge
    University Press, New York, NY, USA.
"""

import warnings
import numpy as np
from scipy.sparse import csr_matrix

import base_multinomial_cm_v2 as base_mcm
import choice_calcs as cc
import mixed_logit_calcs as mlc
from choice_tools import get_dataframe_from_data
from choice_tools import create_design_matrix
from choice_tools import create_long_form_mappings
from display_names import model_type_to_display_name
from estimation import EstimationObj
from estimation import estimate

# Alias necessary functions for model estimation
general_calc_probabilities = cc.calc_probabilities
general_sequence_probs = mlc.calc_choice_sequence_probs
general_log_likelihood = mlc.calc_mixed_log_likelihood
general_gradient = mlc.calc_mixed_logit_gradient
general_bhhh = mlc.calc_bhhh_hessian_approximation_mixed_logit

_msg_1 = "The Mixed MNL Model has no shape parameters. "
_msg_2 = "shape_names and shape_ref_pos will be ignored if passed."
_shape_ignore_msg = _msg_1 + _msg_2

# Create a warning string that will be issued if ridge regression is performed.
_msg_3 = "NOTE: An L2-penalized regression is being performed. The "
_msg_4 = "reported standard errors and robust standard errors "
_msg_5 = "***WILL BE INCORRECT***."
_ridge_warning_msg = _msg_3 + _msg_4 + _msg_5


def split_param_vec(beta, return_all_types=False, *args, **kwargs):
    """
    Parameters
    ----------
    beta : 1D numpy array.
        All elements should by ints, floats, or longs. Should have 1 element
        for each utility coefficient being estimated (i.e. num_features).
    return_all_types : bool, optional.
        Determines whether or not a tuple of 4 elements will be returned (with
        one element for the nest, shape, intercept, and index parameters for
        this model). If False, a tuple of 3 elements will be returned, as
        described below.

    Returns
    -------
    tuple.
        `(None, None, beta)`. This function is merely for compatibility with
        the other choice model files.

    Note
    ----
    If `return_all_types == True` then the function will return a tuple of
    `(None, None, None, beta)`. These values represent the nest, shape, outside
    intercept, and index coefficients for the mixed logit model.
    """
    if return_all_types:
        return None, None, None, beta
    else:
        return None, None, beta


def mnl_utility_transform(sys_utility_array, *args, **kwargs):
    """
    Parameters
    ----------
    sys_utility_array : ndarray.
        Should have 1D or 2D. Should have been created by the dot product of a
        design matrix and an array of index coefficients.

    Returns
    -------
        systematic_utilities : 2D ndarray.
            The input systematic utilities. If `sys_utility_array` is 2D, then
            `sys_utility_array` is returned. Else, returns
{'fraction_non_alphanumeric': 0.07149213883120736, 'fraction_numerical': 0.01156926727973895, 'mean_word_length': 3.2596336070751737, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1167318', 'n_tokens_mistral': 2000, 'n_tokens_neox': 1888, 'n_words': 742}