Exemplo n.º 1
0
    def _compute_test_stat(self, u):
        n = np.shape(u)[0]
        XLOO = LeaveOneOut(self.exog)
        uLOO = LeaveOneOut(u[:,None]).__iter__()
        I = 0
        S2 = 0
        for i, X_not_i in enumerate(XLOO):
            u_j = uLOO.next()
            u_j = np.squeeze(u_j)
            # See Bootstrapping procedure on p. 357 in [1]
            K = gpke(self.bw, data=-X_not_i, data_predict=-self.exog[i, :],
                     var_type=self.var_type, tosum=False)
            f_i = (u[i] * u_j * K)
            assert u_j.shape == K.shape
            I += f_i.sum()  # See eq. 12.7 on p. 355 in [1]
            S2 += (f_i**2).sum()  # See Theorem 12.1 on p.356 in [1]
            assert np.size(I) == 1
            assert np.size(S2) == 1

        I *= 1. / (n * (n - 1))
        ix_cont = _get_type_pos(self.var_type)[0]
        hp = self.bw[ix_cont].prod()
        S2 *= 2 * hp / (n * (n - 1))
        T = n * I * np.sqrt(hp / S2)
        return T
Exemplo n.º 2
0
 def simulate(self, rowid, targets, constraints=None, inputs=None, N=None):
     if self.N == 0:
         raise ValueError('KDE requires at least one observation.')
     if inputs:
         raise ValueError('Prohibited inputs: %s' % (inputs, ))
     if not targets:
         raise ValueError('No targets: %s' % (targets, ))
     if any(q not in self.outputs for q in targets):
         raise ValueError('Unknown targets: %s' % (targets, ))
     if constraints and any(q in constraints for q in targets):
         raise ValueError('Duplicate variable: %s, %s' % (
             targets,
             constraints,
         ))
     constraints = self.populate_constraints(rowid, targets, constraints)
     if constraints:
         full_members = self._dataset(targets + constraints.keys())
         weights = _kernel_base.gpke(
             self._bw(constraints),
             full_members[:, len(targets):],
             constraints.values(),
             self._stattypes(constraints),
             tosum=False,
         )
         targets_members = full_members[:, :len(targets)]
     else:
         targets_members = self._dataset(targets)
         weights = [1] * len(targets_members)
     assert len(weights) == len(targets_members)
     index = gu.pflip(weights, size=N, rng=self.rng)
     if N is None:
         return self._simulate_member(targets_members[index], targets)
     return [
         self._simulate_member(targets_members[i], targets) for i in index
     ]