Esempio n. 1
0
    def test_shape_ignore_msg_in_constructor(self):
        """
        Ensures that a UserWarning is raised when the 'shape_ref_pos' or
        'shape_names' keyword arguments are passed to the Clog-log model
        constructor. This warns people against expecting the Clog-log to use
        shape parameters, and alerts them that they are using an Clog-log
        model when they might have been expecting to instantiate a different
        choice model.
        """
        # Create a variable for the standard arguments to this function.
        standard_args = [
            self.fake_df, self.alt_id_col, self.obs_id_col, self.choice_col,
            self.fake_specification
        ]

        # Create a variable for the kwargs being passed to the constructor
        kwarg_map_1 = {
            "intercept_ref_pos": self.fake_intercept_ref_pos,
            "names": self.fake_names,
            "intercept_names": self.fake_intercept_names,
            "shape_ref_pos": 2
        }
        kwarg_map_2 = {
            "intercept_ref_pos": self.fake_intercept_ref_pos,
            "names": self.fake_names,
            "intercept_names": self.fake_intercept_names,
            "shape_names": OrderedDict([("x", ["foo"])])
        }

        # Test to ensure that the shape ignore message is printed when using
        # either of these two kwargs
        with warnings.catch_warnings(record=True) as context:
            # Use this filter to always trigger the  UserWarnings
            warnings.simplefilter('always', UserWarning)

            for pos, bad_kwargs in enumerate([kwarg_map_1, kwarg_map_2]):
                # Create a Clog-log model object with the irrelevant kwargs.
                # This should trigger a UserWarning
                clog_obj = clog.MNCL(*standard_args, **bad_kwargs)
                # Check that the warning has been created.
                self.assertEqual(len(context), pos + 1)
                self.assertIsInstance(context[-1].category, type(UserWarning))
                self.assertIn(clog._shape_ignore_msg, str(context[-1].message))

        return None
Esempio n. 2
0
    def setUp(self):
        # Create the betas to be used during the tests
        self.fake_betas = np.array([-0.6])

        # Create the fake outside intercepts to be used during the tests
        self.fake_intercepts = np.array([1, 0.5])

        # Create names for the intercept parameters
        self.fake_intercept_names = ["ASC 1", "ASC 2"]

        # Record the position of the intercept that is not being estimated
        self.fake_intercept_ref_pos = 2

        # Create an array of all model parameters
        self.fake_all_params = np.concatenate(
            (self.fake_intercepts, self.fake_betas))

        # The set up being used is one where there are two choice situations,
        # The first having three alternatives, and the second having only two.
        # The mapping between rows and alternatives is given below.
        self.fake_rows_to_alts = csr_matrix(
            np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1], [1, 0, 0], [0, 0, 1]]))

        # Create the fake design matrix with columns denoting X
        # The intercepts are not included because they are kept outside the
        # index in the clog-log model.
        self.fake_design = np.array([[1], [2], [3], [1.5], [3.5]])

        # Create the index array for this set of choice situations
        self.fake_index = self.fake_design.dot(self.fake_betas)

        # Create the needed dataframe for the Clog-log constructor
        self.fake_df = pd.DataFrame({
            "obs_id": [1, 1, 1, 2, 2],
            "alt_id": [1, 2, 3, 1, 3],
            "choice": [0, 1, 0, 0, 1],
            "x": self.fake_design[:, 0],
            "intercept": [1 for i in range(5)]
        })

        # Record the various column names
        self.alt_id_col = "alt_id"
        self.obs_id_col = "obs_id"
        self.choice_col = "choice"

        # Create the index specification  and name dictionaryfor the model
        self.fake_specification = OrderedDict()
        self.fake_names = OrderedDict()
        self.fake_specification["x"] = [[1, 2, 3]]
        self.fake_names["x"] = ["x (generic coefficient)"]

        # Bundle the args and kwargs used to construct the Clog-log model.
        self.constructor_args = [
            self.fake_df, self.alt_id_col, self.obs_id_col, self.choice_col,
            self.fake_specification
        ]

        # Create a variable for the kwargs being passed to the constructor
        self.constructor_kwargs = {
            "intercept_ref_pos": self.fake_intercept_ref_pos,
            "names": self.fake_names,
            "intercept_names": self.fake_intercept_names
        }

        # Initialize a basic clog-log model.
        # Create the clog model object whose coefficients will be estimated.
        self.base_clog = clog.MNCL(*self.constructor_args,
                                   **self.constructor_kwargs)

        return None
