/
auto_ownership.py
75 lines (61 loc) · 1.64 KB
/
auto_ownership.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
import os
import numpy as np
import pandas as pd
import yaml
from typing import Collection
from .. import Dict
from .general import (
remove_apostrophes,
apply_coefficients,
simple_simulate_data,
dict_of_linear_utility_from_spec,
)
from ... import Model, DataFrames, P, X
def auto_ownership_model(
name="auto_ownership",
edb_directory="output/estimation_data_bundle/{name}/",
return_data=False,
):
data = simple_simulate_data(
name=name,
edb_directory=edb_directory,
values_index_col="household_id",
)
coefficients = data.coefficients
# coef_template = data.coef_template # not used
spec = data.spec
chooser_data = data.chooser_data
settings = data.settings
altnames = list(spec.columns[3:])
altcodes = range(len(altnames))
chooser_data = remove_apostrophes(chooser_data)
chooser_data.fillna(0, inplace=True)
# Remove choosers with invalid observed choice
chooser_data = chooser_data[chooser_data['override_choice'] >= 0]
m = Model()
# One of the alternatives is coded as 0, so
# we need to explicitly initialize the MNL nesting graph
# and set to root_id to a value other than zero.
m.initialize_graph(alternative_codes=altcodes, root_id=99)
m.utility_co = dict_of_linear_utility_from_spec(
spec, 'Label', dict(zip(altnames, altcodes)),
)
apply_coefficients(coefficients, m)
d = DataFrames(
co=chooser_data,
av=True,
alt_codes=altcodes,
alt_names=altnames,
)
m.dataservice = d
m.choice_co_code = 'override_choice'
if return_data:
return m, Dict(
edb_directory=data.edb_directory,
chooser_data=chooser_data,
coefficients=coefficients,
spec=spec,
altnames=altnames,
altcodes=altcodes,
)
return m