In order to work with variables that determine an interaction between two datasets, there is a subclass of Dataset, called InteractionDataset. Attributes of this class are stored as two-dimensional arrays (see Section 22.1.8 for details).
To create an interaction set for households and gridcells from the previous sections, do
>>> from opus_core.datasets.interaction_dataset import InteractionDataset >>> interactions = InteractionDataset(dataset1 = households, dataset2 = locations)The dataset name is composed from dataset names of the two interacting datasets:
>>> interactions.get_dataset_name() 'household_x_gridcell'Thus, an interaction variable for such dataset will be implemented in the directory household_x_gridcell.
The InteractionDataset class contains several methods that are useful for variable computation which must return a two-dimensional array. For example, a variable defined as a multiplication of cost and income can be implemented as:
from opus_core.variables.variable import Variable
class cost_times_income(Variable):
def dependencies(self):
return ["gridcell.cost", "household.income"]
def compute(self, dataset_pool):
return self.get_dataset().multiply("income", "cost")
This cost_times_income variable is mostly for illustration; it can also be defined more conveniently as an expression (see Section 21.4).
If we wish that only a subset of each dataset interact (for example for memory reasons), we can pass the corresponding indices to the constructor:
>>> from numpy import arange
>>> interactions = InteractionDataset(dataset1 = households, dataset2 = locations,
index1 = arange(5), index2 = arange(3))
Here only the first 5 households and the first three gridcells interact.
The compute() method of the interaction variable should return an array of
size index1
>>> interactions.compute_variables(
["urbansim.household_x_gridcell.cost_times_income"])
array([[ 500000., 200000., 600000.],
[ 1000000., 400000., 1200000.],
[ 2500000., 1000000., 3000000.],
[ 1500000., 600000., 1800000.],
[ 250000., 100000., 300000.]])
urbansim uses interaction variables mainly in ChoiceModel classes, where agents
interact with dataset of choices. The household location choice model object
hlcm from Section 21.2.2 can be for example estimated using two
variables, one of which is an interaction variable:
>>> specification = EquationSpecification(
variables=array([
"gridcell.cost",
"urbansim.household_x_gridcell.cost_times_income"]),
coefficients=array(["costcoef", "cti_coef"]))
>>> # place households
>>> households.add_primary_attribute(data=[2,8,3,1,5,4,9,7,3,6], name="location")
>>> coef, other_results = hlcm.estimate(specification, households)
Estimating Household Location Choice Model
(from urbansim.models.household_location_choice_model):
started on Thu Nov 6 12:12:47 2008
urbansim.household_x_gridcell.cost_times_income......................0.0 sec
submodel: -2
Convergence achieved.
Akaike's Information Criterion (AIC): 39.8199022026
Number of Iterations: 18
***********************************************
Log-likelihood is: -17.9099511013
Null Log-likelihood is: -21.9722457734
Likelihood ratio index: 0.184882998032
Adj. likelihood ratio index: 0.0938590753688
Number of observations: 10
Suggested |t-value| > 1.51742712939
Convergence statistic is: 0.000302603242051
-----------------------------------------------
Coeff_names estimate std err t-values
costcoef -0.00312452 0.00339464 -0.920429
cti_coef 4.45803e-07 5.34429e-07 0.834168
***********************************************
Elapsed time: 0.02 seconds
Estimating Household Location Choice Model
(from urbansim.models.household_location_choice_model): completed...0.0 sec