Exemplo n.º 1
0
    def multinomial(self, size=None, n=1, pvals=None, ndim=None, dtype='int64',
                    nstreams=None):
        """
        Sample `n` (currently `n` needs to be 1) times from a multinomial
        distribution defined by probabilities pvals.

        Example : pvals = [[.98, .01, .01], [.01, .98, .01]] will
        probably result in [[1,0,0],[0,1,0]].

        .. note::
            `size` and `ndim` are only there keep the same signature as other
            uniform, binomial, normal, etc.
            todo : adapt multinomial to take that into account
        """
        if pvals is None:
            raise TypeError("You have to specify pvals")
        pvals = as_tensor_variable(pvals)
        if size is not None:
            if any([isinstance(i, int) and i <= 0 for i in size]):
                raise ValueError(
                    "The specified size contains a dimension with value <= 0",
                    size)

        if n == 1 and pvals.ndim == 2:
            ndim, size, bcast = raw_random._infer_ndim_bcast(
                    ndim, size, pvals[:,0])
            assert ndim==1
            bcast = bcast+(pvals.type.broadcastable[-1],)
            unis = self.uniform(size=size, ndim=1, nstreams=nstreams)
            op = multinomial.MultinomialFromUniform(dtype)
            return op(pvals, unis)
        else:
            raise NotImplementedError(("MRG_RandomStreams.multinomial only"
                " implemented with n == 1 and pvals.ndim = 2"))
Exemplo n.º 2
0
    def multinomial(self,
                    size=None,
                    n=1,
                    pvals=None,
                    ndim=None,
                    dtype='int64',
                    nstreams=None):
        """
        Sample `n` (currently `n` needs to be 1) times from a multinomial
        distribution defined by probabilities pvals.

        Example : pvals = [[.98,.01, .01], [.01, .98 .01]] will probably result
        in [[1,0,0],[0,1,0]].

        .. note::
            `size` and `ndim` are only there keep the same signature as other
            uniform, binomial, normal, etc.
            todo : adapt multinomial to take that into account
        """
        if pvals is None:
            raise TypeError("You have to specify pvals")
        pvals = as_tensor_variable(pvals)
        if n == 1 and pvals.ndim == 2:
            ndim, size, bcast = raw_random._infer_ndim_bcast(
                ndim, size, pvals[:, 0])
            assert ndim == 1
            bcast = bcast + (pvals.type.broadcastable[-1], )
            unis = self.uniform(size=size, ndim=1, nstreams=nstreams)
            op = multinomial.MultinomialFromUniform(dtype)
            return op(pvals, unis)
        else:
            raise NotImplementedError(
                ("MRG_RandomStreams.multinomial only"
                 " implemented with n == 1 and pvals.ndim = 2"))
Exemplo n.º 3
0
    def multinomial(self,
                    size=None,
                    n=1,
                    pvals=None,
                    ndim=None,
                    dtype='int64',
                    nstreams=None):
        """
        Sample `n` (currently `n` needs to be 1) times from a multinomial
        distribution defined by probabilities pvals.

        Example : pvals = [[.98, .01, .01], [.01, .98, .01]] will
        probably result in [[1,0,0],[0,1,0]].

        .. note::
            -`size` and `ndim` are only there keep the same signature as other
            uniform, binomial, normal, etc.
            todo : adapt multinomial to take that into account

            -Does not do any value checking on pvals, i.e. there is no
             check that the elements are non-negative, less than 1, or
             sum to 1. passing pvals = [[-2., 2.]] will result in
             sampling [[0, 0]]
        """
        if pvals is None:
            raise TypeError("You have to specify pvals")
        pvals = as_tensor_variable(pvals)
        if size is not None:
            if any([isinstance(i, int) and i <= 0 for i in size]):
                raise ValueError(
                    "The specified size contains a dimension with value <= 0",
                    size)

        if n == 1 and pvals.ndim == 2:
            if size is not None:
                raise ValueError(
                    "Provided a size argument to "
                    "MRG_RandomStreams.multinomial, which does not use "
                    "the size argument.")
            if ndim is not None:
                raise ValueError(
                    "Provided an ndim argument to "
                    "MRG_RandomStreams.multinomial, which does not use "
                    "the ndim argument.")
            ndim, size, bcast = raw_random._infer_ndim_bcast(
                ndim, size, pvals[:, 0])
            assert ndim == 1
            bcast = bcast + (pvals.type.broadcastable[-1], )
            unis = self.uniform(size=size, ndim=1, nstreams=nstreams)
            op = multinomial.MultinomialFromUniform(dtype)
            return op(pvals, unis)
        else:
            raise NotImplementedError(
                ("MRG_RandomStreams.multinomial only"
                 " implemented with n == 1 and pvals.ndim = 2"))
