Exemplo n.º 1
0
def test_constraint_ordering_1():
	#m0 = larch.Model.Example(1)
	m1 = larch.Model.Example(1)

	# m0.utility_co[2] = P.ASC_SR2 + P('hhinc#23') * X.hhinc
	# m0.utility_co[3] = P.ASC_SR3P + P('hhinc#23') * X.hhinc
	# m0.remove_unused_parameters()

	c3 = OrderingBound("hhinc#3", "hhinc#2")

	# m0.load_data()
	m1.load_data()

	m1.constraints = [c3,]

	# r0 = m0.maximize_loglike(
	# 	method='slsqp',
	# )

	r1 = m1.maximize_loglike(
		method='slsqp',
	)

	assert m1['hhinc#2'].value / m1['hhinc#3'].value == approx(1.0, rel=1e-3)
	assert r1.message == 'Optimization terminated successfully.'
	assert c3.fun(m1.pf.value) == approx(0, abs=1e-5)

	# m0.calculate_parameter_covariance()
	m1.calculate_parameter_covariance()

	# assert m0.pf.loc['hhinc#23', 'std_err'] == approx(0.001397, rel=5e-3)
	assert m1.pf.loc['hhinc#2', 'std_err'] == approx(0.001397, rel=5e-3)
	assert m1.pf.loc['hhinc#3', 'std_err'] == approx(0.001397, rel=5e-3)
Exemplo n.º 2
0
def test_contraint_interpretation():

	assert interpret_contraint("aaaa > bbbb") == OrderingBound('bbbb', 'aaaa')
	assert interpret_contraint("aaaa < bbbb") == OrderingBound('aaaa', 'bbbb')
	assert interpret_contraint("aaaa/bbbb > 3") == RatioBound('aaaa', 'bbbb', min_ratio=3)
	assert interpret_contraint("aaaa/bbbb < 3") == RatioBound('aaaa', 'bbbb', max_ratio=3)
	assert interpret_contraint("aaaa / bbbb > 3") == RatioBound('aaaa', 'bbbb', min_ratio=3)
	assert interpret_contraint("aaaa / bbbb < 3") == RatioBound('aaaa', 'bbbb', max_ratio=3)
	assert interpret_contraint("aaaa / bbbb >= 3") == RatioBound('aaaa', 'bbbb', min_ratio=3)
	assert interpret_contraint("aaaa / bbbb <= 3") == RatioBound('aaaa', 'bbbb', max_ratio=3)
	assert interpret_contraint("3.1 < aaaa / bbbb <= 3.2") == RatioBound('aaaa', 'bbbb', min_ratio=3.1, max_ratio=3.2)
	assert interpret_contraint("aaaa > 3") == FixedBound('aaaa', minimum=3)
	assert interpret_contraint("aaaa < 3") == FixedBound('aaaa', maximum=3)
	assert interpret_contraint("3.1 < aaaa < 3.5") == FixedBound('aaaa', minimum=3.1, maximum=3.5)

	assert interpret_contraint("aaaa/bbbb > 1/2") == RatioBound('aaaa', 'bbbb', min_ratio=1/2)
	assert interpret_contraint("aaaa/bbbb < 1/2") == RatioBound('aaaa', 'bbbb', max_ratio=1/2)
	assert interpret_contraint("aaaa / bbbb > 1/2") == RatioBound('aaaa', 'bbbb', min_ratio=1/2)
	assert interpret_contraint("aaaa / bbbb < 1/2") == RatioBound('aaaa', 'bbbb', max_ratio=1/2)
	assert interpret_contraint("aaaa / bbbb >= 1/2") == RatioBound('aaaa', 'bbbb', min_ratio=1/2)
	assert interpret_contraint("aaaa / bbbb <= 1/2") == RatioBound('aaaa', 'bbbb', max_ratio=1/2)
	assert interpret_contraint("1/4 < aaaa / bbbb <= 3/4") == RatioBound('aaaa', 'bbbb', min_ratio=1/4, max_ratio=3/4)
	assert interpret_contraint("aaaa > 3/4") == FixedBound('aaaa', minimum=3/4)
	assert interpret_contraint("aaaa < 3/4") == FixedBound('aaaa', maximum=3/4)
	assert interpret_contraint("1/4 < aaaa < 3/4") == FixedBound('aaaa', minimum=1/4, maximum=3/4)
