Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: BUG: ABNORMAL_TERMINATION test from real data #212

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion urbansim/urbanchoice/mnl.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,19 @@ def mnl_loglik(beta, data, chosen, numalts, weights=None, lcgrad=False,
return -1 * loglik, -1 * gradarr


def mnl_loglik_print(beta, data, chosen, numalts, weights=None, lcgrad=False,
stderr=0):
# TODO: REMOVE when gradarr is correct
out = mnl_loglik(beta, data, chosen, numalts, weights, lcgrad, stderr)
approx_gradarr = scipy.optimize.approx_fprime(
beta,
lambda x: mnl_loglik(x, data, chosen, numalts, weights, lcgrad, stderr)[0],
1e-14
)
print(np.sqrt(np.sum((out[1] - approx_gradarr) ** 2)))
return out


def mnl_simulate(data, coeff, numalts, GPU=False, returnprobs=True):
"""
Get the probabilities for each chooser choosing between `numalts`
Expand Down Expand Up @@ -240,7 +253,7 @@ def mnl_estimate(data, chosen, numalts, GPU=False, coeffrange=(-3, 3),

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,
bfgs_result = scipy.optimize.fmin_l_bfgs_b(mnl_loglik_print,
beta,
args=args,
fprime=None,
Expand Down
8 changes: 8 additions & 0 deletions urbansim/urbanchoice/tests/data/chosen_test.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
,0,1,2,3,4
0,1.0,0.0,0.0,0.0,0.0
1,1.0,0.0,0.0,0.0,0.0
2,1.0,0.0,0.0,0.0,0.0
3,1.0,0.0,0.0,0.0,0.0
4,1.0,0.0,0.0,0.0,0.0
5,1.0,0.0,0.0,0.0,0.0
6,1.0,0.0,0.0,0.0,0.0
36 changes: 36 additions & 0 deletions urbansim/urbanchoice/tests/data/data_test.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
,0,1,2
0,4409658.98696,0.0,0.0
1,3320777.25184,0.0,0.0
2,2363051.38483,0.0,1.0
3,3490299.11647,0.0,0.0
4,3257927.25275,0.0,0.0
5,3874655.07666,1.0,0.0
6,3795311.23148,0.0,1.0
7,3242144.92333,0.0,0.0
8,3795311.23148,0.0,0.0
9,3318673.38664,0.0,0.0
10,1683593.37837,0.0,0.0
11,3405177.03566,0.0,0.0
12,3645178.65644,0.0,0.0
13,3220034.79325,0.0,0.0
14,3726544.40875,0.0,0.0
15,4837511.64852,0.0,0.0
16,4177826.683,0.0,1.0
17,3933594.78206,0.0,0.0
18,3310981.74724,0.0,0.0
19,3022298.69346,0.0,0.0
20,5022700.45103,0.0,0.0
21,3339654.76444,0.0,1.0
22,3520258.21448,0.0,0.0
23,3339229.57813,0.0,0.0
24,2630789.88059,0.0,0.0
25,3581951.19946,0.0,0.0
26,3224228.97506,0.0,0.0
27,3841485.17584,0.0,1.0
28,3230092.32976,0.0,0.0
29,3094213.1226,0.0,0.0
30,3726544.40875,0.0,0.0
31,4259414.02226,0.0,0.0
32,2240808.21821,0.0,1.0
33,2407975.76413,0.0,1.0
34,2584192.93162,0.0,0.0
6 changes: 6 additions & 0 deletions urbansim/urbanchoice/tests/test_mnl.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ def test_data(request):
}


def test_mnl_estimate_temp():
data = pd.read_csv(os.path.join(os.path.dirname(__file__), 'data', 'data_test.csv'), index_col=0)
chosen = pd.read_csv(os.path.join(os.path.dirname(__file__), 'data', 'chosen_test.csv'), index_col=0)
mnl.mnl_estimate(data.values, chosen.values, 5, coeffrange=(-999, 999))


@pytest.fixture
def df(test_data):
filen = os.path.join(os.path.dirname(__file__), 'data', test_data['data'])
Expand Down