Exemplo n.º 1
0
    def astep(self, q0, logp):
        self.w = np.resize(self.w, len(q0))  # this is a repmat
        q = np.copy(q0)  # TODO: find out if we need this
        ql = np.copy(q0)  # l for left boundary
        qr = np.copy(q0)  # r for right boudary
        for i in range(len(q0)):
            # uniformly sample from 0 to p(q), but in log space
            y = logp(q) - nr.standard_exponential()
            ql[i] = q[i] - nr.uniform(0, self.w[i])
            qr[i] = q[i] + self.w[i]
            # Stepping out procedure
            while(y <= logp(ql)):  # changed lt to leq  for locally uniform posteriors
                ql[i] -= self.w[i]
            while(y <= logp(qr)):
                qr[i] += self.w[i]

            q[i] = nr.uniform(ql[i], qr[i])
            while logp(q) < y:  # Changed leq to lt, to accomodate for locally flat posteriors
                # Sample uniformly from slice
                if q[i] > q0[i]:
                    qr[i] = q[i]
                elif q[i] < q0[i]:
                    ql[i] = q[i]
                q[i] = nr.uniform(ql[i], qr[i])

            if self.tune:  # I was under impression from MacKays lectures that slice width can be tuned without
                # breaking markovianness. Can we do it regardless of self.tune?(@madanh)
                self.w[i] = self.w[i] * (self.n_tunes / (self.n_tunes + 1)) +\
                    (qr[i] - ql[i]) / (self.n_tunes + 1)  # same as before
            # unobvious and important: return qr and ql to the same point
                qr[i] = q[i]
                ql[i] = q[i]
        if self.tune:
            self.n_tunes += 1
        return q
Exemplo n.º 2
0
    def astep(self, q0, logp):
        """q0 : current state
        logp : log probability function
        """

        # Draw from the normal prior by multiplying the Cholesky decomposition
        # of the covariance with draws from a standard normal
        chol = draw_values([self.prior_chol])
        nu = np.dot(chol, nr.randn(chol.shape[0]))
        y = logp(q0) - nr.standard_exponential()

        # Draw initial proposal and propose a candidate point
        theta = nr.uniform(0, 2 * np.pi)
        theta_max = theta
        theta_min = theta - 2 * np.pi
        q_new = q0 * np.cos(theta) + nu * np.sin(theta)

        while logp(q_new) <= y:
            # Shrink the bracket and propose a new point
            if theta < 0:
                theta_min = theta
            else:
                theta_max = theta
            theta = nr.uniform(theta_min, theta_max)
            q_new = q0 * np.cos(theta) + nu * np.sin(theta)

        return q_new
Exemplo n.º 3
0
    def astep(self, q0, logp):
        self.w = np.resize(self.w, len(q0))
        y = logp(q0) - nr.standard_exponential()

        # Stepping out procedure
        q_left = q0 - nr.uniform(0, self.w)
        q_right = q_left + self.w

        while (y < logp(q_left)).all():
            q_left -= self.w

        while (y < logp(q_right)).all():
            q_right += self.w

        q = nr.uniform(q_left, q_right,
                       size=q_left.size)  # new variable to avoid copies
        while logp(q) <= y:
            # Sample uniformly from slice
            if (q > q0).all():
                q_right = q
            elif (q < q0).all():
                q_left = q
            q = nr.uniform(q_left, q_right, size=q_left.size)

        if self.tune:
            # Tune sampler parameters
            self.w_sum += np.abs(q0 - q)
            self.n_tunes += 1.
            self.w = 2. * self.w_sum / self.n_tunes
        return q
Exemplo n.º 4
0
    def astep(self, q0, logp):
        self.w = np.resize(self.w, len(q0))  # this is a repmat
        q = np.copy(q0)  # TODO: find out if we need this
        ql = np.copy(q0)  # l for left boundary
        qr = np.copy(q0)  # r for right boudary
        for i in range(len(q0)):
            # uniformly sample from 0 to p(q), but in log space
            y = logp(q) - nr.standard_exponential()
            ql[i] = q[i] - nr.uniform(0, self.w[i])
            qr[i] = q[i] + self.w[i]
            # Stepping out procedure
            while(y <= logp(ql)):  # changed lt to leq  for locally uniform posteriors
                ql[i] -= self.w[i]
            while(y <= logp(qr)):
                qr[i] += self.w[i]

            q[i] = nr.uniform(ql[i], qr[i])
            while logp(q) < y:  # Changed leq to lt, to accomodate for locally flat posteriors
                # Sample uniformly from slice
                if q[i] > q0[i]:
                    qr[i] = q[i]
                elif q[i] < q0[i]:
                    ql[i] = q[i]
                q[i] = nr.uniform(ql[i], qr[i])

            if self.tune:  # I was under impression from MacKays lectures that slice width can be tuned without
                # breaking markovianness. Can we do it regardless of self.tune?(@madanh)
                self.w[i] = self.w[i] * (self.n_tunes / (self.n_tunes + 1)) +\
                    (qr[i] - ql[i]) / (self.n_tunes + 1)  # same as before
            # unobvious and important: return qr and ql to the same point
                qr[i] = q[i]
                ql[i] = q[i]
        if self.tune:
            self.n_tunes += 1
        return q
