def skip_test_bma(self):
        storage = StorageFactory().get_storage('dict_storage')

        storage.write_table(table_name='dataset',
                            table_data={
                                "id":
                                arange(100) + 1,
                                "attr1":
                                concatenate((random.randint(0, 10, 50),
                                             random.randint(20, 40, 50))),
                                "attr2":
                                random.ranf(100),
                                "outcome":
                                array(50 * [0] + 50 * [1])
                            })

        ds = Dataset(in_storage=storage, in_table_name='dataset', id_name="id")
        specification = EquationSpecification(
            variables=array(["constant", "attr2", "attr1"]),
            coefficients=array(["constant", "ba2", "ba1"]))

        filename = 'bma_output.pdf'
        model = RegressionModel(
            estimate_config={'bma_imageplot_filename': filename})
        model.estimate(specification,
                       ds,
                       "outcome",
                       procedure="opus_core.bma_for_linear_regression_r")
 def estimate(self,
              specification,
              dataset,
              outcome_attribute="housing_price",
              index=None,
              procedure="opus_core.estimate_linear_regression",
              data_objects=None,
              estimate_config=None,
              debuglevel=0):
     if data_objects is not None:
         self.dataset_pool.add_datasets_if_not_included(data_objects)
     if self.filter_attribute <> None:
         res = Resources({"debug": debuglevel})
         index = dataset.get_filtered_index(self.filter_attribute,
                                            threshold=0,
                                            index=index,
                                            dataset_pool=self.dataset_pool,
                                            resources=res)
     return RegressionModel.estimate(self,
                                     specification,
                                     dataset,
                                     outcome_attribute,
                                     index,
                                     procedure,
                                     estimate_config=estimate_config,
                                     debuglevel=debuglevel)
 def estimate(self, specification, dataset, outcome_attribute, index=None, **kwargs):
     if index is None:
         index = arange(dataset.size())
     data_objects = kwargs.get("data_objects",{})
     if data_objects is not None:
         self.dataset_pool.add_datasets_if_not_included(data_objects)
     # filter out agents for this group
     new_index = self.group_member.get_index_of_my_agents(dataset, index, dataset_pool=self.dataset_pool)
     return RegressionModel.estimate(self,  specification, dataset, outcome_attribute,
                                            index=index[new_index], **kwargs)
示例#4
0
 def estimate(self, specification, dataset, outcome_attribute="urbansim.gridcell.ln_total_land_value", index = None,
                     procedure="opus_core.estimate_linear_regression", data_objects=None,
                     estimate_config=None,  debuglevel=0):
     if data_objects is not None:
         self.dataset_pool.add_datasets_if_not_included(data_objects)
     if self.filter <> None:
         res = Resources({"debug":debuglevel})
         index = dataset.get_filtered_index(self.filter, threshold=0, index=index, dataset_pool=self.dataset_pool,
                                            resources=res)
     return RegressionModel.estimate(self, specification, dataset, outcome_attribute, index, procedure,
                                  estimate_config=estimate_config, debuglevel=debuglevel)
示例#5
0
    def skip_test_bma(self):
        storage = StorageFactory().get_storage('dict_storage')

        storage.write_table(
            table_name='dataset',
            table_data={
                "id":arange(100)+1,
                "attr1":concatenate((random.randint(0,10, 50), random.randint(20,40, 50))),
                "attr2":random.ranf(100),
                "outcome": array(50*[0]+50*[1])
                }
            )

        ds = Dataset(in_storage=storage, in_table_name='dataset', id_name="id")
        specification = EquationSpecification(
                          variables=array(["constant", "attr2", "attr1"]),
                          coefficients=array(["constant", "ba2", "ba1"]))

        filename = 'bma_output.pdf'
        model = RegressionModel(estimate_config={'bma_imageplot_filename': filename})
        model.estimate(specification, ds, "outcome", procedure="opus_core.bma_for_linear_regression_r")
示例#6
0
 def estimate(
         self,
         specification,
         dataset,
         outcome_attribute="urbansim.gridcell.logistic_fraction_residential_land",
         index=None,
         procedure="opus_core.estimate_linear_regression",
         data_objects=None,
         estimate_config=None,
         debuglevel=0):
     return RegressionModel.estimate(self,
                                     specification,
                                     dataset,
                                     outcome_attribute,
                                     index,
                                     procedure,
                                     data_objects=data_objects,
                                     estimate_config=estimate_config,
                                     debuglevel=debuglevel)
 def estimate(self,
              specification,
              dataset,
              outcome_attribute,
              index=None,
              **kwargs):
     if index is None:
         index = arange(dataset.size())
     data_objects = kwargs.get("data_objects", {})
     if data_objects is not None:
         self.dataset_pool.add_datasets_if_not_included(data_objects)
     # filter out agents for this group
     new_index = self.group_member.get_index_of_my_agents(
         dataset, index, dataset_pool=self.dataset_pool)
     return RegressionModel.estimate(self,
                                     specification,
                                     dataset,
                                     outcome_attribute,
                                     index=index[new_index],
                                     **kwargs)
示例#8
0
result = hlcm2.run(specification, coefficients, households)
#coef, results = hlcm2.estimate(specification, agent_set=households, debuglevel=1)
#hlcm2.plot_choice_histograms(capacity=locations.get_attribute("capacity"))
households.get_attribute("location")

