Exemplo n.º 1
0
 def random(self, point=None, size=None, repeat=None):
     if self.lower is None and self.upper is None:
         return self._wrapped.random(point=point, size=size)
     elif self.lower is not None and self.upper is not None:
         lower, upper = draw_values([self.lower, self.upper], point=point)
         return generate_samples(self._random,
                                 lower,
                                 upper,
                                 point,
                                 dist_shape=self.shape,
                                 size=size)
     elif self.lower is not None:
         lower = draw_values([self.lower], point=point)
         return generate_samples(self._random,
                                 lower,
                                 np.inf,
                                 point,
                                 dist_shape=self.shape,
                                 size=size)
     else:
         upper = draw_values([self.upper], point=point)
         return generate_samples(self._random,
                                 -np.inf,
                                 upper,
                                 point,
                                 dist_shape=self.shape,
                                 size=size)
Exemplo n.º 2
0
 def random(self, point=None, size=None):
     if self.lower is None and self.upper is None:
         return self._wrapped.random(point=point, size=size)
     elif self.lower is not None and self.upper is not None:
         lower, upper = draw_values([self.lower, self.upper],
                                    point=point,
                                    size=size)
         return generate_samples(
             self._random,
             lower,
             upper,
             dist_shape=self.shape,
             size=size,
             not_broadcast_kwargs={'point': point},
         )
     elif self.lower is not None:
         lower = draw_values([self.lower], point=point, size=size)
         return generate_samples(
             self._random,
             lower,
             np.inf,
             dist_shape=self.shape,
             size=size,
             not_broadcast_kwargs={'point': point},
         )
     else:
         upper = draw_values([self.upper], point=point, size=size)
         return generate_samples(
             self._random,
             -np.inf,
             upper,
             dist_shape=self.shape,
             size=size,
             not_broadcast_kwargs={'point': point},
         )
Exemplo n.º 3
0
 def random(self, point=None, size=None):
     if self.lower is None and self.upper is None:
         return self._wrapped.random(point=point, size=size)
     elif self.lower is not None and self.upper is not None:
         lower, upper = draw_values([self.lower, self.upper], point=point, size=size)
         return generate_samples(
             self._random,
             lower,
             upper,
             dist_shape=self.shape,
             size=size,
             not_broadcast_kwargs={'point': point},
         )
     elif self.lower is not None:
         lower = draw_values([self.lower], point=point, size=size)
         return generate_samples(
             self._random,
             lower,
             np.inf,
             dist_shape=self.shape,
             size=size,
             not_broadcast_kwargs={'point': point},
         )
     else:
         upper = draw_values([self.upper], point=point, size=size)
         return generate_samples(
             self._random,
             -np.inf,
             upper,
             dist_shape=self.shape,
             size=size,
             not_broadcast_kwargs={'point': point},
         )
Exemplo n.º 4
0
    def random(self, point=None, size=None):
        """
        Draw random values from Bounded distribution.

        Parameters
        ----------
        point: dict, optional
            Dict of variable values on which random values are to be
            conditioned (uses default point if not specified).
        size: int, optional
            Desired size of random sample (returns one sample if not
            specified).

        Returns
        -------
        array
        """
        if self.lower is None and self.upper is None:
            return self._wrapped.random(point=point, size=size)
        elif self.lower is not None and self.upper is not None:
            lower, upper = draw_values([self.lower, self.upper],
                                       point=point,
                                       size=size)
            return generate_samples(
                self._random,
                lower,
                upper,
                dist_shape=self.shape,
                size=size,
                not_broadcast_kwargs={"point": point},
            )
        elif self.lower is not None:
            lower = draw_values([self.lower], point=point, size=size)
            return generate_samples(
                self._random,
                lower,
                np.inf,
                dist_shape=self.shape,
                size=size,
                not_broadcast_kwargs={"point": point},
            )
        else:
            upper = draw_values([self.upper], point=point, size=size)
            return generate_samples(
                self._random,
                -np.inf,
                upper,
                dist_shape=self.shape,
                size=size,
                not_broadcast_kwargs={"point": point},
            )
Exemplo n.º 5
0
    def random(self, point=None, size=None):
        """
        Draw random values from Weibull distribution.
        Parameters
        ----------
        point: dict, optional
            Dict of variable values on which random values are to be
            conditioned (uses default point if not specified).
        size: int, optional
            Desired size of random sample (returns one sample if not
            specified).
        Returns
        -------
        array
        """
        alpha, beta = draw_values([self.alpha, self.beta],
                                  point=point,
                                  size=size)

        def _random(a, b, size=None):
            return b * (-np.log(np.random.uniform(size=size)))**(
                1 / a) + self.offset

        return generate_samples(_random,
                                alpha,
                                beta,
                                dist_shape=self.shape,
                                size=size)
