Ejemplo n.º 1
0
    def sample_parameters(self, random: np.random.Generator,
                          times: np.ndarray) -> Tuple[np.ndarray, np.ndarray]:
        num_samples = times.shape[1]

        log_period = random.uniform(self.log_period_range[0],
                                    self.log_period_range[1], num_samples)
        log_semiamp = random.uniform(
            self.log_semiamp_range[0],
            self.log_semiamp_range[1],
            num_samples,
        )
        phase = random.uniform(-np.pi, np.pi, num_samples)

        if self.ecc_beta_params is None:
            params = np.concatenate(
                (log_semiamp[:, None], log_period[:, None], phase[:, None]),
                axis=1,
            )
            mod = self.compute_fiducial_model(
                times,
                semiamp=np.exp(log_semiamp),
                period=np.exp(log_period),
                phase=phase,
            )
        else:
            ecc = random.beta(self.ecc_beta_params[0], self.ecc_beta_params[1],
                              num_samples)
            omega = random.uniform(-np.pi, np.pi, num_samples)
            params = np.concatenate(
                (
                    log_semiamp[:, None],
                    log_period[:, None],
                    phase[:, None],
                    ecc[:, None],
                    omega[:, None],
                ),
                axis=1,
            )
            mod = self.compute_fiducial_model(
                times,
                semiamp=np.exp(log_semiamp),
                period=np.exp(log_period),
                phase=phase,
                ecc=ecc,
                omega=omega,
            )

        return params, mod
Ejemplo n.º 2
0
def sample_batch_ppp(
    intensity: float,
    bounds: Tuple[float, ...] = (1, 1),
    batch_size: int = 2,
    ome: np.random.Generator = np.random.default_rng()
) -> Iterator[np.ndarray]:
    """Sample from a Poisson point process in batches.

    >>> # generate fixture
    >>> gen = int(1e4)
    >>> bound = np.random.lognormal(size=2)

    >>> # draw iid samples
    >>> y = np.vstack([ppp for ppp in sample_batch_ppp(gen, tuple(bound))])

    >>> # test sampling distribution
    >>> from scipy.stats import kstest, uniform
    >>> alpha = 1e-2
    >>> sample = (y / bound).flatten()
    >>> dist = uniform()
    >>> alpha < kstest(sample, dist.cdf)[1]
    True
    """

    assert 0 < intensity
    assert 0 < min(bounds)
    assert 0 < batch_size

    n_points = ome.poisson(intensity * np.prod(bounds))
    batch_sizes = it.repeat(batch_size, int(n_points // batch_size))
    if n_points % batch_size != 0:
        batch_sizes = it.chain(batch_sizes, [n_points % batch_size])

    ub = 0
    for i in batch_sizes:
        lb, ub = ub, ub + bounds[0] * ome.beta(i, n_points + 1 - i)
        if i == 1:
            u0 = np.array([ub])
        else:
            u0 = np.hstack([np.sort(ome.uniform(lb, ub, i - 1)), ub])
        u = ome.uniform(np.zeros(len(bounds[1:])), bounds[1:],
                        (i, len(bounds[1:])))
        yield np.vstack([u0, u.T]).T