Example #1
0
def test_heritability_estimate_poisson():
    random = RandomState(0)

    G = random.randn(50, 100)
    K = dot(G, G.T)
    z = dot(G, random.randn(100)) / sqrt(100)
    y = random.poisson(exp(z))

    assert_allclose(estimate(y, "poisson", K, verbose=False),
                    0.991766763337491,
                    rtol=1e-3)
Example #2
0
def test_heritability_estimate_poisson():
    random = RandomState(0)
    nsamples = 50

    G = random.randn(50, 100)
    K = dot(G, G.T)
    z = dot(G, random.randn(100)) / sqrt(100)
    y = random.poisson(exp(z))

    assert_allclose(estimate(y, 'poisson', K, verbose=False),
                    0.6998737749318877)
Example #3
0
    def do(self, input_obj: ImageEntity, random_state_obj: RandomState) -> ImageEntity:
        """
        Perform the actual noise insertion
        :param input_obj: the input Entity to which noise is to be added
        :param random_state_obj: a RandomState object used to sample the noise
        :return: the transformed (noise added) Entity
        """
        img = input_obj.get_data()
        vals = len(np.unique(img))
        vals = self.exponent_base ** np.ceil(np.log2(vals))
        noisy = random_state_obj.poisson(img * vals) / float(vals)

        logger.debug("Added Poisson Noise to Image")
        return GenericImageEntity(noisy, input_obj.get_mask())
Example #4
0
def test_glmmexpfam_poisson():
    from numpy import ones, stack, exp, zeros
    from numpy.random import RandomState
    from numpy_sugar.linalg import economic_qs
    from pandas import DataFrame

    random = RandomState(1)

    # sample size
    n = 30

    # covariates
    offset = ones(n) * random.randn()
    age = random.randint(16, 75, n)
    M = stack((offset, age), axis=1)
    M = DataFrame(stack([offset, age], axis=1), columns=["offset", "age"])
    M["sample"] = [f"sample{i}" for i in range(n)]
    M = M.set_index("sample")

    # genetic variants
    G = random.randn(n, 4)

    # sampling the phenotype
    alpha = random.randn(2)
    beta = random.randn(4)
    eps = random.randn(n)
    y = M @ alpha + G @ beta + eps

    # Whole genotype of each sample.
    X = random.randn(n, 50)
    # Estimate a kinship relationship between samples.
    X_ = (X - X.mean(0)) / X.std(0) / sqrt(X.shape[1])
    K = X_ @ X_.T + eye(n) * 0.1
    # Update the phenotype
    y += random.multivariate_normal(zeros(n), K)
    y = (y - y.mean()) / y.std()

    z = y.copy()
    y = random.poisson(exp(z))

    M = M - M.mean(0)
    QS = economic_qs(K)
    glmm = GLMMExpFam(y, "poisson", M, QS)
    assert_allclose(glmm.lml(), -52.479557279193585)
    glmm.fit(verbose=False)
    assert_allclose(glmm.lml(), -34.09720756737648)
Example #5
0
def _test_qtl_scan_st(lik):
    random = RandomState(0)
    n = 30
    ncovariates = 3

    M = random.randn(n, ncovariates)

    v0 = random.rand()
    v1 = random.rand()

    G = random.randn(n, 4)

    K = random.randn(n, n + 1)
    K = normalise_covariance(K @ K.T)

    beta = random.randn(ncovariates)
    alpha = random.randn(G.shape[1])

    m = M @ beta + G @ alpha
    y = mvn(random, m, v0 * K + v1 * eye(n))

    idx = [[0, 1], 2, [3]]

    if lik == "poisson":
        y = random.poisson(exp(y))
    elif lik == "bernoulli":
        y = random.binomial(1, 1 / (1 + exp(-y)))
    elif lik == "probit":
        y = random.binomial(1, st.norm.cdf(y))
    elif lik == "binomial":
        ntrials = random.randint(0, 30, len(y))
        y = random.binomial(ntrials, 1 / (1 + exp(-y)))
        lik = (lik, ntrials)

    r = scan(G, y, lik=lik, idx=idx, K=K, M=M, verbose=False)
    str(r)
    str(r.stats.head())
    str(r.effsizes["h2"].head())
    str(r.h0.trait)
    str(r.h0.likelihood)
    str(r.h0.lml)
    str(r.h0.effsizes)
    str(r.h0.variances)