Exemplo n.º 3
0
def test_parameter_summary():
    import larch
    from larch.model.constraints import RatioBound, OrderingBound, FixedBound

    m0 = larch.Model.Example(1)
    m0.lock_value('tottime', -0.05)
    m0.set_value('totcost', maximum=-0.005, minimum=-0.02)
    m0.constraints.append(OrderingBound("hhinc#3 <= hhinc#2"))
    m0.load_data()

    r0 = m0.maximize_loglike(
        method='slsqp',
        options={'ftol': 1e-09},
        quiet=True,
    )

    m0.calculate_parameter_covariance()

    ps = m0.parameter_summary('df').data

    try:
        assert ps.loc[('LOS', 'totcost'), 'Value'] == "-0.00500"
        assert ps.loc[('LOS', 'tottime'), 'Value'] == "-0.0500"
        assert ps.loc[('Income', 'hhinc#2'), 'Value'] == "-0.00159"
        assert ps.loc[('Income', 'hhinc#3'), 'Value'] == "-0.00159"

        assert ps.loc[('LOS', 'totcost'), 'Std Err'] == " 0.00"
        assert ps.loc[('LOS', 'tottime'), 'Std Err'] == " 0.00"
        assert ps.loc[('Income', 'hhinc#2'), 'Std Err'] == " 0.00140"
        assert ps.loc[('Income', 'hhinc#3'), 'Std Err'] == " 0.00140"

        assert ps.loc[('LOS', 'totcost'), 't Stat'] == " NA"
        assert ps.loc[('LOS', 'tottime'), 't Stat'] == " NA"
        assert ps.loc[('Income', 'hhinc#2'), 't Stat'] == "-1.14"
        assert ps.loc[('Income', 'hhinc#3'), 't Stat'] == "-1.14"

        assert ps.loc[('LOS', 'totcost'), 'Signif'] == ""
        assert ps.loc[('LOS', 'tottime'), 'Signif'] == ""
        assert ps.loc[('Income', 'hhinc#2'), 'Signif'] == ""
        assert ps.loc[('Income', 'hhinc#3'), 'Signif'] == ""

        assert ps.loc[('LOS', 'totcost'), 'Constrained'] == "totcost ≤ -0.005"
        assert ps.loc[('LOS', 'tottime'), 'Constrained'] == "fixed value"
        assert ps.loc[('Income', 'hhinc#2'),
                      'Constrained'] == "hhinc#3 ≤ hhinc#2"
        assert ps.loc[('Income', 'hhinc#3'),
                      'Constrained'] == "hhinc#3 ≤ hhinc#2"
    except:
        print(ps.iloc[:, :3])
        print(ps.iloc[:, 3:])
        raise
Exemplo n.º 4
0
def test_constrained_optimization():
    from larch.numba import example
    m = example(1)
    from larch.model.constraints import RatioBound, OrderingBound
    m.set_value("totcost", -0.001, maximum=0)
    m.set_value("tottime", maximum=0)
    m.constraints = [
        RatioBound(P("totcost"),
                   P("tottime"),
                   min_ratio=0.1,
                   max_ratio=1.0,
                   scale=1),
        OrderingBound(P("ASC_WALK"), P("ASC_BIKE")),
    ]
    m.load_data()
    r = m.maximize_loglike(method='slsqp')
    assert r.loglike == approx(-3647.76149525901)
    x = {
        'ASC_BIKE': -0.8087472748965431,
        'ASC_SR2': -2.193449976582375,
        'ASC_SR3P': -3.744188833076006,
        'ASC_TRAN': -0.7603092451373663,
        'ASC_WALK': -0.8087472751682576,
        'hhinc#2': -0.0021699330391421407,
        'hhinc#3': 0.0003696763687090173,
        'hhinc#4': -0.00509836274463602,
        'hhinc#5': -0.0431749907425252,
        'hhinc#6': -0.002373556571769923,
        'totcost': -0.004910169034222911,
        'tottime': -0.04790588175791953,
    }
    assert dict(r.x) == approx(x)
    assert r.iteration_number == 48

    m.set_values("null")
    m.set_value("totcost", -0.001, maximum=0)
    r2 = m.maximize_loglike(method='slsqp', bhhh_start=3)
    assert r2.iteration_number == 24
    assert r2.loglike == approx(-3647.76149525901)
    assert dict(r2.x) == approx(x, rel=1e-2)