Exemplo n.º 5
0
    def astep(self, q0, logp):
        self.w = np.resize(self.w, len(q0))
        y = logp(q0) - nr.standard_exponential()

        # Stepping out procedure
        q_left = q0 - nr.uniform(0, self.w)
        q_right = q_left + self.w

        while (y < logp(q_left)).all():
            q_left -= self.w

        while (y < logp(q_right)).all():
            q_right += self.w

        q = nr.uniform(q_left, q_right, size=q_left.size)  # new variable to avoid copies
        while logp(q) <= y:
            # Sample uniformly from slice
            if (q > q0).all():
                q_right = q
            elif (q < q0).all():
                q_left = q
            q = nr.uniform(q_left, q_right, size=q_left.size)

        if self.tune:
            # Tune sampler parameters
            self.w_sum += np.abs(q0 - q)
            self.n_tunes += 1.
            self.w = 2. * self.w_sum / self.n_tunes
        return q
Exemplo n.º 6
0
def poisson_2D_array(center, M, d):
    '''
    Create array of 2D positions drawn from Poisson process.

    Parameters
    ----------
    center: array_like
        The center of the array
    M: int
        The number of points in the first dimension
    M: int
        The number of points in the second dimension
    phi: float
        The counterclockwise rotation of the array (from the x-axis)
    d: float
        The distance between neighboring points

    Returns
    -------
    ndarray (2, M * N)
        The array of points
    '''

    from numpy.random import standard_exponential, randint

    R = d * standard_exponential((2, M)) * (2 * randint(0, 2, (2, M)) - 1)
    R = R.cumsum(axis=1)
    R -= R.mean(axis=1)[:, np.newaxis]
    R += np.array([center]).T

    return R
Exemplo n.º 7
0
    def a_step(self, **q0):
        # set the slice
        logL = self.logp(**q0)
        ϑ = logL - nr.standard_exponential()

        # draw from prior
        nu = {}
        for v in self.vars:
            μ = self.mu[v.name]
            Σ = self.sd[v.name]
            nu[v.name] = randn(μ, Σ)

        # set up a bracket around the current point
        φ = nr.uniform(low=0.0, high=self.width)
        φmin = φ - self.width
        φmax = φ

        while True:
            c, s = np.cos(φ), np.sin(φ)
            q1 = {}
            for v in self.vars:
                x, ν, μ = q0[v.name], nu[v.name], self.mu[v.name]
                q1[v.name] = (x - μ) * c + (ν - μ) * s + μ

            logL = self.logp(**q1)
            if logL > ϑ:
                return q1
            if φ > 0:
                φmax = φ
            elif φ < 0:
                φmin = φ
            else:
                raise RuntimeError()
            φ = nr.uniform(low=φmin, high=φmax)
Exemplo n.º 8
0
    def astep(self, q0, logp):
        """q0: current state
        logp: log probability function
        """

        # Draw from the normal prior by multiplying the Cholesky decomposition
        # of the covariance with draws from a standard normal
        # XXX: This needs to be refactored
        chol = None  # draw_values([self.prior_chol])[0]
        nu = np.dot(chol, nr.randn(chol.shape[0]))
        y = logp(q0) - nr.standard_exponential()

        # Draw initial proposal and propose a candidate point
        theta = nr.uniform(0, 2 * np.pi)
        theta_max = theta
        theta_min = theta - 2 * np.pi
        q_new = q0 * np.cos(theta) + nu * np.sin(theta)

        while logp(q_new) <= y:
            # Shrink the bracket and propose a new point
            if theta < 0:
                theta_min = theta
            else:
                theta_max = theta
            theta = nr.uniform(theta_min, theta_max)
            q_new = q0 * np.cos(theta) + nu * np.sin(theta)

        return q_new