Example #6
0
class PoissonArrivalDistribution(ValueEqual):
    def __init__(self, minimal, lambdaFactor, seed=None):
        super().__init__()
        self._minimal = minimal
        self._lambda = lambdaFactor

        if seed is None:
            seed = 0
        self._seed = seed

        self._random = RandomState(seed)
        self._arrivalMap = {}

    def _nonValueFields(self):
        return '_random', '_arrivalMap'

    @property
    def minimal(self):
        return self._minimal

    @property
    def lambdaFactor(self):
        return self._lambda

    def arrivalTime(self, releaseIndex):
        if releaseIndex == 0:
            return 0
        if releaseIndex not in self._arrivalMap:
            previous = self.arrivalTime(releaseIndex - 1)
            sample = self._random.poisson(self._lambda) + self._minimal
            arrival = sample + previous
            self._arrivalMap[releaseIndex] = arrival
        else:
            arrival = self._arrivalMap[releaseIndex]
        return arrival

    def __repr__(self):
        return "PoissonAD({}, {}, {})".format(self._minimal, self._lambda,
                                              self._seed)
Example #7
0
def test_glmmexpfam_poisson():
    random = RandomState(1)

    # sample size
    n = 30

    # covariates
    offset = ones(n) * random.randn()
    age = random.randint(16, 75, n)
    M = stack((offset, age), axis=1)

    # genetic variants
    G = random.randn(n, 4)

    # sampling the phenotype
    alpha = random.randn(2)
    beta = random.randn(4)
    eps = random.randn(n)
    y = M @ alpha + G @ beta + eps

    # Whole genotype of each sample.
    X = random.randn(n, 50)
    # Estimate a kinship relationship between samples.
    X_ = (X - X.mean(0)) / X.std(0) / sqrt(X.shape[1])
    K = X_ @ X_.T + eye(n) * 0.1
    # Update the phenotype
    y += random.multivariate_normal(zeros(n), K)
    y = (y - y.mean()) / y.std()

    z = y.copy()
    y = random.poisson(exp(z))

    M = M - M.mean(0)
    QS = economic_qs(K)
    glmm = GLMMExpFam(y, "poisson", M, QS)
    assert_allclose(glmm.lml(), -52.479557279193585)
    glmm.fit(verbose=False)
    assert_allclose(glmm.lml(), -34.09720756737648)
Example #8
0
def test_poisson_optimize():
    random = RandomState(139)
    nsamples = 30
    nfeatures = 31

    G = random.randn(nsamples, nfeatures) / sqrt(nfeatures)

    u = random.randn(nfeatures)

    z = 0.1 + 2 * dot(G, u) + random.randn(nsamples)

    y = zeros(nsamples)
    for i in range(nsamples):
        y[i] = random.poisson(lam=exp(z[i]))
    (Q0, Q1), S0 = economic_qs_linear(G)

    M = ones((nsamples, 1))
    lik = PoissonProdLik(LogLink())
    lik.noccurrences = y
    ep = ExpFamEP(lik, M, Q0, Q1, S0)
    ep.learn()
    assert_almost_equal(ep.lml(), -77.90919831238075, decimal=2)
    assert_almost_equal(ep.beta[0], 0.314709077094, decimal=1)
    assert_almost_equal(ep.heritability, 0.797775054939, decimal=1)