Exemplo n.º 6
0
 def _comp_samples(self,
                   point=None,
                   size=None,
                   comp_dist_shapes=None,
                   broadcast_shape=None):
     if self.comp_is_distribution:
         samples = self._comp_dists.random(point=point, size=size)
     else:
         if comp_dist_shapes is None:
             comp_dist_shapes = self._comp_dist_shapes
         if broadcast_shape is None:
             broadcast_shape = self._sample_shape
         samples = []
         for dist_shape, generator in zip(comp_dist_shapes,
                                          self._generators):
             sample = generate_samples(
                 generator=generator,
                 dist_shape=dist_shape,
                 broadcast_shape=broadcast_shape,
                 point=point,
                 size=size,
                 not_broadcast_kwargs={"raw_size_": size},
             )
             samples.append(sample)
         samples = np.array(
             broadcast_distribution_samples(samples, size=size))
         # In the logp we assume the last axis holds the mixture components
         # so we move the axis to the last dimension
         samples = np.moveaxis(samples, 0, -1)
     return samples.astype(self.dtype)
    def random(self, point=None, size=None):
        mu, tau = draw_values([self.mu, self.tau], point=point)

        def _random(mean, tau, size=None):
            supp_dim = mean.shape[-1]
            mus_collapsed = mean.reshape((-1, supp_dim))
            taus_collapsed = tau.reshape((-1, supp_dim, supp_dim))
            # FIXME: do something smarter about tau/cov
            covs_collapsed = np.apply_over_axes(lambda x,y: np.linalg.inv(x), taus_collapsed, 0)

            from functools import partial
            mvrvs = partial(stats.multivariate_normal.rvs, size=1)
            res = map(mvrvs, mus_collapsed, covs_collapsed)

            # FIXME: this is a hack; the PyMC sampling framework
            # will incorrectly set `size == Distribution.shape` when a single
            # sample is requested, implying that we want
            # `Distribution.shape`-many samples of a
            # `Distribution.shape` sized object: too many!  That's why
            # we're ignoring `size` right now and only ever asking
            # for a single sample.
            return np.asarray(res).reshape(mean.shape)

        samples = generate_samples(_random,
                                   mean=mu, tau=tau,
                                   dist_shape=self.shape,
                                   broadcast_shape=mu.shape,
                                   size=size)
        return samples
Exemplo n.º 8
0
    def random(self, point=None, size=None):
        """Draw random values from GaussianRandomWalk.

        Parameters
        ----------
        point: dict, optional
            Dict of variable values on which random values are to be
            conditioned (uses default point if not specified).
        size: int, optional
            Desired size of random sample (returns one sample if not
            specified).

        Returns
        -------
        array
        """
        sigma, mu = distribution.draw_values([self.sigma, self.mu],
                                             point=point,
                                             size=size)
        return distribution.generate_samples(
            self._random,
            sigma=sigma,
            mu=mu,
            size=size,
            dist_shape=self.shape,
            not_broadcast_kwargs={"sample_shape": to_tuple(size)},
        )
Exemplo n.º 9
0
    def random(self, point=None, size=None):
        c = draw_values([self.c], point=point, size=size)[0]

        def _random(c, dtype=self.dtype, size=None):
            return np.full(size, fill_value=c, dtype=dtype)

        return generate_samples(_random, c=c, dist_shape=self.shape,
                                size=size).astype(self.dtype)
Exemplo n.º 10
0
 def random(self, point=None, size=None):
     a = draw_values([self.a], point=point)
     samples = generate_samples(lambda a, size=None: \
                                 st.dirichlet.rvs(a, None if size == a.shape else size),
                                a,
                                dist_shape=self.shape,
                                size=size)
     return samples
Exemplo n.º 11
0
 def random(self, point=None, size=None):
     a = draw_values([self.a], point=point)
     samples = generate_samples(lambda a, size=None: \
                                 st.dirichlet.rvs(a, None if size == a.shape else size),
                                a,
                                dist_shape=self.shape,
                                size=size)
     return samples
 def random(self, point=None, size=None):
     n, p = draw_values([self.n, self.p], point=point)
     samples = generate_samples(self._random,
                                n,
                                p,
                                dist_shape=self.shape,
                                size=size)
     return samples
