コード例 #1
0
 def test_loss(self):
     """Test longitudinal multinomial model loss."""
     X, _, _ = LongitudinalFeaturesLagger(n_lags=self.n_lags)\
         .fit_transform(self.X)
     model = ModelSCCS(n_intervals=2, n_lags=self.n_lags)\
         .fit(X, self.y)
     loss = model.loss(coeffs=np.array([0.0, 0.0, 1.0, 0.0]))
     expected_loss = -np.log((np.e / (2 * np.e) * 1 / (1 + np.e))) / 2
     self.assertAlmostEqual(loss, expected_loss)
コード例 #2
0
 def test_grad(self):
     """Test longitudinal multinomial model gradient value."""
     X = [np.array([[0, 0.], [1, 0]]), np.array([[1, 0.], [0, 1]])]
     X, _, _ = LongitudinalFeaturesLagger(n_lags=self.n_lags) \
         .fit_transform(X)
     model = ModelSCCS(n_intervals=2, n_lags=self.n_lags) \
         .fit(X, self.y)
     grad = model.grad(coeffs=np.array([0.0, 0.0, 1.0, 0.0]))
     expected_grad = -np.array([
         -1 / 2 - 1 / (1 + np.e), 1 - np.e / (1 + np.e), 1 - np.e /
         (1 + np.e), 0
     ]) / 2
     np.testing.assert_almost_equal(grad, expected_grad, decimal=15)
コード例 #3
0
 def test_grad_loss_consistency(self):
     """Test longitudinal multinomial model gradient properties."""
     n_intervals = 16
     n_lags = 4
     sim = SimuSCCS(500, n_intervals, 3, n_lags, None,
                    True, "infinite", seed=42, verbose=False)
     X, y, censoring, coeffs = sim.simulate()
     X = LongitudinalFeaturesLagger(n_lags=n_lags) \
         .fit_transform(X, censoring)
     model = ModelSCCS(n_intervals=n_intervals, n_lags=n_lags) \
         .fit(X, y, censoring)
     self._test_grad(model, coeffs)
     X_sparse = [csr_matrix(x) for x in X]
     model = ModelSCCS(n_intervals=n_intervals, n_lags=n_lags) \
         .fit(X_sparse, y, censoring)
     self._test_grad(model, coeffs)
コード例 #4
0
 def test_lipschitz_constant(self):
     """Test longitudinal multinomial model Lipschitz constant."""
     X = [
         np.array([[0, 0, 1], [0, 1, 1], [1, 1, 1]], dtype="float64"),
         np.array([[0, 1, 1], [0, 1, 1], [1, 1, 1]], dtype="float64")
     ]
     y = [
         np.array([0, 1, 0], dtype="int32"),
         np.array([0, 1, 0], dtype="int32")
     ]
     n_lags = np.repeat(1, 3).astype(dtype="uint64")
     X, _, _ = LongitudinalFeaturesLagger(n_lags=n_lags) \
         .fit_transform(X)
     model = ModelSCCS(n_intervals=3, n_lags=n_lags).fit(X, y)
     lip_constant = model.get_lip_max()
     expected_lip_constant = .5
     self.assertEqual(lip_constant, expected_lip_constant)
コード例 #5
0
 def test_convergence_with_lags(self):
     """Test longitudinal multinomial model convergence."""
     n_intervals = 10
     n_lags = 3
     n_samples = 1500
     n_features = 3
     sim = SimuSCCS(n_samples, n_intervals, n_features, n_lags, None,
                    True, "short", seed=42, verbose=False)
     X, y, censoring, coeffs = sim.simulate()
     X = LongitudinalFeaturesLagger(n_lags=n_lags) \
         .fit_transform(X, censoring)
     model = ModelSCCS(n_intervals=n_intervals,
                       n_lags=n_lags).fit(X, y, censoring)
     solver = SVRG(max_iter=15, verbose=False)
     solver.set_model(model).set_prox(ProxZero())
     coeffs_svrg = solver.solve(step=1 / model.get_lip_max())
     np.testing.assert_almost_equal(coeffs, coeffs_svrg, decimal=1)
コード例 #6
0
 def test_grad_loss_consistency(self):
     """Test longitudinal multinomial model gradient properties."""
     n_lags = np.repeat(9, 3).astype(dtype="uint64")
     sim = SimuSCCS(500,
                    36,
                    3,
                    n_lags,
                    None,
                    "single_exposure",
                    seed=42,
                    verbose=False)
     _, X, y, censoring, coeffs = sim.simulate()
     coeffs = np.hstack(coeffs)
     X, _, _ = LongitudinalFeaturesLagger(n_lags=n_lags) \
         .fit_transform(X, censoring)
     model = ModelSCCS(n_intervals=36, n_lags=n_lags)\
         .fit(X, y, censoring)
     self._test_grad(model, coeffs)
     X_sparse = [csr_matrix(x) for x in X]
     model = ModelSCCS(n_intervals=36, n_lags=n_lags)\
         .fit(X_sparse, y, censoring)
     self._test_grad(model, coeffs)
コード例 #7
0
 def test_convergence_with_lags(self):
     """Test longitudinal multinomial model convergence."""
     n_intervals = 10
     n_samples = 800
     n_features = 2
     n_lags = np.repeat(2, n_features).astype(dtype="uint64")
     sim = SimuSCCS(n_samples,
                    n_intervals,
                    n_features,
                    n_lags,
                    None,
                    "multiple_exposures",
                    seed=42)
     _, X, y, censoring, coeffs = sim.simulate()
     coeffs = np.hstack(coeffs)
     X, _, _ = LongitudinalFeaturesLagger(n_lags=n_lags) \
         .fit_transform(X, censoring)
     model = ModelSCCS(n_intervals=n_intervals,
                       n_lags=n_lags).fit(X, y, censoring)
     solver = SVRG(max_iter=15, verbose=False)
     solver.set_model(model).set_prox(ProxZero())
     coeffs_svrg = solver.solve(step=1 / model.get_lip_max())
     np.testing.assert_almost_equal(coeffs, coeffs_svrg, decimal=1)
コード例 #8
0
    def test_sccs_serialize_and_compare(self):
        """Test serialization (cereal/pickle) of SCCS."""
        X = [
            np.array([[0, 0, 1], [0, 1, 1], [1, 1, 1]], dtype="float64"),
            np.array([[0, 1, 1], [0, 1, 1], [1, 1, 1]], dtype="float64")
        ]
        y = [
            np.array([0, 1, 0], dtype="int32"),
            np.array([0, 1, 0], dtype="int32")
        ]
        n_lags = np.repeat(1, 3).astype(dtype="uint64")
        X, _, _ = LongitudinalFeaturesLagger(n_lags=n_lags) \
            .fit_transform(X)
        model = ModelSCCS(n_intervals=3, n_lags=n_lags).fit(X, y)

        pickled = pickle.loads(pickle.dumps(model))

        self.assertTrue(model._model.compare(pickled._model))
コード例 #9
0
 def _construct_model_obj(self):
     return ModelSCCS(self.n_intervals, self.n_lags)