Exemplo n.º 9
0
    def astep(self, q0, logp):
        q0_val = q0.data
        self.w = np.resize(self.w, len(q0_val))  # this is a repmat
        q = np.copy(q0_val)  # TODO: find out if we need this
        ql = np.copy(q0_val)  # l for left boundary
        qr = np.copy(q0_val)  # r for right boudary
        for i in range(len(q0_val)):
            # uniformly sample from 0 to p(q), but in log space
            q_ra = RaveledVars(q, q0.point_map_info)
            y = logp(q_ra) - nr.standard_exponential()
            ql[i] = q[i] - nr.uniform(0, self.w[i])
            qr[i] = q[i] + self.w[i]
            # Stepping out procedure
            cnt = 0
            while y <= logp(
                    RaveledVars(ql, q0.point_map_info)
            ):  # changed lt to leq  for locally uniform posteriors
                ql[i] -= self.w[i]
                cnt += 1
                if cnt > self.iter_limit:
                    raise RuntimeError(LOOP_ERR_MSG % self.iter_limit)
            cnt = 0
            while y <= logp(RaveledVars(qr, q0.point_map_info)):
                qr[i] += self.w[i]
                cnt += 1
                if cnt > self.iter_limit:
                    raise RuntimeError(LOOP_ERR_MSG % self.iter_limit)

            cnt = 0
            q[i] = nr.uniform(ql[i], qr[i])
            while logp(
                    q_ra
            ) < y:  # Changed leq to lt, to accomodate for locally flat posteriors
                # Sample uniformly from slice
                if q[i] > q0_val[i]:
                    qr[i] = q[i]
                elif q[i] < q0_val[i]:
                    ql[i] = q[i]
                q[i] = nr.uniform(ql[i], qr[i])
                cnt += 1
                if cnt > self.iter_limit:
                    raise RuntimeError(LOOP_ERR_MSG % self.iter_limit)

            if (
                    self.tune
            ):  # I was under impression from MacKays lectures that slice width can be tuned without
                # breaking markovianness. Can we do it regardless of self.tune?(@madanh)
                self.w[i] = self.w[i] * (self.n_tunes / (self.n_tunes + 1)) + (
                    qr[i] - ql[i]) / (self.n_tunes + 1)  # same as before
                # unobvious and important: return qr and ql to the same point
                qr[i] = q[i]
                ql[i] = q[i]
        if self.tune:
            self.n_tunes += 1
        return q
Exemplo n.º 10
0
    def poisson(cls, Fs, center, M, d):
        ''' Create beamformer with microphone positions drawn from Poisson process '''

        from numpy.random import standard_exponential, randint

        R = d*standard_exponential((2, M))*(2*randint(0,2, (2,M)) - 1)
        R = R.cumsum(axis=1)
        R -= R.mean(axis=1)[:,np.newaxis]
        R += np.array([center]).T

        return Beamformer(R, Fs)
def poisson2DArray(center, M, d):
    ''' Create array of 2D positions drawn from Poisson process '''

    from numpy.random import standard_exponential, randint

    R = d*standard_exponential((2, M))*(2*randint(0,2, (2,M)) - 1)
    R = R.cumsum(axis=1)
    R -= R.mean(axis=1)[:,np.newaxis]
    R += np.array([center]).T

    return R
Exemplo n.º 12
0
def poisson_2D_array(center, M, d):
    ''' Create array of 2D positions drawn from Poisson process. '''

    from numpy.random import standard_exponential, randint

    R = d * standard_exponential((2, M)) * (2 * randint(0, 2, (2, M)) - 1)
    R = R.cumsum(axis=1)
    R -= R.mean(axis=1)[:, np.newaxis]
    R += np.array([center]).T

    return R