Exemplo n.º 5
0
def test_parameter_summary():
	import larch
	from larch.model.constraints import RatioBound, OrderingBound, FixedBound

	m0 = larch.Model.Example(1)
	m0.lock_value('tottime', -0.05)
	m0.set_value('totcost', maximum=-0.005, minimum=-0.02)
	m0.constraints.append(OrderingBound("hhinc#3 <= hhinc#2"))
	m0.load_data()

	# constraint tests are unstable across platforms
	# r0 = m0.maximize_loglike(
	# 	method='slsqp',
	# 	options={'ftol': 1e-09},
	# 	quiet=True,
	# )
	m0.set_values({
		'ASC_BIKE': -2.401574701017008,
		'ASC_SR2': -2.2234574452767037,
		'ASC_SR3P': -3.630001719635557,
		'ASC_TRAN': -0.7000998451196682,
		'ASC_WALK': -0.25495834390295924,
		'hhinc#2': -0.0015948074747861667,
		'hhinc#3': -0.0015948074747729564,
		'hhinc#4': -0.005385664916039662,
		'hhinc#5': -0.01284021327300144,
		'hhinc#6': -0.009658848920781143,
		'totcost': -0.005000000000003634,
		'tottime': -0.05,
	})

	m0.calculate_parameter_covariance()

	ps = m0.parameter_summary('df').data
	stable_df(ps, 'parameter_summary_test')

	try:
		assert ps.loc[('LOS', 'totcost'), 'Value'] == "-0.00500"
		assert ps.loc[('LOS', 'tottime'), 'Value'] == "-0.0500"
		assert ps.loc[('Income', 'hhinc#2'), 'Value'] == "-0.00159"
		assert ps.loc[('Income', 'hhinc#3'), 'Value'] == "-0.00159"

		assert ps.loc[('LOS', 'totcost'), 'Std Err'] == " NA"
		assert ps.loc[('LOS', 'tottime'), 'Std Err'] == " NA"
		assert ps.loc[('Income', 'hhinc#2'), 'Std Err'] == " 0.00140"
		assert ps.loc[('Income', 'hhinc#3'), 'Std Err'] == " 0.00140"

		assert ps.loc[('LOS', 'totcost'), 't Stat'] == " NA"
		assert ps.loc[('LOS', 'tottime'), 't Stat'] == " NA"
		assert ps.loc[('Income', 'hhinc#2'), 't Stat'] == "-1.14"
		assert ps.loc[('Income', 'hhinc#3'), 't Stat'] == "-1.14"

		assert ps.loc[('LOS', 'totcost'), 'Signif'] == "[***]"
		assert ps.loc[('LOS', 'tottime'), 'Signif'] == ""
		assert ps.loc[('Income', 'hhinc#2'), 'Signif'] == ""
		assert ps.loc[('Income', 'hhinc#3'), 'Signif'] == ""

		assert ps.loc[('LOS', 'totcost'), 'Constrained'] == "totcost ≤ -0.005"
		assert ps.loc[('LOS', 'tottime'), 'Constrained'] == "fixed value"
		assert ps.loc[('Income', 'hhinc#2'), 'Constrained'] == "hhinc#3 ≤ hhinc#2"
		assert ps.loc[('Income', 'hhinc#3'), 'Constrained'] == "hhinc#3 ≤ hhinc#2"
	except:
		print(ps.iloc[:,:3])
		print(ps.iloc[:,3:])
		raise
