-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtest_MNLDiscreteChoiceModel.py
89 lines (65 loc) · 1.93 KB
/
test_MNLDiscreteChoiceModel.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# This is the script for MNLDiscreteChoiceModel estimation and simulation demo - Guangyu
# run it under semcog_urbansim folder
# In[ ]:
from urbansim.models.dcm import MNLDiscreteChoiceModel
import os
import numpy as np
import pandas as pd
import orca
from urbansim.utils import misc
import dataset
import variables
import models
import utils
orca.run(["build_networks"])
orca.run(["neighborhood_vars"])
# ## HLCM Estimation Function old one
#%%
# def estimate_hlcm(yaml_config):
# model = MNLDiscreteChoiceModel.from_yaml(str_or_buffer=misc.config(yaml_config))
# hh = orca.get_table("households").to_frame(model.columns_used()).fillna(0)
# b = orca.get_table("buildings").to_frame(model.columns_used()).fillna(0)
# print(model.fit(hh, b, hh[model.choice_column]))
# return model, model.fit_parameters
# ## HLCM Configs
# In[8]:
# hlcm_configs = [
# "hlcm/large_area_income_quartile/hlcm400099.yaml",
# "hlcm/large_area_income_quartile/hlcm200115.yaml",
# "hlcm/large_area_income_quartile/hlcm100125.yaml",
# ......
# ]
# m1, m1para = estimate_hlcm(hlcm_configs[0])
# m1.to_yaml("./test.yaml")
# ## Estimate a particular HLCM submodel
# %%
###
# test estimation MNLDiscreteChoiceModel
######## estimation demo ###########
# %%
m2 = MNLDiscreteChoiceModel.from_yaml(str_or_buffer=misc.config("test_indiv.yaml"))
m2.probability_mode
# %%
# compile data for estimation
hh = orca.get_table("households").to_frame(m2.columns_used()).fillna(0)
b = orca.get_table("buildings").to_frame(m2.columns_used()).fillna(0)
# %%
m2.fit(hh, b, hh[m2.choice_column])
#%%
m2.to_yaml("test_indiv.yaml") # save configs
# %%
######## simulation demo ###########
# select HH and buildings
hhs = hh.loc[hh.large_area_id == 161]
hhs = hhs.sample(n=5)
hhs.building_id = -1
bs = b.loc[b.large_area_id == 161]
bs = bs.sample(n=5)
# %%
# test simulation
chs = m2.predict(hhs, bs, debug=True)
# %%
chs
# %%
m2prob = m2.probabilities(hhs, bs)
m2prob