Exemplo n.º 13
0
    def _sample(self, q0):
        """
    Helper function which implements slice sampler.
    """
        self.w = np.resize(self.w, len(q0))  # this is a repmat
        q = np.copy(q0)
        ql = np.copy(q0)  # l for left boundary
        qr = np.copy(q0)  # r for right boudary
        for i, _ in enumerate(q0):
            # uniformly sample from 0 to p(q), but in log space
            y = self.model.logp(q) - nr.standard_exponential()
            ql[i] = q[i] - nr.uniform(0, self.w[i])
            qr[i] = q[i] + self.w[i]

            # Stepping out procedure
            cnt = 0
            while y < self.model.logp(ql):
                ql[i] -= self.w[i]
                cnt += 1
                if cnt > self.iter_limit:
                    raise RuntimeError(LOOP_ERR_MSG % self.iter_limit)
            cnt = 0
            while y < self.model.logp(qr):
                qr[i] += self.w[i]
                cnt += 1
                if cnt > self.iter_limit:
                    raise RuntimeError(LOOP_ERR_MSG % self.iter_limit)

            cnt = 0
            q[i] = (qr[i] - ql[i]) * nr.rand() + ql[i]
            while self.model.logp(q) < y:
                # Sample uniformly from slice
                if q[i] > q0[i]:
                    qr[i] = q[i]
                elif q[i] < q0[i]:
                    ql[i] = q[i]
                q[i] = (qr[i] - ql[i]) * nr.rand() + ql[i]
                cnt += 1
                if cnt > self.iter_limit:
                    raise RuntimeError(LOOP_ERR_MSG % self.iter_limit)

            if self.tune:
                self.w[i] = self.w[i] * (self.n_tunes / (self.n_tunes + 1)) +\
                  (qr[i] - ql[i]) / (self.n_tunes + 1)  # same as before
                qr[i] = q[i]
                ql[i] = q[i]

        if self.tune:
            self.n_tunes += 1

        return q
Exemplo n.º 14
0
    def astep(self, q0, logp):

        q = q0.copy()
        self.w = resize(self.w, len(q))

        y = logp(q0) - standard_exponential()

        # Stepping out procedure
        ql = q0.copy()
        ql -= uniform(0, self.w)
        qr = q0.copy()
        qr = ql + self.w

        yl = logp(ql)
        yr = logp(qr)

        while ((y < yl).all()):
            ql -= self.w
            yl = logp(ql)

        while ((y < yr).all()):
            qr += self.w
            yr = logp(qr)

        q_next = q0.copy()
        while True:

            # Sample uniformly from slice
            qi = uniform(ql, qr, size=ql.size)

            yi = logp(qi)

            if yi > y:
                q = qi
                break
            elif (qi > q).all():
                qr = qi
            elif (qi < q).all():
                ql = qi

        if self.tune:
            # Tune sampler parameters
            self.w_tune.append(abs(q0 - q))
            self.w = 2 * sum(self.w_tune, 0) / len(self.w_tune)

        return q
Exemplo n.º 15
0
    def astep(self, q0, logp):

        q = q0.copy()
        self.w = np.resize(self.w, len(q))

        y = logp(q0) - standard_exponential()

        # Stepping out procedure
        ql = q0.copy()
        ql -= uniform(0, self.w)
        qr = q0.copy()
        qr = ql + self.w

        yl = logp(ql)
        yr = logp(qr)

        while((y < yl).all()):
            ql -= self.w
            yl = logp(ql)

        while((y < yr).all()):
            qr += self.w
            yr = logp(qr)

        q_next = q0.copy()
        while True:

            # Sample uniformly from slice
            qi = uniform(ql, qr)

            yi = logp(qi)

            if yi > y:
                q = qi
                break
            elif (qi > q).all():
                qr = qi
            elif (qi < q).all():
                ql = qi

        if self.tune:
            # Tune sampler parameters
            self.w_tune.append(abs(q0 - q))
            self.w = 2 * sum(self.w_tune, 0) / len(self.w_tune)

        return q
Exemplo n.º 16
0
    def astep(self, q0, logp):
        """q0 : current state
        logp : log probability function
        """
        mean, cov = draw_values([self.prior_mean, self.prior_cov])
        nu = nr.multivariate_normal(mean=mean, cov=cov)
        y = logp(q0) - nr.standard_exponential()

        # Draw initial proposal and propose a candidate point
        theta = nr.uniform(0, 2 * np.pi)
        theta_max = theta
        theta_min = theta - 2 * np.pi
        q_new = q0 * np.cos(theta) + nu * np.sin(theta)

        while logp(q_new) <= y:
            # Shrink the bracket and propose a new point
            if theta < 0:
                theta_min = theta
            else:
                theta_max = theta
            theta = nr.uniform(theta_min, theta_max)
            q_new = q0 * np.cos(theta) + nu * np.sin(theta)

        return q_new