Example #9
0
for interval_count in range(4):
    for do_while in range(100):
        start_time = time.time()
        stop_details = []
        rand_generator = RandomState()

        def gen_time(multiplicator, num):
            return ((multiplicator - 1) * 10) + num

        for stop in range(1, 6):
            total_arrival_time = []
            for time_idx in range(len(poisson_arrival)):
                lam = poisson_arrival[time_idx]
                num_passenger = int(
                    rand_generator.poisson(lam * SIMULATION_TIME) / 5)
                passenger_arrival_time = numpy.sort(
                    rand(num_passenger) * SIMULATION_TIME)
                for i in range(len(passenger_arrival_time)):
                    passenger_arrival_time[i] += (time_idx * 60)
                total_arrival_time.extend(passenger_arrival_time)
            stop_details.append(total_arrival_time)

        # at this point, we have list of all queues across all stop
        # now start bus journey
        START_TIME = 5
        LAST_BUS_TIME = 715
        CURR_BUS_INTERVAL = 20 - (interval_count * 5)
        INTERVAL_BETWEEN_STOPS = 10
        BUS_CAPACITY = 50
Example #10
0
class CaoMethod(DirectMethod):
    n_crit = 10
    tauleap_threshold = 10
    micro_steps = 100

    def __init__(self,
                 process,
                 state,
                 epsilon=0.03,
                 t=0.,
                 tmax=float('inf'),
                 steps=None,
                 seed=None):
        super(CaoMethod, self).__init__(process,
                                        state,
                                        t=t,
                                        tmax=tmax,
                                        steps=steps,
                                        seed=seed)
        self.num_reactions = 0
        self.epsilon = epsilon
        self.rng2 = RandomState(seed)
        self.abandon_tauleap = -1

    def __iter__(self):
        while not self.has_reached_end():
            # step 1: partition reactions -- Eq. (10)
            Jcrit, Jncr = self.identify_critical_reactions()

            Irs = set(s for trans in self.transitions for s in trans.reactants)
            Incr = set(s for trans in Jncr for s in trans.reactants)

            if Incr:
                # step 2: determine noncritical tau -- Eqs. (32) and (33)
                mu = {
                    s: sum(
                        trans.stoichiometry.get(s, 0) * a
                        for trans, a in Jncr.items())
                    for s in Incr
                }
                var = {
                    s: sum(
                        trans.stoichiometry.get(s, 0)**2 * a
                        for trans, a in Jncr.items())
                    for s in Incr
                }
                eps = {
                    s: max(self.epsilon * self.state[s] * self.gi(s), 1.)
                    for s in Incr
                }

                tau_ncr1 = min((eps[s] / abs(mu[s])) if mu[s] else float('inf')
                               for s in Incr)
                tau_ncr2 = min(
                    (eps[s]**2 / abs(var[s])) if var[s] else float('inf')
                    for s in Incr)
                tau_ncr = min((tau_ncr1, tau_ncr2, self.tmax - self.time))
            else:
                tau_ncr = float('inf')

            a0 = sum(mult * prop
                     for _, prop, mult in self.propensities.items())

            if not a0:
                break

            while True:
                # step 3: abandon tau leaping if not enough expected gain
                if tau_ncr <= self.tauleap_threshold / a0:
                    if self.abandon_tauleap == -1:
                        self.abandon_tauleap = self.step
                    it = DirectMethod.__iter__(self)
                    for _ in range(self.micro_steps):
                        trans = next(it)
                        self.num_reactions += 1
                        yield self.time, self.state, {trans: 1}
                    break
                elif self.abandon_tauleap != -1:
                    logging.debug("Abandoned tau-leaping for %d steps" %
                                  (self.step - self.abandon_tauleap))
                    self.abandon_tauleap = -1

                # step 4: determine critical tau
                ac = sum(propensity for trans, propensity in Jcrit.items())
                tau_crit = -log(self.rng.random()) / ac if ac else float('inf')

                # step 5: determine actual tau
                tau = min((tau_ncr, tau_crit, self.tmax - self.time))

                # step 5a
                firings = {
                    trans: self.rng2.poisson(propensity * tau)
                    for trans, propensity in Jncr.items()
                }
                firings = {trans: n for trans, n in firings.items() if n}

                # step 5b
                if tau == tau_crit:
                    # fire exactly one ciritical reaction
                    transition = None
                    pick = self.rng.random() * ac
                    for transition, propensity in Jcrit.items():
                        pick -= propensity
                        if pick < 0.:
                            break
                    firings[transition] = 1

                new_reactions = sum(firings.values())

                # avoid overshooting self.steps
                if self.steps and self.step + new_reactions > self.steps:
                    tau_ncr /= 2
                    continue

                all_reactants = sum(
                    (n * trans.true_reactants for trans, n in firings.items()),
                    multiset())
                all_products = sum(
                    (n * trans.true_products for trans, n in firings.items()),
                    multiset())
                net_reactants = all_reactants - all_products
                net_products = all_products - all_reactants

                # step 6a: avoid negative copy numbers
                if any(self.state[s] < n for s, n in net_reactants.items()):
                    tau_ncr /= 2
                    continue

                else:
                    # step 6b: perform transitions
                    self.time += tau
                    self.step += 1
                    self.num_reactions += new_reactions
                    self.state -= net_reactants
                    for rule in self.process.rules:
                        for trans in rule.infer_transitions(
                                net_products, self.state):
                            trans.rule = rule
                            self.add_transition(trans)
                    self.state += net_products

                    # update propensities
                    affected_species = set().union(*(trans.affected_species
                                                     for trans in firings))
                    affected = self.dependency_graph.affected_transitions(
                        affected_species)
                    self.update_propensities(affected)
                    self.prune_transitions()

                    yield self.time, self.state, firings
                    break

        if self.step != self.steps and self.tmax < float('inf'):
            self.time = self.tmax

    def identify_critical_reactions(self):
        def critical(trans, propensity):
            if not propensity:
                return False
            elif not trans.true_reactants:
                return False
            elif L(trans) < self.n_crit:
                return True
            elif any(self.state[s] < rule.order for rule in self.process.rules
                     for s in trans.true_products):
                return True
            else:
                return False

        def L(trans):
            return min(-float(self.state[s]) / trans.stoichiometry.get(s, 0)
                       for s in trans.true_reactants
                       if trans.stoichiometry.get(s, 0) < 0)

        crit = {}
        noncrit = {}
        for trans, a, mult in self.propensities.items():
            partition = crit if critical(trans, a) else noncrit
            partition[trans] = mult * a
        return crit, noncrit

    def gi(self, species):
        g = 0
        for trans in self.transitions:
            if species not in trans.reactants:
                continue
            order = sum(trans.reactants.values())
            if order == 1:
                g = max(g, 1)
            elif order == 2 and trans.reactants[species] == 1:
                g = max(g, 2)
            elif order == 2 and trans.reactants[species] == 2:
                g = max(g, 2 + (self.state[species] -
                                1)**-1) if self.state[species] >= 2 else 3
            elif order == 3 and trans.reactants[species] == 1:
                g = max(g, 3)
            elif order == 3 and trans.reactants[species] == 2:
                g = max(g, 1.5 * (2 + (self.state[species] - 1)**-1)
                        ) if self.state[species] >= 2 else 4.5
            elif order == 3 and trans.reactants[species] == 3:
                g = max(
                    g, 3 + (self.state[species] - 1)**-1 +
                    (self.state[species] -
                     2)**-1) if self.state[species] >= 3 else 5.5
            else:
                raise RuntimeError(
                    "Tau-leaping not implemented for reactions of order %d" %
                    order)
        return g