# Regression model
locations.add_primary_attribute(name="distance_to_cbd", data=[5,10,5,1,20,0,7,7,3])

from opus_core.regression_model import RegressionModel
rm = RegressionModel(regression_procedure="opus_core.linear_regression")
specification = EquationSpecification(
                          variables=array(["constant", "gridcell.distance_to_cbd"]),
                          coefficients=array(["constant", "dcbd_coef"]))

coef, other_results = rm.estimate(specification, dataset=locations, 
                                  outcome_attribute="gridcell.cost", 
                                  procedure="opus_core.estimate_linear_regression")
coef.summary()

dstorage = StorageFactory().get_storage('dict_storage')
dstorage.write_table(
    table_name = 'gridcells',
    table_data = {'id':array([1,2,3,4]),
             'distance_to_cbd':array([2,4,6,8])
             })

ds = Dataset(in_storage=dstorage, in_table_name='gridcells',
             id_name='id', dataset_name='gridcell')

cost = rm.run(specification, coefficients=coef, dataset=ds)
#                   "demand_lag1"
#                    "sum_demand_times_2",
                    "waterdemand.consumption_re.something_like_sum_demand",
                   )
                 )

print "Create a model object"

years = range(2001, 2003)

# single
model = RegressionModel()
print "Estimate coefficients - single"
coefficients, other_est_results = model.estimate(specification, consumption, 
                    outcome_attribute="waterdemand.%s.sum_demand" % consumption_type,  # if outcome_attribute is opus_core.func.ln(), the simulation results need to take exp()
                    index=index_est, 
                    procedure="opus_core.estimate_linear_regression",
                    data_objects=dataset_pool.datasets_in_pool())


"""Simulate over the set of years."""
for year in years:  
    print "\nSimulate water demand %s" % year
    SimulationState().set_current_time(year)
    dataset_pool = SessionConfiguration().get_dataset_pool()
    dataset_pool.remove_all_datasets()
    gridcells = dataset_pool.get_dataset("gridcell")
    
    #create a ConsumptionDataset instance out of gridcells - simulate water demand for every gridcell
    resources = Resources({'data':{
            "grid_id":gridcells.get_id_attribute(),
 def estimate(self, specification, dataset, outcome_attribute="urbansim.gridcell.logistic_fraction_residential_land", index = None,
                     procedure="opus_core.estimate_linear_regression", data_objects=None,
                     estimate_config=None,  debuglevel=0):
     return RegressionModel.estimate(self, specification, dataset, outcome_attribute, index, procedure, data_objects=data_objects,
                                  estimate_config=estimate_config, debuglevel=debuglevel)
示例#11
0
#                   "demand_lag1"
#                    "sum_demand_times_2",
                    "waterdemand.consumption_re.something_like_sum_demand",
                   )
                 )

print "Create a model object"

years = range(2001, 2003)

# single
model = RegressionModel()
print "Estimate coefficients - single"
coefficients, other_est_results = model.estimate(specification, consumption, 
                    outcome_attribute="waterdemand.%s.sum_demand" % consumption_type,  # if outcome_attribute is opus_core.func.ln(), the simulation results need to take exp()
                    index=index_est, 
                    procedure="opus_core.estimate_linear_regression",
                    data_objects=dataset_pool.datasets_in_pool())


"""Simulate over the set of years."""
for year in years:  
    print "\nSimulate water demand %s" % year
    SimulationState().set_current_time(year)
    dataset_pool = SessionConfiguration().get_dataset_pool()
    dataset_pool.remove_all_datasets()
    gridcells = dataset_pool.get_dataset("gridcell")
    
    #create a ConsumptionDataset instance out of gridcells - simulate water demand for every gridcell
    resources = Resources({'data':{
            "grid_id":gridcells.get_id_attribute(),
示例#12
0
result = hlcm2.run(specification, coefficients, households)
#coef, results = hlcm2.estimate(specification, agent_set=households, debuglevel=1)
#hlcm2.plot_choice_histograms(capacity=locations.get_attribute("capacity"))
households.get_attribute("location")

# Regression model
locations.add_primary_attribute(name="distance_to_cbd", data=[5,10,5,1,20,0,7,7,3])

from opus_core.regression_model import RegressionModel
rm = RegressionModel(regression_procedure="opus_core.linear_regression")
specification = EquationSpecification(
                          variables=array(["constant", "gridcell.distance_to_cbd"]),
                          coefficients=array(["constant", "dcbd_coef"]))

coef, other_results = rm.estimate(specification, dataset=locations, 
                                  outcome_attribute="gridcell.cost", 
                                  procedure="opus_core.estimate_linear_regression")
coef.summary()

dstorage = StorageFactory().get_storage('dict_storage')
dstorage.write_table(
    table_name = 'gridcells',
    table_data = {'id':array([1,2,3,4]),
             'distance_to_cbd':array([2,4,6,8])
             })

ds = Dataset(in_storage=dstorage, in_table_name='gridcells',
             id_name='id', dataset_name='gridcell')

cost = rm.run(specification, coefficients=coef, dataset=ds)