def rvs_score(self, theta, theta_score, n, random_state=None):
        logging.debug(
            'Simulating %s evolutions for theta = %s, augmenting with joint score',
            n, theta)

        rng = check_random_state(random_state, use_autograd=True)

        all_x = []
        all_t_xz = []
        all_t_xz_checkpoints = []
        all_histories = []

        for i in range(n):
            logging.debug('  Starting sample %s of %s', i + 1, n)

            _, t_xz, time_series, _, t_xz_checkpoints = self._d_simulate_until_success(
                theta, rng, theta_score)
            all_t_xz.append(t_xz)
            all_t_xz_checkpoints.append(t_xz_checkpoints)
            all_histories.append(time_series)

            x = self._calculate_observables(time_series, rng=rng)
            all_x.append(x)

        all_x = np.asarray(all_x)
        all_t_xz = np.asarray(all_t_xz)
        all_t_xz_checkpoints = np.asarray(all_t_xz_checkpoints)
        all_histories = np.asarray(all_histories)

        return all_x, all_t_xz, all_histories, all_t_xz_checkpoints
    def rvs_ratio(self, theta, theta0, theta1, n, random_state=None):
        logging.debug(
            'Simulating %s evolutions for theta = %s, augmenting with joint ratio between %s and %s',
            n, theta, theta0, theta1)

        rng = check_random_state(random_state, use_autograd=True)

        all_x = []
        all_log_r_xz = []
        all_log_r_xz_checkpoints = []
        all_histories = []

        for i in range(n):
            logging.debug('  Starting sample %s of %s', i + 1, n)

            log_p_xzs, time_series, log_p_xz_checkpoints = self._simulate_until_success(
                theta, rng, [theta0, theta1])

            all_log_r_xz.append(log_p_xzs[0] - log_p_xzs[1])
            all_log_r_xz_checkpoints.append(log_p_xz_checkpoints[:, 0] -
                                            log_p_xz_checkpoints[:, 1])
            all_histories.append(time_series)

            x = self._calculate_observables(time_series, rng=rng)
            all_x.append(x)

        all_x = np.asarray(all_x)
        all_r_xz = np.exp(np.asarray(all_log_r_xz))
        all_r_xz_checkpoints = np.exp(np.asarray(all_log_r_xz_checkpoints))
        all_histories = np.asarray(all_histories)

        return all_x, all_r_xz, all_histories, all_r_xz_checkpoints
Example #3
0
    def rvs(self, theta, n, random_state=None, return_histories=False):

        logging.debug('Simulating %s epidemic evolutions for theta = %s', n,
                      theta)

        rng = check_random_state(random_state)

        all_x = []
        histories = []

        for i in range(n):
            if return_histories:
                _, (state,
                    history) = self._simulate_transmission(theta,
                                                           rng,
                                                           return_history=True)
                histories.append(history)
            else:
                _, state = self._simulate_transmission(theta,
                                                       rng,
                                                       return_history=False)

            x = self._calculate_observables(state)

            all_x.append(x)

        all_x = np.asarray(all_x)

        if return_histories:
            return all_x, histories
        return all_x
Example #4
0
    def rvs_ratio_score(self,
                        theta, theta0, theta1, theta_score,
                        n, random_state=None):

        """ Draws samples x according to p(x|theta), augments them with joint ratio r(x,z|theta0,theta1) and
        joint score t(x,z|theta_score) """

        rng = check_random_state(random_state)

        all_x = []
        all_log_r_xz = []
        all_t_xz = []

        for i in range(n):
            u = rng.rand(self.n_rows)

            log_p_xz_theta0, x = self.trace(theta0, u, theta_ref=theta)
            log_p_xz_theta1, _ = self.trace(theta1, u, theta_ref=theta)
            t_xz, _ = self.d_trace(theta_score, u, theta_ref=theta)

            all_x.append(x)
            all_log_r_xz.append(log_p_xz_theta0 - log_p_xz_theta1)
            all_t_xz.append(t_xz)

        all_x = np.array(all_x)
        all_r_xz = np.exp(np.array(all_log_r_xz))
        all_t_xz = np.array(all_t_xz)

        return all_x, all_r_xz, all_t_xz
    def rvs(self, theta, n, random_state=None, return_z_checkpoints=False, return_histories=False):
        logging.debug('Simulating %s evolutions for theta = %s', n, theta)

        rng = check_random_state(random_state)

        all_x = []
        all_z_checkpoints = []
        all_histories = []

        for i in range(n):
            logging.debug('  Starting sample %s of %s', i + 1, n)

            if return_histories:
                _, time_series, _, history = self._simulate_until_success(theta, rng, return_history=True)
            else:
                _, time_series, _ = self._simulate_until_success(theta, rng)

            if return_z_checkpoints or return_histories:
                all_z_checkpoints.append(time_series)
            if return_histories:
                all_histories.append(history)

            x = self._calculate_observables(time_series, rng=rng)
            all_x.append(x)

        all_x = np.asarray(all_x)
        all_z_checkpoints = np.asarray(all_z_checkpoints)

        if return_histories:
            all_z_checkpoints = np.asarray(all_z_checkpoints)
            return all_x, all_z_checkpoints, all_histories
        if return_z_checkpoints:
            return all_x, all_z_checkpoints
        return all_x