Exemplo n.º 6
0
def test_multi_constraints():
	#m0 = larch.Model.Example(1)
	m1 = larch.Model.Example(1)

	#m0.lock_value('tottime', -0.1)
	m1.set_value('tottime', maximum=-0.1)

	#m0.utility_co[2] = P.ASC_SR2 + P('hhinc#23') * X.hhinc
	#m0.utility_co[3] = P.ASC_SR3P + P('hhinc#23') * X.hhinc
	#m0.remove_unused_parameters()

	m1.constraints.append(OrderingBound("hhinc#3 <= hhinc#2"))

	#m0.load_data()
	m1.load_data()

	# r0 = m0.torch.maximize_loglike(
	# 	method='slsqp',
	# 	options={'ftol': 1e-09},
	# )

	r1 = m1.maximize_loglike(
		method='slsqp',
		options={'ftol': 1e-09},
		quiet=True,
	)
	# assert r1.message == 'Positive directional derivative for linesearch'
	m1.constraints.rescale(0.1)

	r1 = m1.maximize_loglike(
		method='slsqp',
		options={'ftol': 1e-09},
		quiet=True,
	)
	# assert r1.message == 'Positive directional derivative for linesearch'

	m1.constraints.rescale(0.01)
	r1 = m1.maximize_loglike(
		method='slsqp',
		options={'ftol': 1e-09},
		quiet=True,
	)

	assert r1.loglike == approx(-3725.439832484451)

	assert r1.message == 'Optimization terminated successfully.'

	#m0.calculate_parameter_covariance()

	m1.calculate_parameter_covariance()

	assert dict(m1.pf['value']) == approx({
		'ASC_BIKE': -1.8336226490217296,
		'ASC_SR2': -2.008575006161787,
		'ASC_SR3P': -3.372494233637122,
		'ASC_TRAN': 0.23543257975862672,
		'ASC_WALK': 1.1106100812079671,
		'hhinc#2': -0.0016453210270738865,
		'hhinc#3': -0.001645187376205547,
		'hhinc#4': -0.0055451755658633,
		'hhinc#5': -0.012519484434344206,
		'hhinc#6': -0.01079425276590098,
		'totcost': -0.005093387068047046,
		'tottime': -0.1
	}, rel=1e-2)

	# Problem: Travis is failing on this test, giving
	#    tottime as NaN.  Disabling until we find a
	#    similar problem we can test and diagnose
	assert dict(m1.pf['std_err']) == approx({
		'ASC_BIKE': 0.30013699696981455,
		'ASC_SR2': 0.09856612504524623,
		'ASC_SR3P': 0.1250682262709841,
		'ASC_TRAN': 0.12311806485021345,
		'ASC_WALK': 0.17807446535212992,
		'hhinc#2': 0.0014236497001240312,
		'hhinc#3': 0.001423649700124031,
		'hhinc#4': 0.0019212691279916797,
		'hhinc#5': 0.005248506439174827,
		'hhinc#6': 0.003146916477051659,
		'totcost': 0.0002466030726673273,
		'tottime': 0
	}, abs=1e-10, rel=5e-2)

	assert dict(m1.pf['unconstrained_std_err']) == approx({
		'ASC_BIKE': 0.3032386370524189,
		'ASC_SR2': 0.10695557451038115,
		'ASC_SR3P': 0.18033743391454088,
		'ASC_TRAN': 0.1439964957540911,
		'ASC_WALK': 0.20789603304640025,
		'hhinc#2': 0.0015638823624232918,
		'hhinc#3': 0.002640071485204856,
		'hhinc#4': 0.00192260391409179,
		'hhinc#5': 0.0052485070222418155,
		'hhinc#6': 0.0031494462988719317,
		'totcost': 0.00024742269603570923,
		'tottime': 0.004377029904009681
	}, rel=5e-2)