diff --git a/urbansim/urbanchoice/mnl.py b/urbansim/urbanchoice/mnl.py index 463eb252..0f460489 100644 --- a/urbansim/urbanchoice/mnl.py +++ b/urbansim/urbanchoice/mnl.py @@ -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` @@ -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, diff --git a/urbansim/urbanchoice/tests/data/chosen_test.csv b/urbansim/urbanchoice/tests/data/chosen_test.csv new file mode 100644 index 00000000..ceac127f --- /dev/null +++ b/urbansim/urbanchoice/tests/data/chosen_test.csv @@ -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 diff --git a/urbansim/urbanchoice/tests/data/data_test.csv b/urbansim/urbanchoice/tests/data/data_test.csv new file mode 100644 index 00000000..6f961423 --- /dev/null +++ b/urbansim/urbanchoice/tests/data/data_test.csv @@ -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 diff --git a/urbansim/urbanchoice/tests/test_mnl.py b/urbansim/urbanchoice/tests/test_mnl.py index a939e462..efb09063 100644 --- a/urbansim/urbanchoice/tests/test_mnl.py +++ b/urbansim/urbanchoice/tests/test_mnl.py @@ -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'])