예제 #1
0
class TestOrderedProbitModel(unittest.TestCase):
    def setUp(self):
        choices = ['Veh1', 'Veh2', 'Veh3']
        self.coefficients = [{'Constant': 2, 'Var1': 2.11}]
        self.thresholds = [1.2, 2.1]
        self.data = DataArray(
            array([[1, 1.1], [1, -0.25], [1, 3.13], [1, -0.11]]),
            ['CONSTANT', 'VAR1'])

        specification = OLSpecification(choices, self.coefficients,
                                        self.thresholds)
        self.model = OrderedModel(specification)

        specification1 = OLSpecification(choices,
                                         self.coefficients,
                                         self.thresholds,
                                         distribution='probit')
        self.model1 = OrderedModel(specification1)

    def testprobabilitieslogit(self):
        prob = zeros((4, 3))
        obs_utility = self.data.calculate_equation(self.coefficients[0])
        [shape_param] = [
            1,
        ] * genlogistic.numargs
        prob[:, 0] = genlogistic.cdf(self.thresholds[0] - obs_utility,
                                     shape_param)
        prob[:, 1] = (
            genlogistic.cdf(self.thresholds[1] - obs_utility, shape_param) -
            genlogistic.cdf(self.thresholds[0] - obs_utility, shape_param))
        prob[:, 2] = 1 - genlogistic.cdf(self.thresholds[1] - obs_utility,
                                         shape_param)

        prob_model = self.model.calc_probabilities(self.data)
        prob_diff = all(prob == prob_model)
        self.assertEqual(True, prob_diff)

    def testselectionlogit(self):
        choice_act = array([['veh3'], ['veh3'], ['veh1'], ['veh1']])
        choice_model = self.model.calc_chosenalternative(self.data)
        choice_diff = all(choice_act == choice_model)
        self.assertEqual(True, choice_diff)

    def testprobabilitiesprobit(self):
        prob = zeros((4, 3))
        obs_utility = self.data.calculate_equation(self.coefficients[0])
        prob[:, 0] = norm.cdf(self.thresholds[0] - obs_utility)
        prob[:, 1] = (norm.cdf(self.thresholds[1] - obs_utility) -
                      norm.cdf(self.thresholds[0] - obs_utility))
        prob[:, 2] = 1 - norm.cdf(self.thresholds[1] - obs_utility)

        prob_model = self.model1.calc_probabilities(self.data)
        prob_diff = all(prob == prob_model)
        self.assertEqual(True, prob_diff)

    def testselectionprobit(self):
        choice_act = array([['veh3'], ['veh2'], ['veh3'], ['veh2']])
        choice_model = self.model1.calc_chosenalternative(self.data)
        choice_diff = all(choice_act == choice_model)
        self.assertEqual(True, choice_diff)
class TestOrderedProbitModel(unittest.TestCase):
    def setUp(self):
        choices = ['Veh1', 'Veh2', 'Veh3']
        self.coefficients = [{'Constant':2, 'Var1':2.11}]
        self.thresholds = [1.2, 2.1]
        self.data = DataArray(array([[1, 1.1], [1, -0.25], [1, 3.13], [1, -0.11]]),
                         ['CONSTANT', 'VAR1'])

        specification = OLSpecification(choices, self.coefficients, self.thresholds)
        self.model = OrderedModel(specification)

        specification1 = OLSpecification(choices, self.coefficients, self.thresholds,
                                         distribution='probit')
        self.model1 = OrderedModel(specification1)




    def testprobabilitieslogit(self):
        prob = zeros((4,3))
        obs_utility = self.data.calculate_equation(self.coefficients[0])
        [shape_param] = [1,]*genlogistic.numargs
        prob[:,0] = genlogistic.cdf(self.thresholds[0] - obs_utility, shape_param)
        prob[:,1] = (genlogistic.cdf(self.thresholds[1] - obs_utility, shape_param) -
                     genlogistic.cdf(self.thresholds[0] - obs_utility, shape_param))
        prob[:,2] = 1 - genlogistic.cdf(self.thresholds[1] -
                                        obs_utility, shape_param)

        prob_model = self.model.calc_probabilities(self.data)
        prob_diff = all(prob == prob_model)
        self.assertEqual(True, prob_diff)

    def testselectionlogit(self):
        choice_act = array([['veh3'], ['veh3'], ['veh1'], ['veh1']])
        choice_model = self.model.calc_chosenalternative(self.data)
        choice_diff = all(choice_act == choice_model)
        self.assertEqual(True, choice_diff)

    def testprobabilitiesprobit(self):
        prob = zeros((4,3))
        obs_utility = self.data.calculate_equation(self.coefficients[0])
        prob[:,0] = norm.cdf(self.thresholds[0] - obs_utility)
        prob[:,1] = (norm.cdf(self.thresholds[1] - obs_utility) -
                     norm.cdf(self.thresholds[0] - obs_utility))
        prob[:,2] = 1 - norm.cdf(self.thresholds[1] - obs_utility)

        prob_model = self.model1.calc_probabilities(self.data)
        prob_diff = all(prob == prob_model)
        self.assertEqual(True, prob_diff)

    def testselectionprobit(self):
        choice_act = array([['veh3'], ['veh2'], ['veh3'], ['veh2']])
        choice_model = self.model1.calc_chosenalternative(self.data)
        choice_diff = all(choice_act == choice_model)
        self.assertEqual(True, choice_diff)