Esempio n. 3
0
    def make_clog_and_mnl_models(self):
        # The set up being used is one where there are two choice situations,
        # The first having three alternatives, and the second having only two
        # alternatives. There is one generic variable. Two alternative
        # specific constants and all three shape parameters are used.

        # Create the betas to be used during the tests
        fake_betas = np.array([-0.6])

        # Create the fake outside intercepts to be used during the tests
        fake_intercepts = np.array([1, 0.5])

        # Create names for the intercept parameters
        fake_intercept_names = ["ASC 1", "ASC 2"]

        # Record the position of the intercept that is not being estimated
        fake_intercept_ref_pos = 2

        # Create an array of all model parameters
        fake_all_params = np.concatenate((fake_intercepts, fake_betas))

        # Get the mappping between rows and observations
        fake_rows_to_obs = csr_matrix(
            np.array([[1, 0], [1, 0], [1, 0], [0, 1], [0, 1]]))

        # Create the fake design matrix with columns denoting X
        # The intercepts are not included because they are kept outside the
        # index in the scobit model.
        fake_design = np.array([[1], [2], [3], [1.5], [3.5]])

        # Create the index array for this set of choice situations
        fake_index = fake_design.dot(fake_betas)

        # Create the needed dataframe for the model constructor
        fake_df = pd.DataFrame({
            "obs_id": [1, 1, 1, 2, 2],
            "alt_id": [1, 2, 3, 1, 3],
            "choice": [0, 1, 0, 0, 1],
            "x": fake_design[:, 0],
            "intercept": [1 for i in range(5)]
        })

        # Record the various column names
        alt_id_col = "alt_id"
        obs_id_col = "obs_id"
        choice_col = "choice"

        # Create the index specification  and name dictionaryfor the model
        fake_specification = OrderedDict()
        fake_names = OrderedDict()
        fake_specification["x"] = [[1, 2, 3]]
        fake_names["x"] = ["x (generic coefficient)"]

        mnl_spec = OrderedDict()
        mnl_names = OrderedDict()
        mnl_spec["intercept"] = [1, 2]
        mnl_names["intercept"] = fake_intercept_names
        mnl_spec["x"] = fake_specification["x"]
        mnl_names["x"] = fake_names["x"]

        # Bundle args and kwargs used to construct the Asymmetric Logit model.
        clog_args = [
            fake_df, alt_id_col, obs_id_col, choice_col, fake_specification
        ]
        mnl_args = deepcopy(clog_args)
        mnl_args[-1] = mnl_spec

        # Create a variable for the kwargs being passed to the constructor
        clog_kwargs = {
            "names": fake_names,
            "intercept_ref_pos": fake_intercept_ref_pos,
            "intercept_names": fake_intercept_names
        }
        mnl_kwargs = {"names": mnl_names}

        # Initialize a basic Asymmetric Logit model whose coefficients will be
        # estimated.
        clog_obj = clog.MNCL(*clog_args, **clog_kwargs)
        mnl_obj = mnl.MNL(*mnl_args, **mnl_kwargs)

        # Create the desired model attributes for the clog log model
        clog_obj.coefs = pd.Series(fake_betas, index=fake_names["x"])
        clog_obj.intercepts =\
            pd.Series(fake_intercepts, index=fake_intercept_names)
        clog_obj.shapes = None
        clog_obj.nests = None
        clog_obj.params =\
            pd.concat([clog_obj.intercepts, clog_obj.coefs],
                      axis=0, ignore_index=False)

        mnl_obj.params = clog_obj.params.copy()
        mnl_obj.coefs = mnl_obj.params.copy()
        mnl_obj.intercepts = None
        mnl_obj.shapes = None
        mnl_obj.nests = None

        return clog_obj, mnl_obj