Example #11
0
def randomu(seed, di=None, binomial=None, double=False, gamma=False,
            normal=False, poisson=False):
    """
    Replicates the randomu function avaiable within IDL
    (Interactive Data Language, EXELISvis).
    Returns an array of uniformly distributed random numbers of the
    specified dimensions.
    The randomu function returns one or more pseudo-random numbers
    with one or more of the following distributions:
    Uniform (default)
    Gaussian
    binomial
    gamma
    poisson

    :param seed:
        If seed is not of type mtrand.RandomState, then a new state is
        initialised. Othersise seed will be used to generate the random
        values.

    :param di:
        A list specifying the dimensions of the resulting array. If di
        is a scalar then randomu returns a scalar.
        Dimensions are D1, D2, D3...D8 (x,y,z,lambda...).
        The list will be inverted to suit Python's inverted dimensions
        i.e. (D3,D2,D1).

    :param binomial:
        Set this keyword to a list of length 2, [n,p], to generate
        random deviates from a binomial distribution. If an event
        occurs with probablility p, with n trials, then the number of
        times it occurs has a binomial distribution.

    :param double:
        If set to True, then randomu will return a double precision
        random numbers.

    :param gamma:
        Set this keyword to an integer order i > 0 to generate random
        deviates from a gamm distribution.

    :param Long:
        If set to True, then randomu will return integer uniform
        random deviates in the range [0...2^31-1], using the Mersenne
        Twister algorithm. All other keywords will be ignored.

    :param normal:
        If set to True, then random deviates will be generated from a
        normal distribution.

    :param poisson:
        Set this keyword to the mean number of events occurring during
        a unit of time. The poisson keword returns a random deviate
        drawn from a poisson distribution with that mean.

    :param ULong:
        If set to True, then randomu will return unsigned integer
        uniform deviates in the range [0..2^32-1], using the Mersenne
        Twister algorithm. All other keywords will be ignored.

    :return:
        A NumPy array of uniformly distributed random numbers of the
        specified dimensions.

    Example:
        >>> seed = None
        >>> x, sd = randomu(seed, [10,10])
        >>> x, sd = randomu(seed, [100,100], binomial=[10,0.5])
        >>> x, sd = randomu(seed, [100,100], gamma=2)
        >>> # 200x by 100y array of normally distributed values
        >>> x, sd = randomu(seed, [200,100], normal=True)
        >>> # 1000 deviates from a poisson distribution with a mean of 1.5
        >>> x, sd = randomu(seed, [1000], poisson=1.5)
        >>> # Return a scalar from a uniform distribution
        >>> x, sd = randomu(seed)

    :author:
        Josh Sixsmith, [email protected], [email protected]

    :copyright:
        Copyright (c) 2014, Josh Sixsmith
        All rights reserved.

        Redistribution and use in source and binary forms, with or without
        modification, are permitted provided that the following conditions are met:

        1. Redistributions of source code must retain the above copyright notice, this
           list of conditions and the following disclaimer.
        2. Redistributions in binary form must reproduce the above copyright notice,
           this list of conditions and the following disclaimer in the documentation
           and/or other materials provided with the distribution.

        THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
        ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
        WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
        DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
        ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
        (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
        LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
        ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
        (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
        SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

        The views and conclusions contained in the software and documentation are those
        of the authors and should not be interpreted as representing official policies,
        either expressed or implied, of the FreeBSD Project.
    """

    # Initialise the data type
    if double:
        dtype = 'float64'
    else:
        dtype = 'float32'

    # Check the seed
    # http://stackoverflow.com/questions/5836335/consistenly-create-same-random-numpy-array
    if type(seed) != mtrand.RandomState:
        seed = RandomState()

    if di is not None:
        if type(di) is not list:
            raise TypeError("Dimensions must be a list or None.")
        if len(di) > 8:
            raise ValueError("Error. More than 8 dimensions specified.")
        # Invert the dimensions list
        dims = di[::-1]
    else:
        dims = 1

    # Python has issues with overflow:
    # OverflowError: Python int too large to convert to C long
    # Occurs with Long and ULong
    #if Long:
    #    res = seed.random_integers(0, 2**31-1, dims)
    #    if di is None:
    #        res = res[0]
    #    return res, seed

    #if ULong:
    #    res = seed.random_integers(0, 2**32-1, dims)
    #    if di is None:
    #        res = res[0]
    #    return res, seed

    # Check for other keywords
    distributions = 0
    kwds = [binomial, gamma, normal, poisson]
    for kwd in kwds:
        if kwd:
            distributions += 1

    if distributions > 1:
        print("Conflicting keywords.")
        return

    if binomial:
        if len(binomial) != 2:
            msg = "Error. binomial must contain [n,p] trials & probability."
            raise ValueError(msg)

        n = binomial[0]
        p = binomial[1]

        res = seed.binomial(n, p, dims)

    elif gamma:
        res = seed.gamma(gamma, size=dims)

    elif normal:
        res = seed.normal(0, 1, dims)

    elif poisson:
        res = seed.poisson(poisson, dims)

    else:
        res = seed.uniform(size=dims)

    res = res.astype(dtype)

    if di is None:
        res = res[0]

    return res, seed