class TestCountRegressionModel(unittest.TestCase):
    def setUp(self):
        choices = ['Episodes1', 'Episodes2', 'Episodes3']
        self.coefficients = [{'Constant':2, 'Var1':2.11}]
        data = array([[1, 1.1], [1, -0.25], [1, 3.13], [1, -0.11]])
        self.data = DataArray(data, ['CONSTANT', 'VAR1'])

        self.specification = CountSpecification(choices, self.coefficients)
        self.model = CountRegressionModel(self.specification)

    def testprobabilitiespoisson(self):
        prob = zeros((4,3))
        exp_value = self.data.calculate_equation(self.coefficients[0])
        prob[:,0] = poisson.pmf(0, exp_value)
        prob[:,1] = poisson.pmf(1, exp_value)
        prob[:,2] = 1 - poisson.cdf(1, exp_value)

        prob_model = self.model.calc_probabilities(self.data)
        prob_diff = all(prob == prob_model)
        self.assertEqual(True, prob_diff)

    def testselectionpoisson(self):
        choice_act = array([['episodes3'], ['episodes3'], ['episodes1'],
                            ['episodes2']])
        choice_model = self.model.calc_chosenalternative(self.data)
        choice_diff = all(choice_act == choice_model)
        self.assertEqual(True, choice_diff)
예제 #4
0
class TestLinearRegressionModel(unittest.TestCase):
    def setUp(self):
        choice = ['DURATION']
        coefficients = [{'constant': 2, 'Var1': 2.11}]
        data = array([[1, 1.1], [1, -0.25], [1, 3.13], [1, -0.11]])
        variance = array([[1]])

        self.data = DataArray(data, ['Constant', 'VaR1'])
        self.specification = Specification(choice, coefficients)
        self.errorspecification = LinearRegErrorSpecification(variance)

    def testvalues(self):
        model = LinearRegressionModel(self.specification,
                                      self.errorspecification)
        pred_value = model.calc_predvalue(self.data)

        expected_act = self.data.calculate_equation(
            self.specification.coefficients[0])
        expected_act.shape = (4, 1)

        dist = RandomDistribution(seed=1)
        pred_act = dist.return_normal_variables(location=expected_act,
                                                scale=1,
                                                size=(4, 1))

        pred_diff = all(pred_value.data == pred_act)
        self.assertEqual(True, pred_diff)
class TestStocFronRegressionModel(unittest.TestCase):
    def setUp(self):
        choice = ['Frontier']
        coefficients = [{'constant':2, 'Var1':2.11}]
        data = array([[1, 1.1], [1, -0.25], [1, 3.13], [1, -0.11]])       
        variance = array([[1., 0], [0, 1.1]])

        self.data = DataArray(data, ['Constant', 'VaR1'])
        self.specification = Specification(choice, coefficients)
        self.errorspecification = StochasticRegErrorSpecification(variance, 'start')


    def testvalues(self):
        model = StocFronRegressionModel(self.specification, 
                                        self.errorspecification)
        pred_value = model.calc_predvalue(self.data)

        expected_act = self.data.calculate_equation(
                                                    self.specification.coefficients[0])
        expected_act.shape = (4,1)
        variance = self.errorspecification.variance

        dist = RandomDistribution(seed=1)
        err_norm = dist.return_normal_variables(location=0, scale=variance[0,0]**0.5, size=(4,1))
        
        pred_act = (expected_act + err_norm)
        
        pred_diff = all(pred_value.data == pred_act)
        self.assertEquals(True, pred_diff)
class TestStocFronRegressionModel(unittest.TestCase):
    def setUp(self):
        choice = ['Frontier']
        coefficients = [{'constant': 2, 'Var1': 2.11}]
        data = array([[1, 1.1], [1, -0.25], [1, 3.13], [1, -0.11]])
        variance = array([[1., 0], [0, 1.1]])

        self.data = DataArray(data, ['Constant', 'VaR1'])
        self.specification = Specification(choice, coefficients)
        self.errorspecification = StochasticRegErrorSpecification(
            variance, 'start')

    def testvalues(self):
        model = StocFronRegressionModel(self.specification,
                                        self.errorspecification)
        pred_value = model.calc_predvalue(self.data)

        expected_act = self.data.calculate_equation(
            self.specification.coefficients[0])
        expected_act.shape = (4, 1)
        variance = self.errorspecification.variance

        dist = RandomDistribution(seed=1)
        err_norm = dist.return_normal_variables(location=0,
                                                scale=variance[0, 0]**0.5,
                                                size=(4, 1))

        pred_act = (expected_act + err_norm)

        pred_diff = all(pred_value.data == pred_act)
        self.assertEquals(True, pred_diff)