Exemplo n.º 17
0
                    rv = Discrete(Z[risk], w=efron_w[risk])
                    info += rv.cov()
            elif ties == 'cox':
                raise NotImplementedError('Cox tie breaking method not \
implemented')
            else:
                raise NotImplementedError('tie breaking method not recognized')
        return score

if __name__ == '__main__':
    import numpy.random as R
    n = 100
    X = np.array([0]*n + [1]*n)
    b = 0.4
    lin = 1 + b*X
    Y = R.standard_exponential((2*n,)) / lin
    delta = R.binomial(1, 0.9, size=(2*n,))

    subjects = [Observation(Y[i], delta[i]) for i in range(2*n)]
    for i in range(2*n):
        subjects[i].X = X[i]

    import statsmodels.sandbox.formula as F
    x = F.Quantitative('X')
    f = F.Formula(x)

    c = CoxPH(subjects, f)

#    c.cache()
    # temp file cleanup doesn't work on windows
    c = CoxPH(subjects, f, time_dependent=True)
Exemplo n.º 18
0
 def __call__(self):
     size = size(self.s)
     return (standard_exponential(size=size) -
             standard_exponential(size=size)) * self.s
Exemplo n.º 19
0
 def __call__(self, num_draws=None):
     size = (self.s.shape)
     if num_draws:
         size += (num_draws, )
     return (standard_exponential(size=size) -
             standard_exponential(size=size)).T * self.s
Exemplo n.º 20
0
 def __call__(self):
     size = np.size(self.s)
     return (standard_exponential(size=size) - standard_exponential(size=size)) * self.s
Exemplo n.º 21
0
    def astep(self, q0, logp):
        q0_val = q0.data
        self.w = np.resize(self.w, len(q0_val))  # this is a repmat

        q = np.copy(q0_val)
        ql = np.copy(q0_val)  # l for left boundary
        qr = np.copy(q0_val)  # r for right boudary

        # The points are not copied, so it's fine to update them inplace in the
        # loop below
        q_ra = RaveledVars(q, q0.point_map_info)
        ql_ra = RaveledVars(ql, q0.point_map_info)
        qr_ra = RaveledVars(qr, q0.point_map_info)

        for i, wi in enumerate(self.w):
            # uniformly sample from 0 to p(q), but in log space
            y = logp(q_ra) - nr.standard_exponential()

            # Create initial interval
            ql[i] = q[i] - nr.uniform() * wi  # q[i] + r * w
            qr[i] = ql[i] + wi  # Equivalent to q[i] + (1-r) * w

            # Stepping out procedure
            cnt = 0
            while y <= logp(
                    ql_ra
            ):  # changed lt to leq  for locally uniform posteriors
                ql[i] -= wi
                cnt += 1
                if cnt > self.iter_limit:
                    raise RuntimeError(LOOP_ERR_MSG % self.iter_limit)
            cnt = 0
            while y <= logp(qr_ra):
                qr[i] += wi
                cnt += 1
                if cnt > self.iter_limit:
                    raise RuntimeError(LOOP_ERR_MSG % self.iter_limit)

            cnt = 0
            q[i] = nr.uniform(ql[i], qr[i])
            while y > logp(
                    q_ra
            ):  # Changed leq to lt, to accomodate for locally flat posteriors
                # Sample uniformly from slice
                if q[i] > q0_val[i]:
                    qr[i] = q[i]
                elif q[i] < q0_val[i]:
                    ql[i] = q[i]
                q[i] = nr.uniform(ql[i], qr[i])
                cnt += 1
                if cnt > self.iter_limit:
                    raise RuntimeError(LOOP_ERR_MSG % self.iter_limit)

            if self.tune:
                # I was under impression from MacKays lectures that slice width can be tuned without
                # breaking markovianness. Can we do it regardless of self.tune?(@madanh)
                self.w[i] = wi * (self.n_tunes / (self.n_tunes + 1)) + (
                    qr[i] - ql[i]) / (self.n_tunes + 1)

            # Set qr and ql to the accepted points (they matter for subsequent iterations)
            qr[i] = ql[i] = q[i]

        if self.tune:
            self.n_tunes += 1

        return q
Exemplo n.º 22
0
 def __call__(self):
     size = np.size(self.s)
     return (nr.standard_exponential(size=size) -
             nr.standard_exponential(size=size)) * self.s
Exemplo n.º 23
0
def standard_exponential(size, params):
    try:
        return random.standard_exponential(size, params['dtype'],
                                           params['method'], params['out'])
    except ValueError as e:
        exit(e)