Exemplo n.º 13
0
 def random(self, point=None, size=None):
     mu, tau = draw_values([self.mu, self.tau], point=point)
     samples = generate_samples(lambda mean, cov, size=None: \
                                 st.multivariate_normal.rvs(mean, cov, None if size == mean.shape else size),
                                mean=mu, cov=tau,
                                dist_shape=self.shape,
                                broadcast_shape=mu.shape,
                                size=size)
     return samples
Exemplo n.º 14
0
 def random(self, point=None, size=None):
     mu, tau = draw_values([self.mu, self.tau], point=point)
     samples = generate_samples(lambda mean, cov, size=None: \
                                 st.multivariate_normal.rvs(mean, cov, None if size == mean.shape else size),
                                mean=mu, cov=tau,
                                dist_shape=self.shape,
                                broadcast_shape=mu.shape,
                                size=size)
     return samples
Exemplo n.º 15
0
 def random(self, point=None, size=None, repeat=None):
     if self.lower is None and self.upper is None:
         return self._wrapped.random(point=point, size=size)
     elif self.lower is not None and self.upper is not None:
         lower, upper = draw_values([self.lower, self.upper], point=point)
         return generate_samples(self._random, lower, upper, point,
                                 dist_shape=self.shape,
                                 size=size)
     elif self.lower is not None:
         lower = draw_values([self.lower], point=point)
         return generate_samples(self._random, lower, np.inf, point,
                                 dist_shape=self.shape,
                                 size=size)
     else:
         upper = draw_values([self.upper], point=point)
         return generate_samples(self._random, -np.inf, upper, point,
                                 dist_shape=self.shape,
                                 size=size)
Exemplo n.º 16
0
 def random(self, point=None, size=None):
     mu, cov = draw_values([self.mu, self.cov], point=point)
     def _random(mean, cov, size=None):
         return stats.multivariate_normal.rvs(mean, cov, 
                                              None if size==mean.shape else size)
     samples = generate_samples(_random, mean=mu, cov=cov, 
                                dist_shape=self.shape, broadcast_shape=mu.shape,
                                size=size)
     return samples
Exemplo n.º 17
0
 def random(self, point=None, size=None):
     raise NotImplementedError
     # This needs to give Ai(X\beta + (Bi)\epsilon)
     mu, cov = draw_values([self.mu, self.cov], point=point)
     def _random(mean, cov, size=None):
         return stats.multivariate_normal.rvs(mean, cov, 
                                              None if size==mean.shape else size)
     samples = generate_samples(_random, mean=mu, cov=cov, 
                                dist_shape=self.shape, broadcast_shape=mu.shape,
                                size=size)
     return samples
Exemplo n.º 18
0
    def random(self, point=None, size=None):
        """
		Draw random values from HalfNormal distribution.
		Parameters
		----------
		point : dict, optional
			Dict of variable values on which random values are to be
			conditioned (uses default point if not specified).
		size : int, optional
			Desired size of random sample (returns one sample if not
			specified).
		Returns
		-------
		array
		"""
        scale = draw_values([self.scale], point=point)[0]
        return generate_samples(edsd.rvs,
                                L=scale,
                                dist_shape=self.shape,
                                size=size)
Exemplo n.º 19
0
    def random(self, point=None, size=None):
        """
        Draw random values (weights) from the Stick-Breaking Process

        Parameters
        ----------
        point : dict, optional
            Dict of variable values on which random values are to be
            conditioned (uses default point if not specified).
        size : int, optional
            Desired size of random sample (returns one sample if not
            specified).

        Returns
        -------
        array
        """
        wts = draw_values([self.a], point=point, size=size)
        samples = generate_samples(self._random,
                                   wts=wts,
                                   dist_shape=self.shape,
                                   size=size)
        return samples
Exemplo n.º 20
0
    def random(self, point=None, size=None):
        """
		Draw random values from King's distribution.
		Parameters
		----------
		point : dict, optional
			Dict of variable values on which random values are to be
			conditioned (uses default point if not specified).
		size : int, optional
			Desired size of random sample (returns one sample if not
			specified).
		Returns
		-------
		array
		"""
        location, scale, rt = draw_values([self.location, self.scale, self.rt],
                                          point=point,
                                          size=size)
        return generate_samples(king.rvs,
                                loc=location,
                                scale=scale,
                                rt=rt,
                                dist_shape=self.shape,
                                size=size)
Exemplo n.º 21
0
 def random(self, point=None, size=None):
     n, p = draw_values([self.n, self.p], point=point)
     samples = generate_samples(self._random, n, p, dist_shape=self.shape, size=size)
     return samples