def test_production2(self): # single factor attr for model_subtype in ["pow", "exp"]: production = models.Model("production", model_subtype, Test.dataset, "MIGRATIONS", "O_GEOGRAPHY_CODE", ["HOUSEHOLDS", "JOBS"], "DISTANCE") self.assertTrue( rmse( production(xd=[ Test.dataset.HOUSEHOLDS.values, Test.dataset.JOBS.values ]), production.impl.yhat) < 1e-10) Test.dataset["HH_CHANGED"] = Test.dataset.HOUSEHOLDS Test.dataset.loc[Test.dataset.D_GEOGRAPHY_CODE == "E07000178", "HH_CHANGED"] = Test.dataset.loc[ Test.dataset.D_GEOGRAPHY_CODE == "E07000178", "HOUSEHOLDS"] + 300000 Test.dataset["J_CHANGED"] = Test.dataset.JOBS Test.dataset.loc[Test.dataset.D_GEOGRAPHY_CODE == "E07000178", "J_CHANGED"] = Test.dataset.loc[ Test.dataset.D_GEOGRAPHY_CODE == "E07000178", "JOBS"] + 300000 self.assertTrue( rmse( production(xd=[ Test.dataset.HH_CHANGED.values, Test.dataset.HH_CHANGED.values ]), production.impl.yhat) > 1.0)
def test_attraction(self): # single factor prod for model_subtype in ["pow", "exp"]: attraction = models.Model("attraction", model_subtype, Test.dataset, "MIGRATIONS", "PEOPLE", "D_GEOGRAPHY_CODE", "DISTANCE") self.assertTrue( rmse(attraction(xo=Test.dataset.PEOPLE.values), attraction.impl.yhat) < 1e-10) Test.dataset["P_CHANGED"] = Test.dataset.PEOPLE Test.dataset.loc[Test.dataset.D_GEOGRAPHY_CODE == "E07000178", "P_CHANGED"] = Test.dataset.loc[ Test.dataset.D_GEOGRAPHY_CODE == "E07000178", "HOUSEHOLDS"] + 300000 self.assertTrue( rmse(attraction(xo=Test.dataset.P_CHANGED.values), attraction.impl.yhat) > 1.0)
def test_stats(self): np.random.seed(0) x = np.random.normal(0, 1, 10000) y = np.random.normal(0, 1, 10000) # R2 self.assertEqual(r2(x, x), 1.0) self.assertEqual(r2(x, -x), 1.0) # rho=-1 self.assertTrue(-0.0001 < r2(x, y) < 0.0001) # rho = 0.5 res = r2(x, rho * x + np.sqrt(1 - rho**2) * y) self.assertTrue(rho**2 - 0.015 < res < rho**2 + 0.015) z = np.zeros(10000) self.assertEqual(rmse(x, z), np.sqrt(np.mean(x * x))) self.assertEqual(rmse(x, x), 0.0) self.assertEqual(rmse(x, -x), np.sqrt(np.mean(4 * x * x)))
def test_gravity(self): # single factor prod and attr for model_subtype in ["pow", "exp"]: gravity = models.Model("gravity", model_subtype, Test.dataset, "MIGRATIONS", "PEOPLE", "HOUSEHOLDS", "DISTANCE") self.assertTrue( rmse( gravity(Test.dataset.PEOPLE.values, Test.dataset. HOUSEHOLDS.values), gravity.impl.yhat) < 1e-10) Test.dataset["HH_CHANGED"] = Test.dataset.HOUSEHOLDS Test.dataset.loc[Test.dataset.D_GEOGRAPHY_CODE == "E07000178", "HH_CHANGED"] = Test.dataset.loc[ Test.dataset.D_GEOGRAPHY_CODE == "E07000178", "HOUSEHOLDS"] + 300000 self.assertTrue( rmse( gravity(Test.dataset.PEOPLE.values, Test.dataset. HH_CHANGED.values), gravity.impl.yhat) > 1.0)
def test_gravity2(self): # multi factor emission for model_subtype in ["pow", "exp"]: gravity = models.Model("gravity", model_subtype, Test.dataset, "MIGRATIONS", "PEOPLE", ["HOUSEHOLDS", "JOBS"], "DISTANCE") self.assertTrue( rmse( gravity(Test.dataset.PEOPLE.values, [ Test.dataset.HOUSEHOLDS.values, Test.dataset.JOBS.values ]), gravity.impl.yhat) < 1e-10) # multi factor attraction for model_subtype in ["pow", "exp"]: gravity = models.Model("gravity", model_subtype, Test.dataset, "MIGRATIONS", ["PEOPLE", "JOBS"], "HOUSEHOLDS", "DISTANCE") self.assertTrue( rmse( gravity( [Test.dataset.PEOPLE.values, Test.dataset.JOBS.values], Test.dataset.HOUSEHOLDS.values), gravity.impl.yhat) < 1e-10)