Example #6
0
    def rvs(self,
            theta,
            n, random_state=None):

        """ Draws samples x according to p(x|theta) """

        rng = check_random_state(random_state)

        all_x = []

        for i in range(n):
            u = rng.rand(self.n_rows)
            _, x = self.trace(theta, u)
            all_x.append(x)

        return all_x
    def rvs_ratio_score(self, theta, theta0, theta1, theta_score, n, random_state=None, return_histories=False):
        logging.debug('Simulating %s evolutions for theta = %s, augmenting with joint ratio between %s and %s and joint'
                      ' score at  %s', n, theta, theta0, theta1, theta_score)

        rng = check_random_state(random_state, use_autograd=True)

        all_x = []
        all_log_r_xz = []
        all_t_xz = []
        all_t_xz_checkpoints = []
        all_log_r_xz_checkpoints = []
        all_z_checkpoints = []
        all_histories = []

        for i in range(n):
            logging.debug('  Starting sample %s of %s', i + 1, n)

            results = self._d_simulate_until_success(theta, rng, theta_score, [theta0, theta1],
                                                     return_history=return_histories)
            if return_histories:
                log_p_xzs, t_xz, time_series, log_p_xz_checkpoints, t_xz_checkpoints, history = results
            else:
                log_p_xzs, t_xz, time_series, log_p_xz_checkpoints, t_xz_checkpoints = results

            all_t_xz.append(t_xz)
            all_log_r_xz.append(log_p_xzs[0] - log_p_xzs[1])
            all_t_xz_checkpoints.append(t_xz_checkpoints)
            all_log_r_xz_checkpoints.append(log_p_xz_checkpoints[:, 0] - log_p_xz_checkpoints[:, 1])
            all_z_checkpoints.append(time_series)
            if return_histories:
                all_histories.append(history)

            x = self._calculate_observables(time_series, rng=rng)
            all_x.append(x)

        all_x = np.asarray(all_x)
        all_t_xz = np.asarray(all_t_xz)
        all_r_xz = np.exp(np.asarray(all_log_r_xz))
        all_r_xz_checkpoints = np.exp(np.asarray(all_log_r_xz_checkpoints))
        all_t_xz_checkpoints = np.asarray(all_t_xz_checkpoints)
        all_z_checkpoints = np.asarray(all_z_checkpoints)

        if return_histories:
            return all_x, all_r_xz, all_t_xz, all_z_checkpoints, all_r_xz_checkpoints, all_t_xz_checkpoints, all_histories

        return all_x, all_r_xz, all_t_xz, all_z_checkpoints, all_r_xz_checkpoints, all_t_xz_checkpoints
    def rvs(self, theta, n, random_state=None, return_histories=False):
        logging.debug('Simulating %s evolutions for theta = %s', n, theta)

        rng = check_random_state(random_state)

        all_x = []
        histories = []

        for i in range(n):
            logging.debug('  Starting sample %s of %s', i + 1, n)

            _, time_series, _ = self._simulate_until_success(theta, rng)
            if return_histories:
                histories.append(time_series)

            x = self._calculate_observables(time_series, rng=rng)
            all_x.append(x)

        all_x = np.asarray(all_x)

        if return_histories:
            return all_x, histories
        return all_x
Example #9
0
    def rvs_score(self,
                  theta,
                  theta_score,
                  n,
                  random_state=None,
                  return_histories=False):

        logging.debug(
            'Simulating %s epidemic evolutions for theta = %s, augmenting with joint score',
            n, theta)

        rng = check_random_state(random_state)

        all_x = []
        all_t_xz = []
        histories = []

        for i in range(n):
            if return_histories:
                t_xz, (state, history) = self._d_simulate_transmission(
                    theta, rng, return_history=True)
                histories.append(history)
            else:
                t_xz, state = self._d_simulate_transmission(
                    theta, rng, return_history=False)

            x = self._calculate_observables(state)

            all_x.append(x)
            all_t_xz.append(t_xz)

        all_x = np.asarray(all_x)
        all_t_xz = np.asarray(all_t_xz)

        if return_histories:
            return all_x, all_t_xz, histories
        return all_x, all_t_xz