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)
def sample_half_counts(evt0, seed=0): evt = sdict(b = evt0.b, d = evt0.d, ims = evt0.ims, ths = evt0.ths, bkgr = evt0.bkgr/2) N = len(evt0.xc) rnd = RandomState(seed) m = rnd.randint(low=0, high=N, size=rnd.binomial(N,0.5)) evt.xc = evt0.xc[m] evt.yc = evt0.yc[m] evt.w = evt0.w[m] return evt
def multiplier_proposal_vector(q, d=1.05, f=1, rs=0): if not rs: rseed = random.randint(1000, 9999) rs = RandomState(MT19937(SeedSequence(rseed))) S = q.shape ff = rs.binomial(1,f,S) u = rs.random(S) l = 2 * np.log(d) m = np.exp(l * (u - .5)) m[ff==0] = 1. new_q = q * m U=np.sum(np.log(m)) return new_q, 0, U
class SimpleMonteCarloED(object): ''' SimpleMonteCarloED. A very simple monte carlo ED For a given no. of patients simulate 1. Triage assessment and treatment process time 2. Admit or not admit 3. For admitted patients only - delay in admission. ''' def __init__(self, params, random_state=None): ''' Constructor for SimpleMonteCarlosED Parameters: ------- params - ScenarioParmaeters, dataclass for sim model random_state - int, random seed. Allows for common random numbers to be used across multiple versions of the same model and reduces the noise in comparisons. (detault=None.) ''' self._params = params self._rs = RandomState(random_state) def simulate(self, n_patients): '''Performa a single replications/run of the simuation model Params: ------- n_patients - int, no. of patients to simulate. ''' process_times = self._simulate_ed_process_times(n_patients) admissions = self._simulate_admission(n_patients) admit_delays = self._simulate_dta_times(n_patients) #total time in ED for admitted patients ed_times_admit = process_times[admissions == 1] \ + admit_delays[admissions == 1] #total time in ED for non-admitted patients ed_times_not_admit = process_times[admissions == 0] #distribution of ed times. return np.append(ed_times_admit, ed_times_not_admit) def _simulate_ed_process_times(self, n_patients): ''' simulate ed process times for n patients ''' return self._rs.exponential(self._params.mean_process_time, size=n_patients) def _simulate_admission(self, n_patients): '''simulate admission Y/N for n patients ''' return self._rs.binomial(n=1, p=self._params.p_admit, size=n_patients) def _simulate_dta_times(self, n_patients): '''simulate admission delays for n patients ''' return self._rs.exponential(self._params.mean_dta, size=n_patients)
import pytest import numpy as np from ..irr import (compute_ts, simulate_ts_dist, simulate_npc_dist) from ..data import nsgk R = 10 Ns = 35 from numpy.random import RandomState RNG = RandomState(42) res = RNG.binomial(1, .5, (R, Ns)) def test_irr(): rho_s = compute_ts(res) np.testing.assert_almost_equal(rho_s, 0.51936507) def test_simulate_ts_dist(): expected_res1 = {'dist': None, 'geq': 591, 'obs_ts': 0.51936507936507936, 'pvalue': 0.0591, 'num_perm': 10000} res1 = simulate_ts_dist(res, seed=42, plus1=False) np.testing.assert_equal(res1, expected_res1)
import numpy as np from numpy.testing import (assert_equal, assert_almost_equal) from ..irr import (compute_ts, simulate_ts_dist, simulate_npc_dist) from ..data import nsgk R = 10 Ns = 35 from numpy.random import RandomState RNG = RandomState(42) res = RNG.binomial(1, .5, (R, Ns)) def test_irr(): rho_s = compute_ts(res) assert_almost_equal(rho_s, 0.51936507) #res = spt(group, condition, response, iterations=1000) #res1 = spt(group, condition, response, iterations=1000) #assert_less(res[1], 0.01) #assert_almost_equal(res[3], res1[3]) def test_simulate_ts_dist(): expected_res1 = {'dist': None, 'geq': 624, 'obs_ts': 0.51936507936507936,
def _treatment_assignment(covariates, *, random_state: RandomState, **kwargs): random_state = check_random_state(random_state) return random_state.binomial(1, p=expit(covariates[:, 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
def _hard_treatment(covariates, *, random_state: RandomState, **kwargs): return random_state.binomial(1, expit(covariates[:, 1]), size=len(covariates))
def _simple_treatment(covariates, *, random_state: RandomState, **kwargs): return random_state.binomial(1, 0.5, size=len(covariates)) # random assignment