class TestLinearRegressionModel(unittest.TestCase):

    def setUp(self):
        choice = ['DURATION']
        coefficients = [{'constant': 2, 'Var1': 2.11}]
        data = array([[1, 1.1], [1, -0.25], [1, 3.13], [1, -0.11]])
        variance = array([[1]])

        self.data = DataArray(data, ['Constant', 'VaR1'])
        self.specification = Specification(choice, coefficients)
        self.errorspecification = LinearRegErrorSpecification(variance)

    def testvalues(self):
        model = LinearRegressionModel(
            self.specification, self.errorspecification)
        pred_value = model.calc_predvalue(self.data)

        expected_act = self.data.calculate_equation(
            self.specification.coefficients[0])
        expected_act.shape = (4, 1)

        dist = RandomDistribution(seed=1)
        pred_act = dist.return_normal_variables(
            location=expected_act, scale=1, size=(4, 1))

        pred_diff = all(pred_value.data == pred_act)
        self.assertEqual(True, pred_diff)
예제 #8
0
    def update_houseids(self, hhldSyn, persSyn, hhldVars, persVars,
                        highestHid):

        hhldSynDataObj = DataArray(hhldSyn, hhldVars)
        persSynDataObj = DataArray(persSyn, persVars)

        maxFreqCol = amax(hhldSynDataObj.columns(['frequency']).data)
        powFreqCol = floor(log(maxFreqCol, 10)) + 1

        coefficients = {'frequency': 1, 'hhid': 10**powFreqCol}

        newHid = hhldSynDataObj.calculate_equation(coefficients)
        hhldSynDataObj.setcolumn('hhid', newHid)

        newHid = persSynDataObj.calculate_equation(coefficients)
        persSynDataObj.setcolumn('hhid', newHid)

        hhldSynDataObj.sort([self.idSpec.hidName])
        persSynDataObj.sort([self.idSpec.hidName, self.idSpec.pidName])

        hidIndex_popgenH = hhldVars.index('hhid')
        hidIndex_popgenP = persVars.index('hhid')

        self.create_indices(persSynDataObj)

        hhldSyn = hhldSynDataObj.data
        persSyn = persSynDataObj.data

        row = 0
        for hhldIndex in self.hhldIndicesOfPersons:

            firstPersonRec = hhldIndex[1]
            lastPersonRec = hhldIndex[2]

            #print hhldIndex[0], highestHid + 1, firstPersonRec, lastPersonRec

            hhldSyn[row, hidIndex_popgenH] = highestHid + 1
            persSyn[firstPersonRec:lastPersonRec,
                    hidIndex_popgenP] = highestHid + 1

            highestHid += 1
            row += 1

        return hhldSyn, persSyn
    def update_houseids(self, hhldSyn, persSyn, hhldVars, persVars, highestHid):

	hhldSynDataObj = DataArray(hhldSyn, hhldVars)
        persSynDataObj = DataArray(persSyn, persVars)

	
	maxFreqCol = amax(hhldSynDataObj.columns(['frequency']).data)
	powFreqCol = floor(log(maxFreqCol, 10)) + 1

	coefficients = {'frequency':1, 'hhid':10**powFreqCol}

	newHid = hhldSynDataObj.calculate_equation(coefficients)
	hhldSynDataObj.setcolumn('hhid', newHid)

	newHid = persSynDataObj.calculate_equation(coefficients)
	persSynDataObj.setcolumn('hhid', newHid)


	hhldSynDataObj.sort([self.idSpec.hidName])
	persSynDataObj.sort([self.idSpec.hidName, self.idSpec.pidName])

	hidIndex_popgenH = hhldVars.index('hhid')
	hidIndex_popgenP = persVars.index('hhid')

        self.create_indices(persSynDataObj)

	hhldSyn = hhldSynDataObj.data
	persSyn = persSynDataObj.data

	row = 0
        for hhldIndex in self.hhldIndicesOfPersons:

            firstPersonRec = hhldIndex[1]
            lastPersonRec = hhldIndex[2]

	    #print hhldIndex[0], highestHid + 1, firstPersonRec, lastPersonRec

	    hhldSyn[row,hidIndex_popgenH] = highestHid + 1
	    persSyn[firstPersonRec:lastPersonRec,hidIndex_popgenP] = highestHid + 1

	    highestHid += 1
	    row += 1

	return hhldSyn, persSyn