Exemplo n.º 4
0
 def randomfunction(random_state, size=(), low=0.0, high=0.0, ndim=None):
     ndim, size, bcast = raw_random._infer_ndim_bcast(ndim, size)
     if ndim_added < 0:
         bcast = bcast[:ndim_added]
     else:
         bcast = bcast + ((False,) * ndim_added)
     assert len(bcast) == ndim + ndim_added
     op = RandomFunction(
         "uniform", tensor.TensorType(dtype="float64", broadcastable=bcast), ndim_added=ndim_added
     )
     return op(random_state, size, low, high)
Exemplo n.º 5
0
 def randomfunction(random_state, size=(), low=0.0, high=0.0,
                    ndim=None):
     ndim, size, bcast = raw_random._infer_ndim_bcast(ndim, size)
     if ndim_added < 0:
         bcast = bcast[:ndim_added]
     else:
         bcast = bcast + ((False,) * ndim_added)
     assert len(bcast) == ndim + ndim_added
     op = RandomFunction('uniform',
             tensor.TensorType(dtype='float64',
             broadcastable=bcast),
             ndim_added=ndim_added)
     return op(random_state, size, low, high)
Exemplo n.º 6
0
    def multinomial(self, size=None, n=1, pvals=None, ndim=None, dtype="int64", nstreams=None):
        """
        Sample `n` (currently `n` needs to be 1) times from a multinomial
        distribution defined by probabilities pvals.

        Example : pvals = [[.98, .01, .01], [.01, .98, .01]] will
        probably result in [[1,0,0],[0,1,0]].

        .. note::
            -`size` and `ndim` are only there keep the same signature as other
            uniform, binomial, normal, etc.
            todo : adapt multinomial to take that into account

            -Does not do any value checking on pvals, i.e. there is no
             check that the elements are non-negative, less than 1, or
             sum to 1. passing pvals = [[-2., 2.]] will result in
             sampling [[0, 0]]
        """
        if pvals is None:
            raise TypeError("You have to specify pvals")
        pvals = as_tensor_variable(pvals)
        if size is not None:
            if any([isinstance(i, int) and i <= 0 for i in size]):
                raise ValueError("The specified size contains a dimension with value <= 0", size)

        if n == 1 and pvals.ndim == 2:
            if size is not None:
                raise ValueError(
                    "Provided a size argument to "
                    "MRG_RandomStreams.multinomial, which does not use "
                    "the size argument."
                )
            if ndim is not None:
                raise ValueError(
                    "Provided an ndim argument to "
                    "MRG_RandomStreams.multinomial, which does not use "
                    "the ndim argument."
                )
            ndim, size, bcast = raw_random._infer_ndim_bcast(ndim, size, pvals[:, 0])
            assert ndim == 1
            bcast = bcast + (pvals.type.broadcastable[-1],)
            unis = self.uniform(size=size, ndim=1, nstreams=nstreams)
            op = multinomial.MultinomialFromUniform(dtype)
            return op(pvals, unis)
        else:
            raise NotImplementedError(
                ("MRG_RandomStreams.multinomial only" " implemented with n == 1 and pvals.ndim = 2")
            )